1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class GoogleCustomSearchPage_RecordField extends LiteralField |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* total number days to search back |
8
|
|
|
* @var Int |
9
|
|
|
*/ |
10
|
|
|
protected $numberOfDays = 100; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* how many days ago the data-analysis should end |
14
|
|
|
* @var Int |
15
|
|
|
*/ |
16
|
|
|
protected $endingDaysBack = 0; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* minimum number of searches for the data to show up |
20
|
|
|
* @var Int |
21
|
|
|
*/ |
22
|
|
|
protected $minimumCount = 1; |
23
|
|
|
|
24
|
|
|
public function __construct($name, $title = "") |
25
|
|
|
{ |
26
|
|
|
$totalNumberOfDaysBack = $this->numberOfDays + $this->endingDaysBack; |
27
|
|
|
$data = DB::query(" |
28
|
|
|
SELECT COUNT(ID) myCount, \"Title\" |
29
|
|
|
FROM \"GoogleCustomSearchPage_Record\" |
30
|
|
|
WHERE Created > ( NOW() - INTERVAL ".$totalNumberOfDaysBack." DAY ) |
31
|
|
|
AND Created < ( NOW() - INTERVAL ".$this->endingDaysBack." DAY ) |
32
|
|
|
GROUP BY \"Title\" |
33
|
|
|
HAVING COUNT(\"ID\") >= $this->minimumCount |
34
|
|
|
ORDER BY myCount DESC |
35
|
|
|
"); |
36
|
|
|
if (!$this->minimumCount) { |
37
|
|
|
$this->minimumCount++; |
38
|
|
|
} |
39
|
|
|
$content = ""; |
40
|
|
|
if ($title) { |
41
|
|
|
$content .= "<h2>".$title."</h2>"; |
42
|
|
|
} |
43
|
|
|
$content .= " |
44
|
|
|
<div id=\"SearchHistoryTableForCMS\"> |
45
|
|
|
<h3>Search Phrases entered at least ".$this->minimumCount." times between ".date("Y-M-d", strtotime("-".$totalNumberOfDaysBack." days"))." and ".date("Y-M-d", strtotime("-".$this->endingDaysBack." days"))."</h3> |
46
|
|
|
<table class=\"highToLow\" style=\"width: 100%;\">"; |
47
|
|
|
$list = array(); |
48
|
|
|
foreach ($data as $key => $row) { |
49
|
|
|
if (!$key) { |
50
|
|
|
$maxWidth = $row["myCount"]; |
51
|
|
|
} |
52
|
|
|
$multipliedWidthInPercentage = floor(($row["myCount"] / $maxWidth)* 100); |
|
|
|
|
53
|
|
|
$list[$row["myCount"]."-".$key] = $row["Title"]; |
54
|
|
|
$content .=' |
55
|
|
|
<tr> |
56
|
|
|
<td style="text-align: right; width: 30%; padding: 5px;"> |
57
|
|
|
'.$row["Title"].' |
58
|
|
|
</td> |
59
|
|
|
<td style="background-color: silver; padding: 5px; width: 70%;"> |
60
|
|
|
<div style="width: '.$multipliedWidthInPercentage.'%; background-color: #0066CC; color: #fff;">'.$row["myCount"].'</div> |
61
|
|
|
</td> |
62
|
|
|
</tr>'; |
63
|
|
|
} |
64
|
|
|
$content .= ' |
65
|
|
|
</table>'; |
66
|
|
|
//a - z |
67
|
|
|
asort($list); |
68
|
|
|
$content .= " |
69
|
|
|
<h3>A - Z with favourite links clicked</h3> |
70
|
|
|
<table class=\"aToz\" style=\"width: 100%;\">"; |
71
|
|
|
foreach ($list as $key => $title) { |
72
|
|
|
$array = explode("-", $key); |
73
|
|
|
$multipliedWidthInPercentage = floor(($array[0] / $maxWidth)* 100); |
74
|
|
|
$titleData = DB::query(" |
75
|
|
|
SELECT COUNT(ID) myCount, \"URL\" |
76
|
|
|
FROM \"GoogleCustomSearchPage_Record\" |
77
|
|
|
WHERE Created > ( NOW() - INTERVAL ".$totalNumberOfDaysBack." DAY ) |
78
|
|
|
AND Created < ( NOW() - INTERVAL ".$this->endingDaysBack." DAY ) |
79
|
|
|
AND \"Title\" = '".$title."' |
80
|
|
|
GROUP BY \"URL\" |
81
|
|
|
HAVING COUNT(\"ID\") >= $this->minimumCount |
82
|
|
|
ORDER BY myCount DESC |
83
|
|
|
LIMIT 3; |
84
|
|
|
"); |
85
|
|
|
unset($urlArray); |
86
|
|
|
$urlArray = array(); |
87
|
|
|
foreach ($titleData as $urlRow) { |
88
|
|
|
$urlArray[] = "<li>".$urlRow["myCount"].": ".$urlRow["URL"]."</li>"; |
89
|
|
|
} |
90
|
|
|
$content .=' |
91
|
|
|
<tr> |
92
|
|
|
<td style="text-align: right; width: 30%; padding: 5px;">'.$title.'</td> |
93
|
|
|
<td style="background-color: silver; padding: 5px; width: 70%"> |
94
|
|
|
<div style="width: '.$multipliedWidthInPercentage.'%; background-color: #0066CC; color: #fff;">'.trim($array[0]).'</div> |
95
|
|
|
<ul style="font-size: 10px">'.implode($urlArray).'</ul> |
96
|
|
|
</td> |
97
|
|
|
</tr>'; |
98
|
|
|
} |
99
|
|
|
$content .= ' |
100
|
|
|
</table> |
101
|
|
|
</div>'; |
102
|
|
|
return parent::__construct($name, $content); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Int |
107
|
|
|
* @return Field |
|
|
|
|
108
|
|
|
*/ |
109
|
|
|
public function setNumberOfDays($days) |
110
|
|
|
{ |
111
|
|
|
$this->numberOfDays = intval($days); |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Int |
117
|
|
|
* @return Field |
|
|
|
|
118
|
|
|
*/ |
119
|
|
|
public function setMinimumCount($count) |
120
|
|
|
{ |
121
|
|
|
$this->minimumCount = intval($count); |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Int |
127
|
|
|
* @return Field |
|
|
|
|
128
|
|
|
*/ |
129
|
|
|
public function setEndingDaysBack($count) |
130
|
|
|
{ |
131
|
|
|
$this->endingDaysBack = intval($count); |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.