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
|
|
|
if (!ShortList::isBrowser()) { |
42
|
|
|
return $this->httpError(404); |
43
|
|
|
} |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
$shortlist = $this->getSessionShortList(); |
47
|
|
|
|
48
|
|
|
if (!$shortlist || !$shortlist->exists()) { |
49
|
|
|
$shortlist = new ShortList(); |
50
|
|
|
$shortlist->write(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// render with empty template. |
55
|
|
|
return $this->renderWith(array('Page', 'ShortList_empty')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the absolute URL of this controller. |
60
|
|
|
* */ |
61
|
|
|
public function Link($action = null) |
62
|
|
|
{ |
63
|
|
|
$shortlist = $this->getSessionShortList(); |
64
|
|
|
$url = Config::inst()->get('ShortList', 'URLSegment'); |
65
|
|
|
|
66
|
|
|
if ($shortlist) { |
67
|
|
|
$url .= $shortlist->URL; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $url; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function renderList($request) |
74
|
|
|
{ |
75
|
|
|
$shortlist = DataObject::get_one('ShortList', $filter = array('URL' => $request->param('URL'))); |
76
|
|
|
|
77
|
|
|
if (is_null(session_id()) || |
78
|
|
|
!$request->param('URL') || |
79
|
|
|
!$shortlist || |
80
|
|
|
!$shortlist->exists() |
81
|
|
|
) { |
82
|
|
|
return $this->httpError(404); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$link = false; |
86
|
|
|
$count = 0; |
87
|
|
|
|
88
|
|
|
if ($shortlist && $shortlist->exists()) { |
89
|
|
|
$link = $shortlist->Link(); |
90
|
|
|
$count = $shortlist->ShortListItems()->Count(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->customise(array( |
94
|
|
|
'ShortlistURL' => $link, |
95
|
|
|
'ShortlistCount' => $count |
96
|
|
|
))->renderWith( |
97
|
|
|
array('ShortList', 'Page') |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function performAction($request) |
102
|
|
|
{ |
103
|
|
|
if (is_null(session_id()) || |
104
|
|
|
!$request->getVar('id') || |
105
|
|
|
!$request->getVar('type') || |
106
|
|
|
!$request->getVar('s') || |
107
|
|
|
$request->getVar('s') != session_id() |
108
|
|
|
) { |
109
|
|
|
return $this->httpError(404); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$matches = array(); |
113
|
|
|
preg_match('/remove|add/', $request->getURL(), $matches); |
114
|
|
|
|
115
|
|
|
switch ($matches[0]) { |
116
|
|
View Code Duplication |
case 'add': |
|
|
|
|
117
|
|
|
$action = new AddToshortlistAction(); |
118
|
|
|
$status = $action->performAction( |
119
|
|
|
$shortlist = $this->getSessionShortList(), |
|
|
|
|
120
|
|
|
$ID = $request->getVar('id'), |
121
|
|
|
$type = $request->getVar('type'), |
122
|
|
|
$session = $request->getVar('s') |
123
|
|
|
); |
124
|
|
|
break; |
125
|
|
View Code Duplication |
case 'remove': |
|
|
|
|
126
|
|
|
$action = new RemoveFromshortlistAction(); |
127
|
|
|
$status = $action->performAction( |
128
|
|
|
$shortlist = $this->getSessionShortList(), |
|
|
|
|
129
|
|
|
$ID = $request->getVar('id'), |
130
|
|
|
$type = $request->getVar('type'), |
131
|
|
|
$session = $request->getVar('s') |
132
|
|
|
); |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($request->isAjax()) { |
137
|
|
|
$shortlist = $this->getSessionShortList(); |
138
|
|
|
$url = false; |
139
|
|
|
|
140
|
|
|
if ($shortlist && $shortlist->exists()) { |
141
|
|
|
$url = $shortlist->Link(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return json_encode(array( |
145
|
|
|
'count' => $this->shortListCount($session), |
|
|
|
|
146
|
|
|
'url' => $url |
147
|
|
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (array_key_exists('output', $request->getVars())) { |
151
|
|
|
return $status; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->redirectBack(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the number of items in the current short list. |
160
|
|
|
* |
161
|
|
|
* @param session The session to check & find a shortlist for. |
162
|
|
|
* @return mixed false if no session exists - else the number of items in the shortlist. |
163
|
|
|
* */ |
164
|
|
|
public function shortListCount($session = false) |
165
|
|
|
{ |
166
|
|
|
if (is_null(session_id()) || !$session || $session != session_id()) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$shortlist = $this->getSessionShortList(); |
171
|
|
|
|
172
|
|
|
if (!$shortlist || !$shortlist->exists()) { |
173
|
|
|
return 0; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $shortlist->Items()->count(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function getSessionShortList() |
180
|
|
|
{ |
181
|
|
|
return DataObject::get_one('ShortList', $filter = array('SessionID' => session_id()), $cache = false); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public static function getShortListSession() |
185
|
|
|
{ |
186
|
|
|
return DataObject::get_one('ShortList', $filter = array('SessionID' => session_id())); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.