Completed
Pull Request — master (#15)
by Jason
06:57 queued 02:37
created

CollectionExtension::getCollectionObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Collection;
4
5
use League\Flysystem\Exception;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\ORM\DataList;
14
use SilverStripe\ORM\GroupedList;
15
use SilverStripe\ORM\PaginatedList;
16
17
class CollectionExtension extends Extension
18
{
19
    /**
20
     * @var array
21
     */
22
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
23
        'CollectionSearchForm',
24
    );
25
26
    /**
27
     * @var DataList|ArrayList
28
     */
29
    private $collection;
30
31
    /**
32
     * @var
33
     */
34
    private $collection_object;
35
36
    /**
37
     * @return ArrayList|DataList
38
     */
39 3
    public function getCollection()
40
    {
41 3
        if (!$this->collection) {
42 1
            $this->setCollection($this->owner->request);
43
        }
44 3
        return $this->collection;
45
    }
46
47
    /**
48
     * @param HTTPRequest|null $request
49
     * @return $this
50
     */
51 1
    public function setCollection(HTTPRequest $request = null)
52
    {
53 1
        if ($request === null) {
54
            $request = $this->owner->request;
55
        }
56 1
        $searchCriteria = $request->requestVars();
57
58
        // allow $searchCriteria to be updated via extension
59 1
        $this->owner->extend('updateCollectionFilters', $searchCriteria);
60
61 1
        $object = $this->getCollectionObject();
62
63 1
        $context = (method_exists($object, 'getCustomSearchContext'))
64
            ? $object->getCustomSearchContext()
65 1
            : $object->getDefaultSearchContext();
66
67 1
        $sort = ($request->getVar('Sort'))
68
            ? (string) $request->getVar('Sort')
69 1
            : $object->stat('default_sort');
70
71 1
        $collection = $context->getResults($searchCriteria)->sort($sort);
72
73
        // allow $collection to be updated via extension
74 1
        $this->owner->extend('updateCollectionItems', $collection, $searchCriteria);
75
76 1
        $this->collection = $collection;
77 1
        return $this;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 3
    public function getCollectionObject()
84
    {
85 3
        if (!$this->collection_object) {
86 1
            $this->setCollectionObject();
87
        }
88 3
        return $this->collection_object;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94 1
    public function setCollectionObject()
95
    {
96
        try {
97 1
            $collection_object = $this->owner->config()->get('managed_object');
98 1
            $this->collection_object = $collection_object::create();
99
        } catch (Exception $e) {
100
            trigger_error($e, E_USER_NOTICE);
101
        }
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * @return int
108
     */
109 2
    public function getCollectionSize()
110
    {
111 2
        if ($object = $this->owner->config()->page_size) {
112
            return (int) $object;
113
        }
114
115 2
        return 10;
116
    }
117
118
    /**
119
     * @param HTTPRequest|null $request
120
     * @return mixed
121
     */
122 1
    public function PaginatedList(HTTPRequest $request = null)
123
    {
124 1
        if ($request === null) {
125 1
            $request = $this->owner->request;
126
        }
127 1
        $start = ($request->getVar('start')) ? (int) $request->getVar('start') : 0;
128
129 1
        $records = PaginatedList::create($this->getCollection(), $this->owner->request);
130 1
        $records->setPageStart($start);
131 1
        $records->setPageLength($this->getCollectionSize());
132
133
        // allow $records to be updated via extension
134 1
        $this->owner->extend('updatePaginatedList', $records);
135
136 1
        return $records;
137
    }
138
139
    /**
140
     * @return GroupedList
141
     */
142 1
    public function GroupedList()
143
    {
144 1
        $records = GroupedList::create($this->getCollection());
145
146
        // allow $records to be updated via extension
147 1
        $this->owner->extend('updateGroupedList', $records);
148
149 1
        return $records;
150
    }
151
152
    /**
153
     * @return Form
154
     */
155 1
    public function CollectionSearchForm()
156
    {
157 1
        $object = $this->getCollectionObject();
158 1
        $request = ($this->owner->request) ? $this->owner->request : $this->owner->parentController->getRequest();
159 1
        $sort = ($request->getVar('Sort')) ? (string) $request->getVar('Sort') : $object->stat('default_sort');
160
161 1
        $context = ($object->hasMethod('getCustomSearchContext'))
162
            ? $object->getCustomSearchContext()
163 1
            : $object->getDefaultSearchContext();
164 1
        $fields = $context->getSearchFields();
165
166
        // add sort field if managed object specs getSortOptions()
167 1
        if ($object->hasMethod('getSortOptions')) {
168 1
            $sortOptions = $object->getSortOptions();
169 1
            if ($object->stat('default_sort')) {
170
                $defaultSort = array(str_replace('"', '', $object->stat('default_sort')) => 'Default');
171
                $sortOptions = array_merge($defaultSort, $sortOptions);
172
            }
173 1
            $fields->add(
174 1
                DropdownField::create('Sort', 'Sort by:', $sortOptions, $sort)
175
            );
176
        }
177
178 1
        $actions = new FieldList(
179 1
            new FormAction($this->owner->Link(), 'Search')
180
        );
181
182 1
        if (class_exists(BootstrapForm::class)) {
0 ignored issues
show
Bug introduced by
The type Dynamic\Collection\BootstrapForm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
            $form = BootstrapForm::create(
184
                $this->owner,
185
                'CollectionSearchForm',
186
                $fields,
187
                $actions
188
            );
189
        } else {
190 1
            $form = Form::create(
191 1
                $this->owner,
192 1
                'CollectionSearchForm',
193 1
                $fields,
194 1
                $actions
195
            );
196
        }
197
198
        // allow $form to be extended via extension
199 1
        $this->owner->extend('updateCollectionForm', $form);
200
201
        $form
202 1
            ->setFormMethod('get')
203 1
            ->disableSecurityToken()
204 1
            ->loadDataFrom($request->getVars())
205 1
            ->setFormAction($this->owner->Link())
206
        ;
207
208 1
        return $form;
209
    }
210
}
211