Completed
Push — master ( ea22d3...df2857 )
by Nic
21s queued 11s
created

src/Page/ProductHolder.php (1 issue)

Severity
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';
25
26
    /**
27
     * @var string
28
     */
29
    private static $plural_name = 'Product Groups';
30
31
    /**
32
     * @var string
33
     */
34
    private static $description = 'Display a list of related products';
35
36
    /**
37
     * @var array
38
     */
39
    private static $many_many = [
40
        'Products' => ProductPage::class,
41
    ];
42
43
    /**
44
     * @var array
45
     */
46
    private static $many_many_extraFields = [
0 ignored issues
show
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';
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');
100
    }
101
102
    /**
103
     * loadDescendantProductGroupIDListInto function.
104
     *
105
     * @param mixed &$idList
106
     */
107
    public function loadDescendantProductGroupIDListInto(&$idList)
108
    {
109
        if ($children = $this->AllChildren()) {
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) {
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';
169
170
            $entries = ProductPage::get()->where($filter);
171
        }
172
173
        $list = new PaginatedList($entries, Controller::curr()->getRequest());
174
        $list->setPageLength($limit);
175
176
        return $list;
177
    }
178
}
179