Completed
Pull Request — master (#287)
by Nic
07:49
created

ProductHolder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 112
ccs 20
cts 35
cp 0.5714
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getCMSFields() 0 24 2
B loadDescendantProductGroupIDListInto() 0 13 5
A ProductGroupIDs() 0 6 1
A getPaginatedProducts() 0 4 1
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
    /**
16
     * @var string
17
     */
18
    private static $plural_name = 'Product Groups';
19
20
    /**
21
     * @var string
22
     */
23
    private static $description = 'Display a list of related products';
24
25
    /**
26
     * @var array
27
     */
28
    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...
29
        'ProductHolder',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    private static $many_many = [
36
        'Products' => 'FoxyStripeProduct',
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $many_many_extraFields = array(
43
        'Products' => array(
44
            'SortOrder' => 'Int'
45
        )
46
    );
47
48
    /**
49
     * @return FieldList
50
     */
51 1
    public function getCMSFields()
52
    {
53 1
        $fields = parent::getCMSFields();
54
55 1
        $config = GridFieldConfig_RelationEditor::create();
56 1
        if (class_exists('GridFieldSortableRows')) {
57
            $config->addComponent(new GridFieldSortableRows('SortOrder'));
58
        }
59 1
        $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
60 1
        $config->addComponent(new GridFieldAddExistingSearchButton());
61 1
        $fields->addFieldToTab(
62 1
            'Root.Products',
63 1
            GridField::create(
64 1
                'Products',
65 1
                _t('ProductHolder.Products', 'Products'),
66 1
                $this->Products(),
67
                $config
68 1
            )
69 1
        );
70
71 1
        $this->extend('updateCMSFields', $fields);
72
73 1
        return $fields;
74
    }
75
76
    /**
77
     * loadDescendantProductGroupIDListInto function.
78
     *
79
     * @access public
80
     * @param mixed &$idList
81
     */
82 1
    public function loadDescendantProductGroupIDListInto(&$idList)
83
    {
84
        if ($children = $this->AllChildren()) {
85 1
            foreach ($children as $child) {
86
                if (in_array($child->ID, $idList)) continue;
87
88
                if ($child instanceof ProductHolder) {
89
                    $idList[] = $child->ID;
90
                    $child->loadDescendantProductGroupIDListInto($idList);
91
                }
92
            }
93
        }
94
    }
95
96
    /**
97
     * ProductGroupIDs function.
98
     *
99
     * @access public
100
     * @return array
101
     */
102
    public function ProductGroupIDs()
103
    {
104
        $holderIDs = [];
105
        $this->loadDescendantProductGroupIDListInto($holderIDs);
106
        return $holderIDs;
107
    }
108
109
    /**
110
     * @param int $limit
111
     * @return PaginatedList
112
     */
113 1
    public function getPaginatedProducts($limit = 10)
114
    {
115 1
        return PaginatedList::create($this->Products()->sort('SortOrder'))->setPageLength($limit);
116
    }
117
118
}
119
120
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...
121
{
122
    private static $allowed_actions = [
123
        'product',
124
    ];
125
126
    /**
127
     *
128
     */
129
    public function init()
130
    {
131
        parent::init();
132
133
    }
134
135
    public function product(SS_HTTPRequest $request)
136
    {
137
138
        if (!$slug = $request->latestParam('ID')) {
139
            return $this->httpError(404);
140
        }
141
142
        if (!$product = FoxyStripeProduct::get()->byUrlSegment($slug)) {
143
            return $this->httpError(404);
144
        }
145
146
        return $this->customise([
147
            'Product' => $product,
148
        ]);
149
150
    }
151
}