Welcome Guest, Not a member yet? Register   Sign In
Include variables from a file.
#1

[eluser]Lazos[/eluser]
Right now I have the following array inside each of my controllers but now I decided that I want to include some more variables in the array which means that I have to edit each controller Sad.

Do you know a way that I can put this array in a file or something and include it inside each controller?
Code:
$this->headervars = array (
            'V_BREADCRUMBS'                => (isset($breadcrumbs)) ? $breadcrumbs : '',
            'V_CMS_TITLE'                => $this->siteprefs['adminsitename'],
            'V_PAGE_TITLE'                => $this->pagetitle,
            'V_MENU'                    => $topmenu,
            'L_WELCOME_USER'             => $this->lang->line('welcome'),        
            'L_ADMIN_PANEL_TITLE'        => $this->lang->line('admin_panel_title'),
            'V_ADMIN_PANEL_TITLE'         => $this->siteprefs['sitename'],
            'V_FIRST_LAST_NAME'         => $this->userdata['first_name']." ".$this->userdata['last_name']
            );


One of my Controllers:
Code:
class Tinymce extends Controller {
    var $userip;
    var $pagetitle;
    var $userdata;
    var $siteprefs;
    var $groupperms;
    var $headervars;
    var $footervars;
    var $userprefs;
    var $errormsg;
function __construct(){
        parent::Controller();
        $this->groupperms = array();
        // Header
        $this->user_ip = $this->input->ip_address();
        $this->userdata = $this->my_session->sess_read($this->user_ip, TINYMCE);
        $this->pagetitle = TINYMCE;
        $this->errormsg = '';
        if ($this->userdata['session_logged_in'] != 1) {
            redirect('/index.php/admin/login', 'location', 301);
        }
        $this->load->model('admin/header_model', 'header_model');
        $this->groupperms = $this->header_model->getAllGroupPerms($this->userdata['group_id']);
        $this->load->library('admin_theme');
        $this->load->library('admin_content');
        $this->load->library('tinymce_lib');
        $this->admin_content->initialize($this->userdata['user_id'], $this->userdata['group_id'], $this->groupperms);
        $this->siteprefs = $this->header_model->site_preferences();
        $this->tinymce_lib->setInitialValues($this->siteprefs);
        $this->admin_theme->_constructor($this->userdata['user_id'], $this->userdata['group_id']);
        $headers = $this->admin_theme->SendHeaders();
        $topmenu = $this->admin_theme->DoTopMenu();
        $this->headervars = array (
            'V_BREADCRUMBS'                => (isset($breadcrumbs)) ? $breadcrumbs : '',
            'V_CMS_TITLE'                => $this->siteprefs['adminsitename'],
            'V_PAGE_TITLE'                => $this->pagetitle,
            'V_MENU'                    => $topmenu,
            'L_WELCOME_USER'             => $this->lang->line('welcome'),        
            'L_ADMIN_PANEL_TITLE'        => $this->lang->line('admin_panel_title'),
            'V_ADMIN_PANEL_TITLE'         => $this->siteprefs['sitename'],
            'V_FIRST_LAST_NAME'         => $this->userdata['first_name']." ".$this->userdata['last_name']
            );
        $this->footervars = array (
            'V_CMS_NAME'                => $this->siteprefs['cmsname'],
            'V_CMS_VERSION'                => $this->siteprefs['cmsversion']
            );
    }
}
#2

[eluser]Teks[/eluser]
You can use the Config Class to create your own configuration files. You can then have your file auto-loaded - see instructions in the Config Class documentation.
#3

[eluser]Lazos[/eluser]
This is not working because the variables cannot be defined.

Because I have $this-> I am getting an error that I cannot call it when I am not inside an object. What I want is just include that part without executing it. The config file is executing the file.
#4

[eluser]Teks[/eluser]
So, if I understand correctly, you need to have certain 'standard' variables available to several of your controllers. Some of the values in these variables will be the same - ie., they will be functionally similar to constants - but other will vary from controller to controller, and even depend on the user, and the page being accessed.

A possible solution would be to create a new model class - a 'Headerprefs' class - that would have all the variables you need inside it. Inside the class' method definitions, you can load language files, populate the variables with default values as needed, and use custom getters/setters to provide changing values for the others. The class can be auto-loaded, and be available everywhere in your application by default, just like generic settings. If you decide to add extra variables, they will be immediately available everywhere.

I hope this helps.
#5

[eluser]Lazos[/eluser]
[quote author="Teks" date="1227602915"]So, if I understand correctly, you need to have certain 'standard' variables available to several of your controllers. Some of the values in these variables will be the same - ie., they will be functionally similar to constants - but other will vary from controller to controller, and even depend on the user, and the page being accessed.

A possible solution would be to create a new model class - a 'Headerprefs' class - that would have all the variables you need inside it. Inside the class' method definitions, you can load language files, populate the variables with default values as needed, and use custom getters/setters to provide changing values for the others. The class can be auto-loaded, and be available everywhere in your application by default, just like generic settings. If you decide to add extra variables, they will be immediately available everywhere.

I hope this helps.[/quote]

Why a model class? Model class is not it just for database use?

Can you give an example if is possible?
#6

[eluser]Teks[/eluser]
Quote:Why a model class? Model class is not it just for database use?

