Matteo Enna
Matteo Enna

Reputation: 1301

PrestaShop backend, Fatal error in ajax call

On the backend of a PrestaShop site I'm using this function:

public function hookAjax($action, $id_product, $id_lang, $title, $descript, $order, $id = NULL)
{

    /* various code*/

    $this->context->smarty->assign(
        array(
            'block_define'  => $this->getFormDesc($id_product)
        )
    );
    return $this->context->smarty->fetch($this->local_path.'views/templates/hook/admin_extra_desc.tpl');
}

public function getFormDesc($id_product) {
    $array = array();

    foreach (Language::getLanguages() as $lang) {
        /*various code*/

        foreach($result as $k=> $r) {
            $files   = array();
            $helper = new HelperImageUploader();
            $helper->setMultiple(false)->setUseAjax(true)->setName('thumbnail_'.$r['id'].'_'.$r['id_lang'])->setFiles($files)->setMaxFiles(3)->setUrl('../modules/module-name/imgAjaxCall.php?');
            $result[$k]['img-form'] = $helper->render();
            $result[$k]['img'] = $result[$k]['img'] ? _PS_BASE_URL_.__PS_BASE_URI__.'modules/module-name/upload/'.$result[$k]['img'] : '';
        }

        $array[$lang["id_lang"]] = array(
            'lang_data'     =>  $lang,
            'count'         =>  count($result),
            'data'          =>  $result
        );
    }

    return $array;

}

HookAjax is called by:

<?php 
    include(dirname(__FILE__).'/../../config/config.inc.php');


    $context = Context::getContext();
    $addDesc = Module::getInstanceByName('module-name');

    echo $addDesc->hookAjax($_POST['action'],$_POST['id_prodotto'],$_POST['lang'],$_POST['title'], $_POST['text_desc'], NULL, $_POST['row']);

?>

But I struggle with this error:

Fatal error: Call to a member function addJs() on a non-object in {my_site}/classes/helper/HelperUploader.php on line 257

Upvotes: 1

Views: 718

Answers (1)

TheDrot
TheDrot

Reputation: 4337

You need to include init.php in HookAjax after including config.inc.php so that controller is initialized in context.

include(dirname(__FILE__).'/../../init.php');

Note that this is just bad practice, respect the MVC and use proper controllers for your AJAX calls and data validation/processing in them, not inside main module class.

Upvotes: 1

Related Questions