zend 1 view helper functions use in another view helper class - php

i have two helper classes
link are :
C:\xampp\htdocs\ecom\application\views\helpers\comman.php
C:\xampp\htdocs\ecom\application\views\helpers\RefineUrl.php
class Zend_View_Helper_Refinestr
{
public function Refinestr($str, $options = array()){
..............
.............
return $str;
}
}
second is
class Zend_View_Helper_Comman
{
public function Comman(){
return $this;
}
public function getPageContent($pageId){
// return $pageId;
$mapper = new Application_Model_StaticpageMapper();
$selectedFields=array('desc');
$tblName=array($mapper->getDbTable()->_name);
$whr= "`id`=$pageId";
$content=$mapper->fetchSelectedFields($tblName,$selectedFields,$whr);
$des=$content[0]['desc'];
// here i want to use function Refinestr() of another helper class how i use this
$des=$this->Refinestr($des);
// not working , searching this function inside comman class
} }
How to use one helper class function in another helper class function?

You can use below trick for your case.
While calling getPageContent() helper from your view file pass the view object in helper as a param (like $pageId) and use that view object to call another helper in helper definition.
View file:
<?php echo $this->getPageContent($pageId, $this); ?>
Helper File:
class Zend_View_Helper_GetPageContent {
public function getPageContent($pageId, $viewObj) {
// return $pageId;
$mapper = new Application_Model_StaticpageMapper ();
$selectedFields = array ('desc'
);
$tblName = array ($mapper->getDbTable ()->_name
);
$whr = "`id`=$pageId";
$content = $mapper->fetchSelectedFields ( $tblName, $selectedFields, $whr );
$des = $content [0] ['desc'];
// here i want to use function Refinestr() of another helper class how i
// use this
$des = $viewObj->Refinestr($des); //use view object to call another helper
}
}
Another helper will remain as it is.
One more solution to this problem could be, set view object in Zend Registry at the time of bootstrapping and use that registry variable in helper file to call another helper.
In Bootstrap File:
protected function _initConfig() {
$this->bootstrap('view');
$this->_view = $this->getResource('view');
Zend_Registry::set('viewObj', $this->_view);
}
Helper File:
class Zend_View_Helper_GetPageContent {
public function getPageContent($pageId) {
// return $pageId;
$mapper = new Application_Model_StaticpageMapper ();
$selectedFields = array ('desc');
$tblName = array ($mapper->getDbTable ()->_name);
$whr = "`id`=$pageId";
$content = $mapper->fetchSelectedFields ( $tblName, $selectedFields, $whr );
$des = $content [0] ['desc'];
// here i want to use function Refinestr() of another helper class how i
// use this
$viewObj = Zend_Registry::get('viewObj');
$des = $viewObj->Refinestr($des); //use view object to call another helper
}
}

I usually do the following:
Inside helper1
$this->helper1()->view->helper2();
In case helper1 is taking some arguments, I modify it to take no arguments and just return. Try it out, may work.

Related

Codeigniter: Using static variable

