forked from PrestaShop/statssearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatssearch.php
More file actions
168 lines (151 loc) · 6.18 KB
/
statssearch.php
File metadata and controls
168 lines (151 loc) · 6.18 KB
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
<?php
/**
* 2007-2020 PrestaShop SA and Contributors
*
* 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.txt.
* 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://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class statssearch extends ModuleGraph
{
private $html = '';
private $query = '';
private $query_group_by = '';
public function __construct()
{
$this->name = 'statssearch';
$this->tab = 'administration';
$this->version = '2.1.0';
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->query = 'SELECT `keywords`, COUNT(TRIM(`keywords`)) as occurences, MAX(results) as total
FROM `' . _DB_PREFIX_ . 'statssearch`
WHERE 1
' . Shop::addSqlRestriction() . '
AND `date_add` BETWEEN ';
$this->query_group_by = 'GROUP BY `keywords`
HAVING occurences > 1
ORDER BY occurences DESC';
$this->displayName = $this->trans('Shop search', [], 'Modules.Statssearch.Admin');
$this->description = $this->trans('Enrich your stats, add a tab showing what keywords have been searched by your visitors.', [], 'Modules.Statssearch.Admin');
$this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
}
public function install()
{
if (!parent::install() || !$this->registerHook('actionSearch') || !$this->registerHook('displayAdminStatsModules')) {
return false;
}
return Db::getInstance()->execute('
CREATE TABLE `' . _DB_PREFIX_ . 'statssearch` (
id_statssearch INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
id_shop INTEGER UNSIGNED NOT NULL DEFAULT \'1\',
id_shop_group INTEGER UNSIGNED NOT NULL DEFAULT \'1\',
keywords VARCHAR(255) NOT NULL,
results INT(6) NOT NULL DEFAULT 0,
date_add DATETIME NOT NULL,
PRIMARY KEY(id_statssearch)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8');
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return Db::getInstance()->execute('DROP TABLE `' . _DB_PREFIX_ . 'statssearch`');
}
/**
* Insert keywords in statssearch table when a search is launched on FO
*/
public function hookActionSearch($params)
{
$sql = 'INSERT INTO `' . _DB_PREFIX_ . 'statssearch` (`id_shop`, `id_shop_group`, `keywords`, `results`, `date_add`)
VALUES (' . (int) $this->context->shop->id . ', ' . (int) $this->context->shop->id_shop_group . ', \'' . pSQL($params['expr']) . '\', ' . (int) $params['total'] . ', NOW())';
Db::getInstance()->execute($sql);
}
public function hookDisplayAdminStatsModules()
{
if (Tools::getValue('export')) {
$this->csvExport(['type' => 'pie']);
}
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($this->query . ModuleGraph::getDateBetween() . $this->query_group_by);
$this->html = '
<div class="panel-heading">
' . $this->displayName . '
</div>';
$table = '
<table class="table">
<thead>
<tr>
<th><span class="title_box active">' . $this->trans('Keywords', [], 'Modules.Statssearch.Admin') . '</span></th>
<th><span class="title_box active">' . $this->trans('Occurrences', [], 'Modules.Statssearch.Admin') . '</span></th>
<th><span class="title_box active">' . $this->trans('Results', [], 'Modules.Statssearch.Admin') . '</span></th>
</tr>
</thead>
<tbody>';
foreach ($result as $row) {
if (Tools::strlen($row['keywords']) >= Configuration::get('PS_SEARCH_MINWORDLEN')) {
$table .= '<tr>
<td>' . $row['keywords'] . '</td>
<td>' . $row['occurences'] . '</td>
<td>' . $row['total'] . '</td>
</tr>';
}
}
$table .= '
</tbody>
</table>';
if (count($result)) {
$this->html .= '<div>' . $this->engine(['type' => 'pie']) . '</div>
<a class="btn btn-default" href="' . Tools::safeOutput($_SERVER['REQUEST_URI']) . '&export=1">
<i class="icon-cloud-upload"></i> ' . $this->trans('CSV Export', [], 'Modules.Statssearch.Admin') . '
</a>' . $table;
} else {
$this->html .= '<p>' . $this->trans('Cannot find any keywords that have been searched for more than once.', [], 'Modules.Statssearch.Admin') . '</p>';
}
return $this->html;
}
protected function getData($layers)
{
$this->_titles['main'] = $this->trans('Top 10 keywords', [], 'Modules.Statssearch.Admin');
$total_result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($this->query . $this->getDate() . $this->query_group_by);
$total = 0;
$total2 = 0;
foreach ($total_result as $total_row) {
$total += $total_row['occurences'];
}
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($this->query . $this->getDate() . $this->query_group_by . ' LIMIT 9');
foreach ($result as $row) {
if (!$row['occurences']) {
continue;
}
$this->_legend[] = $row['keywords'];
$this->_values[] = $row['occurences'];
$total2 += $row['occurences'];
}
if ($total > $total2) {
$this->_legend[] = $this->trans('Others', [], 'Modules.Statssearch.Admin');
$this->_values[] = $total - $total2;
}
}
}