Completed
Push — master ( 1a2f79...b57ebd )
by Simon
02:07
created

ShortListController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
class ShortListController extends Page_Controller
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $allowed_actions = array(
6
        'renderList',
7
        'addOrRemove'
8
    );
9
10
    private static $url_handlers = array(
11
        'add'       => 'addOrRemove',
12
        'remove'    => 'addOrRemove',
13
        '$URL!'     => 'renderList',
14
    );
15
16
    public function init()
17
    {
18
        parent::init();
19
20
        if ($this->request->getVar('page')) {
21
            $this->currentPage = $this->request->getVar('page');
22
        }
23
    }
24
25
    /**
26
     * When landing on the homepage, if there is a shortlist for the current
27
     * user, redirect to the correct URL. Otherwise, 404.
28
     * */
29
    public function index($request)
30
    {
31
        if (($shortlist = $this->getSessionShortList())) {
32
            return $this->redirect($shortlist->URL);
33
        }
34
35
        // render with empty template.
36
        return $this->renderWith(array('Page', 'ShortList_empty'));
37
    }
38
39
    /**
40
     * Get the absolute URL of this controller.
41
     * */
42
    public function Link($action = null)
43
    {
44
        $shortlist = $this->getSessionShortList();
45
        $url = Config::inst()->get('ShortList', 'URLSegment');
46
47
        if ($shortlist) {
48
            $url .= $shortlist->URL;
49
        }
50
51
        return $url;
52
    }
53
54
    /**
55
     * Create a new list if necessary. If a bot, do nothing!
56
     * */
57
    public function initList()
58
    {
59
        if (!ShortList::isBrowser()) {
60
            return $this->httpError(404);
61
        }
62
63
        if ($this->request->getVar('page')) {
64
            $this->currentPage = $this->request->getVar('page');
65
        }
66
67
        $shortlist = $this->getSessionShortList();
68
69
        if (!$shortlist || !$shortlist->exists()) {
70
            $shortlist = new ShortList();
71
            $shortlist->write();
72
        }
73
    }
74
75
    public function renderList($request)
76
    {
77
        $shortlist = DataObject::get_one('ShortList', $filter = array('URL' => $request->param('URL')));
78
79
        if (is_null(session_id()) ||
80
            !$request->param('URL') ||
81
            !$shortlist ||
82
            !$shortlist->exists() ||
83
            !ShortList::isBrowser()
84
        ) {
85
            return $this->httpError(404);
86
        }
87
88
        return $this->customise(array(
89
            'ShortlistURL' => $shortlist && $shortlist->exists() ? $shortlist->Link() : false,
90
            'ShortlistCount' => $shortlist && $shortlist->exists() ? $shortlist->ShortListItems()->Count() : 0
91
        ))->renderWith(
92
            array('ShortList', 'Page')
93
        );
94
    }
95
96
    /**
97
     * Add an item to the shortlist.
98
     *
99
     * @param ID id of the object to add.
100
     * @param type classname of the item to remove
101
     * @param session session id.
102
     *
103
     * */
104
    public function addToShortList($ID = false, $type = null, $session = false)
105
    {
106
        if (!$ID || is_null($type) || !$session) {
107
            return false;
108
        }
109
110
        $shortlist = $this->getSessionShortList();
111
112
        if (!$shortlist || !$shortlist->exists()) {
113
            $shortlist = new ShortList();
114
            $shortlist->write();
115
        }
116
117
        $item = DataObject::get_by_id($type, $ID);
118
119
        if ($item && $item->exists()) {
120
            $shortlistItem = new ShortListItem();
121
            $shortlistItem->ShortListID = $shortlist->ID;
122
            $shortlistItem->ItemID = $item->ID;
123
            $shortlistItem->ItemType = $type;
124
125
            $shortlist->ShortListItems()->add($shortlistItem);
126
            $shortlist->write();
127
        }
128
129
        return true;
130
    }
131
132
    public function addOrRemove($request)
