|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class EcommerceVote extends DataObject |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
public static $db = array( |
|
6
|
|
|
"SessionID" => "Varchar(64)" |
|
7
|
|
|
); |
|
8
|
|
|
|
|
9
|
|
|
public static $has_one = array( |
|
10
|
|
|
"Page" => "SiteTree" |
|
11
|
|
|
); |
|
12
|
|
|
|
|
13
|
|
|
protected static $array_of_classes_used = array(); |
|
14
|
|
|
public static function set_array_of_classes_used($v) |
|
15
|
|
|
{ |
|
16
|
|
|
self::$array_of_classes_used = $v; |
|
17
|
|
|
} |
|
18
|
|
|
public static function get_array_of_classes_used() |
|
19
|
|
|
{ |
|
20
|
|
|
return self::$array_of_classes_used; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected static $create_defaults = false; |
|
24
|
|
|
public static function set_create_defaults($v) |
|
25
|
|
|
{ |
|
26
|
|
|
self::$create_defaults = $v; |
|
27
|
|
|
} |
|
28
|
|
|
public static function get_create_defaults() |
|
29
|
|
|
{ |
|
30
|
|
|
return self::$create_defaults; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected static $default_votes = 100; |
|
34
|
|
|
public static function set_default_votes($v) |
|
35
|
|
|
{ |
|
36
|
|
|
self::$default_votes = $v; |
|
37
|
|
|
} |
|
38
|
|
|
public static function get_default_votes() |
|
39
|
|
|
{ |
|
40
|
|
|
return self::$default_votes; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected static $random_size_to_add_to_default = 10; |
|
44
|
|
|
public static function set_random_size_to_add_to_default($v) |
|
45
|
|
|
{ |
|
46
|
|
|
self::$random_size_to_add_to_default = $v; |
|
47
|
|
|
} |
|
48
|
|
|
public static function get_random_size_to_add_to_default() |
|
49
|
|
|
{ |
|
50
|
|
|
return self::$random_size_to_add_to_default; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static $has_many = array(); |
|
54
|
|
|
|
|
55
|
|
|
public static $many_many = array(); |
|
56
|
|
|
|
|
57
|
|
|
public static $belongs_many_many = array(); |
|
58
|
|
|
|
|
59
|
|
|
public static $many_many_extraFields = array(); |
|
60
|
|
|
|
|
61
|
|
|
//database related settings |
|
62
|
|
|
public static $indexes = array( |
|
63
|
|
|
"SessionID" => true, |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
public function onBeforeWrite() |
|
67
|
|
|
{ |
|
68
|
|
|
parent::onBeforeWrite(); |
|
69
|
|
|
if (!$this->SessionID) { |
|
|
|
|
|
|
70
|
|
|
$this->SessionID = Session_ID(); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function onAfterWrite() |
|
75
|
|
|
{ |
|
76
|
|
|
parent::onAfterWrite(); |
|
77
|
|
|
if (DataObject::get_one("EcommerceVote", "SessionID = '".Session_ID()."' AND PageID =".$this->PageID." AND ID <> ".$this->ID)) { |
|
|
|
|
|
|
78
|
|
|
$this->delete(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
public function requireDefaultRecords() |
|
84
|
|
|
{ |
|
85
|
|
|
parent::requireDefaultRecords(); |
|
86
|
|
|
$objects = DataObject::get("EcommerceVote", $filter = "", $sort = "Created DESC", $join = "", $limit = "0, 1000"); |
|
87
|
|
|
$array = array(); |
|
88
|
|
|
if ($objects) { |
|
89
|
|
|
foreach ($objects as $obj) { |
|
90
|
|
|
if (isset($array[$obj->PageID])) { |
|
91
|
|
|
if ($array[$obj->PageID] == $obj->SessionID) { |
|
92
|
|
|
$obj->delete(); |
|
93
|
|
|
DB::alteration_message("deleting double vote", "deleted"); |
|
94
|
|
|
} |
|
95
|
|
|
} else { |
|
96
|
|
|
$array[$obj->PageID] = $obj->SessionID; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
unset($array); |
|
101
|
|
|
$array = null; |
|
|
|
|
|
|
102
|
|
|
if (self::get_create_defaults()) { |
|
103
|
|
|
$array = self::get_array_of_classes_used(); |
|
104
|
|
|
if (!is_array($array)|| !count($array)) { |
|
105
|
|
|
$array = array("SiteTree"); |
|
106
|
|
|
} |
|
107
|
|
|
if (count($array)) { |
|
108
|
|
|
foreach ($array as $className) { |
|
109
|
|
|
$pages = DataObject::get($className, "EcommerceVote.ID IS NULL", $sort = "", $join = "LEFT JOIN EcommerceVote on EcommerceVote.PageID = SiteTree.ID"); |
|
110
|
|
|
if ($pages) { |
|
111
|
|
|
foreach ($pages as $page) { |
|
112
|
|
|
$number = intval(self::get_default_votes() + rand(0, self::get_random_size_to_add_to_default())); |
|
113
|
|
|
$i = 0; |
|
114
|
|
|
while ($i < $number) { |
|
115
|
|
|
$obj = new EcommerceVote(); |
|
116
|
|
|
$obj->PageID = $page->ID; |
|
|
|
|
|
|
117
|
|
|
$obj->SessionID = "default_votes_".$i; |
|
|
|
|
|
|
118
|
|
|
$obj->write(); |
|
119
|
|
|
DB::alteration_message("creating vote $i for ".$page->Title, "created"); |
|
120
|
|
|
$i++; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} else { |
|
124
|
|
|
DB::alteration_message("no pages for $className", "deleted"); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
|
|
DB::alteration_message("classname array is empty", "deleted"); |
|
129
|
|
|
} |
|
130
|
|
|
} else { |
|
131
|
|
|
DB::alteration_message("not creating defaults in EcommerceVote", "deleted"); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public static $searchable_fields = array("PageID"); |
|
136
|
|
|
|
|
137
|
|
|
public static $field_labels = array("PageID" => "Page"); |
|
138
|
|
|
|
|
139
|
|
|
public static $summary_fields = array("Page.Title"); |
|
140
|
|
|
|
|
141
|
|
|
public static $singular_name = "Vote"; |
|
142
|
|
|
public function i18n_single_name() |
|
143
|
|
|
{ |
|
144
|
|
|
return _t("EcommerceVote.ECOMMERCEVOTE", "Vote"); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public static $plural_name = "Votes"; |
|
148
|
|
|
public function i18n_plural_name() |
|
149
|
|
|
{ |
|
150
|
|
|
return _t("EcommerceVote.ECOMMERCEVOTES", "Votes"); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
class EcommerceVote_ModelAdmin extends ModelAdmin |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
public static $managed_models = array("EcommerceVote"); |
|
158
|
|
|
public static $url_segment = 'votes'; |
|
159
|
|
|
public static $menu_title = 'Votes'; |
|
160
|
|
|
} |
|
161
|
|
|
|
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.