Advanced module: Undefined variable user

suwsuw
edited March 2012 in Modules
Hello
I need some help, I tried to make my own advanced module, but I get this error in top right corner:

"Undefined variable user, Filename: blocks/_file_header.php, line number 36"

here's my class code:

class Massmail extends Fuel_base_controller {

public $nav_selected = 'tools/massmail';
public $view_location = 'massmail';

function __construct()
{
parent::__construct(FALSE);

$this->load->module_config(MASSMAIL_FOLDER, 'massmail');
if ($this->config->item('massmail_authenticate'))
{
$this->_check_login();
}
}


function _remap(){
$element = $this->uri->segment(4);
switch($element){
default:
$this->_lista();
}
}

function _lista(){
$vars=array();
$this->_render('massmail_view', $vars);
}
}

Comments

  • edited 10:29PM
    Is massmail_authenticate true? Could we see "blocks/_file_header.php".

    By the looks of that, you have no $user variable defined.

    Assuming _lista() is running, what happens when you add $vars['user'] = 'suw'; to it?
  • suwsuw
    edited 10:29PM
    massmail_authenticate is true,

    1. I took a look at fuel's fuel_header.php it seems I need to declare

    $vars["user"]=array("user_name"=>"suw");
    How can I get the current logged username? isn't that automatically done by the fuel render? Did I missed something?

    2. what's the difference between

    $this->load->module_config(MASSMAIL_FOLDER, 'massmail');
    AND
    $this->config->load("massmail");
    Both commands load the config file? which one should we use?
  • suwsuw
    edited 10:29PM
    Hmm I solve it somehow, but it's ugly, still - no more error
    I added a private global variable in class and filled it with current username value.

    class Massmail extends Fuel_base_controller {
    private $loggedUserName;
    ...
    function __construct() {
    ...
    $session_key = $this->fuel_auth->get_session_namespace();
    $user_data = $this->session->userdata($session_key);
    $this->loggedUserName["user_name"]=$user_data["user_name"];
    }

    function _lista(){
    $vars["user"]=$this->loggedUserName;
    ...
    }
    Please let me know if there's other way to do this!
  • edited 10:29PM
    I think Lance had a good question as to whether "massmail_authenticate" is set to TRUE. If it isn't, then the user value is not set (if you look at line 77 of the Fuel_base_controller).
  • suwsuw
    edited 10:29PM
    It is set on true,

    config/massmail.php

    ...
    $config['massmail_authenticate'] = TRUE;
    I also removed the If command - to force check login

    $this->_check_login();
    $this->_validate_user("tools/massmail");
    I have same error - unless I read manually the user status with this:

    $session_key = $this->fuel_auth->get_session_namespace();
    $user_data = $this->session->userdata($session_key);
    $this->loggedUserName["user_name"]=$user_data["user_name"];
    I am pretty shure somewhere I did something wrong - maybe not included something..
    here's my whole code.
    controller/massmail.php

    require_once(FUEL_PATH.'/libraries/Fuel_base_controller.php');

    class Massmail extends Fuel_base_controller {

    public $nav_selected = 'tools/massmail';
    public $view_location = 'massmail';
    private $loggedUserName; //array with username - needed for render!

    function __construct()
    {
    parent::__construct(FALSE);
    $this->load->module_config(MASSMAIL_FOLDER, 'massmail');
    //$this->config->load("massmail"); // same as above line...

    $this->load->language("massmail");

    //if ($this->config->item('massmail_authenticate'))
    $this->_check_login();
    $this->_validate_user("tools/massmail");

    //force read user name
    $session_key = $this->fuel_auth->get_session_namespace();
    $user_data = $this->session->userdata($session_key);
    $this->loggedUserName["user_name"]=$user_data["user_name"];
    }



    function _remap(){
    $element = $this->uri->segment(4);
    switch($element){
    default:
    $this->_lista();
    }
    }

    function _lista(){
    $vars["user"]=$this->loggedUserName;
    $this->_render('massmail_view', @$vars);
    }
    }
    and config file: config/massmail.php

    $config['nav']['tools']['tools/massmail'] = "Mass Mailer";

    // user guide requires user authentication to view
    $config['massmail_authenticate'] = TRUE;

    View file is simple



    "><?=lang('section_tools')?> > Mass Mailer



    <?=$notifications?>

    that's it - anyone put those toghether and comment "force read username" - get that error...
  • suwsuw
    edited 10:29PM
    sorry for the viewfile - tought pre will show it....
  • suwsuw
    edited 10:29PM
    I solve it - I've seen my error - after putting the code HERE -
    parent::__construct(FALSE); => must be TRUE

    sorry for this - maybe someone will learn from it - I DID!
Sign In or Register to comment.