Passed
Pull Request — master (#309)
by Jason
13:47
created

ProductHolder::ProductList()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 8
nop 1
1
<?php
2
3
namespace Dynamic\FoxyStripe\Page;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
9
use SilverStripe\ORM\DataList;
10
use SilverStripe\ORM\PaginatedList;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
13
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
14
15
class ProductHolder extends \Page
16
{
17
    /**
18
     * @var string
19
     */
20
    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...
21
22
    /**
23
     * @var string
24
     */
25
    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...
26
27
    /**
28
     * @var string
29
     */
30
    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...
31
32
    /**
33
     * @var array
34
     */
35
    private static $many_many = array(
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
36
        'Products' => ProductPage::class,
37
    );
38
39
    /**
40
     * @var array
41
     */
42
    private static $many_many_extraFields = array(
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
43
        'Products' => array(
44
            'SortOrder' => 'Int'
45
        )
46
    );
47
48
    /**
49
     * @var array
50
     */
51
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
    private static $allowed_children = [
53
        ProductHolder::class,
54
        ProductPage::class,
55
    ];
56
    */
57
58
    /**
59
     * @var string
60
     */
61
    private static $table_name = 'FS_ProductHolder';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
62
63
    /**
64
     * @return FieldList
65
     */
66
    public function getCMSFields()
67
    {
68
        $fields = parent::getCMSFields();
69
70
        if (SiteConfig::current_site_config()->MultiGroup) {
71
            $config = GridFieldConfig_RelationEditor::create();
72
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
73
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
74
            $config->addComponent(new GridFieldAddExistingSearchButton());
75
76
            $fields->addFieldToTab(
77
                'Root.Products',
78
                GridField::create(
79
                    'Products',
0 ignored issues
show
Bug introduced by
'Products' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

79
                    /** @scrutinizer ignore-type */ 'Products',
Loading history...
80
                    _t('ProductHolder.Products', 'Products'),
81
                    $this->Products(),
0 ignored issues
show
Bug introduced by
$this->Products() of type SilverStripe\ORM\RelationList is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

81
                    /** @scrutinizer ignore-type */ $this->Products(),
Loading history...
82
                    $config
83
                )
84
            );
85
        }
86
87
        return $fields;
88
    }
89
90
    /**
91
     * @return DataList
92
     */
93
    public function Products()
94
    {
95
        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...
96
    }
97
98
    /**
99
     * loadDescendantProductGroupIDListInto function.
100
     * 
101
     * @access public
102
     * @param mixed &$idList
103
     * @return void
104
     */
105
    public function loadDescendantProductGroupIDListInto(&$idList)
106
    {
107
        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

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