133
    {
134
        if (is_null(session_id()) ||
135
            !$request->getVar('id') ||
136
            !$request->getVar('type') ||
137
            !$request->getVar('s') ||
138
            $request->getVar('s') != session_id() ||
139
            !ShortList::isBrowser()
140
        ) {
141
            return $this->httpError(404);
142
        }
143
144
        if (strpos($request->getVar('url'), 'remove') !== false) {
145
            $status = $this->removeFromShortList(
146
                $ID = $request->getVar('id'),
147
                $type = $request->getVar('type'),
148
                $session = $request->getVar('s')
149
            );
150
        } else {
151
            $status = $this->addToShortList(
152
                $ID = $request->getVar('id'),
153
                $type = $request->getVar('type'),
154
                $session = $request->getVar('s')
155
            );
156
        }
157
158
159
        if ($request->isAjax()) {
160
            $shortlist = $this->getSessionShortList();
161
            $url = false;
162
163
            if ($shortlist && $shortlist->exists()) {
164
                $url = $shortlist->Link();
165
            }
166
167
            return json_encode(array(
168
                'status' => $status,
169
                'count' => $this->shortListCount($session),
170
                'url' => $url
171
            ));
172
        }
173
174
        return $status;
175
    }
176
177
    /**
178
     * Remove an item from the shortlist.
179
     *
180
     * @param ID id of the object to remove.
181
     * @param type classname of the item to remove
182
     * @param session session id.
183
     *
184
     * */
185
    private function removeFromShortList($ID = false, $type = null, $session = false)
186
    {
187
        $shortlist = $this->getSessionShortList();
188
189
        if (!$shortlist || !$shortlist->exists()) {
190
            return true;
191
        }
192
193
        $item = DataObject::get_one('ShortListItem', $filter = "ItemType = '" . $type . "' AND ItemID = " . $ID);
194
195
        if ($item && $item->exists()) {
196
            $item->delete();
197
        } else {
198
	        return false;
199
        }
200
201
        return true;
202
    }
203
204
    /**
205
     * Get the number of items in the current short list.
206
     *
207
     * @param session The session to check & find a shortlist for.
208
     * @return mixed false if no session exists - else the number of items in the shortlist.
209
     * */
210
    public function shortListCount($session = false)
211
    {
212
        if (is_null(session_id()) || !$session || $session != session_id() || !ShortList::isBrowser()) {
213
            return false;
214
        }
215
216
        $shortlist = $this->getSessionShortList();
217
218
        if (!$shortlist || !$shortlist->exists()) {
219
            return 0;
220
        }
221
222
        return $shortlist->Items()->count();
223
    }
224
225
226
	/**
227
	 * Get a paginated list of the shortlist items.
228
	 *
229
	 * @return mixed the paginated list of items, or false if the list cannot be found.
230
	 * */
231
    public function paginatedItems()
232
    {
233
        if (!$this->getRequest()->param('URL') || !ShortList::isBrowser()) {
234
            return false;
235
        }
236
237
        $items = false;
238
        $list = DataObject::get_one('ShortList', $filter = array('URL' => $this->getRequest()->param('URL')));
239
240
        if ($list) {
241
            $items = $list->ShortListItems();
242
        }
243
244
        $this->list = new PaginatedList($items, $this->getRequest());
245
        $this->list->setPageLength(Config::inst()->get('ShortList', 'PaginationCount'));
246
        $this->list->setPaginationGetVar('page');
247
248
        if ($this->currentPage) {
249
            $this->list->setCurrentPage($this->currentPage);
250
        }
251
252
        return $this->list;
253
    }
254
255
    public function nextPage()
256
    {
257
        if ($this->list->CurrentPage() < $this->list->TotalPages()) {
258
            return '?page=' . ($this->list->CurrentPage() + 1);
259
        }
260
261
        return false;
262
    }
263
264
    public function prevPage()
265
    {
266
        if ($this->list->CurrentPage() > 1) {
267
            return '?page=' . ($this->list->CurrentPage() - 1);
268
        }
269
270
        return false;
271
    }
272
273
274
    private function getSessionShortList()
275
    {
276
        return DataObject::get_one('ShortList', $filter = array('SessionID' => session_id()));
277
    }
278
}
279