1<?php
2/*
3* 2007-2016 PrestaShop
4*
5* NOTICE OF LICENSE
6*
7* This source file is subject to the Academic Free License (AFL 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/afl-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-2016 PrestaShop SA
23*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
24*  International Registered Trademark & Property of PrestaShop SA
25*/
26
27if (!defined('_PS_VERSION_'))
28	exit;
29
30class GraphNvD3 extends ModuleGraphEngine
31{
32	private $_width;
33	private $_height;
34	private $_values;
35	private $_legend;
36	private $_titles;
37
38	function __construct($type = null)
39	{
40		if ($type !== null)
41			return parent::__construct($type);
42
43		$this->name = 'graphnvd3';
44		$this->tab = 'administration';
45		$this->version = '2.0.1';
46		$this->author = 'PrestaShop';
47		$this->need_instance = 0;
48
49		Module::__construct();
50
51		$this->displayName = $this->trans('NVD3 Charts', array(), 'Modules.Graphnvd3.Admin');
52		$this->description = '';
53		$this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
54	}
55
56	function install()
57	{
58		return (parent::install() && $this->registerHook('GraphEngine') && $this->registerHook('actionAdminControllerSetMedia'));
59	}
60
61	public function hookActionAdminControllerSetMedia($params)
62	{
63		$admin_webpath = str_ireplace(_PS_ROOT_DIR_, '', _PS_ADMIN_DIR_);
64		$admin_webpath = preg_replace('/^'.preg_quote(DIRECTORY_SEPARATOR, '/').'/', '', $admin_webpath);
65
66		$this->context->controller->addJS(array(
67			_PS_JS_DIR_.'vendor/d3.v3.min.js',
68			__PS_BASE_URI__.$admin_webpath.'/themes/'.$this->context->employee->bo_theme.'/js/vendor/nv.d3.min.js',
69		));
70		$this->context->controller->addCSS(__PS_BASE_URI__.$admin_webpath.'/themes/'.$this->context->employee->bo_theme.'/css/vendor/nv.d3.css');
71	}
72
73	public static function hookGraphEngine($params, $drawer)
74	{
75		static $divid = 1;
76
77		if (strpos($params['width'], '%') !== false)
78			$params['width'] = (int)preg_replace('/\s*%\s*/', '', $params['width']).'%';
79		else
80			$params['width'] = (int)$params['width'].'px';
81
82		$nvd3_func = array(
83			'line' => '
84				nv.models.lineChart()',
85			'pie' => '
86				nv.models.pieChart()
87					.x(function(d) { return d.label; })
88					.y(function(d) { return d.value; })
89					.showLabels(true)
90					.showLegend(false)'
91		);
92
93		return '
94		<div id="nvd3_chart_'.$divid.'" class="chart with-transitions">
95			<svg style="width:'.$params['width'].';height:'.(int)$params['height'].'px"></svg>
96		</div>
97		<script>
98			$.ajax({
99			url: "'.addslashes($drawer).'",
100			dataType: "json",
101			type: "GET",
102			cache: false,
103			headers: {"cache-control": "no-cache"},
104			success: function(jsonData){
105				nv.addGraph(function(){
106					var chart = '.$nvd3_func[$params['type']].';
107
108					if (jsonData.axisLabels.xAxis != null)
109						chart.xAxis.axisLabel(jsonData.axisLabels.xAxis);
110					if (jsonData.axisLabels.yAxis != null)
111						chart.yAxis.axisLabel(jsonData.axisLabels.yAxis);
112
113					d3.select("#nvd3_chart_'.($divid++).' svg")
114						.datum(jsonData.data)
115						.transition().duration(500)
116						.call(chart);
117
118					nv.utils.windowResize(chart.update);
119
120					return chart;
121				});
122			}
123		});
124		</script>';
125	}
126
127	public function createValues($values)
128	{
129		$this->_values = $values;
130	}
131
132	public function setSize($width, $height)
133	{
134		$this->_width = $width;
135		$this->_height = $height;
136	}
137
138	public function setLegend($legend)
139	{
140		$this->_legend = $legend;
141	}
142
143	public function setTitles($titles)
144	{
145		$this->_titles = $titles;
146	}
147
148	public function draw()
149	{
150		$array = array(
151			'axisLabels' => array('xAxis' => $this->_titles['x'], 'yAxis' => $this->_titles['y']),
152			'data' => array()
153		);
154
155		if (!isset($this->_values[0]) || !is_array($this->_values[0]))
156		{
157			$nvd3_values = array();
158			if (Tools::getValue('type') == 'pie')
159			{
160				foreach ($this->_values as $x => $y)
161					$nvd3_values[] = array('label' => $this->_legend[$x], 'value' => $y);
162				$array['data'] = $nvd3_values;
163			}
164			else
165			{
166				foreach ($this->_values as $x => $y)
167					$nvd3_values[] = array('x' => $x, 'y' => $y);
168				$array['data'][] = array('values' => $nvd3_values, 'key' => $this->_titles['main']);
169			}
170		}
171		else
172			foreach ($this->_values as $layer => $gross_values)
173			{
174				$nvd3_values = array();
175				foreach ($gross_values as $x => $y)
176					$nvd3_values[] = array('x' => $x, 'y' => $y);
177				$array['data'][] = array('values' => $nvd3_values, 'key' => $this->_titles['main'][$layer]);
178			}
179		echo preg_replace('/"([0-9]+)"/', '$1', Tools::jsonEncode($array));
180	}
181}
182