PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/helper/HelperList.php

https://github.com/netplayer/PrestaShop
PHP | 697 lines | 470 code | 105 blank | 122 comment | 107 complexity | a5b5bf0af4b7ea46459b7f5e73256b93 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, 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. protected $_pagination = array(20, 50, 100, 300, 1000);
  39. /** @var integer Default number of results in list per page */
  40. protected $_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' => Tools::safeOutput($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. // Check all available actions to add to the current list row
  172. foreach ($this->actions as $action)
  173. {
  174. //Check if the action is available for the current row
  175. if (!array_key_exists($action, $this->list_skip_actions) || !in_array($id, $this->list_skip_actions[$action]))
  176. {
  177. $method_name = 'display'.ucfirst($action).'Link';
  178. if (method_exists($this->context->controller, $method_name))
  179. $this->_list[$index][$action] = $this->context->controller->$method_name($this->token, $id, $name);
  180. else if ($this->module instanceof Module && method_exists($this->module, $method_name))
  181. $this->_list[$index][$action] = $this->module->$method_name($this->token, $id, $name);
  182. elseif (method_exists($this, $method_name))
  183. $this->_list[$index][$action] = $this->$method_name($this->token, $id, $name);
  184. }
  185. }
  186. // @todo skip action for bulk actions
  187. // $this->_list[$index]['has_bulk_actions'] = true;
  188. foreach ($this->fields_list as $key => $params)
  189. {
  190. $tmp = explode('!', $key);
  191. $key = isset($tmp[1]) ? $tmp[1] : $tmp[0];
  192. if (isset($params['active']))
  193. {
  194. // If method is defined in calling controller, use it instead of the Helper method
  195. if (method_exists($this->context->controller, 'displayEnableLink'))
  196. $calling_obj = $this->context->controller;
  197. elseif (isset($this->module) && method_exists($this->module, 'displayEnableLink'))
  198. $calling_obj = $this->module;
  199. else
  200. $calling_obj = $this;
  201. if (!isset($params['ajax']))
  202. $params['ajax'] = false;
  203. $this->_list[$index][$key] = $calling_obj->displayEnableLink(
  204. $this->token,
  205. $id,
  206. $tr[$key],
  207. $params['active'],
  208. Tools::getValue('id_category'),
  209. Tools::getValue('id_product'),
  210. $params['ajax']
  211. );
  212. }
  213. elseif (isset($params['activeVisu']))
  214. $this->_list[$index][$key] = (bool)$tr[$key];
  215. elseif (isset($params['position']))
  216. {
  217. $this->_list[$index][$key] = array(
  218. 'position' => $tr[$key],
  219. 'position_url_down' => $this->currentIndex.
  220. (isset($key_to_get) ? '&'.$key_to_get.'='.(int)$position_group_identifier : '').
  221. '&'.$this->position_identifier.'='.$id.
  222. '&way=1&position='.((int)$tr['position'] + 1).'&token='.$this->token,
  223. 'position_url_up' => $this->currentIndex.
  224. (isset($key_to_get) ? '&'.$key_to_get.'='.(int)$position_group_identifier : '').
  225. '&'.$this->position_identifier.'='.$id.
  226. '&way=0&position='.((int)$tr['position'] - 1).'&token='.$this->token
  227. );
  228. }
  229. elseif (isset($params['image']))
  230. {
  231. // item_id is the product id in a product image context, else it is the image id.
  232. $item_id = isset($params['image_id']) ? $tr[$params['image_id']] : $id;
  233. if ($params['image'] != 'p' || Configuration::get('PS_LEGACY_IMAGES'))
  234. $path_to_image = _PS_IMG_DIR_.$params['image'].'/'.$item_id.(isset($tr['id_image']) ? '-'.(int)$tr['id_image'] : '').'.'.$this->imageType;
  235. else
  236. $path_to_image = _PS_IMG_DIR_.$params['image'].'/'.Image::getImgFolderStatic($tr['id_image']).(int)$tr['id_image'].'.'.$this->imageType;
  237. $this->_list[$index][$key] = ImageManager::thumbnail($path_to_image, $this->table.'_mini_'.$item_id.'_'.$this->context->shop->id.'.'.$this->imageType, 45, $this->imageType);
  238. }
  239. elseif (isset($params['icon']) && isset($tr[$key]) && (isset($params['icon'][$tr[$key]]) || isset($params['icon']['default'])))
  240. {
  241. if (!$this->bootstrap)
  242. {
  243. if (isset($params['icon'][$tr[$key]]) && is_array($params['icon'][$tr[$key]]))
  244. $this->_list[$index][$key] = array(
  245. 'src' => $params['icon'][$tr[$key]]['src'],
  246. 'alt' => $params['icon'][$tr[$key]]['alt'],
  247. );
  248. else
  249. $this->_list[$index][$key] = array(
  250. 'src' => isset($params['icon'][$tr[$key]]) ? $params['icon'][$tr[$key]] : $params['icon']['default'],
  251. 'alt' => isset($params['icon'][$tr[$key]]) ? $params['icon'][$tr[$key]] : $params['icon']['default'],
  252. );
  253. }
  254. else
  255. if (isset($params['icon'][$tr[$key]]))
  256. $this->_list[$index][$key] = $params['icon'][$tr[$key]];
  257. }
  258. elseif (isset($params['type']) && $params['type'] == 'float')
  259. $this->_list[$index][$key] = rtrim(rtrim($tr[$key], '0'), '.');
  260. elseif (isset($tr[$key]))
  261. {
  262. $echo = $tr[$key];
  263. if (isset($params['callback']))
  264. {
  265. $callback_obj = (isset($params['callback_object'])) ? $params['callback_object'] : $this->context->controller;
  266. $this->_list[$index][$key] = call_user_func_array(array($callback_obj, $params['callback']), array($echo, $tr));
  267. }
  268. else
  269. $this->_list[$index][$key] = $echo;
  270. }
  271. }
  272. }
  273. $this->content_tpl->assign(array_merge($this->tpl_vars, array(
  274. 'shop_link_type' => $this->shopLinkType,
  275. 'name' => isset($name) ? $name : null,
  276. 'position_identifier' => $this->position_identifier,
  277. 'identifier' => $this->identifier,
  278. 'table' => $this->table,
  279. 'token' => $this->token,
  280. 'color_on_bg' => $this->colorOnBackground,
  281. 'position_group_identifier' => isset($position_group_identifier) ? $position_group_identifier : false,
  282. 'bulk_actions' => $this->bulk_actions,
  283. 'positions' => isset($positions) ? $positions : null,
  284. 'order_by' => $this->orderBy,
  285. 'order_way' => $this->orderWay,
  286. 'is_cms' => $this->is_cms,
  287. 'fields_display' => $this->fields_list,
  288. 'list' => $this->_list,
  289. 'actions' => $this->actions,
  290. 'no_link' => $this->no_link,
  291. 'current_index' => $this->currentIndex,
  292. 'view' => in_array('view', $this->actions),
  293. 'edit' => in_array('edit', $this->actions),
  294. 'has_actions' => !empty($this->actions),
  295. 'list_skip_actions' => $this->list_skip_actions,
  296. 'row_hover' => $this->row_hover,
  297. 'list_id' => isset($this->list_id) ? $this->list_id : $this->table,
  298. 'checked_boxes' => Tools::getValue((isset($this->list_id) ? $this->list_id : $this->table).'Box')
  299. )));
  300. return $this->content_tpl->fetch();
  301. }
  302. /**
  303. * Display duplicate action link
  304. */
  305. public function displayDuplicateLink($token = null, $id, $name = null)
  306. {
  307. $tpl = $this->createTemplate('list_action_duplicate.tpl');
  308. if (!array_key_exists('Bad SQL query', self::$cache_lang))
  309. self::$cache_lang['Duplicate'] = $this->l('Duplicate', 'Helper');
  310. if (!array_key_exists('Copy images too?', self::$cache_lang))
  311. self::$cache_lang['Copy images too?'] = $this->l('Would you like to copy the images too?', 'Helper');
  312. $duplicate = $this->currentIndex.'&'.$this->identifier.'='.$id.'&duplicate'.$this->table;
  313. $tpl->assign(array(
  314. 'href' => Tools::safeOutput($this->currentIndex.'&'.$this->identifier.'='.$id.'&view'.$this->table.'&token='.($token != null ? $token : $this->token)),
  315. 'action' => self::$cache_lang['Duplicate'],
  316. 'confirm' => self::$cache_lang['Copy images too?'],
  317. 'location_ok' => Tools::safeOutput($duplicate.'&token='.($token != null ? $token : $this->token)),
  318. 'location_ko' => Tools::safeOutput($duplicate.'&noimage=1&token='.($token ? $token : $this->token)),
  319. ));
  320. return $tpl->fetch();
  321. }
  322. /**
  323. * Display action show details of a table row
  324. * This action need an ajax request with a return like this:
  325. * {
  326. * use_parent_structure: true // If false, data need to be an html
  327. * data:
  328. * [
  329. * {field_name: 'value'}
  330. * ],
  331. * fields_display: // attribute $fields_list of the admin controller
  332. * }
  333. * or somethins like this:
  334. * {
  335. * use_parent_structure: false // If false, data need to be an html
  336. * data:
  337. * '<p>My html content</p>',
  338. * fields_display: // attribute $fields_list of the admin controller
  339. * }
  340. */
  341. public function displayDetailsLink($token = null, $id, $name = null)
  342. {
  343. $tpl = $this->createTemplate('list_action_details.tpl');
  344. if (!array_key_exists('Details', self::$cache_lang))
  345. self::$cache_lang['Details'] = $this->l('Details', 'Helper');
  346. $ajax_params = $this->ajax_params;
  347. if (!is_array($ajax_params) || !isset($ajax_params['action']))
  348. $ajax_params['action'] = 'details';
  349. $tpl->assign(array(
  350. 'id' => Tools::safeOutput($id),
  351. 'href' => Tools::safeOutput($this->currentIndex.'&'.$this->identifier.'='.$id.'&details'.$this->table.'&token='.($token != null ? $token : $this->token)),
  352. 'controller' => str_replace('Controller', '', get_class($this->context->controller)),
  353. 'token' => Tools::safeOutput($token != null ? $token : $this->token),
  354. 'action' => self::$cache_lang['Details'],
  355. 'params' => $ajax_params,
  356. 'json_params' => Tools::jsonEncode($ajax_params)
  357. ));
  358. return $tpl->fetch();
  359. }
  360. /**
  361. * Display view action link
  362. */
  363. public function displayViewLink($token = null, $id, $name = null)
  364. {
  365. $tpl = $this->createTemplate('list_action_view.tpl');
  366. if (!array_key_exists('View', self::$cache_lang))
  367. self::$cache_lang['View'] = $this->l('View', 'Helper');
  368. $tpl->assign(array(
  369. 'href' => Tools::safeOutput($this->currentIndex.'&'.$this->identifier.'='.$id.'&view'.$this->table.'&token='.($token != null ? $token : $this->token)),
  370. 'action' => self::$cache_lang['View'],
  371. ));
  372. return $tpl->fetch();
  373. }
  374. /**
  375. * Display edit action link
  376. */
  377. public function displayEditLink($token = null, $id, $name = null)
  378. {
  379. $tpl = $this->createTemplate('list_action_edit.tpl');
  380. if (!array_key_exists('Edit', self::$cache_lang))
  381. self::$cache_lang['Edit'] = $this->l('Edit', 'Helper');
  382. $tpl->assign(array(
  383. 'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&update'.$this->table.'&token='.($token != null ? $token : $this->token),
  384. 'action' => self::$cache_lang['Edit'],
  385. 'id' => $id
  386. ));
  387. return $tpl->fetch();
  388. }
  389. /**
  390. * Display delete action link
  391. */
  392. public function displayDeleteLink($token = null, $id, $name = null)
  393. {
  394. $tpl = $this->createTemplate('list_action_delete.tpl');
  395. if (!array_key_exists('Delete', self::$cache_lang))
  396. self::$cache_lang['Delete'] = $this->l('Delete', 'Helper');
  397. if (!array_key_exists('DeleteItem', self::$cache_lang))
  398. self::$cache_lang['DeleteItem'] = $this->l('Delete selected item?', 'Helper', true, false);
  399. if (!array_key_exists('Name', self::$cache_lang))
  400. self::$cache_lang['Name'] = $this->l('Name:', 'Helper', true, false);
  401. if (!is_null($name))
  402. $name = addcslashes('\n\n'.self::$cache_lang['Name'].' '.$name, '\'');
  403. $data = array(
  404. $this->identifier => $id,
  405. 'href' => Tools::safeOutput($this->currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&token='.($token != null ? $token : $this->token)),
  406. 'action' => self::$cache_lang['Delete'],
  407. );
  408. if ($this->specificConfirmDelete !== false)
  409. $data['confirm'] = !is_null($this->specificConfirmDelete) ? '\r'.$this->specificConfirmDelete : Tools::safeOutput(self::$cache_lang['DeleteItem'].$name);
  410. $tpl->assign(array_merge($this->tpl_delete_link_vars, $data));
  411. return $tpl->fetch();
  412. }
  413. /**
  414. * Display delete action link
  415. */
  416. public function displayDefaultLink($token = null, $id, $name = null)
  417. {
  418. $tpl = $this->createTemplate('list_action_default.tpl');
  419. if (!array_key_exists('Default', self::$cache_lang))
  420. self::$cache_lang['Default'] = $this->l('Default', 'Helper');
  421. $tpl->assign(array_merge($this->tpl_delete_link_vars, array(
  422. 'href' => Tools::safeOutput($this->currentIndex).'&'.Tools::safeOutput($this->identifier).'='.(int)$id.'&delete'.Tools::safeOutput($this->table).'&token='.Tools::safeOutput(($token != null ? $token : $this->token)),
  423. 'action' => self::$cache_lang['Default'],
  424. 'name' => Tools::safeOutput($name),
  425. )));
  426. return $tpl->fetch();
  427. }
  428. /**
  429. * Display list header (filtering, pagination and column names)
  430. */
  431. public function displayListHeader()
  432. {
  433. if (!isset($this->list_id))
  434. $this->list_id = $this->table;
  435. $id_cat = (int)Tools::getValue('id_'.($this->is_cms ? 'cms_' : '').'category');
  436. if (!isset($token) || empty($token))
  437. $token = $this->token;
  438. /* Determine total page number */
  439. $pagination = $this->_default_pagination;
  440. if (in_array((int)Tools::getValue($this->list_id.'_pagination'), $this->_pagination))
  441. $pagination = (int)Tools::getValue($this->list_id.'_pagination');
  442. elseif (isset($this->context->cookie->{$this->list_id.'_pagination'}) && $this->context->cookie->{$this->list_id.'_pagination'})
  443. $pagination = $this->context->cookie->{$this->list_id.'_pagination'};
  444. $total_pages = max(1, ceil($this->listTotal / $pagination));
  445. $identifier = Tools::getIsset($this->identifier) ? '&'.$this->identifier.'='.(int)Tools::getValue($this->identifier) : '';
  446. $order = '';
  447. if (Tools::getIsset($this->table.'Orderby'))
  448. $order = '&'.$this->table.'Orderby='.urlencode($this->orderBy).'&'.$this->table.'Orderway='.urlencode(strtolower($this->orderWay));
  449. $action = $this->currentIndex.$identifier.'&token='.$token.'#'.$this->list_id;
  450. /* Determine current page number */
  451. $page = (int)Tools::getValue('submitFilter'.$this->list_id);
  452. if (!$page)
  453. $page = 1;
  454. if ($page > $total_pages)
  455. $page = $total_pages;
  456. /* Choose number of results per page */
  457. $selected_pagination = Tools::getValue($this->list_id.'_pagination',
  458. isset($this->context->cookie->{$this->list_id.'_pagination'}) ? $this->context->cookie->{$this->list_id.'_pagination'} : $this->_default_pagination
  459. );
  460. if (!isset($this->table_id) && $this->position_identifier && (int)Tools::getValue($this->position_identifier, 1))
  461. $this->table_id = substr($this->identifier, 3, strlen($this->identifier));
  462. if ($this->position_identifier && ($this->orderBy == 'position' && $this->orderWay != 'DESC'))
  463. $table_dnd = true;
  464. $prefix = isset($this->controller_name) ? str_replace(array('admin', 'controller'), '', Tools::strtolower($this->controller_name)) : '';
  465. $ajax = false;
  466. foreach ($this->fields_list as $key => $params)
  467. {
  468. if (!isset($params['type']))
  469. $params['type'] = 'text';
  470. $value = Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.(array_key_exists('filter_key', $params) && $key != 'active' ? $params['filter_key'] : $key)};
  471. switch ($params['type'])
  472. {
  473. case 'bool':
  474. if (isset($params['ajax']) && $params['ajax'])
  475. $ajax = true;
  476. break;
  477. case 'date':
  478. case 'datetime':
  479. if (is_string($value))
  480. $value = Tools::unSerialize($value);
  481. if (!Validate::isCleanHtml($value[0]) || !Validate::isCleanHtml($value[1]))
  482. $value = '';
  483. $name = $this->list_id.'Filter_'.(isset($params['filter_key']) ? $params['filter_key'] : $key);
  484. $name_id = str_replace('!', '__', $name);
  485. $params['id_date'] = $name_id;
  486. $params['name_date'] = $name;
  487. $this->context->controller->addJqueryUI('ui.datepicker');
  488. break;
  489. case 'select':
  490. foreach ($params['list'] as $option_value => $option_display)
  491. {
  492. if (isset(Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.$params['filter_key']})
  493. && Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.$params['filter_key']} == $option_value
  494. && Context::getContext()->cookie->{$prefix.$this->list_id.'Filter_'.$params['filter_key']} != '')
  495. $this->fields_list[$key]['select'][$option_value]['selected'] = 'selected';
  496. }
  497. break;
  498. case 'text':
  499. if (!Validate::isCleanHtml($value))
  500. $value = '';
  501. }
  502. $params['value'] = $value;
  503. $this->fields_list[$key] = $params;
  504. }
  505. $has_value = false;
  506. $has_search_field = false;
  507. foreach ($this->fields_list as $key => $field)
  508. {
  509. if (isset($field['value']) && $field['value'] !== false && $field['value'] !== '')
  510. {
  511. if (is_array($field['value']) && trim(implode('', $field['value'])) == '')
  512. continue;
  513. $has_value = true;
  514. break;
  515. }
  516. if (!(isset($field['search']) && $field['search'] === false))
  517. $has_search_field = true;
  518. }
  519. Context::getContext()->smarty->assign(array(
  520. 'page' => $page,
  521. 'simple_header' => $this->simple_header,
  522. 'total_pages' => $total_pages,
  523. 'selected_pagination' => $selected_pagination,
  524. 'pagination' => $this->_pagination,
  525. 'list_total' => $this->listTotal,
  526. 'token' => $this->token,
  527. 'table' => $this->table,
  528. 'bulk_actions' => $this->bulk_actions,
  529. 'show_toolbar' => $this->show_toolbar,
  530. 'toolbar_scroll' => $this->toolbar_scroll,
  531. 'toolbar_btn' => $this->toolbar_btn,
  532. 'has_bulk_actions' => $this->hasBulkActions($has_value),
  533. ));
  534. $this->header_tpl->assign(array_merge(array(
  535. 'ajax' => $ajax,
  536. 'title' => array_key_exists('title', $this->tpl_vars) ? $this->tpl_vars['title'] : $this->title,
  537. 'show_filters' => ((count($this->_list) > 1 && $has_search_field) || $has_value ),
  538. 'filters_has_value' => $has_value,
  539. 'currentIndex' => $this->currentIndex,
  540. 'action' => $action,
  541. 'is_order_position' => $this->position_identifier && $this->orderBy == 'position',
  542. 'order_way' => $this->orderWay,
  543. 'order_by' => $this->orderBy,
  544. 'fields_display' => $this->fields_list,
  545. 'delete' => in_array('delete', $this->actions),
  546. 'identifier' => $this->identifier,
  547. 'id_cat' => $id_cat,
  548. 'shop_link_type' => $this->shopLinkType,
  549. 'has_actions' => !empty($this->actions),
  550. 'table_id' => isset($this->table_id) ? $this->table_id : null,
  551. 'table_dnd' => isset($table_dnd) ? $table_dnd : null,
  552. 'name' => isset($name) ? $name : null,
  553. 'name_id' => isset($name_id) ? $name_id : null,
  554. 'row_hover' => $this->row_hover,
  555. 'list_id' => isset($this->list_id) ? $this->list_id : $this->table
  556. ), $this->tpl_vars));
  557. return $this->header_tpl->fetch();
  558. }
  559. public function hasBulkActions($has_value = false)
  560. {
  561. if ($this->force_show_bulk_actions)
  562. return true;
  563. if (count($this->_list) <= 1 && !$has_value)
  564. return false;
  565. if (isset($this->list_skip_actions) && count($this->list_skip_actions)
  566. && isset($this->bulk_actions) && is_array($this->bulk_actions) && count($this->bulk_actions))
  567. {
  568. foreach ($this->bulk_actions as $action => $data)
  569. if (array_key_exists($action, $this->list_skip_actions))
  570. {
  571. foreach ($this->_list as $key => $row)
  572. if (!in_array($row[$this->identifier], $this->list_skip_actions[$action]))
  573. return true;
  574. return false;
  575. }
  576. }
  577. return !empty($this->bulk_actions);
  578. }
  579. /**
  580. * Close list table and submit button
  581. */
  582. public function displayListFooter()
  583. {
  584. if (!isset($this->list_id))
  585. $this->list_id = $this->table;
  586. $this->footer_tpl->assign(array_merge($this->tpl_vars, array(
  587. 'current' => $this->currentIndex,
  588. 'list_id' => $this->list_id
  589. )));
  590. return $this->footer_tpl->fetch();
  591. }
  592. }