1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*@author nicolaas[at]sunnysideup.co.nz |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
**/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class EcommerceVoteDecorator extends Extension |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
public static $allowed_actions = array( |
14
|
|
|
"addecommercevote" => true, |
15
|
|
|
"removeallecommercevotes" => true |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public function addecommercevote() |
21
|
|
|
{ |
22
|
|
|
$id = intval(Director::URLParam("ID")); |
|
|
|
|
23
|
|
|
if ($id) { |
24
|
|
|
if ($page = DataObject::get_by_id("SiteTree", $id)) { |
|
|
|
|
25
|
|
|
$ecommerceVote = new EcommerceVote(); |
26
|
|
|
$ecommerceVote->PageID = $id; |
|
|
|
|
27
|
|
|
$ecommerceVote->write(); |
28
|
|
|
if (Director::is_ajax()) { |
29
|
|
|
return "voted"; |
30
|
|
|
} else { |
31
|
|
|
Director::redirectBack(); |
|
|
|
|
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
if (Director::is_ajax()) { |
37
|
|
|
return "vote ERROR"; |
38
|
|
|
} else { |
39
|
|
|
Director::redirectBack(); |
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
public function TopEcommerceVotes($numberOfEntries = 5) |
46
|
|
|
{ |
47
|
|
|
$sqlQuery = new SQLQuery( |
48
|
|
|
$select = "SiteTree.ID MyPageID, COUNT(EcommerceVote.ID) c", |
49
|
|
|
$from = array('SiteTree INNER JOIN EcommerceVote ON SiteTree.ID = EcommerceVote.PageID'), |
50
|
|
|
$where = "", |
51
|
|
|
$orderby = "c DESC", |
52
|
|
|
$groupby = "SiteTree.ID", |
53
|
|
|
$having = "", |
54
|
|
|
$limit = "0, $numberOfEntries" |
55
|
|
|
); |
56
|
|
|
$results = $sqlQuery->execute(); |
57
|
|
|
if ($results) { |
58
|
|
|
$stage = Versioned::current_stage(); |
59
|
|
|
$baseClass = "SiteTree"; |
60
|
|
|
$stageTable = ($stage == 'Stage') ? $baseClass : "{$baseClass}_{$stage}"; |
|
|
|
|
61
|
|
|
$dataObjectSet = new DataObjectSet(); |
62
|
|
|
foreach ($results as $result) { |
63
|
|
|
$page = DataObject::get_by_id("SiteTree", $result["MyPageID"]); |
64
|
|
|
$page->EcommerceVoteCounter = $result["c"]; |
65
|
|
|
$dataObjectSet->push($page); |
66
|
|
|
} |
67
|
|
|
return $dataObjectSet; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function EcommerceVoteTopFive() |
72
|
|
|
{ |
73
|
|
|
return $this->TopEcommerceVotes(5); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function removeallecommercevotes() |
77
|
|
|
{ |
78
|
|
|
if ($m = Member::currentUser()) { |
79
|
|
|
if ($m->isAdmin()) { |
80
|
|
|
DB::query("DELETE FROM EcommerceVote"); |
81
|
|
|
die("all votes have been deleted"); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
die("you have to be logged in as an administrator"); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.