Completed
Pull Request — master (#5)
by Jason
08:15
created

updateCollectionItems()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 2
crap 30
1
<?php
2
3
class ProductDocCollectionDataExtension extends DataExtension
4
{
5
    /**
6
     * @param $object
7
     */
8
    public function updateCollectionObject(&$object)
9
    {
10
        if ($class = $this->owner->data()->ManagedClass) {
11
            $object = (string) $class;
12
        }
13
    }
14
15
    /**
16
     * @param $form
17
     */
18
    public function updateCollectionForm(&$form)
19
    {
20
        $fields = $form->Fields();
21
        $fields->insertAfter(
22
            SelectboxDropdownField::create('CategoryID', 'Category', ProductCategory::get()->map())
23
                ->setEmptyString('All categories'),
24
            'Products__ID'
25
        );
26
27
        $fields->removeByName([
28
            'Name',
29
            'Title',
30
            'Products__ID',
31
        ]);
32
    }
33
34
    public function updateCollectionItems(&$collection, &$searchCriteria)
35
    {
36
        $class = $this->owner->data()->ManagedClass;
37
38
        if (isset($searchCriteria['CategoryID']) && $searchCriteria['CategoryID'] != '') {
39
            $category = ProductCategory::get()->byID($searchCriteria['CategoryID']);
40
            $products = $category->Products();
41
            $docs = new ArrayList();
42
43
            foreach($products as $product) {
44
                $records = $class::get()->filter(['Products.ID' => $product->ID]);
45
                foreach ($records as $record) {
46
                    $docs->push($record);
47
                }
48
            }
49
50
            $collection = $docs;
51
        }
52
    }
53
}