Completed
Push — master ( 437f27...43f73f )
by Simon
02:02
created

ShortListController::getSecurityToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
use Jaybizzle\CrawlerDetect\CrawlerDetect;
3
4
class ShortListController extends Page_Controller
5
{
6
    private static $allowed_actions = array(
7
        'renderList',
8
        'performAction'
9
    );
10
11
    private static $url_handlers = array(
12
        'add'       => 'performAction',
13
        'remove'    => 'performAction',
14
        '$URL!'     => 'renderList',
15
    );
16
17
    private static $extensions = array(
18
        'ShortListPaginationExtension'
19
    );
20
21
    public function init()
22
    {
23
        parent::init();
24
25
        Session::start();
26
27
        if ($this->request->getVar('page')) {
28
            $this->currentPage = $this->request->getVar('page');
29
        }
30
    }
31
32
    /**
33
     * When landing on the homepage, if there is a shortlist for the current
34
     * user, redirect to the correct URL. Otherwise, 404.
35
     * */
36
    public function index($request)
37
    {
38
        if (($shortlist = $this->getSessionShortList())) {
39
            return $this->redirect(Config::inst()->get('ShortList', 'URLSegment').$shortlist->URL);
40
        } else {
41
            $CrawlerDetect = new CrawlerDetect;
42
43
            // Check the user agent of the current 'visitor'
44
            if ($CrawlerDetect->isCrawler()) {
45
                return $this->httpError(403);
46
            }
47
48
            $shortlist = $this->getSessionShortList();
49
50
            if (!$shortlist || !$shortlist->exists()) {
51
                $shortlist = new ShortList();
52
                $shortlist->write();
53
            }
54
        }
55
56
        // render with empty template.
57
        return $this->renderWith(array('Page', 'ShortList_empty'));
58
    }
59
60
    /**
61
     * Get the absolute URL of this controller.
62
     * */
63
    public function Link($action = null)
64
    {
65
        $shortlist = $this->getSessionShortList();
66
        $url = Config::inst()->get('ShortList', 'URLSegment');
67
68
        if ($shortlist) {
69
            $url .= $shortlist->URL;
70
        }
71
72
        return $url;
73
    }
74
75
    public function renderList($request)
76
    {
77
        $shortlist = DataObject::get_one('ShortList', $filter = array('URL' => $request->param('URL')));
78
        $link = false;
79
        $count = 0;
80
81
        if ($this->dontRender($shortlist, $request)) {
82
            return $this->httpError(404);
83
        }
84
85
        if ($shortlist && $shortlist->exists()) {
86
            $link = $shortlist->Link();
87
            $count = $shortlist->ShortListItems()->Count();
88
        }
89
90
        return $this->customise(array(
91
            'ShortlistURL' => $link,
92
            'ShortlistCount' => $count
93
        ))->renderWith(
94
            array('ShortList', 'Page')
95
        );
96
    }
97
98
    public function performAction($request)
99
    {
100
        if ($this->dontPerformAction($request)) {
101
            return $this->httpError(404);
102
        }
103
104
        $matches = array();
105
        preg_match('/remove|add/', $request->getURL(), $matches);
106
107
        $action = new AddToshortlistAction();
108
109
        if ($matches[0] == 'remove') {
110
            $action = new RemoveFromshortlistAction();
111
        }
112
113
        $status = $action->performAction(
114
            $shortlist = $this->getSessionShortList(),
115
            $ID = $request->getVar('id'),
116
            $type = $request->getVar('type'),
117
            $session = $request->getVar('s')
118
        );
119
120
        if ($request->isAjax()) {
121
            $shortlist = $this->getSessionShortList();
122
            $url = false;
123
124
            if ($shortlist && $shortlist->exists()) {
125
                $url = $shortlist->Link();
126
            }
127
128
            return json_encode(array(
129
                'count' => $this->shortListCount($session),
130
                'url' => $url
131
            ));
132
        }
133
134
        if (array_key_exists('output', $request->getVars())) {
135
            return $status;
136
        }
137
138
        return $this->redirectBack();
139
    }
140
141
142
    /**
143
     * Get the number of items in the current short list.
144
     *
145
     * @param session The session to check & find a shortlist for.
146
     * @return mixed false if no session exists - else the number of items in the shortlist.
147
     * */
148
    public function shortListCount($session = false)
149
    {
150
        if (is_null(self::getSecurityToken()) || !$session || $session != self::getSecurityToken()) {
151
            return false;
152
        }
153
154
        $shortlist = $this->getSessionShortList();
155
156
        if (!$shortlist || !$shortlist->exists()) {
157
            return 0;
158
        }
159
160
        return $shortlist->Items()->count();
161
    }
162
163
    private function getSessionShortList()
164
    {
165
        return (ShortList) DataObject::get_one('ShortList',
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting ';'
Loading history...
166
            $filter = array('SessionID' => self::getSecurityToken()),
167
            $cache = false
168
        );
169
    }
170
171
    /**
172
     * Don't render the template!
173
     * */
174
    private function dontRender($shortlist, $request)
175
    {
176
        return is_null(self::getSecurityToken()) || !$request->param('URL') || !$shortlist || !$shortlist->exists();
177
    }
178
179
    /**
180
     * Don't perform an action.
181
     * */
182
    private function dontPerformAction($request)
183
    {
184
        return is_null(self::getSecurityToken()) || !$request->getVar('id') || !$request->getVar('type') ||
185
            !$request->getVar('s') || $request->getVar('s') != self::getSecurityToken();
186
    }
187
188
    public static function getShortListSession()
189
    {
190
        return DataObject::get_one('ShortList', $filter = array('SessionID' => self::getSecurityToken()));
191
    }
192
193
    /**
194
     * Get the token to use to add/remove from shortlist.
195
     * */
196
    public static function getSecurityToken()
197
    {
198
        return Utilities::getSecurityToken();
199
    }
200
}
201