In the Model-View-Controller pattern (MVC), model classes should be responsible for the data handling (validation, storage, retrieval). Whether the data is stored in a database, or a file, or a bunch of configuration settings, it is still data. If you need to create a class that will be responsible for managing data, and data integrity, then under an MVC architecture that will be a model class.

Quote:Can you give an example if is possible?

You could use a 'Preferences' class, which in CodeIgniter could be defined roughly like this:

Code:
class Preferences extends Model {
    
    //variables might have set default values:
    var $default_charset = 'utf-8';
    var $default_lang = 'en';
    var $default_template = 'default';
    [...]
    
    //The constructor could be made responsible for
    //setting/changing variables, as needed:
    function Preferences()
    {
        //call parent constructor:
        parent::Model();
        
        $this->default_charset = (current_charset_from_db);
        $this->default_lang = (current_lang_from_db);
        [...]
    }
    
    //this class could also provide other preference-data related functions:
    function available_languages()
    {
        $langs = (list_of_supported_languages);
        return $langs;
    }

    function available_templates()
    {
        $templates = (list_of_available_templates);
        return $templates;
    }
}

This class should be accessible in controllers and views, by loading it with:

Code:
$this->load->model('Preferences');

Once the model class is loaded, you should be able to access its variables and functions by using:

Code:
$langs = $this->Preferences->available_languages();

I hope this helps.
#7

[eluser]Lazos[/eluser]
Thanks for your replies.
Because I am running out of time I did not try your idea but I will look at it for sure after I finish my dissertation.

I use something else though. I do not know if is a good or bad practice. I divided the header and footer into one new class call Headerfooter-lib.

In that class I enter everything that is related to header and footer.

And in the Controller I load the library file (which I think since is loaded every time is better to put it inside autoload), then I sent to the library all the important variables to generate the header and footer and then 2 more functions to retrieve the complete header and footer.

Well at least now if I want to enter new variables in footer and header I do not have to edit 30 files.

Headerfooter-lib Class
Code:
<?php

/**
* @author Lazaros Lazarou
* @copyright 21112008
* @email
*/
class Headerfooter_lib {

    var $siteprefs;
    var $pagetitle;
    var $userdata;
    var $topmenu;
    var $headers;
    var $groupperms;
    
    function __construct() {
        $this->CI =& get_instance();    
    }
    
    function initialValues($siteprefs, $pagetitle, $userdata) {
        $this->siteprefs = $siteprefs;
        $this->pagetitle = $pagetitle;
        $this->userdata = $userdata;
        $this->CI->load->library('admin_theme');
        $this->CI->admin_theme->_constructor($this->userdata['user_id'], $this->userdata['group_id']);
        $this->headers = $this->CI->admin_theme->SendHeaders();
        $this->topmenu = $this->CI->admin_theme->DoTopMenu();    
    }
    
    function headerPrefs() {
        $header = array (
                'V_BREADCRUMBS'                => (isset($breadcrumbs)) ? $breadcrumbs : '',
                'V_CMS_TITLE'                => $this->siteprefs['adminsitename'],
                'V_PAGE_TITLE'                => $this->pagetitle,
                'V_MENU'                    => $this->topmenu,
                'L_WELCOME_USER'             => $this->CI->lang->line('welcome'),        
                'L_ADMIN_PANEL_TITLE'        => $this->CI->lang->line('admin_panel_title'),
                'V_ADMIN_PANEL_TITLE'         => $this->siteprefs['sitename'],
                'V_FIRST_LAST_NAME'         => $this->userdata['first_name']." ".$this->userdata['last_name']
                );
        return $header;
    }
    
    function footerPrefs() {
        $footer = array (
                'V_CMS_NAME'                => $this->siteprefs['cmsname'],
                'V_CMS_VERSION'                => $this->siteprefs['cmsversion']
                );    
        return $footer;
    }
}
?>


Example User Controller Class
Code:
class Users extends Controller {
    var $userip;
    var $pagetitle;
    var $userdata;
    var $siteprefs;
    var $groupperms;
    var $headervars;
    var $footervars;
    var $errormsg;
    
    function __construct(){
        parent::Controller();
        $this->user_ip = $this->input->ip_address();
        $this->userdata = $this->my_session->sess_read($this->user_ip, USERS);
        if ($this->userdata['session_logged_in'] != 1) {
            redirect('/index.php/admin/login', 'location', 301);
        }
        // Page Title
        $this->pagetitle = USERS;
        // Header Model
        $this->load->model('admin/header_model', 'header_model');
        $this->groupperms = $this->header_model->getAllGroupPerms($this->userdata['group_id']);
        $this->siteprefs = $this->header_model->site_preferences();
        // Header-Footer Library
        $this->load->library('headerfooter_lib');
        $this->headerfooter_lib->initialValues($this->siteprefs, $this->pagetitle, $this->userdata);
        $this->headervars = $this->headerfooter_lib->headerPrefs();
        $this->footervars = $this->headerfooter_lib->footerPrefs();
        // Users Library
        $this->load->library('users_lib');
        $this->users_lib->SetInitialValues($this->userdata['user_id'], $this->groupperms);
        // Error Message
        $this->errormsg = '';
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB