1<?php 2/** 3 * 2007-2016 PrestaShop 4 * 5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA 6 * Copyright (C) 2017-2018 thirty bees 7 * 8 * NOTICE OF LICENSE 9 * 10 * This source file is subject to the Open Software License (OSL 3.0) 11 * that is bundled with this package in the file LICENSE.txt. 12 * It is also available through the world-wide-web at this URL: 13 * http://opensource.org/licenses/osl-3.0.php 14 * If you did not receive a copy of the license and are unable to 15 * obtain it through the world-wide-web, please send an email 16 * to license@thirtybees.com so we can send you a copy immediately. 17 * 18 * DISCLAIMER 19 * 20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 21 * versions in the future. If you wish to customize PrestaShop for your 22 * needs please refer to https://www.thirtybees.com for more information. 23 * 24 * @author thirty bees <contact@thirtybees.com> 25 * @author PrestaShop SA <contact@prestashop.com> 26 * @copyright 2017-2018 thirty bees 27 * @copyright 2007-2016 PrestaShop SA 28 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 * PrestaShop is an internationally registered trademark & property of PrestaShop SA 30 */ 31 32/** 33 * Class ModuleGraphEngineCore 34 * 35 * @since 1.0.0 36 */ 37class ModuleGraphEngineCore extends Module 38{ 39 // @codingStandardsIgnoreStart 40 protected $_type; 41 protected $_width; 42 protected $_height; 43 protected $_values; 44 protected $_legend; 45 protected $_titles; 46 // @codingStandardsIgnoreEnd 47 48 /** 49 * ModuleGraphEngineCore constructor. 50 * 51 * @param null|string $type 52 * 53 * @since 1.0.0 54 * @version 1.0.0 Initial version 55 */ 56 public function __construct($type = null) 57 { 58 $this->_type = $type; 59 } 60 61 /** 62 * @return bool 63 * 64 * @since 1.0.0 65 * @version 1.0.0 Initial version 66 */ 67 public function install() 68 { 69 if (!parent::install()) { 70 return false; 71 } 72 73 return Configuration::updateValue('PS_STATS_RENDER', $this->name); 74 } 75 76 /** 77 * @return array 78 * 79 * @since 1.0.0 80 * @version 1.0.0 Initial version 81 */ 82 public static function getGraphEngines() 83 { 84 $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( 85 (new DbQuery()) 86 ->select('m.`name`') 87 ->from('module', 'm') 88 ->leftJoin('hook_module', 'hm', 'hm.`id_module` = m.`id_module`') 89 ->leftJoin('hook', 'h', 'hm.`id_hook` = h.`id_hook`') 90 ->where('h.`name` = \'displayAdminStatsGraphEngine\'') 91 ); 92 93 $arrayEngines = []; 94 foreach ($result as $module) { 95 $instance = Module::getInstanceByName($module['name']); 96 if (!$instance) { 97 continue; 98 } 99 $arrayEngines[$module['name']] = [$instance->displayName, $instance->description]; 100 } 101 102 return $arrayEngines; 103 } 104 105 public static function hookGraphEngine($params, $drawer) 106 { 107 static $divid = 1; 108 109 if (strpos($params['width'], '%') !== false) { 110 $params['width'] = (int) preg_replace('/\s*%\s*/', '', $params['width']).'%'; 111 } else { 112 $params['width'] = (int) $params['width'].'px'; 113 } 114 115 $nvd3Func = [ 116 'line' => ' 117 nv.models.lineChart()', 118 'pie' => ' 119 nv.models.pieChart() 120 .x(function(d) { return d.label; }) 121 .y(function(d) { return d.value; }) 122 .showLabels(true) 123 .showLegend(false)' 124 ]; 125 126 return ' 127 <div id="nvd3_chart_'.$divid.'" class="chart with-transitions"> 128 <svg style="width:'.$params['width'].';height:'.(int)$params['height'].'px"></svg> 129 </div> 130 <script> 131 $.ajax({ 132 url: "'.addslashes($drawer).'", 133 dataType: "json", 134 type: "GET", 135 cache: false, 136 headers: {"cache-control": "no-cache"}, 137 success: function(jsonData){ 138 nv.addGraph(function(){ 139 var chart = '.$nvd3Func[$params['type']].'; 140 141 if (jsonData.axisLabels.xAxis != null) 142 chart.xAxis.axisLabel(jsonData.axisLabels.xAxis); 143 if (jsonData.axisLabels.yAxis != null) 144 chart.yAxis.axisLabel(jsonData.axisLabels.yAxis); 145 146 d3.select("#nvd3_chart_'.($divid++).' svg") 147 .datum(jsonData.data) 148 .transition().duration(500) 149 .call(chart); 150 151 nv.utils.windowResize(chart.update); 152 153 return chart; 154 }); 155 } 156 }); 157 </script>'; 158 } 159 160 public function createValues($values) 161 { 162 $this->_values = $values; 163 } 164 165 public function setSize($width, $height) 166 { 167 $this->_width = $width; 168 $this->_height = $height; 169 } 170 171 public function setLegend($legend) 172 { 173 $this->_legend = $legend; 174 } 175 176 public function setTitles($titles) 177 { 178 $this->_titles = $titles; 179 } 180 181 public function draw() 182 { 183 $array = [ 184 'axisLabels' => ['xAxis' => isset($this->_titles['x']) ? $this->_titles['x'] : null, 'yAxis' => isset($this->_titles['y']) ? $this->_titles['y'] : null], 185 'data' => [], 186 ]; 187 188 if (!isset($this->_values[0]) || !is_array($this->_values[0])) { 189 $nvd3Values = []; 190 if (Tools::getValue('type') == 'pie') { 191 foreach ($this->_values as $x => $y) { 192 $nvd3Values[] = ['label' => $this->_legend[$x], 'value' => $y]; 193 } 194 $array['data'] = $nvd3Values; 195 } else { 196 foreach ($this->_values as $x => $y) { 197 $nvd3Values[] = ['x' => $x, 'y' => $y]; 198 } 199 $array['data'][] = ['values' => $nvd3Values, 'key' => $this->_titles['main']]; 200 } 201 } else { 202 foreach ($this->_values as $layer => $grossValues) { 203 $nvd3Values = []; 204 foreach ($grossValues as $x => $y) { 205 $nvd3Values[] = ['x' => $x, 'y' => $y]; 206 } 207 $array['data'][] = ['values' => $nvd3Values, 'key' => $this->_titles['main'][$layer]]; 208 } 209 } 210 die(preg_replace('/"([0-9]+)"/', '$1', json_encode($array))); 211 } 212} 213