I am making a website in which i have to save a global variable.
I am using this person code globals_helper.php custom global variable class
But i always get static variable value null.
globals_helper.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Application specific global variables
class Globals
{
private static $authenticatedMemberId = null;
private static $initialized = false;
private static function initialize()
{
if (self::$initialized)
return;
self::$authenticatedMemberId = null;
self::$initialized = true;
}
public static function setAuthenticatedMemeberId($memberId)
{
self::initialize();
self::$authenticatedMemberId = $memberId;
}
public static function authenticatedMemeberId()
{
self::initialize();
return self::$authenticatedMemberId;
}
}
I have done all the steps like add globals_helper.php in helper folders and updated autoload file. Now I am trying to access those static variable from a custom library "Ctrl_utility" function "get_search_term" and my controllers calling get_search_term function
Ctrl_utility.php
class Ctrl_utility {
protected $CI;
public static $static_search = "";
public function __construct()
{
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
}
public function get_search_term($searchTerm){
$searchTerm = $this->CI->security->xss_clean(htmlspecialchars($searchTerm));
if (isset($searchTerm) && strlen($searchTerm)>0) {
Globals::setAuthenticatedMemeberId($searchTerm);
} else {
$searchTerm = Globals::authenticatedMemeberId();
}
return $searchTerm;
}
One of my controller and they all have class ctrl_utility, get_search_term function:
class Blog_controller extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->model('blogs_model');
}
public function index(){
//Get SearchTerm Values
$searchTerm = $this->ctrl_utility->get_search_term($this->input->post('searchTerm'));
//Get Url First Parameter
$start = $this->ctrl_utility->get_url_first_parameter();
// Get Data from solr
$rows = 10;
$data = $this->blogs_model->solrData($start, $rows, $searchTerm); //give start of documents
//Pagination
$this->pagination->initialize($this->ctrl_utility->pagination_config($this->uri->segment(1), $rows, $data['found']));
//Views
$this->load->view('tabs/blogs', $data);
}
}
Am i doing something wrong?
Now when it comes to define them in CodeIgniter, there are several ways do that. I’ve listed some of them below:
Create your own file in application/libraries in which class constructor contains an array as an argument. Now create a new file in /application/config with same name as given in application/libraries and declare your global variables in it. Now to use these variables, autoload the newly created library.
Create your own file in application/core and declare the global variables in it. Than in controller you need to extend your file name instead of CI_Controller.
If the Global Variables are true constants, just add them in application/config/constants.php file and name them in all uppercase like the others are defined.
If you want to set application constants create new config file and add the variables. Now load it as $this->config->load(‘filename’); And access those variables as
$this->config->item(‘variable_name’);
Instead of creating helper create a library
Step 1: First of all, open application/libraries and create a custom
class name globals.php. It contains a constructor function which
contains an array as an argument.
<?php
class Globals {
// Pass array as an argument to constructor function
public function __construct($config = array()) {
// Create associative array from the passed array
foreach ($config as $key => $value) {
$data[$key] = $value;
}
// Make instance of CodeIgniter to use its resources
$CI = & get_instance();
// Load data into CodeIgniter
$CI->load->vars($data);
}
}
?>
Step 2: Now to make config file, open application/config and create
file as globals.php and write the code given below. This file contains
the config variable which will be passed as an array to constructor of
Globals class where its keys and values are stored in $data
<?php
// Create customized config variables
$config['web_Address']= 'https://www.example.com/blog';
$config['title']= 'CodeIgniter Global Variable';
?>
When constructor function loads, it will take the config variables
from the config file in order to use these variables anywhere.
Note: Name of the above file must be same as the class created in
libraries folder otherwise the code will not work.
Step 3: But before using these variables we have to autoload the newly
created library globals as given below.
And load library in autoload
$autoload['libraries'] = array('globals');
Now, you can use global variables in your controller
<?php
class CI_Global_Variable_Tutorial extends CI_Controller{
public function __construct() {
parent::__construct();
}
// Load view page
public function index() {
$this->load->view('show_global_variables');
}
}
?>
Views : show_global_variables.php
In view page, we can use global variables according to our need.
<?php
echo "Title of the blog post : ".$title;
echo "<a href='$web_Address'>"."Click here to go to blog page"."</a>";
?>

How to use other class methods inside of another class in same file?

I've a scenario where i have to use a Class that extends another class which is not a codeigniter class and inside of the method, i need to call another method to perform Database operation.
How can i achieve?
<?php
require_once(APPPATH.'third_party/OauthPhirehose.php');
class Get extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('tweets_model');
}
public function getall()
{
$tags = $this->tags_model->get();
$result = $tags->row();
$first = $result->first_tag;
$second = $result->second_tag;
return array($first, $second);
}
public function insert_raw_tweet($raw_tweet, $tweet_id)
{
$this->tweets_model->save_raw($raw_tweet, $tweet_id);
}
}
class Consume extends OauthPhire {
public function enqueueStatus($status) {
$tweet_object = #json_decode($status);
if (!(isset($tweet_object->id_str))) { return;}
$tweet_id = $tweet_object->id_str;
$raw_tweet = base64_encode(serialize($tweet_object));
echo $tweet_object->text . "\r\n";
// here i need to use top class method to insert data
//$this->tasks->insert_raw_tweet($tweet_object, $tweet_id);
}
}
$get_track = new Get();
$list = $get_track->getall();
$stream = new Consume(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER);
$stream->setTrack($list);
$stream->consume();
Load it just like you load a normal third party library stored in /library/ folder, use this method :
$this->load->library('OauthPhirehose');
Hope this works.

Opencart: How To Pass Data Variable From Parent To Child Controller

