Jump to content

HelperList filter not working


dvi

Recommended Posts

Hello,

Thank you in advance. Recently I created a module extension for admin order controller, with more columns and filters in the backend.

But I don't understand, when I apply one search with filter the page redirects to the index page of modules, and not apply the filter correctly.

Can someone please help me??

I attached my helper code:

$helper_list = New HelperList();
    $helper_list->module = $this;
    $helper_list->title = $this->l('Todos los pedidos');
    //$helper_list->shopLinkType = '';
    //$helper->table = 'orders';
    //$helper_list->no_link = true;
    $helper_list->show_toolbar = true;
    $helper_list->simple_header = false;
    $helper_list->identifier = 'id_order';
    $helper_list->token = Tools::getAdminTokenLite('AdminModules');
    $helper_list->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
    $helper_list->actions = array('editOrder');
    $helper_list->listTotal = count($pedidos);
 
    return $helper_list->generateList($pedidos, $fields_list);
 
  }

Thank you!!

Link to comment
Share on other sites

Ok, the HelperList inside a Module::getContent() is not a good idea because search and filters are handled in AdminController::initProcess(), when you are in a Module::getContent() your assignment of list is done after AdminController::init() so it's not working.

See https://github.com/PrestaShop/PrestaShop/blob/70d12a5ffbd9f1123e76cded1be8d2f78d5f015c/classes/controller/AdminController.php#L2853

You should create an ModuleAdminController to access all useful method available for helpers.

I don't use Module::getContent() anymore in my modules, in fact I just use it to make a redirection to my ModuleAdminController.

If you use an HelperList inside a Module::getContent() you cannot use advanced features of this helper which is designed to be used in an AdminController, usage in Module::getContent() is tricky, I think it's a bad practice.

Did you know how to create an ModuleAdminController ?

Link to comment
Share on other sites

I don't know how to do that. You can see this: the helper its not in the getcontent..

public function getContent() {
 
    $this->_html .= $this->initListPedidos();
 
    return $this->_html;
  }
Edited by dvi (see edit history)
Link to comment
Share on other sites

Ok I will try it, and then in 

class Pedidos extends ModuleAdminController {
 
here in the ModuleAdminController I have to put all the helper list, right??
Link to comment
Share on other sites

You still need to have a class Module inside your module and you have to create a new file in /modules/pedidos/controllers/admin/AdminPedidosController.php

class AdminPedidosController extends ModuleAdminController
[Your code here]

Here some example of code you have to write inside your Module class

if (!defined('_PS_VERSION_')) {
    exit;
}

class Pedidos extends Module
{
    /**
     * Name of your ModuleAdminController
     */
    const MODULE_ADMIN_CONTROLLER = 'AdminPedidos';

    /**
     * @var array List of hooks you use
     */
    public $hooks = array(
        'displayHome', // For example
    );

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->name = 'pedidos';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Pedidos';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Pedidos');
        $this->description = $this->l('Pedidos description');
    }

    /**
     * Install Module
     *
     * @return bool
     */
    public function install()
    {
        return parent::install()
            && $this->registerHook($this->hooks)
            && $this->installTabs()
        ;
    }

    /**
     * Install Tabs
     *
     * @return bool
     */
    public function installTabs()
    {
        if (Tab::getIdFromClassName(static::MODULE_ADMIN_CONTROLLER)) {
            return true;
        }

        $tab = new Tab();
        $tab->class_name = static::MODULE_ADMIN_CONTROLLER;
        $tab->module = $this->name;
        $tab->active = true;
        $tab->id_parent = -1;
        $tab->name = array_fill_keys(
            Language::getIDs(false),
            $this->displayName
        );

        return $tab->add();
    }

    /**
     * Uninstall Module
     *
     * @return bool
     */
    public function uninstall()
    {
        return parent::uninstall()
            && $this->uninstallTabs()
        ;
    }

    /**
     * Uninstall Tabs
     *
     * @return bool
     */
    public function uninstallTabs()
    {
        $id_tab = (int) Tab::getIdFromClassName(static::MODULE_ADMIN_CONTROLLER);

        if ($id_tab) {
            $tab = new Tab($id_tab);
            return $tab->delete();
        }

        return true;
    }

    /**
     * Redirect to your ModuleAdminController when click on Configure button
     */
    public function getContent()
    {
        Tools::redirectAdmin($this->context->link->getAdminLink(static::MODULE_ADMIN_CONTROLLER));
    }

    /**
     * Display something on homepage
     *
     * @param array $params
     *
     * @return string
     */
    public function hookDisplayHome($params)
    {
        $this->smarty->assign(array(
            'hook_name' => 'hookDisplayHome', // For example
        ));

        return $this->display(__FILE__, 'displayHome.tpl');
    }
}

 

Edited by Janett (see edit history)
  • Thanks 1
Link to comment
Share on other sites

Thank you for your patience. I already have a class with like type of example code, but I miss the ModuleAdminController, what code put here?

This is a fragment of class code:

class Pedidos extends Module {
  protected $_html = '';
 
