Completed
Push — 2 ( 35f095...3c1312 )
by Jason
07:51
created

ProductHolder::ProductGroupIDs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
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');
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('GridFieldAddExistingSearchButton')) {
59
                $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
60
                $config->addComponent(new GridFieldAddExistingSearchButton());
61
            }
62
            $config->removeComponentsByType('GridFieldAddNewButton');
63
            $fields->addFieldToTab(
64
                'Root.Products',
65
                GridField::create(
66
                    'Products',
67
                    _t('ProductHolder.Products', 'Products'),
68
                    $this->Products(),
69
                    $config
70
                )
71
            );
72
        }
73
74 1
        return $fields;
75
    }
76
77
    /**
78
     * @return DataList
79
     */
80 1
    public function Products()
81
    {
82 1
        return $this->getManyManyComponents('Products')->sort('SortOrder');
83
    }
84
85
    /**
86
     * loadDescendantProductGroupIDListInto function.
87
     * 
88
     * @access public
89
     * @param mixed &$idList
90
     * @return void
91
     */
92
    public function loadDescendantProductGroupIDListInto(&$idList)
93
    {
94
        if ($children = $this->AllChildren()) {
95
            foreach ($children as $child) {
96
                if (in_array($child->ID, $idList)) {
97
                    continue;
98
                }
99
                
100
                if ($child instanceof ProductHolder) {
101
                    $idList[] = $child->ID;
102
                    $child->loadDescendantProductGroupIDListInto($idList);
103
                }
104
            }
105
        }
106
    }
107
    
108
    /**
109
     * ProductGroupIDs function.
110
     * 
111
     * @access public
112
     * @return array
113
     */
114
    public function ProductGroupIDs()
115
    {
116
        $holderIDs = array();
117
        $this->loadDescendantProductGroupIDListInto($holderIDs);
118
        return $holderIDs;
119
    }
120
    
121
    /**
122
     * Products function.
123
     * 
124
     * @access public
125
     * @return array
126
     */
127
    public function ProductList($limit = 10)
128
    {
129
        $config = SiteConfig::current_site_config();
130
131
        if ($config->ProductLimit>0) {
132
            $limit = $config->ProductLimit;
133
        }
134
135
        if ($config->MultiGroup) {
136
            $entries = $this->Products()->sort('SortOrder');
137
        } else {
138
            $filter = '"ParentID" = ' . $this->ID;
139
140
            // Build a list of all IDs for ProductGroups that are children
141
            $holderIDs = $this->ProductGroupIDs();
142
143
            // If no ProductHolders, no ProductPages. So return false
144
            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...
145
                // Otherwise, do the actual query
146
                if ($filter) {
147
                    $filter .= ' OR ';
148
                }
149
                $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")";
150
            }
151
152
            $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...
153
154
            $entries = ProductPage::get()->where($filter);
155
        }
156
157
158
        $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...
159
        $list->setPageLength($limit);
160
        return $list;
161
    }
162
}
163
164
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...
165
{
166
}
167