ProductCategory::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Products\Page;
4
5
use SilverStripe\CMS\Model\RedirectorPage;
6
use SilverStripe\CMS\Model\VirtualPage;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\NumericField;
9
use SilverStripe\Security\Security;
10
11
class ProductCategory extends \Page
12
{
13
    /**
14
     * @var array
15
     */
16
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
17
        'ProductsPerPage' => 'Int',
18
    ];
19
20
    /**
21
     * @var array
22
     */
23
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
24
        'ProductsPerPage' => 12,
25
    ];
26
27
    /**
28
     * @var string
29
     */
30
    private static $table_name = 'ProductCategory';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
31
32
    /**
33
     * @var array
34
     */
35
    private static $allowed_children = [
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
36
        ProductCategory::class,
37
        Product::class,
38
        RedirectorPage::class,
39
        VirtualPage::class,
40
    ];
41
42
    /**
43
     * @return \SilverStripe\Forms\FieldList
44
     */
45
    public function getCMSFields()
46
    {
47
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
48
            $fields->addFieldToTab(
49
                'Root.Main',
50
                NumericField::create('ProductsPerPage')
51
                    ->setTitle(_t(__CLASS__ . '.ProductsPerPage', 'Products Per Page')),
52
                'Content'
53
            );
54
        });
55
56
        return parent::getCMSFields();
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getProductList()
63
    {
64
        $categories = ProductCategory::get()->filter('ParentID', $this->data()->ID)->column('ID');
65
        $categories[] = $this->data()->ID;
66
        $products = Product::get()
67
            ->filterAny([
68
                'ParentID' => $categories,
69
            ]);
70
71
        $this->extend('updateProductList', $products, $categories);
72
73
        $products = $products->filterByCallback(function ($page) {
74
            return $page->canView(Security::getCurrentUser());
75
        });
76
77
        return $products;
78
    }
79
}
80