PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/module/ModuleGrid.php

https://gitlab.com/jslee1/PrestaShop
PHP | 201 lines | 138 code | 31 blank | 32 comment | 29 complexity | f0b12e57922b42c8be7a0fc3fd33942b MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 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-2015 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. abstract class ModuleGridCore extends Module
  27. {
  28. protected $_employee;
  29. /** @var array of strings graph data */
  30. protected $_values = array();
  31. /** @var int total number of values **/
  32. protected $_totalCount = 0;
  33. /**@var string graph titles */
  34. protected $_title;
  35. /**@var int start */
  36. protected $_start;
  37. /**@var int limit */
  38. protected $_limit;
  39. /**@var string column name on which to sort */
  40. protected $_sort = null;
  41. /**@var string sort direction DESC/ASC */
  42. protected $_direction = null;
  43. /** @var ModuleGridEngine grid engine */
  44. protected $_render;
  45. abstract protected function getData();
  46. public function setEmployee($id_employee)
  47. {
  48. $this->_employee = new Employee($id_employee);
  49. }
  50. public function setLang($id_lang)
  51. {
  52. $this->_id_lang = $id_lang;
  53. }
  54. public function create($render, $type, $width, $height, $start, $limit, $sort, $dir)
  55. {
  56. if (!Validate::isModuleName($render)) {
  57. die(Tools::displayError());
  58. }
  59. if (!Tools::file_exists_cache($file = _PS_ROOT_DIR_.'/modules/'.$render.'/'.$render.'.php')) {
  60. die(Tools::displayError());
  61. }
  62. require_once($file);
  63. $this->_render = new $render($type);
  64. $this->_start = $start;
  65. $this->_limit = $limit;
  66. $this->_sort = $sort;
  67. $this->_direction = $dir;
  68. $this->getData();
  69. $this->_render->setTitle($this->_title);
  70. $this->_render->setSize($width, $height);
  71. $this->_render->setValues($this->_values);
  72. $this->_render->setTotalCount($this->_totalCount);
  73. $this->_render->setLimit($this->_start, $this->_limit);
  74. }
  75. public function render()
  76. {
  77. $this->_render->render();
  78. }
  79. public function engine($params)
  80. {
  81. if (!($render = Configuration::get('PS_STATS_GRID_RENDER'))) {
  82. return Tools::displayError('No grid engine selected');
  83. }
  84. if (!Validate::isModuleName($render)) {
  85. die(Tools::displayError());
  86. }
  87. if (!file_exists(_PS_ROOT_DIR_.'/modules/'.$render.'/'.$render.'.php')) {
  88. return Tools::displayError('Grid engine selected is unavailable.');
  89. }
  90. $grider = 'grider.php?render='.$render.'&module='.Tools::safeOutput(Tools::getValue('module'));
  91. $context = Context::getContext();
  92. $grider .= '&id_employee='.(int)$context->employee->id;
  93. $grider .= '&id_lang='.(int)$context->language->id;
  94. if (!isset($params['width']) || !Validate::IsUnsignedInt($params['width'])) {
  95. $params['width'] = 600;
  96. }
  97. if (!isset($params['height']) || !Validate::IsUnsignedInt($params['height'])) {
  98. $params['height'] = 920;
  99. }
  100. if (!isset($params['start']) || !Validate::IsUnsignedInt($params['start'])) {
  101. $params['start'] = 0;
  102. }
  103. if (!isset($params['limit']) || !Validate::IsUnsignedInt($params['limit'])) {
  104. $params['limit'] = 40;
  105. }
  106. $grider .= '&width='.$params['width'];
  107. $grider .= '&height='.$params['height'];
  108. if (isset($params['start']) && Validate::IsUnsignedInt($params['start'])) {
  109. $grider .= '&start='.$params['start'];
  110. }
  111. if (isset($params['limit']) && Validate::IsUnsignedInt($params['limit'])) {
  112. $grider .= '&limit='.$params['limit'];
  113. }
  114. if (isset($params['type']) && Validate::IsName($params['type'])) {
  115. $grider .= '&type='.$params['type'];
  116. }
  117. if (isset($params['option']) && Validate::IsGenericName($params['option'])) {
  118. $grider .= '&option='.$params['option'];
  119. }
  120. if (isset($params['sort']) && Validate::IsName($params['sort'])) {
  121. $grider .= '&sort='.$params['sort'];
  122. }
  123. if (isset($params['dir']) && Validate::isSortDirection($params['dir'])) {
  124. $grider .= '&dir='.$params['dir'];
  125. }
  126. require_once(_PS_ROOT_DIR_.'/modules/'.$render.'/'.$render.'.php');
  127. return call_user_func(array($render, 'hookGridEngine'), $params, $grider);
  128. }
  129. protected function csvExport($datas)
  130. {
  131. $this->_sort = $datas['defaultSortColumn'];
  132. $this->setLang(Context::getContext()->language->id);
  133. $this->getData();
  134. $layers = isset($datas['layers']) ? $datas['layers'] : 1;
  135. if (isset($datas['option'])) {
  136. $this->setOption($datas['option'], $layers);
  137. }
  138. if (count($datas['columns'])) {
  139. foreach ($datas['columns'] as $column) {
  140. $this->_csv .= $column['header'].';';
  141. }
  142. $this->_csv = rtrim($this->_csv, ';')."\n";
  143. foreach ($this->_values as $value) {
  144. foreach ($datas['columns'] as $column) {
  145. $this->_csv .= $value[$column['dataIndex']].';';
  146. }
  147. $this->_csv = rtrim($this->_csv, ';')."\n";
  148. }
  149. }
  150. $this->_displayCsv();
  151. }
  152. protected function _displayCsv()
  153. {
  154. if (ob_get_level() && ob_get_length() > 0) {
  155. ob_end_clean();
  156. }
  157. header('Content-Type: application/octet-stream');
  158. header('Content-Disposition: attachment; filename="'.$this->displayName.' - '.time().'.csv"');
  159. echo $this->_csv;
  160. exit;
  161. }
  162. public function getDate()
  163. {
  164. return ModuleGraph::getDateBetween($this->_employee);
  165. }
  166. public function getLang()
  167. {
  168. return $this->_id_lang;
  169. }
  170. }