1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*@author nicolaas [at] sunnysideup . co . nz |
5
|
|
|
* |
6
|
|
|
**/ |
7
|
|
|
|
8
|
|
|
class PageRating extends DataObject |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var bool |
13
|
|
|
*/ |
14
|
|
|
private static $admin_can_create = true; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
private static $admin_can_edit = true; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private static $admin_can_delete = true; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var boolean |
28
|
|
|
*/ |
29
|
|
|
private static $round_rating = true; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $stars = array( |
|
|
|
|
35
|
|
|
'1' => array("Code" => 'OneStar', "Title" => "One Star"), |
36
|
|
|
'2' => array("Code" => 'TwoStar', "Title" => "Two Stars"), |
37
|
|
|
'3' => array("Code" => 'ThreeStar', "Title" => "Three Stars"), |
38
|
|
|
'4' => array("Code" => 'FourStar', "Title" => "Four Stars"), |
39
|
|
|
'5' => array("Code" => 'FiveStar', "Title" => "Five Stars") |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @return array |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
protected static function get_stars() |
47
|
|
|
{ |
48
|
|
|
return Config::inst()->get("PageRating", "stars"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* returns something like OneStar |
53
|
|
|
* @param int $value |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public static function get_star_entry_code($value) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$stars = self::get_stars(); |
60
|
|
|
if (isset($stars[$value]["Code"])) { |
61
|
|
|
return $stars[$value]["Code"]; |
62
|
|
|
} |
63
|
|
|
return "N/A"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* returns something like One Star |
68
|
|
|
* @param int $value |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public static function get_star_entry_name($value) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$stars = self::get_stars(); |
75
|
|
|
if (isset($stars[$value]["Title"])) { |
76
|
|
|
return $stars[$value]["Title"]; |
77
|
|
|
} |
78
|
|
|
return "N/A"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* returns something like |
83
|
|
|
* |
84
|
|
|
* 1 => One Star, |
85
|
|
|
* 2 => Two Star |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public static function get_star_dropdowndown() |
90
|
|
|
{ |
91
|
|
|
$stars = self::get_stars(); |
92
|
|
|
$newArray = array(); |
93
|
|
|
if (count($stars)) { |
94
|
|
|
foreach ($stars as $key => $star) { |
|
|
|
|
95
|
|
|
$newArray[$key] = $star["Title"]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return $newArray; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* return int |
103
|
|
|
*/ |
104
|
|
|
public static function get_number_of_stars() |
105
|
|
|
{ |
106
|
|
|
return count(self::get_stars()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private static $_star_details_as_array_data = array(); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* |
114
|
|
|
* @return ArrayData |
115
|
|
|
*/ |
116
|
|
|
public static function get_star_details_as_array_data($score, $parentID, $method = "unkown") |
117
|
|
|
{ |
118
|
|
|
$key = $score."_".$parentID."_".$method; |
119
|
|
|
if (! isset(self::$_star_details_as_array_data[$key])) { |
120
|
|
|
$stars = $score; |
121
|
|
|
if (Config::inst()->get("PageRating", "round_rating")) { |
122
|
|
|
$stars = round($stars); |
123
|
|
|
} |
124
|
|
|
$widthOutOfOneHundredForEachStar = 100 / PageRating::get_number_of_stars(); |
125
|
|
|
$percentage = round($score * $widthOutOfOneHundredForEachStar); |
126
|
|
|
$roundedPercentage = round($stars * $widthOutOfOneHundredForEachStar); |
127
|
|
|
$reversePercentage = round(100 - $percentage); |
128
|
|
|
$reverseRoundedPercentage = round(100 - $roundedPercentage); |
129
|
|
|
$starClass = PageRating::get_star_entry_code($stars); |
130
|
|
|
$page = SiteTree::get()->byId($parentID); |
131
|
|
|
self::$_star_details_as_array_data[$key] = ArrayData::create( |
132
|
|
|
array( |
133
|
|
|
'Rating' => "Stars", |
134
|
|
|
'Method' => $method, |
135
|
|
|
'Score' => $score, |
136
|
|
|
'Stars' => $stars, |
137
|
|
|
'Percentage' => $percentage, |
138
|
|
|
'RoundedPercentage' => $percentage, |
139
|
|
|
'ReversePercentage' => $reversePercentage, |
140
|
|
|
'ReverseRoundedPercentage' => $reverseRoundedPercentage, |
141
|
|
|
'StarClass' => $starClass, |
142
|
|
|
'Page' => $page |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
return self::$_star_details_as_array_data[$key]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private static $db = array( |
|
|
|
|
150
|
|
|
"Rating" => "Int", |
151
|
|
|
"Name" => "Varchar(100)", |
152
|
|
|
"Title" => "Varchar(100)", |
153
|
|
|
"Comment" => "Text", |
154
|
|
|
"IsApproved" => "Boolean", |
155
|
|
|
"IsDefault" => "Boolean" |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
private static $has_one = array( |
|
|
|
|
159
|
|
|
"Parent" => "Page" |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
private static $summary_fields = array( |
|
|
|
|
163
|
|
|
"Created" => "When", |
164
|
|
|
"Rating" => "Score", |
165
|
|
|
"Parent.Title" => "Page", |
166
|
|
|
"Comment" => "Comment", |
167
|
|
|
"IsApproved.Nice" => "Approved" |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
private static $field_labels = array( |
|
|
|
|
171
|
|
|
"Rating" => "Score", |
172
|
|
|
"Parent.Title" => "Page", |
173
|
|
|
"IsDefault" => "Default Entry Only", |
174
|
|
|
"Parent" => "Rating for", |
175
|
|
|
"IsApproved" => "Approved" |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
private static $casting = array( |
|
|
|
|
179
|
|
|
'Method' => "Varchar", |
180
|
|
|
'Stars' => "Float", |
181
|
|
|
'Percentage' => "Float", |
182
|
|
|
'RoundedPercentage' => "Int", |
183
|
|
|
'ReversePercentage' => "Float", |
184
|
|
|
'ReverseRoundedPercentage' => "Int", |
185
|
|
|
'StarClass' => "Varchar" |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
private static $default_sort = "Created DESC"; |
|
|
|
|
189
|
|
|
|
190
|
|
|
private static $singular_name = 'Page Rating'; |
|
|
|
|
191
|
|
|
|
192
|
|
|
private static $plural_name = 'Page Ratings'; |
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function getCMSFields() |
196
|
|
|
{ |
197
|
|
|
$fields = parent::getCMSFields(); |
198
|
|
|
$labels = $this->FieldLabels(); |
199
|
|
|
$fields->replaceField("Rating", OptionSetField::create("Rating", $labels["Rating"], self::get_star_dropdowndown())); |
200
|
|
|
//$fields->removeFieldFromTab("Root.Main", "Comment"); |
|
|
|
|
201
|
|
|
|
202
|
|
|
if ($this->ParentID && $this->Parent() && $this->Parent()->exists()) { |
|
|
|
|
203
|
|
|
$fields->addFieldToTab("Root.Main", $readonlyField = ReadonlyField::create("ParentDescription", $labels["Parent"], "<p><a href=\"".$this->Parent()->CMSEditLink()."\">".$this->Parent()->Title."</a></p>")); |
|
|
|
|
204
|
|
|
$readonlyField->dontEscape = true; |
205
|
|
|
$fields->removeFieldFromTab("Root.Main", "ParentID"); |
206
|
|
|
} else { |
207
|
|
|
$fields->replaceField( |
208
|
|
|
"ParentID", |
209
|
|
|
TreeDropdownField::create( |
210
|
|
|
'ParentID', |
211
|
|
|
'Parent', |
212
|
|
|
'SiteTree' |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
if (Config::inst()->get("PageRaterStarField", "allow_name")) { |
|
|
|
|
217
|
|
|
//do nothing |
218
|
|
|
} else { |
219
|
|
|
$fields->removeFieldFromTab("Root.Main", "Name"); |
220
|
|
|
} |
221
|
|
|
if (Config::inst()->get("PageRaterStarField", "allow_title")) { |
|
|
|
|
222
|
|
|
//do nothing |
223
|
|
|
} else { |
224
|
|
|
$fields->removeFieldFromTab("Root.Main", "Title"); |
225
|
|
|
} |
226
|
|
|
if (Config::inst()->get("PageRaterStarField", "allow_comments")) { |
|
|
|
|
227
|
|
|
//do nothing |
228
|
|
|
} else { |
229
|
|
|
$fields->removeFieldFromTab("Root.Main", "Comment"); |
230
|
|
|
} |
231
|
|
|
if (class_exists('DataObjectOneFieldUpdateController')) { |
232
|
|
|
$linkForApproval = DataObjectOneFieldUpdateController::popup_link( |
233
|
|
|
'PageRating', |
234
|
|
|
'IsApproved', |
235
|
|
|
"IsApproved = 0", |
236
|
|
|
'', |
237
|
|
|
'Mass Approve' |
238
|
|
|
); |
239
|
|
|
$linkForDisapproval = DataObjectOneFieldUpdateController::popup_link( |
240
|
|
|
'PageRating', |
241
|
|
|
'IsApproved', |
242
|
|
|
"IsApproved = 1", |
243
|
|
|
'', |
244
|
|
|
'Mass Disapprove' |
245
|
|
|
); |
246
|
|
|
$fields->addFieldToTab( |
247
|
|
|
'Root.Batch', |
248
|
|
|
new LiteralField( |
249
|
|
|
'QuickActions', |
250
|
|
|
'<p class="message good">' |
251
|
|
|
._t('ProductVariation.QUICK_UPDATE', 'Quick update') |
252
|
|
|
.': ' |
253
|
|
|
."<span class=\"action ss-ui-button\">$linkForApproval</span> " |
254
|
|
|
."<span class=\"action ss-ui-button\">$linkForDisapproval</span>" |
255
|
|
|
.'</p>' |
256
|
|
|
), |
257
|
|
|
'IsDefault' |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$fields->makeFieldReadonly("IsDefault"); |
262
|
|
|
return $fields; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* updates Page Rating for a Page and saves it with the Page. |
267
|
|
|
* Updates SiteTree.PageRating field ... |
268
|
|
|
* |
269
|
|
|
* if $siteTreeID is 0 then ALL pages are updated ... |
270
|
|
|
* |
271
|
|
|
* @param integer $siteTreeID |
272
|
|
|
*/ |
273
|
|
|
public static function update_ratings($siteTreeID = 0) |
274
|
|
|
{ |
275
|
|
|
if ($siteTreeID) { |
276
|
|
|
$where = "PageRating.ParentID = ".$siteTreeID; |
277
|
|
|
} else { |
278
|
|
|
$where = "PageRating.ParentID > 0"; |
279
|
|
|
} |
280
|
|
|
DB::query("DELETE FROM PageRating_TEMP;"); |
281
|
|
|
DB::query( |
282
|
|
|
" |
283
|
|
|
INSERT INTO PageRating_TEMP |
284
|
|
|
SELECT ParentID, (ROUND(AVG(PageRating.Rating) * 100)) |
285
|
|
|
FROM PageRating |
286
|
|
|
WHERE $where |
287
|
|
|
GROUP BY PageRating.ParentID; |
288
|
|
|
" |
289
|
|
|
); |
290
|
|
|
DB::query(" |
291
|
|
|
UPDATE SiteTree |
292
|
|
|
INNER JOIN PageRating_TEMP ON SiteTree.ID = PageRating_TEMP.ParentID |
293
|
|
|
SET SiteTree.PageRating = (PageRating_TEMP.Rating / 100); |
294
|
|
|
"); |
295
|
|
|
DB::query(" |
296
|
|
|
UPDATE SiteTree_Live |
297
|
|
|
INNER JOIN PageRating_TEMP ON SiteTree_Live.ID = PageRating_TEMP.ParentID |
298
|
|
|
SET SiteTree_Live.PageRating = (PageRating_TEMP.Rating / 100); |
299
|
|
|
"); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function onBeforeWrite() |
303
|
|
|
{ |
304
|
|
|
parent::onBeforeWrite(); |
305
|
|
|
if ($this->ParentID) { |
|
|
|
|
306
|
|
|
self::update_ratings($this->ParentID); |
|
|
|
|
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function requireDefaultRecords() |
311
|
|
|
{ |
312
|
|
|
parent::requireDefaultRecords(); |
313
|
|
|
DB::query("DROP TABLE IF EXISTS PageRating_TEMP;"); |
314
|
|
|
DB::query("CREATE TABLE PageRating_TEMP (ParentID INTEGER(11), Rating INTEGER);"); |
315
|
|
|
DB::query("ALTER TABLE \"PageRating_TEMP\" ADD INDEX ( \"ParentID\" ) "); |
316
|
|
|
DB::query("ALTER TABLE \"PageRating_TEMP\" ADD INDEX ( \"Rating\" ) "); |
317
|
|
|
DB::alteration_message("create PageRating_TEMP", "created"); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function canCreate($member = null) |
321
|
|
|
{ |
322
|
|
|
if ($this->Config()->get("admin_can_create")) { |
323
|
|
|
return parent::canCreate($member); |
324
|
|
|
} |
325
|
|
|
return false; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function canDelete($member = null) |
329
|
|
|
{ |
330
|
|
|
if ($this->Config()->get("admin_can_delete")) { |
331
|
|
|
return parent::canDelete($member); |
332
|
|
|
} |
333
|
|
|
return false; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function canEdit($member = null) |
337
|
|
|
{ |
338
|
|
|
if ($this->Config()->get("admin_can_edit")) { |
339
|
|
|
return parent::canEdit($member); |
340
|
|
|
} |
341
|
|
|
return false; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @alias |
346
|
|
|
* |
347
|
|
|
* @return string |
348
|
|
|
*/ |
349
|
|
|
public function Method() |
350
|
|
|
{ |
351
|
|
|
return $this->getMethod(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* casted variable |
356
|
|
|
* |
357
|
|
|
* @return string |
358
|
|
|
*/ |
359
|
|
|
public function getMethod() |
360
|
|
|
{ |
361
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
362
|
|
|
return $arrayData->Method; |
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @alias |
367
|
|
|
* |
368
|
|
|
* @return int | float |
369
|
|
|
*/ |
370
|
|
|
public function Stars() |
371
|
|
|
{ |
372
|
|
|
return $this->getStars(); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* casted variable |
377
|
|
|
* |
378
|
|
|
* @return int | float |
379
|
|
|
*/ |
380
|
|
|
public function getStars() |
381
|
|
|
{ |
382
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
383
|
|
|
return $arrayData->Stars; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @alias |
388
|
|
|
* |
389
|
|
|
* @return string |
|
|
|
|
390
|
|
|
*/ |
391
|
|
|
public function Percentage() |
392
|
|
|
{ |
393
|
|
|
return $this->getPercentage(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* casted variable |
398
|
|
|
* |
399
|
|
|
* @return float |
400
|
|
|
*/ |
401
|
|
|
public function getPercentage() |
402
|
|
|
{ |
403
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
404
|
|
|
return $arrayData->Percentage; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @alias |
409
|
|
|
* |
410
|
|
|
* @return int |
411
|
|
|
*/ |
412
|
|
|
public function RoundedPercentage() |
413
|
|
|
{ |
414
|
|
|
return $this->getRoundedPercentage(); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* casted variable |
419
|
|
|
* |
420
|
|
|
* @return int |
421
|
|
|
*/ |
422
|
|
|
public function getRoundedPercentage() |
423
|
|
|
{ |
424
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
425
|
|
|
return $arrayData->RoundedPercentage; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* casted alias |
430
|
|
|
* |
431
|
|
|
* @return string |
|
|
|
|
432
|
|
|
*/ |
433
|
|
|
public function ReversePercentage() |
434
|
|
|
{ |
435
|
|
|
return $this->getReversePercentage(); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* casted variable |
440
|
|
|
* |
441
|
|
|
* @return float |
442
|
|
|
*/ |
443
|
|
|
public function getReversePercentage() |
444
|
|
|
{ |
445
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
446
|
|
|
return $arrayDat->ReversePercentage; |
|
|
|
|
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* @alias |
451
|
|
|
* |
452
|
|
|
* @return int |
453
|
|
|
*/ |
454
|
|
|
public function ReverseRoundedPercentage() |
455
|
|
|
{ |
456
|
|
|
return $this->getReverseRoundedPercentage(); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* casted variable |
461
|
|
|
* |
462
|
|
|
* @return int |
463
|
|
|
*/ |
464
|
|
|
public function getReverseRoundedPercentage() |
465
|
|
|
{ |
466
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
467
|
|
|
return $arrayData->ReverseRoundedPercentage; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* @alias |
472
|
|
|
* |
473
|
|
|
* @return string |
474
|
|
|
*/ |
475
|
|
|
public function StarClass() |
476
|
|
|
{ |
477
|
|
|
return $this->getStarClass(); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* casted variable |
482
|
|
|
* |
483
|
|
|
* @return string |
484
|
|
|
*/ |
485
|
|
|
public function getStarClass() |
486
|
|
|
{ |
487
|
|
|
$arrayData = self::get_star_details_as_array_data($this->Rating, $this->ParentID); |
|
|
|
|
488
|
|
|
return $arrayData->StarClass; |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
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.