André Luiz
André Luiz

Reputation: 7302

PrestaShop $this->context->smarty is null

I'm making a PrestaShop 1.6 module and something weird is happening. In the configure page of this module I offer the user two forms. One to configure an automatic email and other to test the e-mail. My problem is that after submitting the second form the $this->context->smarty is null giving me this error:

Fatal error: Call to a member function assign() on null in /Users/andre/Projects/Web/xxx/modules/closecustomerthreademail/closecustomerthreademail.php on line 90

In line 90 I have: $this->context->smarty->assign('module_dir', $this->_path); which is inside of this function:

public function getContent()
{
    /**
     * If values have been submitted in the form, process.
     */
    if (((bool)Tools::isSubmit('submitClosecustomerthreademailModule')) == true) {
        $this->postProcess();
    }

    $this->context->smarty->assign('module_dir', $this->_path);
    $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');

    return $output.$this->renderForm();
}

This is how I add two forms in the screen (renderForm method): return $helper->generateForm(array($this->getConfigForm(), $this->getTestForm()));

The postProcess. Sorry for pasting so much code, I'm trying to provide all the details I find relevant.

protected function postProcess()
{
    if(Tools::isSubmit('test_form')) {
        $this->sendTestEmailTo(Tools::getValue('CLOSECUSTOMERTHREADEMAIL_EMAIL_TO_TEST'));
    }
    elseif(Tools::isSubmit('config_form')) {

        $form_values = $this->getConfigFormValues('config_form');

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }
}

The sendTestEmailTo just has an IF statement and then call this function:

public function sendEmail($to_email, $to_name = null){

    $id_lang = $this->context->language->id;
    $template_dir = _PS_MODULE_DIR_.$this->name.'/views/templates/email/';

    $vars = array(
        '{html}' => Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_HTML_BODY'),
        '{text}' => Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_TEXT_BODY')
    );

    require(_PS_CONFIG_DIR_.'config.inc.php');
    require_once(_PS_ROOT_DIR_.'/init.php');

    $send = Mail::Send(
        (int)$id_lang,
        'email',
        Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_SUBJECT'),
        $vars,
        $to_email,
        $to_name,
        null,
        null,
        null,
        null,
        $template_dir,
        false,
        (int)$this->context->shop->id,
        null
    );

    return $send;
}

I'm failing to see what could be causing $this->context->smarty to be null. Would you have any tips to help me investigate?

EDIT

The construct method:

public function __construct()
{
    $this->name = 'closecustomerthreademail';
    $this->tab = 'others';
    $this->version = '1.0.0';
    $this->author = 'andre';
    $this->need_instance = 0;

    /**
     * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
     */
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('Close Customer Thread e-mail');
    $this->description = $this->l('Sends an e-mail whenever you close a customer thread');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');

    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}

Upvotes: 1

Views: 2067

Answers (2)

marsaldev
marsaldev

Reputation: 3349

I'm glad that you've solved. But I guess that the problem is this:

require(_PS_CONFIG_DIR_.'config.inc.php');
require_once(_PS_ROOT_DIR_.'/init.php');

Remove those lines :), you shouldn't never include that files in a class method.

Upvotes: 2

André Luiz
André Luiz

Reputation: 7302

I couldn't figure out before the delivery so I had to do this to solve, a redirect:

protected function postProcess()
{
    if(Tools::isSubmit('test_form')) {
        $this->sendTestEmailTo(Tools::getValue('CLOSECUSTOMERTHREADEMAIL_EMAIL_TO_TEST'));
    }
    elseif(Tools::isSubmit('config_form')) {

        $form_values = $this->getConfigFormValues('config_form');

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

    $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    Tools::redirectAdmin($actual_link);
}

Upvotes: 0

Related Questions