  public function __construct() {
    $this->name = 'sgrsh';
    $this->tab = 'thhth';
    $this->version = '1.6.1';
    $this->author = 'tddhdt';
    $this->need_instance = 0;
    $this->secure_key = Tools::encrypt($this->name);
    $this->bootstrap = true;
 
    parent::__construct();
 
    $this->displayName = $this->l('dhthtdhdt');
    $this->description = $this->l('Consulta.');
    $this->ps_versions_compliancy = array('min' => '1.6.0.4', 'max' => '1.6.99.99');
  }
 
  /**
   * @see Module::install()
   */
  public function install() {
    /* Adds Module */
    if (parent::install()) {
 
    }
 
    return true;
  }
 
  /**
   * @see Module::uninstall()
   */
  public function uninstall() {
 
    return true;
  }
Link to comment
Share on other sites

Only display existing data of prestashop orders. Now when I enter at the module and use the filters to search, the page reload or goes back to the modules page. But never applies and show the data. The only thing that works, is the DESC and ORDER BY.

if (Tools::getValue('configurationOrderby')) {
      $orderway = Tools::getValue('configurationOrderway');
      $orderby = Tools::getValue('configurationOrderby');
      $sql .= ' ORDER BY ' . $orderby . ' ' . $orderway;
    } else {
      $sql .= ' ORDER BY ps_orders.id_order DESC';
    }
    $pedidos = Db::getInstance()->executeS($sql);
Link to comment
Share on other sites

In this case, you just have to looks in AdminOrders.php to get your inspiration, like this :

/modules/pedidos/controllers/admin/AdminPedidosController.php

<?php

class AdminPedidosController extends ModuleAdminController
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->bootstrap = true;
        $this->table = 'order';
        $this->className = 'Order';
        $this->lang = false;
        $this->explicitSelect = true;
        $this->allow_export = true;
        $this->deleted = false;
        $this->addRowAction('view');

        parent::__construct();

        $this->_select = '
		a.id_currency,
		CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
		osl.`name` AS `osname`,
		os.`color`,
		IF(a.valid, 1, 0) badge_success';

        $this->_join = '
		LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = a.`id_customer`)
		LEFT JOIN `' . _DB_PREFIX_ . 'order_state` os ON (os.`id_order_state` = a.`current_state`)
		LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = ' . (int) $this->context->language->id . ')';
        $this->_orderBy = 'id_order';
        $this->_orderWay = 'DESC';
        $this->_use_found_rows = true;

        $statuses = [];
        foreach (OrderState::getOrderStates((int) $this->context->language->id) as $status) {
            $statuses[(int) $status['id_order_state']] = $status['name'];
        }

        $this->fields_list = array(
            'id_order' => array(
                'title' => $this->l('ID'),
                'align' => 'text-center',
                'class' => 'fixed-width-xs',
            ),
            'reference' => array(
                'title' => $this->l('Reference'),
            ),
            'customer' => array(
                'title' => $this->l('Customer'),
                'havingFilter' => true,
            ),
            'total_paid_tax_incl' => array(
                'title' => $this->l('Total'),
                'align' => 'text-right',
                'type' => 'price',
                'currency' => true,
                'callback' => 'setOrderCurrency',
                'badge_success' => true,
            ),
            'payment' => array(
                'title' => $this->l('Payment'),
            ),
            'osname' => array(
                'title' => $this->l('Status'),
                'type' => 'select',
                'color' => 'color',
                'list' => $statuses,
                'filter_key' => 'os!id_order_state',
                'filter_type' => 'int',
                'order_key' => 'osname',
            ),
            'date_add' => array(
                'title' => $this->l('Date'),
                'align' => 'text-right',
                'type' => 'datetime',
                'filter_key' => 'a!date_add',
            ),
        );

        $this->shopLinkType = 'shop';
        $this->shopShareDatas = Shop::SHARE_ORDER;
    }

    /**
     * Callback for total_paid_tax_incl column to display price
     *
     * @param string $echo Value of this column
     * @param array $tr All columns of this row
     *
     * @return string
     */
    public static function setOrderCurrency($echo, $tr)
    {
        if (!empty($tr['id_currency'])) {
            $idCurrency = (int) $tr['id_currency'];
        } else {
            $order = new Order($tr['id_order']);
            $idCurrency = (int) $order->id_currency;
        }

        return Tools::displayPrice($echo, $idCurrency);
    }
}

 

Edited by Janett (see edit history)
Link to comment
Share on other sites

Yes, but please note I'm in vacation, I don't have computer, so I can reply really slowly especially for this type of demand. I can only do that on my free time. So please be explicit in order to not lose my time to ask information.

Link to comment
Share on other sites

30 minutes ago, Janett said:

Yes, but please note I'm in vacation, I don't have computer, so I can reply really slowly especially for this type of demand. I can only do that on my free time. So please be explicit in order to not lose my time to ask information.

Oh thank you in advance. This is my code of pedidos.php, then I only need an idea to continue and do the controller. Note my level of PHP is null. Remember that in my case when I do a search in the order list of module, the filters not works and the page redirect to index, the same with pagination, etc. When you have time and you want please check it. Thank you!!

sent it by inbox
Edited by dvi
private (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...