-
Notifications
You must be signed in to change notification settings - Fork 20
/
dashactivity.php
executable file
·433 lines (398 loc) · 19.6 KB
/
dashactivity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class dashactivity extends Module
{
protected static $colors = ['#1F77B4', '#FF7F0E', '#2CA02C'];
public function __construct()
{
$this->name = 'dashactivity';
$this->tab = 'administration';
$this->version = '2.1.1';
$this->author = 'PrestaShop';
parent::__construct();
$this->displayName = $this->trans('Dashboard Activity', [], 'Modules.Dashactivity.Admin');
$this->description = $this->trans('Check in one glance what is happening on your store with a list of KPI on your dashboard.', [], 'Modules.Dashactivity.Admin');
$this->ps_versions_compliancy = ['min' => '1.7.7.0', 'max' => _PS_VERSION_];
}
public function install()
{
Configuration::updateValue('DASHACTIVITY_CART_ACTIVE', 30);
Configuration::updateValue('DASHACTIVITY_CART_ABANDONED_MIN', 24);
Configuration::updateValue('DASHACTIVITY_CART_ABANDONED_MAX', 48);
Configuration::updateValue('DASHACTIVITY_VISITOR_ONLINE', 30);
return parent::install()
&& $this->registerHook('dashboardZoneOne')
&& $this->registerHook('dashboardData')
&& $this->registerHook('actionAdminControllerSetMedia')
;
}
public function hookActionAdminControllerSetMedia()
{
if (get_class($this->context->controller) == 'AdminDashboardController') {
$this->context->controller->addJs($this->_path . 'views/js/' . $this->name . '.js');
$this->context->controller->addJs(
[
_PS_JS_DIR_ . 'date.js',
_PS_JS_DIR_ . 'tools.js',
] // retro compat themes 1.5
);
}
}
public function hookDashboardZoneOne($params)
{
$this->context->smarty->assign($this->getConfigFieldsValues());
$this->context->smarty->assign(
[
'dashactivity_config_form' => $this->renderConfigForm(),
'date_subtitle' => $this->trans('(from %s to %s)', [], 'Modules.Dashactivity.Admin'),
'date_format' => $this->context->language->date_format_lite,
'link' => $this->context->link,
]
);
return $this->display(__FILE__, 'dashboard_zone_one.tpl');
}
public function hookDashboardData($params)
{
if (Tools::strlen($params['date_from']) == 10) {
$params['date_from'] .= ' 00:00:00';
}
if (Tools::strlen($params['date_to']) == 10) {
$params['date_to'] .= ' 23:59:59';
}
if (Configuration::get('PS_DASHBOARD_SIMULATION')) {
$days = (strtotime($params['date_to']) - strtotime($params['date_from'])) / 3600 / 24;
$online_visitor = rand(10, 50);
$visits = rand(200, 2000) * $days;
return [
'data_value' => [
'pending_orders' => round(rand(0, 5)),
'return_exchanges' => round(rand(0, 5)),
'abandoned_cart' => round(rand(5, 50)),
'products_out_of_stock' => round(rand(1, 10)),
'new_messages' => round(rand(1, 10) * $days),
'product_reviews' => round(rand(5, 50) * $days),
'new_customers' => round(rand(1, 5) * $days),
'online_visitor' => round($online_visitor),
'active_shopping_cart' => round($online_visitor / 10),
'new_registrations' => round(rand(1, 5) * $days),
'total_suscribers' => round(rand(200, 2000)),
'visits' => round($visits),
'unique_visitors' => round($visits * 0.6),
],
'data_trends' => [
'orders_trends' => ['way' => 'down', 'value' => 0.42],
],
'data_list_small' => [
'dash_traffic_source' => [
'<i class="icon-circle" style="color:' . self::$colors[0] . '"></i> prestashop.com' => round($visits / 2),
'<i class="icon-circle" style="color:' . self::$colors[1] . '"></i> google.com' => round($visits / 3),
'<i class="icon-circle" style="color:' . self::$colors[2] . '"></i> Direct Traffic' => round($visits / 4),
],
],
'data_chart' => [
'dash_trends_chart1' => [
'chart_type' => 'pie_chart_trends',
'data' => [
['key' => 'prestashop.com', 'y' => round($visits / 2), 'color' => self::$colors[0]],
['key' => 'google.com', 'y' => round($visits / 3), 'color' => self::$colors[1]],
['key' => 'Direct Traffic', 'y' => round($visits / 4), 'color' => self::$colors[2]],
],
],
],
];
}
$visits = $unique_visitors = 0;
$row = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getRow('
SELECT COUNT(*) as visits, COUNT(DISTINCT `id_guest`) as unique_visitors
FROM `' . _DB_PREFIX_ . 'connections`
WHERE `date_add` BETWEEN "' . pSQL($params['date_from']) . '" AND "' . pSQL($params['date_to']) . '"
' . Shop::addSqlRestriction(false)
);
extract($row);
if ($maintenance_ips = Configuration::get('PS_MAINTENANCE_IP')) {
$maintenance_ips = implode(',', array_filter(array_map('ip2long', array_map('trim', explode(',', $maintenance_ips))), '\strlen'));
}
if (Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) {
$sql = 'SELECT c.id_guest, c.ip_address, c.date_add, c.http_referer, pt.name as page
FROM `' . _DB_PREFIX_ . 'connections` c
LEFT JOIN `' . _DB_PREFIX_ . 'connections_page` cp ON c.id_connections = cp.id_connections
LEFT JOIN `' . _DB_PREFIX_ . 'page` p ON p.id_page = cp.id_page
LEFT JOIN `' . _DB_PREFIX_ . 'page_type` pt ON p.id_page_type = pt.id_page_type
INNER JOIN `' . _DB_PREFIX_ . 'guest` g ON c.id_guest = g.id_guest
WHERE (g.id_customer IS NULL OR g.id_customer = 0)
' . Shop::addSqlRestriction(false, 'c') . '
AND cp.`time_end` IS NULL
AND (\'' . pSQL(date('Y-m-d H:i:00', time() - 60 * (int) Configuration::get('DASHACTIVITY_VISITOR_ONLINE'))) . '\' < cp.`time_start`)
' . ($maintenance_ips ? 'AND c.ip_address NOT IN (' . preg_replace('/[^,0-9]/', '', $maintenance_ips) . ')' : '') . '
GROUP BY c.id_connections
ORDER BY c.date_add DESC';
} else {
$sql = 'SELECT c.id_guest, c.ip_address, c.date_add, c.http_referer, "-" as page
FROM `' . _DB_PREFIX_ . 'connections` c
INNER JOIN `' . _DB_PREFIX_ . 'guest` g ON c.id_guest = g.id_guest
WHERE (g.id_customer IS NULL OR g.id_customer = 0)
' . Shop::addSqlRestriction(false, 'c') . '
AND (\'' . pSQL(date('Y-m-d H:i:00', time() - 60 * (int) Configuration::get('DASHACTIVITY_VISITOR_ONLINE'))) . '\' < c.`date_add`)
' . ($maintenance_ips ? 'AND c.ip_address NOT IN (' . preg_replace('/[^,0-9]/', '', $maintenance_ips) . ')' : '') . '
ORDER BY c.date_add DESC';
}
Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($sql);
$online_visitor = Db::getInstance()->NumRows();
$pending_orders = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'orders` o
LEFT JOIN `' . _DB_PREFIX_ . 'order_state` os ON (o.current_state = os.id_order_state)
WHERE os.paid = 1 AND os.shipped = 0
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
$abandoned_cart = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'cart`
WHERE `date_upd` BETWEEN "' . pSQL(date('Y-m-d H:i:s', strtotime('-' . (int) Configuration::get('DASHACTIVITY_CART_ABANDONED_MAX') . ' MIN'))) . '" AND "' . pSQL(date('Y-m-d H:i:s', strtotime('-' . (int) Configuration::get('DASHACTIVITY_CART_ABANDONED_MIN') . ' MIN'))) . '"
AND id_cart NOT IN (SELECT id_cart FROM `' . _DB_PREFIX_ . 'orders`)
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
$return_exchanges = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'orders` o
LEFT JOIN `' . _DB_PREFIX_ . 'order_return` or2 ON o.id_order = or2.id_order
WHERE or2.`date_add` BETWEEN "' . pSQL($params['date_from']) . '" AND "' . pSQL($params['date_to']) . '"
' . Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o')
);
$products_out_of_stock = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT SUM(IF(IFNULL(stock.quantity, 0) > 0, 0, 1))
FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON p.id_product = pa.id_product
' . Product::sqlStock('p', 'pa') . '
WHERE p.active = 1'
);
$new_messages = AdminStatsController::getPendingMessages();
$active_shopping_cart = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'cart`
WHERE date_upd > "' . pSQL(date('Y-m-d H:i:s', strtotime('-' . (int) Configuration::get('DASHACTIVITY_CART_ACTIVE') . ' MIN'))) . '"
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
$new_customers = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'customer`
WHERE `date_add` BETWEEN "' . pSQL($params['date_from']) . '" AND "' . pSQL($params['date_to']) . '"
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
$new_registrations = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'customer`
WHERE `newsletter_date_add` BETWEEN "' . pSQL($params['date_from']) . '" AND "' . pSQL($params['date_to']) . '"
AND newsletter = 1
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
$total_suscribers = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'customer`
WHERE newsletter = 1
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
$product_reviews = 0;
if (Module::isInstalled('productcomments')) {
$product_reviews += Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'product_comment` pc
LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON (pc.id_product = p.id_product)
' . Shop::addSqlAssociation('product', 'p') . '
WHERE pc.deleted = 0
AND pc.`date_add` BETWEEN "' . pSQL($params['date_from']) . '" AND "' . pSQL($params['date_to']) . '"
' . Shop::addSqlRestriction(Shop::SHARE_ORDER)
);
}
return [
'data_value' => [
'pending_orders' => (int) $pending_orders,
'return_exchanges' => (int) $return_exchanges,
'abandoned_cart' => (int) $abandoned_cart,
'products_out_of_stock' => (int) $products_out_of_stock,
'new_messages' => (int) $new_messages,
'product_reviews' => (int) $product_reviews,
'new_customers' => (int) $new_customers,
'online_visitor' => (int) $online_visitor,
'active_shopping_cart' => (int) $active_shopping_cart,
'new_registrations' => (int) $new_registrations,
'total_suscribers' => (int) $total_suscribers,
'visits' => (int) $visits,
'unique_visitors' => (int) $unique_visitors,
],
'data_trends' => [
'orders_trends' => ['way' => 'down', 'value' => 0.42],
],
'data_list_small' => [
'dash_traffic_source' => $this->getTrafficSources($params['date_from'], $params['date_to']),
],
'data_chart' => [
'dash_trends_chart1' => $this->getChartTrafficSource($params['date_from'], $params['date_to']),
],
];
}
protected function getChartTrafficSource($date_from, $date_to)
{
$referers = $this->getReferer($date_from, $date_to);
$return = ['chart_type' => 'pie_chart_trends', 'data' => []];
$i = 0;
foreach ($referers as $referer_name => $n) {
$return['data'][] = ['key' => $referer_name, 'y' => $n, 'color' => self::$colors[$i++]];
}
return $return;
}
protected function getTrafficSources($date_from, $date_to)
{
$referrers = $this->getReferer($date_from, $date_to, 3);
$traffic_sources = [];
$i = 0;
foreach ($referrers as $referrer_name => $n) {
$traffic_sources['<i class="icon-circle" style="color:' . self::$colors[$i++] . '"></i> ' . $referrer_name] = $n;
}
return $traffic_sources;
}
protected function getReferer($date_from, $date_to, $limit = 3)
{
$direct_link = $this->trans('Direct link', [], 'Admin.Orderscustomers.Notification');
$websites = [$direct_link => 0];
$result = Db::getInstance()->ExecuteS('
SELECT http_referer
FROM ' . _DB_PREFIX_ . 'connections
WHERE date_add BETWEEN "' . pSQL($date_from) . '" AND "' . pSQL($date_to) . '"
' . Shop::addSqlRestriction() . '
LIMIT ' . (int) $limit
);
foreach ($result as $row) {
if (!isset($row['http_referer']) || empty($row['http_referer'])) {
++$websites[$direct_link];
} else {
$website = preg_replace('/^www./', '', parse_url($row['http_referer'], PHP_URL_HOST));
if (!isset($websites[$website])) {
$websites[$website] = 1;
} else {
++$websites[$website];
}
}
}
arsort($websites);
return $websites;
}
public function renderConfigForm()
{
$fields_form = [
'form' => [
'id_form' => 'step_carrier_general',
'input' => [],
'submit' => [
'title' => $this->trans('Save', [], 'Admin.Actions'),
'class' => 'btn btn-default pull-right submit_dash_config',
'reset' => [
'title' => $this->trans('Cancel', [], 'Admin.Actions'),
'class' => 'btn btn-default cancel_dash_config',
],
],
],
];
$fields_form['form']['input'][] = [
'label' => $this->trans('Active cart', [], 'Modules.Dashactivity.Admin'),
'hint' => $this->trans('How long (in minutes) a cart is to be considered as active after the last recorded change (default: 30 min).', [], 'Modules.Dashactivity.Admin'),
'name' => 'DASHACTIVITY_CART_ACTIVE',
'type' => 'select',
'options' => [
'query' => [
['id' => 15, 'name' => 15],
['id' => 30, 'name' => 30],
['id' => 45, 'name' => 45],
['id' => 60, 'name' => 60],
['id' => 90, 'name' => 90],
['id' => 120, 'name' => 120],
],
'id' => 'id',
'name' => 'name',
],
];
$fields_form['form']['input'][] = [
'label' => $this->trans('Online visitor', [], 'Modules.Dashactivity.Admin'),
'hint' => $this->trans('How long (in minutes) a visitor is to be considered as online after their last action (default: 30 min).', [], 'Modules.Dashactivity.Admin'),
'name' => 'DASHACTIVITY_VISITOR_ONLINE',
'type' => 'select',
'options' => [
'query' => [
['id' => 15, 'name' => 15],
['id' => 30, 'name' => 30],
['id' => 45, 'name' => 45],
['id' => 60, 'name' => 60],
['id' => 90, 'name' => 90],
['id' => 120, 'name' => 120],
],
'id' => 'id',
'name' => 'name',
],
];
$fields_form['form']['input'][] = [
'label' => $this->trans('Abandoned cart (min)', [], 'Modules.Dashactivity.Admin'),
'hint' => $this->trans('How long (in hours) after the last action a cart is to be considered as abandoned (default: 24 hrs).', [], 'Modules.Dashactivity.Admin'),
'name' => 'DASHACTIVITY_CART_ABANDONED_MIN',
'type' => 'text',
'suffix' => $this->trans('hrs', [], 'Modules.Dashactivity.Admin'),
];
$fields_form['form']['input'][] = [
'label' => $this->trans('Abandoned cart (max)', [], 'Modules.Dashactivity.Admin'),
'hint' => $this->trans('How long (in hours) after the last action a cart is no longer to be considered as abandoned (default: 24 hrs).', [], 'Modules.Dashactivity.Admin'),
'name' => 'DASHACTIVITY_CART_ABANDONED_MAX',
'type' => 'text',
'suffix' => $this->trans('hrs', [], 'Modules.Dashactivity.Admin'),
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->id = (int) Tools::getValue('id_carrier');
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitDashConfig';
$helper->tpl_vars = [
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([$fields_form]);
}
public function getConfigFieldsValues()
{
return [
'DASHACTIVITY_CART_ACTIVE' => Tools::getValue('DASHACTIVITY_CART_ACTIVE', Configuration::get('DASHACTIVITY_CART_ACTIVE')),
'DASHACTIVITY_CART_ABANDONED_MIN' => Tools::getValue('DASHACTIVITY_CART_ABANDONED_MIN', Configuration::get('DASHACTIVITY_CART_ABANDONED_MIN')),
'DASHACTIVITY_CART_ABANDONED_MAX' => Tools::getValue('DASHACTIVITY_CART_ABANDONED_MAX', Configuration::get('DASHACTIVITY_CART_ABANDONED_MAX')),
'DASHACTIVITY_VISITOR_ONLINE' => Tools::getValue('DASHACTIVITY_VISITOR_ONLINE', Configuration::get('DASHACTIVITY_VISITOR_ONLINE')),
];
}
}