PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/helper/HelperList.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 720 lines | 487 code | 111 blank | 122 comment | 114 complexity | b3465bbcce8b4936a786b61f7a3c94fc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class HelperListCore extends Helper
  30. {
  31. /** @var array Cache for query results */
  32. protected $_list = array();
  33. /** @var integer Number of results in list */
  34. public $listTotal = 0;
  35. /** @var array WHERE clause determined by filter fields */
  36. protected $_filter;
  37. /** @var array Number of results in list per page (used in select field) */
  38. public $_pagination = array(20, 50, 100, 300, 1000);
  39. /** @var integer Default number of results in list per page */
  40. public $_default_pagination = 50;
  41. /** @var string ORDER BY clause determined by field/arrows in list header */
  42. public $orderBy;
  43. /** @var string Default ORDER BY clause when $orderBy is not defined */
  44. public $_defaultOrderBy = false;
  45. /** @var array : list of vars for button delete*/
  46. public $tpl_delete_link_vars = array();
  47. /** @var string Order way (ASC, DESC) determined by arrows in list header */
  48. public $orderWay;
  49. public $identifier;
  50. protected $deleted = 0;
  51. /** @var array $cache_lang use to cache texts in current language */
  52. public static $cache_lang = array();
  53. public $is_cms = false;
  54. public $position_identifier;
  55. public $table_id;
  56. /**
  57. * @var array Customize list display
  58. *
  59. * align : determine value alignment
  60. * prefix : displayed before value
  61. * suffix : displayed after value
  62. * image : object image
  63. * icon : icon determined by values
  64. * active : allow to toggle status
  65. */
  66. protected $fields_list;
  67. /** @var boolean Content line is clickable if true */
  68. public $no_link = false;
  69. protected $header_tpl = 'list_header.tpl';
  70. protected $content_tpl = 'list_content.tpl';
  71. protected $footer_tpl = 'list_footer.tpl';
  72. /** @var array list of required actions for each list row */
  73. public $actions = array();
  74. /** @var array list of row ids associated with a given action for witch this action have to not be available */
  75. public $list_skip_actions = array();
  76. public $bulk_actions = false;
  77. public $force_show_bulk_actions = false;
  78. public $specificConfirmDelete = null;
  79. public $colorOnBackground;
  80. /** @var bool If true, activates color on hover */
  81. public $row_hover = true;
  82. /** @var if not null, a title will be added on that list */
  83. public $title = null;
  84. /** @var boolean ask for simple header : no filters, no paginations and no sorting */
  85. public $simple_header = false;
  86. public $ajax_params = array();
  87. public function __construct()
  88. {
  89. $this->base_folder = 'helpers/list/';
  90. $this->base_tpl = 'list.tpl';
  91. parent::__construct();
  92. }
  93. /**
  94. * Return an html list given the data to fill it up
  95. *
  96. * @param array $list entries to display (rows)
  97. * @param array $fields_display fields (cols)
  98. * @return string html
  99. */
  100. public function generateList($list, $fields_display)
  101. {
  102. // Append when we get a syntax error in SQL query
  103. if ($list === false)
  104. {
  105. $this->context->controller->warnings[] = $this->l('Bad SQL query', 'Helper');
  106. return false;
  107. }
  108. $this->tpl = $this->createTemplate($this->base_tpl);
  109. $this->header_tpl = $this->createTemplate($this->header_tpl);
  110. $this->content_tpl = $this->createTemplate($this->content_tpl);
  111. $this->footer_tpl = $this->createTemplate($this->footer_tpl);
  112. $this->_list = $list;
  113. $this->fields_list = $fields_display;
  114. $this->orderBy = preg_replace('/^([a-z _]*!)/Ui', '', $this->orderBy);
  115. $this->orderWay = preg_replace('/^([a-z _]*!)/Ui', '', $this->orderWay);
  116. $this->tpl->assign(array(
  117. 'header' => $this->displayListHeader(), // Display list header (filtering, pagination and column names)
  118. 'content' => $this->displayListContent(), // Show the content of the table
  119. 'footer' => $this->displayListFooter() // Close list table and submit button
  120. ));
  121. return parent::generate();
  122. }
  123. /**
  124. * Fetch the template for action enable
  125. *
  126. * @param string $token
  127. * @param int $id
  128. * @param int $value state enabled or not
  129. * @param string $active status
  130. * @param int $id_category
  131. * @param int $id_product
  132. * @return string
  133. */
  134. public function displayEnableLink($token, $id, $value, $active, $id_category = null, $id_product = null, $ajax = false)
  135. {
  136. $tpl_enable = $this->createTemplate('list_action_enable.tpl');
  137. $tpl_enable->assign(array(
  138. 'ajax' => $ajax,
  139. 'enabled' => (bool)$value,
  140. 'url_enable' => $this->currentIndex.'&'.$this->identifier.'='.(int)$id.'&'.$active.$this->table.($ajax ? '&action='.$active.$this->table.'&ajax='.(int)$ajax : '').
  141. ((int)$id_category && (int)$id_product ? '&id_category='.(int)$id_category : '').'&token='.($token != null ? $token : $this->token)
  142. ));
  143. return $tpl_enable->fetch();
  144. }
  145. public function displayListContent()
  146. {
  147. if (isset($this->fields_list['position']))
  148. {
  149. if ($this->position_identifier)
  150. if (isset($this->position_group_identifier))
  151. $position_group_identifier = Tools::getIsset($this->position_group_identifier) ? Tools::getValue($this->position_group_identifier) : $this->position_group_identifier;
  152. else
  153. $position_group_identifier = (int)Tools::getValue('id_'.($this->is_cms ? 'cms_' : '').'category', ($this->is_cms ? '1' : Category::getRootCategory()->id ));
  154. else
  155. $position_group_identifier = Category::getRootCategory()->id;
  156. $positions = array_map(create_function('$elem', 'return (int)($elem[\'position\']);'), $this->_list);
  157. sort($positions);
  158. }
  159. // key_to_get is used to display the correct product category or cms category after a position change
  160. $identifier = in_array($this->identifier, array('id_category', 'id_cms_category')) ? '_parent' : '';
  161. if ($identifier)
  162. $key_to_get = 'id_'.($this->is_cms ? 'cms_' : '').'category'.$identifier;
  163. foreach ($this->_list as $index => $tr)
  164. {
  165. $id = null;
  166. if (isset($tr[$this->identifier]))
  167. $id = $tr[$this->identifier];
  168. $name = isset($tr['name']) ? $tr['name'] : null;
  169. if ($this->shopLinkType)
  170. $this->_list[$index]['short_shop_name'] = Tools::strlen($tr['shop_name']) > 15 ? Tools::substr($tr['shop_name'], 0, 15).'...' : $tr['shop_name'];
  171. $is_first = true;
  172. // Check all available actions to add to the current list row
  173. foreach ($this->actions as $action)
  174. {
  175. //Check if the action is available for the current row
  176. if (!array_key_exists($action, $this->list_skip_actions) || !in_array($id, $this->list_skip_actions[$action]))
  177. {
  178. $method_name = 'display'.ucfirst($action).'Link';
  179. if (method_exists($this->context->controller, $method_name))
  180. $this->_list[$index][$action] = $this->context->controller->$method_name($this->token, $id, $name);
  181. elseif ($this->module instanceof Module && method_exists($this->module, $method_name))
  182. $this->_list[$index][$action] = $this->module->$method_name($this->token, $id, $name);
  183. elseif (method_exists($this, $method_name))
  184. $this->_list[$index][$action] = $this->$method_name($this->token, $id, $name);
  185. }
  186. if ($is_first && isset($this->_list[$index][$action])) {
  187. $is_first = false;
  188. if (!preg_match('/a\s*.*class/', $this->_list[$index][$action]))
  189. $this->_list[$index][$action] = preg_replace('/href\s*=\s*\"([^\"]*)\"/',
  190. 'href="$1" class="btn btn-default"', $this->_list[$index][$action]);
  191. elseif (!preg_match('/a\s*.*class\s*=\s*\".*btn.*\"/', $this->_list[$index][$action]))
  192. $this->_list[$index][$action] = preg_replace('/a(\s*.*)class\s*=\s*\"(.*)\"/',
  193. 'a $1 class="$2 btn btn-default"', $this->_list[$index][$action]);
  194. }
  195. }
  196. // @todo skip action for bulk actions
  197. // $this->_list[$index]['has_bulk_actions'] = true;
  198. foreach ($this->fields_list as $key => $params)
  199. {
  200. $tmp = explode('!', $key);
  201. $key = isset($tmp[1]) ? $tmp[1] : $tmp[0];
  202. if (isset($params['active']))
  203. {
  204. // If method is defined in calling controller, use it instead of the Helper method
  205. if (method_exists($this->context->controller, 'displayEnableLink'))
  206. $calling_obj = $this->context->controller;
  207. elseif (isset($this->module) && method_exists($this->module, 'displayEnableLink'))
  208. $calling_obj = $this->module;
  209. else
  210. $calling_obj = $this;
  211. if (!isset($params['ajax']))
  212. $params['ajax'] = false;
  213. $this->_list[$index][$key] = $calling_obj->displayEnableLink(
  214. $this->token,
  215. $id,
  216. $tr[$key],
  217. $params['active'],
  218. Tools::getValue('id_category'),
  219. Tools::getValue('id_product'),
  220. $params['ajax']
  221. );
  222. }
  223. elseif (isset($params['activeVisu']))
  224. $this->_list[$index][$key] = (bool)$tr[$key];
  225. elseif (isset($params['position']))
  226. {
  227. $this->_list[$index][$key] = array(
  228. 'position' => $tr[$key],
  229. 'position_url_down' => $this->currentIndex.
  230. (isset($key_to_get) ? '&'.$key_to_get.'='.(int)$position_group_identifier : '').
  231. '&'.$this->position_identifier.'='.$id.
  232. '&way=1&position='.((int)$tr['position'] + 1).'&token='.$this->token,
  233. 'position_url_up' => $this->currentIndex.
  234. (isset($key_to_get) ? '&'.$key_to_get.'='.(int)$position_group_identifier : '').
  235. '&'.$this->position_identifier.'='.$id.
  236. '&way=0&position='.((int)$tr['position'] - 1).'&token='.$this->token
  237. );
  238. }
  239. elseif (isset($params['image']))
  240. {
  241. // item_id is the product id in a product image context, else it is the image id.
  242. $item_id = isset($params['image_id']) ? $tr[$params['image_id']] : $id;
  243. if ($params['image'] != 'p' || Configuration::get('PS_LEGACY_IMAGES'))
  244. $path_to_image = _PS_IMG_DIR_.$params['image'].'/'.$item_id.(isset($tr['id_image']) ? '-'.(int)$tr['id_image'] : '').'.'.$this->imageType;
  245. else
  246. $path_to_image = _PS_IMG_DIR_.$params['image'].'/'.Image::getImgFolderStatic($tr['id_image']).(int)$tr['id_image'].'.'.$this->imageType;
  247. $this->_list[$index][$key] = ImageManager::thumbnail($path_to_image, $this->table.'_mini_'.$item_id.'_'.$this->context->shop->id.'.'.$this->imageType, 45, $this->imageType);
  248. }
  249. elseif (isset($params['icon']) && isset($tr[$key]) && (isset($params['icon'][$tr[$key]]) || isset($params['icon']['default'])))
  250. {
  251. if (!$this->bootstrap)
  252. {
  253. if (isset($params['icon'][$tr[$key]]) && is_array($params['icon'][$tr[$key]]))
  254. $this->_list[$index][$key] = array(
  255. 'src' => $params['icon'][$tr[$key]]['src'],
  256. 'alt' => $params['icon'][$tr[$key]]['alt'],
  257. );
  258. else
  259. $this->_list[$index][$key] = array(
  260. 'src' => isset($params['icon'][$tr[$key]]) ? $params['icon'][$tr[$key]] : $params['icon']['default'],
  261. 'alt' => isset($params['icon'][$tr[$key]]) ? $params['icon'][$tr[$key]] : $params['icon']['default'],
  262. );
  263. }
  264. else
  265. if (isset($params['icon'][$tr[$key]]))
  266. $this->_list[$index][$key] = $params['icon'][$tr[$key]];
  267. }
  268. elseif (isset($params['type']) && $params['type'] == 'float')
  269. $this->_list[$index][$key] = rtrim(rtrim($tr[$key], '0'), '.');
  270. elseif (isset($tr[$key]))
  271. {
  272. $echo = $tr[$key];
  273. if (isset($params['callback']))
  274. {
  275. $callback_obj = (isset($params['callback_object'])) ? $params['callback_object'] : $this->context->controller;
  276. $this->_list[$index][$key] = call_user_func_array(array($callback_obj, $params['callback']), array($echo, $tr));
  277. }
  278. else
  279. $this->_list[$index][$key] = $echo;
  280. }
  281. }
  282. }
  283. $this->content_tpl->assign(array_merge($this->tpl_vars, array(
  284. 'shop_link_type' => $this->shopLinkType,
  285. 'name' => isset($name) ? $name : null,
  286. 'position_identifier' => $this->position_identifier,
  287. 'identifier' => $this->identifier,
  288. 'table' => $this->table,
  289. 'token' => $this->token,
  290. 'color_on_bg' => $this->colorOnBackground,
  291. 'position_group_identifier' => isset($position_group_identifier) ? $position_group_identifier : false,
  292. 'bulk_actions' => $this->bulk_actions,
  293. 'positions' => isset($positions) ? $positions : null,
  294. 'order_by' => $this->orderBy,
  295. 'order_way' => $this->orderWay,
  296. 'is_cms' => $this->is_cms,
  297. 'fields_display' => $this->fields_list,
  298. 'list' => $this->_list,
  299. 'actions' => $this->actions,
  300. 'no_link' => $this->no_link,
  301. 'current_index' => $this->currentIndex,
  302. 'view' => in_array('view', $this->actions),
  303. 'edit' => in_array('edit', $this->actions),
  304. 'has_actions' => !empty($this->actions),
  305. 'list_skip_actions' => $this->list_skip_actions,
  306. 'row_hover' => $this->row_hover,
  307. 'list_id' => isset($this->list_id) ? $this->list_id : $this->table,
  308. 'checked_boxes' => Tools::getValue((isset($this->list_id) ? $this->list_id : $this->table).'Box')
  309. )));
  310. return $this->content_tpl->fetch();
  311. }
  312. /**
  313. * Display duplicate action link
  314. */
  315. public function displayDuplicateLink($token = null, $id, $name = null)
  316. {
  317. $tpl = $this->createTemplate('list_action_duplicate.tpl');
  318. if (!array_key_exists('Bad SQL query', self::$cache_lang))
  319. self::$cache_lang['Duplicate'] = $this->l('Duplicate', 'Helper');
  320. if (!array_key_exists('Copy images too?', self::$cache_lang))
  321. self::$cache_lang['Copy images too?'] = $this->l('This will copy the images too. If you wish to proceed, click "Yes". If not, click "No".', 'Helper');
  322. $duplicate = $this->currentIndex.'&'.$this->identifier.'='.$id.'&duplicate'.$this->table;
  323. $confirm = self::$cache_lang['Copy images too?'];
  324. if (($this->table == 'product') && !(bool)Image::getImages($this->context->language->id, (int)$id))
  325. $confirm = '';
  326. $tpl->assign(array(
  327. 'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&view'.$this->table.'&token='.($token != null ? $token : $this->token),
  328. 'action' => self::$cache_lang['Duplicate'],
  329. 'confirm' => $confirm,
  330. 'location_ok' => $duplicate.'&token='.($token != null ? $token : $this->token),
  331. 'location_ko' => $duplicate.'&noimage=1&token='.($token ? $token : $this->token),
  332. ));
  333. return $tpl->fetch();
  334. }
  335. /**
  336. * Display action show details of a table row
  337. * This action need an ajax request with a return like this:
  338. * {
  339. * use_parent_structure: true // If false, data need to be an html
  340. * data:
  341. * [
  342. * {field_name: 'value'}
  343. * ],
  344. * fields_display: // attribute $fields_list of the admin controller
  345. * }
  346. * or somethins like this:
  347. * {
  348. * use_parent_structure: false // If false, data need to be an html
  349. * data:
  350. * '<p>My html content</p>',
  351. * fields_display: // attribute $fields_list of the admin controller
  352. * }
  353. */
  354. public function displayDetailsLink($token = null, $id, $name = null)
  355. {
  356. $tpl = $this->createTemplate('list_action_details.tpl');
  357. if (!array_key_exists('Details', self::$cache_lang))
  358. self::$cache_lang['Details'] = $this->l('Details', 'Helper');
  359. $ajax_params = $this->ajax_params;
  360. if (!is_array($ajax_params) || !isset($ajax_params['action']))
  361. $ajax_params['action'] = 'details';
  362. $tpl->assign(array(
  363. 'id' => $id,
  364. 'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&details'.$this->table.'&token='.($token != null ? $token : $this->token),
  365. 'controller' => str_replace('Controller', '', get_class($this->context->controller)),
  366. 'token' => $token != null ? $token : $this->token,
  367. 'action' => self::$cache_lang['Details'],
  368. 'params' => $ajax_params,
  369. 'json_params' => Tools::jsonEncode($ajax_params)
  370. ));
  371. return $tpl->fetch();
  372. }
  373. /**
  374. * Display view action link
  375. */
  376. public function displayViewLink($token = null, $id, $name = null)
  377. {
  378. $tpl = $this->createTemplate('list_action_view.tpl');
  379. if (!array_key_exists('View', self::$cache_lang))
  380. self::$cache_lang['View'] = $this->l('View', 'Helper');
  381. $tpl->assign(array(
  382. 'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&view'.$this->table.'&token='.($token != null ? $token : $this->token),
  383. 'action' => self::$cache_lang['View'],
  384. ));
  385. return $tpl->fetch();
  386. }
  387. /**
  388. * Display edit action link
  389. */
  390. public function displayEditLink($token = null, $id, $name = null)
  391. {
  392. $tpl = $this->createTemplate('list_action_edit.tpl');
  393. if (!array_key_exists('Edit', self::$cache_lang))
  394. self::$cache_lang['Edit'] = $this->l('Edit', 'Helper');
  395. $tpl->assign(array(
  396. 'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&update'.$this->table.'&token='.($token != null ? $token : $this->token),
  397. 'action' => self::$cache_lang['Edit'],
  398. 'id' => $id
  399. ));
  400. return $tpl->fetch();
  401. }
  402. /**
  403. * Display delete action link
  404. */
  405. public function displayDeleteLink($token = null, $id, $name = null)
  406. {
  407. $tpl = $this->createTemplate('list_action_delete.tpl');
  408. if (!array_key_exists('Delete', self::$cache_lang))
  409. self::$cache_lang['Delete'] = $this->l('Delete', 'Helper');
  410. if (!array_key_exists('DeleteItem', self::$cache_lang))
  411. self::$cache_lang['DeleteItem'] = $this->l('Delete selected item?', 'Helper', true, false);
  412. if (!array_key_exists('Name', self::$cache_lang))
  413. self::$cache_lang['Name'] = $this->l('Name:', 'Helper', true, false);
  414. if (!is_null($name))
  415. $name = addcslashes('\n\n'.self::$cache_lang['Name'].' '.$name, '\'');
  416. $data = array(
  417. $this->identifier => $id,
  418. 'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&token='.($token != null ? $token : $this->token),
  419. 'action' => self::$cache_lang['Delete'],
  420. );
  421. if ($this->specificConfirmDelete !== false)
  422. $data['confirm'] = !is_null($this->specificConfirmDelete) ? '\r'.$this->specificConfirmDelete : Tools::safeOutput(self::$cache_lang['DeleteItem'].$name);
  423. $tpl->assign(array_merge($this->tpl_delete_link_vars, $data));
  424. return $tpl->fetch();
  425. }
  426. /**
  427. * Display delete action link
  428. */
  429. public function displayDefaultLink($token = null, $id, $name = null)
  430. {
  431. $tpl = $this->createTemplate('list_action_default.tpl');
  432. if (!array_key_exists('Default', self::$cache_lang))
  433. self::$cache_lang['Default'] = $this->l('Default', 'Helper');
  434. $tpl->assign(array_merge($this->tpl_delete_link_vars, array(
  435. 'href' => $this->currentIndex.'&'.$this->identifier.'='.(int)$id.'&delete'.$this->table.'&token='.($token != null ? $token : $this->token),
  436. 'action' => self::$cache_lang['Default'],
  437. 'name' => $name,
  438. )));
  439. return $tpl->fetch();
  440. }
  441. /**
  442. * Display list header (filtering, pagination and column names)
  443. */
  444. public function displayListHeader()
  445. {
  446. if (!isset($this->list_id))
  447. $this->list_id = $this->table;
  448. $id_cat = (int)Tools::getValue('id_'.($this->is_cms ? 'cms_' : '').'category');
  449. if (!isset($token) || empty($token))
  450. $token = $this->token;
  451. /* Determine total page number */
  452. $pagination = $this->_default_pagination;
  453. if (in_array((int)Tools::getValue($this->list_id.'_pagination'), $this->_pagination))
  454. $pagination = (int)Tools::getValue($this->list_id.'_pagination');
  455. elseif (isset($this->context->cookie->{$this->list_id.'_pagination'}) && $this->context->cookie->{$this->list_id.'_pagination'})
  456. $pagination = $this->context->cookie->{$this->list_id.'_pagination'};
  457. $total_pages = max(1, ceil($this->listTotal / $pagination));
  458. $identifier = Tools::getIsset($this->identifier) ? '&'.$this->identifier.'='.(int)Tools::getValue($this->identifier) : '';
  459. $order = '';
  460. if (Tools::getIsset($this->table.'Orderby'))
  461. $order = '&'.$this->table.'Orderby='.urlencode($this->orderBy).'&'.$this->table.'Orderway='.urlencode(strtolower($this->orderWay));
  462. $action = $this->currentIndex.$identifier.'&token='.$token.'#'.$this->list_id;
  463. /* Determine current page number */
  464. $page = (int)Tools::getValue('submitFilter'.$this->list_id);
  465. if (!$page)
  466. $page = 1;
  467. if ($page > $total_pages)
  468. $page = $total_pages;
  469. /* Choose number of results per page */
  470. $selected_pagination = Tools::getValue($this->list_id.'_pagination',
  471. isset($this->context->cookie->{$this->list_id.'_pagination'}) ? $this->context->cookie->{$this->list_id.'_pagination'} : $this->_default_pagination
  472. );
  473. if (!isset($this->table_id) && $this->position_identifier && (int)Tools::getValue($this->position_identifier, 1))
  474. $this->table_id = substr($this->identifier, 3, strlen($this->identifier));
  475. if ($this->position_identifier && ($this->orderBy == 'position' && $this->orderWay != 'DESC'))
  476. $table_dnd = true;
  477. $prefix = isset($this->controller_name) ? str_replace(array('admin', 'controller'), '', Tools::strtolower($this->controller_name)) : '';
  478. $ajax = false;
  479. foreach ($this->fields_list as $key => $params)
  480. {
  481. if (!isset($params['type']))
  482. $params['type'] = 'text';
  483. $value_key = $prefix.$this->list_id.'Filter_'.(array_key_exists('filter_key', $params) && $key != 'active' ? $params['filter_key'] : $key);
  484. $value = Context::getContext()->cookie->{$value_key};
  485. if (!$value && Tools::getIsset($value_key))
  486. $value = Tools::getValue($value_key);
  487. switch ($params['type'])
  488. {
  489. case 'bool':
  490. if (isset($params['ajax']) && $params['ajax'])
  491. $ajax = true;
  492. break;
  493. case 'date':
  494. case 'datetime':
  495. if (is_string($value))
  496. $value = Tools::unSerialize($value);
  497. if (!Validate::isCleanHtml($value[0]) || !Validate::isCleanHtml($value[1]))
  498. $value = '';
  499. $name = $this->list_id.'Filter_'.(isset($params['filter_key']) ? $params['filter_key'] : $key);
  500. $name_id = str_replace('!', '__', $name);
  501. $params['id_date'] = $name_id;
  502. $params['name_date'] = $name;
  503. $this->context->controller->addJqueryUI('ui.datepicker');
  504. break;
  505. case 'select':
  506. foreach ($params['list'] as $option_value => $option_display)
  507. {
  508. if (isset(Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.$params['filter_key']})
  509. && Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.$params['filter_key']} == $option_value
  510. && Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.$params['filter_key']} != '')
  511. $this->fields_list[$key]['select'][$option_value]['selected'] = 'selected';
  512. }
  513. break;
  514. case 'text':
  515. if (!Validate::isCleanHtml($value))
  516. $value = '';
  517. }
  518. $params['value'] = $value;
  519. $this->fields_list[$key] = $params;
  520. }
  521. $has_value = false;
  522. $has_search_field = false;
  523. foreach ($this->fields_list as $key => $field)
  524. {
  525. if (isset($field['value']) && $field['value'] !== false && $field['value'] !== '')
  526. {
  527. if (is_array($field['value']) && trim(implode('', $field['value'])) == '')
  528. continue;
  529. $has_value = true;
  530. break;
  531. }
  532. if (!(isset($field['search']) && $field['search'] === false))
  533. $has_search_field = true;
  534. }
  535. Context::getContext()->smarty->assign(array(
  536. 'page' => $page,
  537. 'simple_header' => $this->simple_header,
  538. 'total_pages' => $total_pages,
  539. 'selected_pagination' => $selected_pagination,
  540. 'pagination' => $this->_pagination,
  541. 'list_total' => $this->listTotal,
  542. 'sql' => isset($this->sql) && $this->sql ? str_replace("\n", " ", str_replace("\r", "", $this->sql)) : false,
  543. 'token' => $this->token,
  544. 'table' => $this->table,
  545. 'bulk_actions' => $this->bulk_actions,
  546. 'show_toolbar' => $this->show_toolbar,
  547. 'toolbar_scroll' => $this->toolbar_scroll,
  548. 'toolbar_btn' => $this->toolbar_btn,
  549. 'has_bulk_actions' => $this->hasBulkActions($has_value),
  550. ));
  551. $this->header_tpl->assign(array_merge(array(
  552. 'ajax' => $ajax,
  553. 'title' => array_key_exists('title', $this->tpl_vars) ? $this->tpl_vars['title'] : $this->title,
  554. 'show_filters' => ((count($this->_list) > 1 && $has_search_field) || $has_value ),
  555. 'filters_has_value' => $has_value,
  556. 'currentIndex' => $this->currentIndex,
  557. 'action' => $action,
  558. 'is_order_position' => $this->position_identifier && $this->orderBy == 'position',
  559. 'order_way' => $this->orderWay,
  560. 'order_by' => $this->orderBy,
  561. 'fields_display' => $this->fields_list,
  562. 'delete' => in_array('delete', $this->actions),
  563. 'identifier' => $this->identifier,
  564. 'id_cat' => $id_cat,
  565. 'shop_link_type' => $this->shopLinkType,
  566. 'has_actions' => !empty($this->actions),
  567. 'table_id' => isset($this->table_id) ? $this->table_id : null,
  568. 'table_dnd' => isset($table_dnd) ? $table_dnd : null,
  569. 'name' => isset($name) ? $name : null,
  570. 'name_id' => isset($name_id) ? $name_id : null,
  571. 'row_hover' => $this->row_hover,
  572. 'list_id' => isset($this->list_id) ? $this->list_id : $this->table
  573. ), $this->tpl_vars));
  574. return $this->header_tpl->fetch();
  575. }
  576. public function hasBulkActions($has_value = false)
  577. {
  578. if ($this->force_show_bulk_actions)
  579. return true;
  580. if (count($this->_list) <= 1 && !$has_value)
  581. return false;
  582. if (isset($this->list_skip_actions) && count($this->list_skip_actions)
  583. && isset($this->bulk_actions) && is_array($this->bulk_actions) && count($this->bulk_actions))
  584. {
  585. foreach ($this->bulk_actions as $action => $data)
  586. if (array_key_exists($action, $this->list_skip_actions))
  587. {
  588. foreach ($this->_list as $key => $row)
  589. if (!in_array($row[$this->identifier], $this->list_skip_actions[$action]))
  590. return true;
  591. return false;
  592. }
  593. }
  594. return !empty($this->bulk_actions);
  595. }
  596. /**
  597. * Close list table and submit button
  598. */
  599. public function displayListFooter()
  600. {
  601. if (!isset($this->list_id))
  602. $this->list_id = $this->table;
  603. $this->footer_tpl->assign(array_merge($this->tpl_vars, array(
  604. 'current' => $this->currentIndex,
  605. 'list_id' => $this->list_id
  606. )));
  607. return $this->footer_tpl->fetch();
  608. }
  609. }