EventHolderController::getUpdateName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\PageTypes;
4
5
use SilverStripe\ORM\FieldType\DBDatetime;
6
use SilverStripe\ORM\PaginatedList;
7
8
/**
9
 * The parameters apply in the following preference order:
10
 *  - Highest priority: Tag & date (or date range)
11
 *  - Month (and Year)
12
 *  - Pagination
13
 *
14
 * So, when the user click on a tag link, the pagination, and month will be reset, but not the date filter. Also,
15
 * changing the date will not affect the tag, but will reset the month and pagination.
16
 *
17
 * When the user clicks on a month, pagination will be reset, but tags retained. Pagination retains all other
18
 * parameters.
19
 */
20
class EventHolderController extends DatedUpdateHolderController
21
{
22
    public function getUpdateName()
23
    {
24
        $params = $this->parseParams();
25
        if ($params['upcomingOnly']) {
26
            return _t(__CLASS__ . '.Upcoming', 'Upcoming events');
27
        }
28
29
        return _t(__CLASS__ . '.Events', 'Events');
30
    }
31
32
    /**
33
     * Parse URL parameters.
34
     *
35
     * @param boolean $produceErrorMessages Set to false to omit session messages.
36
     */
37
    public function parseParams($produceErrorMessages = true)
38
    {
39
        $params = parent::parseParams($produceErrorMessages);
40
41
        // We need to set whether or not we're supposed to be displaying only upcoming events or all events.
42
        $params['upcomingOnly'] = !($params['from'] || $params['to'] || $params['year'] || $params['month']);
43
44
        return $params;
45
    }
46
47
    /**
48
     * Get the events based on the current query.
49
     */
50
    public function FilteredUpdates($pageSize = 20)
51
    {
52
        $params = $this->parseParams();
53
54
        $items = $this->Updates(
55
            $params['tag'],
56
            $params['from'],
57
            $params['to'],
58
            $params['year'],
59
            $params['month']
60
        );
61
62
        if ($params['upcomingOnly']) {
63
            $items = $items->filter(['Date:LessThan:Not' => DBDatetime::now()->Format('y-MM-dd')]);
64
        }
65
66
        // Apply pagination
67
        $list = PaginatedList::create($items, $this->getRequest());
68
        $list->setPageLength($pageSize);
69
        return $list;
70
    }
71
}
72