Completed
Pull Request — master (#24)
by Jason
20:11 queued 05:05
created

ProductDocCollectionDataExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 50
c 0
b 0
f 0
ccs 15
cts 30
cp 0.5
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCollectionObject() 0 6 2
A updateCollectionForm() 0 14 1
B updateCollectionItems() 0 19 5
1
<?php
2
3
namespace Dynamic\ProductCatalog\ORM;
4
5
use Dynamic\ProductCatalog\Page\CatalogCategory;
6
use SilverStripe\Dev\Debug;
7
use SilverStripe\Forms\DropdownField;
8 2
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\ORM\DataExtension;
10 2
11 2
class ProductDocCollectionDataExtension extends DataExtension
12 2
{
13 2
    /**
14
     * @param $object
15
     */
16
    public function updateCollectionObject(&$object)
17
    {
18 1
        if ($class = $this->owner->data()->ManagedClass) {
19
            $object = (string) $class;
20 1
        }
21 1
    }
22 1
23 1
    /**
24
     * @param $form
25 1
     */
26
    public function updateCollectionForm(&$form)
27 1
    {
28 1
        $fields = $form->Fields();
29 1
        $fields->push(
30 1
            DropdownField::create('CategoryID', 'Category', CatalogCategory::get()->map())
31 1
                ->setEmptyString('All categories')
32 1
        );
33
34
        $fields->removeByName([
35
            'Name',
36
            'Title',
37
            //'Products__ID',
38
        ]);
39
    }
40
41
    public function updateCollectionItems(&$collection, &$searchCriteria)
42
    {
43
        $class = $this->owner->data()->ManagedClass;
44
45
        if (isset($searchCriteria['CategoryID']) && $searchCriteria['CategoryID'] != '') {
46
            $category = CatalogCategory::get()->byID($searchCriteria['CategoryID']);
47
            $products = $category->Products();
48
            $docs = new ArrayList();
49
50
            foreach($products as $product) {
51
                $records = $class::get()->filter(['Products.ID' => $product->ID]);
52
                foreach ($records as $record) {
53
                    $docs->push($record);
54
                }
55
            }
56
57
            $collection = $docs;
58
        }
59
    }
60
}