Completed
Push — master ( d41ddb...67a224 )
by Simon
02:11
created

ShortListController::getSessionShortList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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