Completed
Push — master ( a05403...d3048a )
by Simon
02:08
created

ShortListController::prevPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
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 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
class ShortListController extends Page_Controller
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
    private static $extensions = array(
1 ignored issue
show
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
            /*
1 ignored issue
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
    /**
102
     * Add an item to the shortlist.
103
     *
104
     * @param ID id of the object to add.
105
     * @param type classname of the item to remove
106
     * @param session session id.
107
     *
108
     * */
109
    public function addToShortList($ID = false, $type = null, $session = false)
110
    {
111
        if (!$ID || is_null($type) || !$session) {
112
            return false;
113
        }
114
115
        $shortlist = $this->getSessionShortList();
116
117
        if (!$shortlist || !$shortlist->exists()) {
118
            $shortlist = new ShortList();
119
            $shortlist->write();
120
        }
121
122
        // check whether the itme is already in the list
123
        // before attempting to add it.
124
        $existing = $shortlist->ShortListItems()->filterAny(
125
            array('ItemID' => $ID, 'ItemType' => $type)
126
        );
127
128
        if ($existing->count() == 1) {
129
            return true;
130
        }
131
132
        $shortlistItem = new ShortListItem();
133
        $shortlistItem->ShortListID = $shortlist->ID;
134
        $shortlistItem->ItemID = $ID;
135
        $shortlistItem->ItemType = $type;
136
137
        $shortlist->ShortListItems()->add($shortlistItem);
138
        $shortlist->write();
139
140
        return true;
141
    }
142
143
    public function addOrRemove($request)
144
    {
145
        if (is_null(session_id()) ||
146
            !$request->getVar('id') ||
147
            !$request->getVar('type') ||
148
            !$request->getVar('s') ||
149
            $request->getVar('s') != session_id()
150
        ) {
151
            return $this->httpError(404);
152
        }
153
154
        if (strpos($request->getURL(), 'remove') !== false) {
155
            $status = $this->removeFromShortList(
156
                $ID = $request->getVar('id'),
157
                $type = $request->getVar('type'),
158
                $session = $request->getVar('s')
159
            );
160
        } else {
161
            $status = $this->addToShortList(
162
                $ID = $request->getVar('id'),
163
                $type = $request->getVar('type'),
164
                $session = $request->getVar('s')
165
            );
166
        }
167
168
169
        if ($request->isAjax()) {
170
            $shortlist = $this->getSessionShortList();
171
            $url = false;
172
173
            if ($shortlist && $shortlist->exists()) {
174
                $url = $shortlist->Link();
175
            }
176
177
            return json_encode(array(
178
                'status' => $status,
179
                'count' => $this->shortListCount($session),
180
                'url' => $url
181
            ));
182
        }
183
184
        if (array_key_exists('output', $request->getVars())) {
185
            return true;
186
        }
187
188
        return $this->redirectBack();
189
    }
190
191
    /**
192
     * Remove an item from the shortlist.
193
     *
194
     * @param ID id of the object to remove.
195
     * @param type classname of the item to remove
196
     * @param session session id.
197
     *
198
     * */
199
    private function removeFromShortList($ID = false, $type = null, $session = false)
200
    {
201
        $shortlist = $this->getSessionShortList();
202
203
        if (!$shortlist || !$shortlist->exists()) {
204
            return true;
205
        }
206
207
        $item = DataObject::get_one('ShortListItem', $filter = "ItemType = '" . $type . "' AND ItemID = " . $ID);
208
209
        if ($item && $item->exists()) {
210
            $item->delete();
211
        } else {
212
            return false;
213
        }
214
215
        return true;
216
    }
217
218
    /**
219
     * Get the number of items in the current short list.
220
     *
221
     * @param session The session to check & find a shortlist for.
222
     * @return mixed false if no session exists - else the number of items in the shortlist.
223
     * */
224
    public function shortListCount($session = false)
225
    {
226
        if (is_null(session_id()) || !$session || $session != session_id()) {
227
            return false;
228
        }
229
230
        $shortlist = $this->getSessionShortList();
231
232
        if (!$shortlist || !$shortlist->exists()) {
233
            return 0;
234
        }
235
236
        return $shortlist->Items()->count();
237
    }
238
239
    private function getSessionShortList()
240
    {
241
        return DataObject::get_one('ShortList', $filter = array('SessionID' => session_id()));
242
    }
243
}
244