1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class EcommerceSearchHistoryFormField extends LiteralField |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* total number days to search back. |
7
|
|
|
* |
8
|
|
|
* @var int |
9
|
|
|
*/ |
10
|
|
|
protected $numberOfDays = 100; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* how many days ago the data-analysis should end. |
14
|
|
|
* |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected $endingDaysBack = 0; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* minimum number of searches for the data to show up. |
21
|
|
|
* |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $minimumCount = 30; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* maximum number of searches for the data to show up. |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $maxRows = 20; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $addTitle = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $addAtoZ = true; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* minimum number of searches for the data to show up. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $showMoreLink = false; |
51
|
|
|
|
52
|
|
|
public function __construct($name, $title = '') |
53
|
|
|
{ |
54
|
|
|
return parent::__construct($name, $title); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param int |
59
|
|
|
* |
60
|
|
|
* @return EcommerceSearchHistoryFormField |
61
|
|
|
*/ |
62
|
|
|
public function setNumberOfDays($days) |
63
|
|
|
{ |
64
|
|
|
$this->numberOfDays = intval($days); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int |
71
|
|
|
* |
72
|
|
|
* @return EcommerceSearchHistoryFormField |
73
|
|
|
*/ |
74
|
|
|
public function setMinimumCount($count) |
75
|
|
|
{ |
76
|
|
|
$this->minimumCount = intval($count); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int |
83
|
|
|
* |
84
|
|
|
* @return EcommerceSearchHistoryFormField |
85
|
|
|
*/ |
86
|
|
|
public function setShowMoreLink($b) |
87
|
|
|
{ |
88
|
|
|
$this->showMoreLink = $b; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param int |
95
|
|
|
* |
96
|
|
|
* @return EcommerceSearchHistoryFormField |
97
|
|
|
*/ |
98
|
|
|
public function setEndingDaysBack($count) |
99
|
|
|
{ |
100
|
|
|
$this->endingDaysBack = intval($count); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int |
107
|
|
|
* |
108
|
|
|
* @return EcommerceSearchHistoryFormField |
109
|
|
|
*/ |
110
|
|
|
public function setMaxRows($number) |
111
|
|
|
{ |
112
|
|
|
$this->maxRows = $number; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool |
119
|
|
|
* |
120
|
|
|
* @return EcommerceSearchHistoryFormField |
121
|
|
|
*/ |
122
|
|
|
public function setAddTitle($b) |
123
|
|
|
{ |
124
|
|
|
$this->addTitle = $b; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param bool |
131
|
|
|
* |
132
|
|
|
* @return EcommerceSearchHistoryFormField |
133
|
|
|
*/ |
134
|
|
|
public function setAddAtoZ($b) |
135
|
|
|
{ |
136
|
|
|
$this->addAtoZ = $b; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
public function FieldHolder($properties = array()) |
143
|
|
|
{ |
144
|
|
|
return $this->Field($properties); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function Field($properties = array()) |
148
|
|
|
{ |
149
|
|
|
$title = $this->getContent(); |
150
|
|
|
$totalNumberOfDaysBack = $this->numberOfDays + $this->endingDaysBack; |
151
|
|
|
$minCountSafety = SearchHistory::get()->count() / 500; |
152
|
|
|
if($this->minimumCount < $minCountSafety) { |
153
|
|
|
$this->minimumCount = $minCountSafety; |
154
|
|
|
} |
155
|
|
|
$data = DB::query(' |
156
|
|
|
SELECT COUNT(ID) myCount, "Title" |
157
|
|
|
FROM "SearchHistory" |
158
|
|
|
WHERE Created > ( NOW() - INTERVAL '.$totalNumberOfDaysBack.' DAY ) |
159
|
|
|
AND Created < ( NOW() - INTERVAL '.$this->endingDaysBack." DAY ) |
160
|
|
|
GROUP BY \"Title\" |
161
|
|
|
HAVING COUNT(\"ID\") >= $this->minimumCount |
162
|
|
|
ORDER BY myCount DESC |
163
|
|
|
LIMIT ".$this->maxRows." |
164
|
|
|
"); |
165
|
|
|
if (!$this->minimumCount) { |
166
|
|
|
++$this->minimumCount; |
167
|
|
|
} |
168
|
|
|
$content = ''; |
169
|
|
|
$tableContent = ''; |
170
|
|
|
if ($title && $this->addTitle) { |
171
|
|
|
$content .= '<h3>'.$title.'</h3>'; |
172
|
|
|
} |
173
|
|
|
$content .= ' |
174
|
|
|
<div id="SearchHistoryTableForCMS"> |
175
|
|
|
<h3> |
176
|
|
|
Search Phrases' |
177
|
|
|
.($this->minimumCount > 1 ? ', entered at least '.$this->minimumCount.' times' : '') |
178
|
|
|
.($this->maxRows < 1000 ? ', limited to '.$this->maxRows.' entries, ' : '') |
179
|
|
|
.' between '.date('j-M-Y', strtotime('-'.$totalNumberOfDaysBack.' days')).' and '.date('j-M-Y', strtotime('-'.$this->endingDaysBack.' days')).' |
180
|
|
|
</h3>'; |
181
|
|
|
$count = 0; |
182
|
|
|
if ($data && count($data)) { |
183
|
|
|
$tableContent .= ' |
184
|
|
|
<table class="highToLow" style="widht: 100%">'; |
185
|
|
|
$list = array(); |
186
|
|
|
foreach ($data as $key => $row) { |
187
|
|
|
$count++; |
188
|
|
|
//for the highest count, we work out a max-width |
189
|
|
|
if (!$key) { |
190
|
|
|
$maxWidth = $row['myCount']; |
191
|
|
|
} |
192
|
|
|
$multipliedWidthInPercentage = floor(($row['myCount'] / $maxWidth) * 100); |
|
|
|
|
193
|
|
|
$list[$row['myCount'].'-'.$key] = $row['Title']; |
194
|
|
|
$tableContent .= ' |
195
|
|
|
<tr> |
196
|
|
|
<td style="text-align: right; width: 30%; padding: 5px;">'.$row['Title'].'</td> |
197
|
|
|
<td style="background-color: silver; padding: 5px; width: 70%;"> |
198
|
|
|
<div style="width: '.$multipliedWidthInPercentage.'%; background-color: #C51162; color: #fff;">'.$row['myCount'].'</div> |
199
|
|
|
</td> |
200
|
|
|
</tr>'; |
201
|
|
|
} |
202
|
|
|
$tableContent .= ' |
203
|
|
|
</table>'; |
204
|
|
|
if ($count && $this->addAtoZ) { |
205
|
|
|
asort($list); |
206
|
|
|
$tableContent .= ' |
207
|
|
|
<h3>A - Z</h3> |
208
|
|
|
<table class="aToz" style="widht: 100%">'; |
209
|
|
|
foreach ($list as $key => $title) { |
210
|
|
|
$array = explode('-', $key); |
211
|
|
|
$multipliedWidthInPercentage = floor(($array[0] / $maxWidth) * 100); |
212
|
|
|
$tableContent .= ' |
213
|
|
|
<tr> |
214
|
|
|
<td style="text-align: right; width: 30%; padding: 5px;">'.$title.'</td> |
215
|
|
|
<td style="background-color: silver; padding: 5px; width: 70%"> |
216
|
|
|
<div style="width: '.$multipliedWidthInPercentage.'%; background-color: #004D40; color: #fff;">'.trim($array[0]).'</div> |
217
|
|
|
</td> |
218
|
|
|
</tr>'; |
219
|
|
|
} |
220
|
|
|
$tableContent .= ' |
221
|
|
|
</table>'; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
if ($count === 0) { |
225
|
|
|
//we replace table content here... |
226
|
|
|
$tableContent = '<p class="warning message">No searches found.</p>'; |
227
|
|
|
} |
228
|
|
|
$content .= $tableContent; |
229
|
|
|
if ($this->showMoreLink) { |
230
|
|
|
$content .= ' |
231
|
|
|
<p> |
232
|
|
|
<a href="/dev/tasks/EcommerceTaskReviewSearches/">Query more resuts</a> |
233
|
|
|
</p>'; |
234
|
|
|
} else { |
|
|
|
|
235
|
|
|
} |
236
|
|
|
$content .= ' |
237
|
|
|
</div>'; |
238
|
|
|
|
239
|
|
|
return $content; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.