Is there any simple solution to pass $this->data variable from parent controller to all children controllers.
All what I try bring empty array to child.
When I change the file "system/engine/controller.php" like below:
protected function getChild($child, $args = array()) {
$action = new Action($child, $args);
if (file_exists($action->getFile())) {
require_once($action->getFile());
$class = $action->getClass();
$controller = new $class($this->registry);
//$controller->{$action->getMethod()}($action->getArgs());
$controller->{$action->getMethod()}($action->getArgs()+array('parent-data'=>$this->data));
return $controller->output;
} else {
trigger_error('Error: Could not load controller ' . $child . '!');
exit();
}
}
Then I try to read the variable 'parent-data' from the passed arguments in the child controller:
if (isset($setting['parent-data'])) {
echo "<pre>".print_R($setting['parent-data'],true)."</pre>";
}
As a result I get an empty array:
Array
(
[modules] => Array
(
)
)
The data variable is empty. Thats why it prints blank array.
Also there is no need to pass the data varaiable.Its a global one and you will get it till upto the .tpl files.
The issue was in the parent that was the column controller with empty data variable rather the main page parent controller that has had necessary data variable.
on opencart2 and maybe older:
ex: parse ver (steps) from parent controller product-custom to child controller header
parent
<?php
class ControllerProductCustom extends Controller {
private $error = array();
public function index() {
$this->load->language('product/product');
...
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header', array('steps' => true));
child
<?php
class ControllerCommonHeader extends Controller {
public function index($arg) {
$data['title'] = $this->document->getTitle();
if (isset($arg['steps'])) $data['steps'] = $arg['steps'];
...

Why array is not full after initialization in constructor PHP?

I have a two classes PHP:
Profile
Publicprofile
At Publicprofile class there are:
public function index($id){
$profile = new profile($id, true);
$profile->index();
}
So, here I create a new object profile.
Let's some to see class Profile:
class Profile extends Auth {
public $data = array();
public function __construct($idUser = null, $view = false){
parent::__construct();
$this->getTemplate();
}
}
}
The function getTemplate(); - forms an array $this->data
If to show at once this array after execute getTemplate(), will see (via var_dump), that array contains data with a keys:
view_inside
view
When is called a method $profile->index() - this array not is same:
Method index in class Profile:
public function index(){
var_dump($this->data); die(); // Here there are not keys view_inside, view
$this->route();
}
What is wrong at my code, that the source array in one class is different at two methods?
Function GetTemplate():
public function getTemplate(){
$this->data['view_inside'] = 'profile/doctor/index';
$this->data['view_left'] = 'profile/doctor/left';
$this->data['view_edit'] = 'profile/doctor/personal_update';
$this->data['view_personal'] = 'users/doctor/personal';
var_dump($this->data); // All right
}

How to pass data to custom view helper called from a layout - zend

I have a Controller, Layout, Custom view helper. I'm passing a data from controller $this->view->foo = 'foo'; normally I get it on my layout.phtml,here I'm calling a custom view helper $this->navbar(); on layout.
How can I access that foo within my view helper?
<?php
class Zend_View_Helper_Navbar extends Zend_View_Helper_Abstract
{
public function setView( Zend_View_Interface $view )
{
$view = new Zend_View();
$view->setScriptPath(APPLICATION_PATH . '/views/scripts/partials/');
$this->_view = $view;
}
public function navbar()
{
return $this->_view->render('navbar.phtml');
}
}
this is my view helper
Change your helper function such that it accepts a parameter, as shown:
In Zend_View_Helper_Navbar:
public function navbar($foo="")
{
$this->_view->bar = $foo;
return $this->_view->render('navbar.phtml');
}
Then, in navbar.phtml:
<?php echo $this->bar; ?>
This way, whatever parameter value passed to the helper function will be displayed in the navbar.phtml. After that, you can pass the parameter from your controller file as usual.
In your controller file:
$this->view->foo = "custom parameter";
In your view script, or layout.phtml, call the navbar helper passing the parameter:
<?php echo $this->navbar($this->foo);?>
Zend_View_Helper_Navbar extends Zend_View_Helper_Abstract which contains $view.
All you have to do is :
public function navbar()
{
$this->view->setScriptPath(APPLICATION_PATH . '/views/scripts/partials/');
$foo = (isset($this->view->foo)) ? $this->view->foo : '';
// your code using $foo
return $this->view->render('navbar.phtml');
}

Categories