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

/classes/module/ModuleGraph.php

https://bitbucket.org/enurkov/prestashop
PHP | 334 lines | 263 code | 31 blank | 40 comment | 77 complexity | 7b0435455732a2e8a7947b6436faab0b MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 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-2012 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 ModuleGraphCore extends Module
  27. {
  28. protected $_employee;
  29. /** @var integer array graph data */
  30. protected $_values = array();
  31. /** @var string array graph legends (X axis) */
  32. protected $_legend = array();
  33. /**@var string graph titles */
  34. protected $_titles = array('main' => null, 'x' => null, 'y' => null);
  35. /** @var ModuleGraphEngine graph engine */
  36. protected $_render;
  37. abstract protected function getData($layers);
  38. public function setEmployee($id_employee)
  39. {
  40. $this->_employee = new Employee($id_employee);
  41. }
  42. public function setLang($id_lang)
  43. {
  44. $this->_id_lang = $id_lang;
  45. }
  46. protected function setDateGraph($layers, $legend = false)
  47. {
  48. // Get dates in a manageable format
  49. $from_array = getdate(strtotime($this->_employee->stats_date_from));
  50. $to_array = getdate(strtotime($this->_employee->stats_date_to));
  51. // If the granularity is inferior to 1 day
  52. if ($this->_employee->stats_date_from == $this->_employee->stats_date_to)
  53. {
  54. if ($legend)
  55. for ($i = 0; $i < 24; $i++)
  56. {
  57. if ($layers == 1)
  58. $this->_values[$i] = 0;
  59. else
  60. for ($j = 0; $j < $layers; $j++)
  61. $this->_values[$j][$i] = 0;
  62. $this->_legend[$i] = ($i % 2) ? '' : sprintf('%02dh', $i);
  63. }
  64. if (is_callable(array($this, 'setDayValues')))
  65. $this->setDayValues($layers);
  66. }
  67. // If the granularity is inferior to 1 month TODO : change to manage 28 to 31 days
  68. elseif (strtotime($this->_employee->stats_date_to) - strtotime($this->_employee->stats_date_from) <= 2678400)
  69. {
  70. if ($legend)
  71. {
  72. $days = array();
  73. if ($from_array['mon'] == $to_array['mon'])
  74. for ($i = $from_array['mday']; $i <= $to_array['mday']; ++$i)
  75. $days[] = $i;
  76. else
  77. {
  78. $imax = date('t', mktime(0, 0, 0, $from_array['mon'], 1, $from_array['year']));
  79. for ($i = $from_array['mday']; $i <= $imax; ++$i)
  80. $days[] = $i;
  81. for ($i = 1; $i <= $to_array['mday']; ++$i)
  82. $days[] = $i;
  83. }
  84. foreach ($days as $i)
  85. {
  86. if ($layers == 1)
  87. $this->_values[$i] = 0;
  88. else
  89. for ($j = 0; $j < $layers; $j++)
  90. $this->_values[$j][$i] = 0;
  91. $this->_legend[$i] = ($i % 2) ? '' : sprintf('%02d', $i);
  92. }
  93. }
  94. if (is_callable(array($this, 'setMonthValues')))
  95. $this->setMonthValues($layers);
  96. }
  97. // If the granularity is less than 1 year
  98. elseif (strtotime('-1 year', strtotime($this->_employee->stats_date_to)) < strtotime($this->_employee->stats_date_from))
  99. {
  100. if ($legend)
  101. {
  102. $months = array();
  103. if ($from_array['year'] == $to_array['year'])
  104. for ($i = $from_array['mon']; $i <= $to_array['mon']; ++$i)
  105. $months[] = $i;
  106. else
  107. {
  108. for ($i = $from_array['mon']; $i <= 12; ++$i)
  109. $months[] = $i;
  110. for ($i = 1; $i <= $to_array['mon']; ++$i)
  111. $months[] = $i;
  112. }
  113. foreach ($months as $i)
  114. {
  115. if ($layers == 1)
  116. $this->_values[$i] = 0;
  117. else
  118. for ($j = 0; $j < $layers; $j++)
  119. $this->_values[$j][$i] = 0;
  120. $this->_legend[$i] = sprintf('%02d', $i);
  121. }
  122. }
  123. if (is_callable(array($this, 'setYearValues')))
  124. $this->setYearValues($layers);
  125. }
  126. // If the granularity is greater than 1 year
  127. else
  128. {
  129. if ($legend)
  130. {
  131. $years = array();
  132. for ($i = $from_array['year']; $i <= $to_array['year']; ++$i)
  133. $years[] = $i;
  134. foreach ($years as $i)
  135. {
  136. if ($layers == 1)
  137. $this->_values[$i] = 0;
  138. else
  139. for ($j = 0; $j < $layers; $j++)
  140. $this->_values[$j][$i] = 0;
  141. $this->_legend[$i] = sprintf('%04d', $i);
  142. }
  143. }
  144. if (is_callable(array($this, 'setAllTimeValues')))
  145. $this->setAllTimeValues($layers);
  146. }
  147. }
  148. protected function csvExport($datas)
  149. {
  150. $context = Context::getContext();
  151. $this->setEmployee($context->employee->id);
  152. $this->setLang($context->language->id);
  153. $layers = isset($datas['layers']) ? $datas['layers'] : 1;
  154. if (isset($datas['option']))
  155. $this->setOption($datas['option'], $layers);
  156. $this->getData($layers);
  157. // @todo use native CSV PHP functions ?
  158. // Generate first line (column titles)
  159. if (is_array($this->_titles['main']))
  160. for ($i = 0, $total_main = count($this->_titles['main']); $i <= $total_main; $i++)
  161. {
  162. if ($i > 0)
  163. $this->_csv .= ';';
  164. if (isset($this->_titles['main'][$i]))
  165. $this->_csv .= $this->_titles['main'][$i];
  166. }
  167. else // If there is only one column title, there is in fast two column (the first without title)
  168. $this->_csv .= ';'.$this->_titles['main'];
  169. $this->_csv .= "\n";
  170. if (count($this->_legend))
  171. {
  172. $total = 0;
  173. if ($datas['type'] == 'pie')
  174. foreach ($this->_legend as $key => $legend)
  175. for ($i = 0, $total_main = (is_array($this->_titles['main']) ? count($this->_values) : 1); $i < $total_main; ++$i)
  176. $total += (is_array($this->_values[$i]) ? $this->_values[$i][$key] : $this->_values[$key]);
  177. foreach ($this->_legend as $key => $legend)
  178. {
  179. $this->_csv .= $legend.';';
  180. for ($i = 0, $total_main = (is_array($this->_titles['main']) ? count($this->_values) : 1); $i < $total_main; ++$i)
  181. {
  182. if (!isset($this->_values[$i]) || !is_array($this->_values[$i]))
  183. if (isset($this->_values[$key]))
  184. {
  185. // We don't want strings to be divided. Example: product name
  186. if (is_numeric($this->_values[$key]))
  187. $this->_csv .= $this->_values[$key] / (($datas['type'] == 'pie') ? $total : 1);
  188. else
  189. $this->_csv .= $this->_values[$key];
  190. }
  191. else
  192. $this->_csv .= '0';
  193. else
  194. {
  195. // We don't want strings to be divided. Example: product name
  196. if (is_numeric($this->_values[$i][$key]))
  197. $this->_csv .= $this->_values[$i][$key] / (($datas['type'] == 'pie') ? $total : 1);
  198. else
  199. $this->_csv .= $this->_values[$i][$key];
  200. }
  201. $this->_csv .= ';';
  202. }
  203. $this->_csv .= "\n";
  204. }
  205. }
  206. $this->_displayCsv();
  207. }
  208. protected function _displayCsv()
  209. {
  210. ob_end_clean();
  211. header('Content-Type: application/octet-stream');
  212. header('Content-Disposition: attachment; filename="'.$this->displayName.' - '.time().'.csv"');
  213. echo $this->_csv;
  214. exit;
  215. }
  216. public function create($render, $type, $width, $height, $layers)
  217. {
  218. if (!Validate::isModuleName($render))
  219. die(Tools::displayError());
  220. if (!Tools::file_exists_cache($file = _PS_ROOT_DIR_.'/modules/'.$render.'/'.$render.'.php'))
  221. die(Tools::displayError());
  222. require_once($file);
  223. $this->_render = new $render($type);
  224. $this->getData($layers);
  225. $this->_render->createValues($this->_values);
  226. $this->_render->setSize($width, $height);
  227. $this->_render->setLegend($this->_legend);
  228. $this->_render->setTitles($this->_titles);
  229. }
  230. public function draw()
  231. {
  232. $this->_render->draw();
  233. }
  234. /**
  235. * @todo Set this method as abstracted ? Quid of module compatibility.
  236. */
  237. public function setOption($option, $layers = 1)
  238. {
  239. }
  240. public function engine($params)
  241. {
  242. $context = Context::getContext();
  243. if (!($render = Configuration::get('PS_STATS_RENDER')))
  244. return Tools::displayError('No graph engine selected');
  245. if (!Validate::isModuleName($render))
  246. die(Tools::displayError());
  247. if (!file_exists(_PS_ROOT_DIR_.'/modules/'.$render.'/'.$render.'.php'))
  248. return Tools::displayError('Graph engine selected is unavailable.');
  249. $id_employee = (int)$context->employee->id;
  250. $id_lang = (int)$context->language->id;
  251. if (!isset($params['layers']))
  252. $params['layers'] = 1;
  253. if (!isset($params['type']))
  254. $params['type'] = 'column';
  255. if (!isset($params['width']))
  256. $params['width'] = 550;
  257. if (!isset($params['height']))
  258. $params['height'] = 270;
  259. $url_params = $params;
  260. $url_params['render'] = $render;
  261. $url_params['module'] = Tools::getValue('module');
  262. $url_params['id_employee'] = $id_employee;
  263. $url_params['id_lang'] = $id_lang;
  264. $drawer = 'drawer.php?'.http_build_query(array_map('Tools::safeOutput', $url_params), '', '&');
  265. require_once(_PS_ROOT_DIR_.'/modules/'.$render.'/'.$render.'.php');
  266. return call_user_func(array($render, 'hookGraphEngine'), $params, $drawer);
  267. }
  268. protected static function getEmployee($employee = null, Context $context = null)
  269. {
  270. if (!Validate::isLoadedObject($employee))
  271. {
  272. if (!$context)
  273. $context = Context::getContext();
  274. if (!Validate::isLoadedObject($context->employee))
  275. return false;
  276. $employee = $context->employee;
  277. }
  278. if (empty($employee->stats_date_from) || empty($employee->stats_date_to)
  279. || $employee->stats_date_from == '0000-00-00' || $employee->stats_date_to == '0000-00-00')
  280. {
  281. if (empty($employee->stats_date_from) || $employee->stats_date_from == '0000-00-00')
  282. $employee->stats_date_from = date('Y').'-01-01';
  283. if (empty($employee->stats_date_to) || $employee->stats_date_to == '0000-00-00')
  284. $employee->stats_date_to = date('Y').'-12-31';
  285. $employee->update();
  286. }
  287. return $employee;
  288. }
  289. public function getDate()
  290. {
  291. return ModuleGraph::getDateBetween($this->_employee);
  292. }
  293. public static function getDateBetween($employee = null)
  294. {
  295. if ($employee = ModuleGraph::getEmployee($employee))
  296. return ' \''.$employee->stats_date_from.' 00:00:00\' AND \''.$employee->stats_date_to.' 23:59:59\' ';
  297. return ' \''.date('Y-m').'-01 00:00:00\' AND \''.date('Y-m-t').' 23:59:59\' ';
  298. }
  299. public function getLang()
  300. {
  301. return $this->_id_lang;
  302. }
  303. }