|
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'; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $plural_name = 'Product Groups'; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $description = 'Display a list of related products'; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $many_many = array( |
|
|
|
|
|
|
36
|
|
|
'Products' => ProductPage::class, |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $many_many_extraFields = array( |
|
|
|
|
|
|
43
|
|
|
'Products' => array( |
|
44
|
|
|
'SortOrder' => 'Int' |
|
45
|
|
|
) |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
/* |
|
|
|
|
|
|
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'; |
|
|
|
|
|
|
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', |
|
|
|
|
|
|
80
|
|
|
_t('ProductHolder.Products', 'Products'), |
|
81
|
|
|
$this->Products(), |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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()) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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'; |
|
|
|
|
|
|
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
|
|
|
|