ProductHolder::getCMSFields()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.5772

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 22
ccs 4
cts 15
cp 0.2667
rs 9.7998
cc 2
nc 1
nop 0
crap 3.5772
1
<?php
2
3
namespace Dynamic\FoxyStripe\Page;
4
5
use Dynamic\FoxyStripe\Model\FoxyStripeSetting;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\ORM\PaginatedList;
12
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
13
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
14
15
/**\
16
 * Class ProductHolder
17
 * @package Dynamic\FoxyStripe\Page
18
 */
19
class ProductHolder extends \Page
20
{
21
    /**
22
     * @var string
23
     */
24
    private static $singular_name = 'Product Group';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var string
28
     */
29
    private static $plural_name = 'Product Groups';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
30
31
    /**
32
     * @var string
33
     */
34
    private static $description = 'Display a list of related products';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var array
38
     */
39
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
40
        'Products' => ProductPage::class,
41
    ];
42
43
    /**
44
     * @var array
45
     */
46
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
47
        'Products' => [
48
            'SortOrder' => 'Int',
49
        ],
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    /*
56
    private static $allowed_children = [
57
        ProductHolder::class,
58
        ProductPage::class,
59
    ];
60
    */
61
62
    /**
63
     * @var string
64
     */
65
    private static $table_name = 'ProductHolder';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
66
67
    /**
68
     * @return FieldList
69
     */
70
    public function getCMSFields()
71
    {
72 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
73 1
            if (FoxyStripeSetting::current_foxystripe_setting()->MultiGroup) {
74
                $config = GridFieldConfig_RelationEditor::create();
75
                $config->addComponent(new GridFieldOrderableRows('SortOrder'));
76
                $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
77
                $config->addComponent(new GridFieldAddExistingSearchButton());
78
79
                $fields->addFieldToTab(
80
                    'Root.Products',
81
                    GridField::create(
82
                        'Products',
83
                        _t('ProductHolder.Products', 'Products'),
84
                        $this->Products(),
85
                        $config
86
                    )
87
                );
88
            }
89 1
        });
90
91 1
        return parent::getCMSFields();
92
    }
93
94
    /**
95
     * @return DataList
96
     */
97 1
    public function Products()
98
    {
99 1
        return $this->getManyManyComponents('Products')->sort('SortOrder');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getManyMan...ts')->sort('SortOrder') also could return the type SilverStripe\ORM\UnsavedRelationList which is incompatible with the documented return type SilverStripe\ORM\DataList.
Loading history...
100
    }
101
102
    /**
103
     * loadDescendantProductGroupIDListInto function.
104
     *
105
     * @param mixed &$idList
106
     */
107
    public function loadDescendantProductGroupIDListInto(&$idList)
108
    {
109
        if ($children = $this->AllChildren()) {
0 ignored issues
show
Bug introduced by
The method AllChildren() does not exist on Dynamic\FoxyStripe\Page\ProductHolder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        if ($children = $this->/** @scrutinizer ignore-call */ AllChildren()) {
Loading history...
110
            foreach ($children as $child) {
111
                if (in_array($child->ID, $idList)) {
112
                    continue;
113
                }
114
115
                if ($child instanceof self) {
116
                    $idList[] = $child->ID;
117
                    $child->loadDescendantProductGroupIDListInto($idList);
118
                }
119
            }
120
        }
121
    }
122
123
    /**
124
     * ProductGroupIDs function.
125
     *
126
     * @return array
127
     */
128
    public function ProductGroupIDs()
129
    {
130
        $holderIDs = [];
131
        $this->loadDescendantProductGroupIDListInto($holderIDs);
132
133
        return $holderIDs;
134
    }
135
136
    /**
137
     * @param int $limit
138
     *
139
     * @return PaginatedList
140
     *
141
     * @throws \Exception
142
     */
143
    public function ProductList($limit = 10)
144
    {
145
        $config = FoxyStripeSetting::current_foxystripe_setting();
146
147
        if ($config->ProductLimit > 0) {
148
            $limit = $config->ProductLimit;
149
        }
150
151
        if ($config->MultiGroup) {
152
            $entries = $this->Products()->sort('SortOrder');
153
        } else {
154
            $filter = '"ParentID" = ' . $this->ID;
155
156
            // Build a list of all IDs for ProductGroups that are children
157
            $holderIDs = $this->ProductGroupIDs();
158
159
            // If no ProductHolders, no ProductPages. So return false
160
            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...
161
                // Otherwise, do the actual query
162
                if ($filter) {
163
                    $filter .= ' OR ';
164
                }
165
                $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ')';
166
            }
167
168
            $order = '"SiteTree"."Title" ASC';
0 ignored issues
show
Unused Code introduced by
The assignment to $order is dead and can be removed.
Loading history...
169
170
            $entries = ProductPage::get()->where($filter);
171
        }
172
173
        $list = new PaginatedList($entries, Controller::curr()->getRequest());
174
        $list->setPageLength($limit);
0 ignored issues
show
Bug introduced by
It seems like $limit can also be of type SilverStripe\ORM\FieldType\DBInt; however, parameter $length of SilverStripe\ORM\PaginatedList::setPageLength() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
        $list->setPageLength(/** @scrutinizer ignore-type */ $limit);
Loading history...
175
176
        return $list;
177
    }
178
}
179