Search code examples
phpinheritanceprestashopcontrollers

PrestaShop, Class not found in ModuleAdminController::getController


i'm trying to develop a PrestaShop module with controllers i placed, for example in: /modules/mymodule/controllers/admin/myControlController.php

class MyControlController extends ModuleAdminController {
public function __construct() {           
      $this->module = 'mymodule'; 
      $this->bootstrap = true;
      $this->context = Context::getContext();     
      $token = Tools::getAdminTokenLite('AdminModules');
      $currentIndex='index.php?controller=AdminModules&token='.$token.'&configure=mymodule&tab_module=administration&module_name=mymodule';
      Tools::redirectAdmin($currentIndex);
      parent::__construct();                
}
public function showForm() {
  die("hello");
}}

Controller works (construct method is called) if i call it form url http://myshop.com/adminxxx/index.php?controller=MyControl&token=9faf638aa961468c8563ffb030b3c4a8

But i can't access methods of controller from main class of module: ModuleAdminController::getController('MyControl')->showForm(); I received "Class not found" ever

Is that the correct method to access a control from outside? Thanks!


Solution

  • If you want to show anything that concern a form you should use renderForm(). Your should try parent::showForm(); or $this->showForm();.

    Here is an example of controller that can work :

    require_once _PS_MODULE_DIR_.'modulename/models/ModuleNameLog.php';
    require_once _PS_MODULE_DIR_.'modulename/modulename.php';
    
     class AdminModuleNameLogController extends ModuleAdminController
     {
      protected $_defaultOrderBy = 'id_modulenamelog';
      protected $_defaultOrderWay = 'DESC';
    
      public function __construct()
      {
        $this->table = 'modulenamelog';
        $this->className = 'ModuleNameLog';
    
        $this->context = Context::getContext();
        $this->lang = false;
        $this->bootstrap = true;
        $this->actions_available = array();
        $this->actions = array();
        $this->show_toolbar = false;
        $this->toolbar_btn['new'] = array();
        $this->tabAccess['add'] = '0';
        $this->allow_export = true;
        $this->requiredDatabase = true;
        $this->page_header_toolbar_title = $this->l('Example Module Name logs');
    
        $this->_select = 'SUM(a.quantity) as total_quantity';
        $this->_group = ' GROUP BY a.id_product, a.id_product_attribute ';
    
        $this->fields_list = array(
            'id_product' => array(
                'title' => $this->l('Product'),
                'align' => 'center',
                'callback' => 'getProductName',
            ),
            'id_product_attribute' => array(
                'title' => $this->l('Combination'),
                'align' => 'center',
                'callback' => 'getAttributeName',
            ),
            'total_quantity' => array(
                'title' => $this->l('Total Quantity'),
                'align' => 'center',
            ),
        );
    
        $this->mod = new ModuleName();
        $this->mod->cleanLogs();
    
        $this->context = Context::getContext();
    
        parent::__construct();
    }
    
    public function getProductName($id)
    {
        if (!empty($id)) {
            $product = new Product($id, true, $this->context->cookie->id_lang);
    
            return $product->name;
        }
    }
    public function getAttributeName($id)
    {
        if (!empty($id)) {
            $combination = new Combination($id);
            $names = $combination->getAttributesName($this->context->cookie->id_lang);
            $str = array();
    
            if (!empty($names)) {
                foreach ($names as $value) {
                    $str[] = $value['name'];
                }
            }
    
            return implode(' - ', $str);
        } else {
            return '-';
        }
    }
    
    public function postProcess()
    {
        if (Tools::isSubmit('purge_id')) {
    
            // Do something here
            $id = (int) Tools::getValue('purge_id');
    
    
            Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModuleNameLog').'&conf=4');
    
        }
    
        parent::postProcess();
    }
    
    public function renderList()
    {
        $carts = Db::getInstance()->executeS('SELECT ct.*, cs.`firstname`, cs.`lastname` FROM '._DB_PREFIX_.'cart ct LEFT JOIN '._DB_PREFIX_.'customer cs ON ct.id_customer = cs.id_customer WHERE 1 ORDER BY id_cart DESC LIMIT 0,2000');
    
        $tpl = $this->context->smarty->createTemplate(_PS_MODULE_DIR_.'modulename/views/templates/admin/preform.tpl');
    
        $tpl->assign(array(
            'carts' => $carts,
        ));
    
        $html = $tpl->fetch();
    
        return $html.parent::renderList();
    }
    
    public function renderForm()
    {
        if (!$this->loadObject(true)) {
            return;
        }
    
        $obj = $this->loadObject(true);
    
        if (isset($obj->id)) {
            $this->display = 'edit';
        } else {
            $this->display = 'add';
        }
    
        $array_submit = array(
            array(
                'type' => 'select',
                'label' => $this->l('Cart :'),
                'name' => 'id_cart',
                'options' => array(
                    'query' => Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'cart WHERE id_cart > 0 ORDER BY id_cart DESC LIMIT 0,500'),
                    'id' => 'id_cart',
                    'name' => 'id_cart',
                ),
            ),
            array(
                'type' => 'text',
                'label' => $this->l('Quantity translation here'),
                'hint' => $this->l('Description and translation here'),
                'name' => 'quantity',
            ),
        );
    
        $this->fields_form[0]['form'] = array(
            'tinymce' => false,
            'legend' => array(
                'title' => $this->l('Form title'),
            ),
            'input' => $array_submit,
            'submit' => array(
                'title' => $this->l('Save'),
                'class' => 'btn btn-default',
            ),
        );
    
        $this->multiple_fieldsets = true;
    
        return parent::renderForm();
    }
    }