Completed
Push — master ( a10f05...2ab214 )
by Jason
9s
created

ProductHolder_Controller::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 *
4
 * @package FoxyStripe
5
 *
6
 */
7
8
class ProductHolder extends Page
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 $many_many = array(
29
        'Products' => 'ProductPage'
30
    );
31
32
    /**
33
     * @var array
34
     */
35
    private static $many_many_extraFields = array(
36
        'Products' => array(
37
            'SortOrder' => 'Int'
38
        )
39
    );
40
41
    /**
42
     * @var array
43
     */
44
    private static $allowed_children = array('ProductHolder', 'ProductPage');
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...
45
46
    /**
47
     * @return FieldList
48
     */
49 1
    public function getCMSFields()
50
    {
51 1
        $fields = parent::getCMSFields();
52
53 1
        if (SiteConfig::current_site_config()->MultiGroup) {
54
            $config = GridFieldConfig_RelationEditor::create();
55
            if (class_exists('GridFieldSortableRows')) {
56
                $config->addComponent(new GridFieldSortableRows('SortOrder'));
57
            }
58
            if (class_exists('GridFieldManyRelationHandler')) {
59
                $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
60
                $config->addComponent(new GridFieldManyRelationHandler());
61
            }
62
            $fields->addFieldToTab(
63
                'Root.Products',
64
                GridField::create(
65
                    'Products',
66
                    _t('ProductHolder.Products', 'Products'),
67
                    $this->Products(),
68
                    $config
69
                )
70
            );
71
        }
72
73 1
        $this->extend('updateCMSFields', $fields);
74
75 1
        return $fields;
76
    }
77
78
    /**
79
     * @return DataList
80
     */
81 1
    public function Products()
82
    {
83 1
        return $this->getManyManyComponents('Products')->sort('SortOrder');
84
    }
85
86
    /**
87
     * loadDescendantProductGroupIDListInto function.
88
     * 
89
     * @access public
90
     * @param mixed &$idList
91
     * @return void
92
     */
93
    public function loadDescendantProductGroupIDListInto(&$idList)
94
    {
95
        if ($children = $this->AllChildren()) {
96
            foreach ($children as $child) {
97
                if (in_array($child->ID, $idList)) {
98
                    continue;
99
                }
100
                
101
                if ($child instanceof ProductHolder) {
102
                    $idList[] = $child->ID;
103
                    $child->loadDescendantProductGroupIDListInto($idList);
104
                }
105
            }
106
        }
107
    }
108
    
109
    /**
110
     * ProductGroupIDs function.
111
     * 
112
     * @access public
113
     * @return array
114
     */
115
    public function ProductGroupIDs()
116
    {
117
        $holderIDs = array();
118
        $this->loadDescendantProductGroupIDListInto($holderIDs);
119
        return $holderIDs;
120
    }
121
    
122
    /**
123
     * Products function.
124
     * 
125
     * @access public
126
     * @return array
127
     */
128
    public function ProductList($limit = 10)
129
    {
130
        $config = SiteConfig::current_site_config();
131
132
        if ($config->ProductLimit>0) {
133
            $limit = $config->ProductLimit;
134
        }
135
136
        if ($config->MultiGroup) {
137
            $entries = $this->Products()->sort('SortOrder');
138
        } else {
139
            $filter = '"ParentID" = ' . $this->ID;
140
141
            // Build a list of all IDs for ProductGroups that are children
142
            $holderIDs = $this->ProductGroupIDs();
143
144
            // If no ProductHolders, no ProductPages. So return false
145
            if ($holderIDs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $holderIDs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
146
                // Otherwise, do the actual query
147
                if ($filter) {
148
                    $filter .= ' OR ';
149
                }
150
                $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")";
151
            }
152
153
            $order = '"SiteTree"."Title" ASC';
0 ignored issues
show
Unused Code introduced by
$order is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
155
            $entries = ProductPage::get()->where($filter);
156
        }
157
158
159
        $list = new PaginatedList($entries, Controller::curr()->request);
0 ignored issues
show
Documentation introduced by
\Controller::curr()->request is of type object<SS_HTTPRequest>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
        $list->setPageLength($limit);
161
        return $list;
162
    }
163
}
164
165
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...
166
{
167
}
168