2017-04-10 59 views
1

我正在製作PrestaShop 1.6模塊,並且發生了一些奇怪的事情。在這個模塊的配置頁面中,我向用戶提供兩種形式。一個配置自動電子郵件和其他測試電子郵件。我的問題是,提交第二形式$this->context->smarty是空給我這個錯誤後:

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

在90行,我有:$this->context->smarty->assign('module_dir', $this->_path);這是這個函數的內部:

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(); 
} 

這是怎麼了我在屏幕中添加兩個表單(renderForm方法):return $helper->generateForm(array($this->getConfigForm(), $this->getTestForm()));

postProcess。對不起粘貼這麼多代碼,我試圖提供所有我認爲相關的細節。

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)); 
     } 
    } 
} 

sendTestEmailTo只是有一個IF語句,然後調用這個函數:

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; 
} 

我沒有看到這可能是導致$this->context->smartynull。你有任何提示可以幫助我調查嗎?

編輯

的構造方法:

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_); 
} 
+0

你可以發佈你的__construct方法嗎?謝謝:) – sarcom

+1

@sarcom剛剛,謝謝 –

回答

2

我很高興你已經解決了。但我想這是問題是這樣的:

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

刪除這些行:),你不應該永遠不會包含該文件的類方法。

+1

你是對的,我不需要插入它。我做了,因爲我在PrestaShop論壇的另一個線程中看到。 –

0

交付之前,我無法弄清楚,所以我不得不這樣做是爲了解決,重定向:

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); 
} 
相關問題