Completed
Pull Request — master (#5)
by Jason
06:46
created

updateCollectionObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
class ProductDocCollectionDataExtension extends DataExtension
4
{
5
    /**
6
     * @param $object
7
     */
8 2
    public function updateCollectionObject(&$object)
9
    {
10 2
        if ($class = $this->owner->data()->ManagedClass) {
11 2
            $object = (string) $class;
12 2
        }
13 2
    }
14
15
    /**
16
     * @param $form
17
     */
18 1
    public function updateCollectionForm(&$form)
19
    {
20 1
        $fields = $form->Fields();
21 1
        $fields->insertAfter(
22 1
            SelectboxDropdownField::create('CategoryID', 'Category', CatalogCategory::get()->map())
23 1
                ->setEmptyString('All categories'),
24
            'Products__ID'
25 1
        );
26
27 1
        $fields->removeByName([
28 1
            'Name',
29 1
            'Title',
30 1
            'Products__ID',
31 1
        ]);
32 1
    }
33
34
    public function updateCollectionItems(&$collection, &$searchCriteria)
35
    {
36
        $class = $this->owner->data()->ManagedClass;
37
38
        if (isset($searchCriteria['CategoryID']) && $searchCriteria['CategoryID'] != '') {
39
            $category = CatalogCategory::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
}