Completed
Pull Request — master (#287)
by Nic
04:16
created

ProductHolder_Controller::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @package FoxyStripe
5
 * @method Products|SS_List $Products
6
 */
7
class ProductHolder extends Page
8
{
9
10
    /**
11
     * @var string
12
     */
13
    private static $singular_name = 'Product Group';
14
    /**
15
     * @var string
16
     */
17
    private static $plural_name = 'Product Groups';
18
    /**
19
     * @var string
20
     */
21
    private static $description = 'Display a list of related products';
22
23
    /**
24
     * @var array
25
     */
26
    private static $allowed_children = [
0 ignored issues
show
Unused Code introduced by
The property $allowed_children is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
        'ProductHolder',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $many_many = [
34
        'Products' => 'FoxyStripeProduct',
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $many_many_extraFields = array(
41
        'Products' => array(
42
            'SortOrder' => 'Int'
43
        )
44
    );
45
46
    /**
47
     * @return FieldList
48
     */
49
    public function getCMSFields()
50
    {
51
        $fields = parent::getCMSFields();
52
53
        $config = GridFieldConfig_RelationEditor::create();
54
        if (class_exists('GridFieldSortableRows')) {
55
            $config->addComponent(new GridFieldSortableRows('SortOrder'));
56
        }
57
        $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
58
        $config->addComponent(new GridFieldAddExistingSearchButton());
59
        $fields->addFieldToTab(
60
            'Root.Products',
61
            GridField::create(
62
                'Products',
63
                _t('ProductHolder.Products', 'Products'),
64
                $this->Products(),
65
                $config
66
            )
67
        );
68
69
        $this->extend('updateCMSFields', $fields);
70
71
        return $fields;
72
    }
73
74
    /**
75
     * loadDescendantProductGroupIDListInto function.
76
     *
77
     * @access public
78
     * @param mixed &$idList
79
     */
80
    public function loadDescendantProductGroupIDListInto(&$idList)
81
    {
82
        if ($children = $this->AllChildren()) {
83
            foreach ($children as $child) {
84
                if (in_array($child->ID, $idList)) continue;
85
86
                if ($child instanceof ProductHolder) {
87
                    $idList[] = $child->ID;
88
                    $child->loadDescendantProductGroupIDListInto($idList);
89
                }
90
            }
91
        }
92
    }
93
94
    /**
95
     * ProductGroupIDs function.
96
     *
97
     * @access public
98
     * @return array
99
     */
100
    public function ProductGroupIDs()
101
    {
102
        $holderIDs = [];
103
        $this->loadDescendantProductGroupIDListInto($holderIDs);
104
        return $holderIDs;
105
    }
106
107
}
108
109
/**
110
 * Class ProductHolder_Controller
111
 */
112
class ProductHolder_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
113
{
114
115
    /**
116
     * @var array
117
     */
118
    private static $allowed_actions = [
119
        'product',
120
    ];
121
122
    /**
123
     *
124
     */
125
    public function init()
126
    {
127
        parent::init();
128
129
    }
130
131
    public function product(SS_HTTPRequest $request)
132
    {
133
134
        if (!$slug = $request->latestParam('ID')) {
135
            return $this->httpError(404);
136
        }
137
138
        if (!$product = FoxyStripeProduct::get()->byUrlSegment($slug)) {
139
            return $this->httpError(404);
140
        }
141
142
        return $this->customise([
143
            'Product' => $product,
144
        ]);
145
146
    }
147
}