1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ShortListController extends Page_Controller |
4
|
|
|
{ |
5
|
|
|
private static $allowed_actions = array( |
6
|
|
|
'renderList', |
7
|
|
|
'performAction' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
|
private static $url_handlers = array( |
11
|
|
|
'add' => 'performAction', |
12
|
|
|
'remove' => 'performAction', |
13
|
|
|
'$URL!' => 'renderList', |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
private static $extensions = array( |
17
|
|
|
'ShortListPaginationExtension' |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
public function init() |
21
|
|
|
{ |
22
|
|
|
parent::init(); |
23
|
|
|
|
24
|
|
|
Session::start(); |
25
|
|
|
|
26
|
|
|
if ($this->request->getVar('page')) { |
27
|
|
|
$this->currentPage = $this->request->getVar('page'); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* When landing on the homepage, if there is a shortlist for the current |
33
|
|
|
* user, redirect to the correct URL. Otherwise, 404. |
34
|
|
|
* */ |
35
|
|
|
public function index($request) |
36
|
|
|
{ |
37
|
|
|
if (($shortlist = $this->getSessionShortList())) { |
38
|
|
|
return $this->redirect(Config::inst()->get('ShortList', 'URLSegment').$shortlist->URL); |
39
|
|
|
} else { |
40
|
|
|
|
41
|
|
|
$shortlist = $this->getSessionShortList(); |
42
|
|
|
|
43
|
|
|
if (!$shortlist || !$shortlist->exists()) { |
44
|
|
|
$shortlist = new ShortList(); |
45
|
|
|
$shortlist->write(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// render with empty template. |
50
|
|
|
return $this->renderWith(array('Page', 'ShortList_empty')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the absolute URL of this controller. |
55
|
|
|
* */ |
56
|
|
|
public function Link($action = null) |
57
|
|
|
{ |
58
|
|
|
$shortlist = $this->getSessionShortList(); |
59
|
|
|
$url = Config::inst()->get('ShortList', 'URLSegment'); |
60
|
|
|
|
61
|
|
|
if ($shortlist) { |
62
|
|
|
$url .= $shortlist->URL; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $url; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function renderList($request) |
69
|
|
|
{ |
70
|
|
|
$shortlist = DataObject::get_one('ShortList', $filter = array('URL' => $request->param('URL'))); |
71
|
|
|
$link = false; |
72
|
|
|
$count = 0; |
73
|
|
|
|
74
|
|
|
if ($this->dontRender($shortlist, $request)) { |
75
|
|
|
return $this->httpError(404); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($shortlist && $shortlist->exists()) { |
79
|
|
|
$link = $shortlist->Link(); |
80
|
|
|
$count = $shortlist->ShortListItems()->Count(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->customise(array( |
84
|
|
|
'ShortlistURL' => $link, |
85
|
|
|
'ShortlistCount' => $count |
86
|
|
|
))->renderWith( |
87
|
|
|
array('ShortList', 'Page') |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function performAction($request) |
92
|
|
|
{ |
93
|
|
|
if ($this->dontPerformAction($request)) { |
94
|
|
|
return $this->httpError(404); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$action = $this->determineAction($request->getURL()); |
98
|
|
|
|
99
|
|
|
$status = $action->performAction( |
100
|
|
|
$shortlist = $this->getSessionShortList(), |
101
|
|
|
$ID = $request->getVar('id'), |
102
|
|
|
$type = $request->getVar('type'), |
103
|
|
|
$session = $request->getVar('s') |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if ($request->isAjax()) { |
107
|
|
|
return $this->renderAjax($session); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (array_key_exists('output', $request->getVars())) { |
111
|
|
|
return $status; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->redirectBack(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the number of items in the current short list. |
120
|
|
|
* |
121
|
|
|
* @param session The session to check & find a shortlist for. |
122
|
|
|
* @return mixed false if no session exists - else the number of items in the shortlist. |
123
|
|
|
* */ |
124
|
|
|
public function shortListCount($session = false) |
125
|
|
|
{ |
126
|
|
|
if ($this->isSessionValid($session)) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$shortlist = $this->getSessionShortList(); |
131
|
|
|
|
132
|
|
|
if (!$shortlist || !$shortlist->exists()) { |
133
|
|
|
return 0; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $shortlist->Items()->count(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public static function getShortListSession() |
140
|
|
|
{ |
141
|
|
|
return DataObject::get_one('ShortList', $filter = array('SessionID' => self::getSecurityToken())); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the token to use to add/remove from shortlist. |
146
|
|
|
* */ |
147
|
|
|
public static function getSecurityToken() |
148
|
|
|
{ |
149
|
|
|
return Utilities::getSecurityToken(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function determineAction($url) { |
153
|
|
|
preg_match('/remove|add/', $url, $matches = array()); |
|
|
|
|
154
|
|
|
|
155
|
|
|
switch ($matches[0]) { |
156
|
|
|
case 'remove': |
157
|
|
|
return new RemoveFromshortlistAction(); |
158
|
|
|
case 'add': |
159
|
|
|
return new AddToshortlistAction(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return a valid shortlist - or null. |
167
|
|
|
* */ |
168
|
|
|
private function getSessionShortList() |
169
|
|
|
{ |
170
|
|
|
return DataObject::get_one('ShortList', |
171
|
|
|
$filter = array('SessionID' => self::getSecurityToken()), |
172
|
|
|
$cache = false |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return the json encoded count & url for the current session |
178
|
|
|
* */ |
179
|
|
|
private function renderAjax($session) |
180
|
|
|
{ |
181
|
|
|
$shortlist = $this->getSessionShortList(); |
182
|
|
|
$url = false; |
183
|
|
|
|
184
|
|
|
if ($shortlist && $shortlist->exists()) { |
185
|
|
|
$url = $shortlist->Link(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return json_encode(array( |
189
|
|
|
'count' => $this->shortListCount($session), |
190
|
|
|
'url' => $url |
191
|
|
|
)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Don't render the template! |
196
|
|
|
* */ |
197
|
|
|
private function dontRender($shortlist, $request) |
198
|
|
|
{ |
199
|
|
|
return is_null(self::getSecurityToken()) || !$request->param('URL') || !$shortlist || !$shortlist->exists(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Is this session valid? |
204
|
|
|
* */ |
205
|
|
|
private function isSessionValid($session) { |
206
|
|
|
return is_null(self::getSecurityToken()) || !$session || $session != self::getSecurityToken(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Don't perform an action. |
211
|
|
|
* */ |
212
|
|
|
private function dontPerformAction($request) |
213
|
|
|
{ |
214
|
|
|
return is_null(self::getSecurityToken()) || !$request->getVar('id') || !$request->getVar('type') || |
215
|
|
|
!$request->getVar('s') || $request->getVar('s') != self::getSecurityToken(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|