|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class PageRowPageExtension extends SiteTreeExtension |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
8
|
|
|
'PageRows' => 'PageRow' |
|
9
|
|
|
]; |
|
10
|
|
|
|
|
11
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
|
|
12
|
|
|
'PageRows' => [ |
|
13
|
|
|
'SortOrder' => 'Int' |
|
14
|
|
|
] |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
public function PageRows() |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
return $this->getPageRows(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getPageRows() |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
return $this->owner->getManyManyComponents('PageRows')->sort(['SortOrder' => 'ASC']); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
####################### |
|
28
|
|
|
### Further DB Field Details |
|
29
|
|
|
####################### |
|
30
|
|
|
|
|
31
|
|
|
####################### |
|
32
|
|
|
### Field Names and Presentation Section |
|
33
|
|
|
####################### |
|
34
|
|
|
|
|
35
|
|
|
private static $field_labels = [ |
|
|
|
|
|
|
36
|
|
|
'PageRows' => 'Content Blocks' |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
private static $field_labels_right = [ |
|
|
|
|
|
|
40
|
|
|
'PageRows' => 'Please edit with care! You can add content blocks that are not ready for publication, but they will not be visible until they are ticked as available.', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
####################### |
|
45
|
|
|
### Casting Section |
|
46
|
|
|
####################### |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
####################### |
|
50
|
|
|
### can Section |
|
51
|
|
|
####################### |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
####################### |
|
56
|
|
|
### write Section |
|
57
|
|
|
####################### |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function onAfterWrite() |
|
62
|
|
|
{ |
|
63
|
|
|
if (Security::database_is_ready() && $this->owner->HasPageRows()) { |
|
64
|
|
|
// debug::Log('-------------------------'); |
|
|
|
|
|
|
65
|
|
|
$currentPageRows = []; |
|
66
|
|
|
if ($this->owner->PageRows()->count() > 0) { |
|
67
|
|
|
$sortOrder = 1; |
|
68
|
|
|
foreach ($this->owner->PageRows() as $pageRow) { |
|
69
|
|
|
$currentPageRows[$sortOrder] = $pageRow; |
|
70
|
|
|
$sortOrder++; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
$rowClassNames = $this->owner->DefaultPageRows(); |
|
74
|
|
|
$sortOrder = 1; |
|
75
|
|
|
foreach ($rowClassNames as $className) { |
|
76
|
|
|
$childClassName = null; |
|
77
|
|
|
if (is_array($className)) { |
|
78
|
|
|
$childClassName = $className['Child']; |
|
79
|
|
|
// debug::log($childClassName); |
|
|
|
|
|
|
80
|
|
|
$className = $className['Parent']; |
|
81
|
|
|
} |
|
82
|
|
|
if (isset($currentPageRows[$sortOrder])) { |
|
83
|
|
|
$row = $currentPageRows[$sortOrder]; |
|
84
|
|
|
if ($row->ClassName === $className) { |
|
|
|
|
|
|
85
|
|
|
//all OK! |
|
86
|
|
|
} else { |
|
|
|
|
|
|
87
|
|
|
// $this->owner->removePageRowFromThisPage($row); |
|
|
|
|
|
|
88
|
|
|
//we do not delete the row as it may be used somewhere else ... |
|
89
|
|
|
// $this->owner->deletePageRowFromMe($row); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
$row = $className::create(); |
|
93
|
|
|
$row->Title = 'Title '.$row->singular_name().' #'.$className::get()->count().' for '.$this->owner->MenuTitle; |
|
94
|
|
|
$row->write(); |
|
95
|
|
|
$this->owner->PageRows()->add($row, ['SortOrder' => $sortOrder]); |
|
96
|
|
|
$currentPageRows[$sortOrder] = $row; |
|
97
|
|
|
} |
|
98
|
|
|
DB::query(' |
|
99
|
|
|
UPDATE "Page_PageRows" |
|
100
|
|
|
SET "SortOrder" = '.$sortOrder.' |
|
101
|
|
|
WHERE |
|
102
|
|
|
"PageID" ='.$this->owner->ID.' AND |
|
103
|
|
|
"PageRowID" = '.$row->ID.' |
|
104
|
|
|
LIMIT 1; |
|
105
|
|
|
'); |
|
106
|
|
|
if ($childClassName) { |
|
107
|
|
|
$childClassMethod = $row->ChildClassMethodName(); |
|
108
|
|
|
if ($childClassMethod) { |
|
109
|
|
|
$child = $row->$childClassMethod(); |
|
110
|
|
|
if ($child && $child->exists()) { |
|
|
|
|
|
|
111
|
|
|
} else { |
|
112
|
|
|
$childClassMethodFieldName = $childClassMethod.'ID'; |
|
113
|
|
|
$child = $childClassName::create(); |
|
114
|
|
|
$child->Title = 'New '.$child->singular_name().' for '.$row->getTitle(); |
|
115
|
|
|
$child->write(); |
|
116
|
|
|
// debug::log($childClassMethodFieldName); |
|
|
|
|
|
|
117
|
|
|
// debug::log($child->ID); |
|
|
|
|
|
|
118
|
|
|
// debug::log($child->Title); |
|
|
|
|
|
|
119
|
|
|
$row->$childClassMethodFieldName = $child->ID; |
|
120
|
|
|
$row->write(); |
|
121
|
|
|
} |
|
122
|
|
|
} else { |
|
123
|
|
|
user_error('no childclass method set in '.$row->ClassName); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
$sortOrder++; |
|
127
|
|
|
} |
|
128
|
|
|
$sortOrder = 1; |
|
129
|
|
|
foreach ($this->owner->PageRows() as $pageRow) { |
|
130
|
|
|
$sortOrder++; |
|
131
|
|
|
$delete = false; |
|
132
|
|
|
if (!isset($rowClassNames[$sortOrder])) { |
|
133
|
|
|
$delete = true; |
|
134
|
|
|
} elseif ($rowClassNames[$sortOrder] !== $pageRow->ClassName) { |
|
135
|
|
|
$delete = true; |
|
136
|
|
|
} |
|
137
|
|
|
if ($delete) { |
|
|
|
|
|
|
138
|
|
|
// $this->owner->removePageRowFromThisPage($pageRow); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function removePageRowFromThisPage($rowOrRowID) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($rowOrRowID instanceof PageRow) { |
|
147
|
|
|
$rowOrRowID = $rowOrRowID->ID; |
|
148
|
|
|
} |
|
149
|
|
|
DB::query(' |
|
150
|
|
|
DELETE |
|
151
|
|
|
FROM "Page_PageRows" |
|
152
|
|
|
WHERE |
|
153
|
|
|
"PageID" ='.$this->owner->ID.' AND |
|
154
|
|
|
"PageRowID" = '.$rowOrRowID.' |
|
155
|
|
|
LIMIT 1; |
|
156
|
|
|
'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
public function DefaultPageRows() |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
if($this->owner->hasMethod('MyDefaultPageRows')) { |
|
163
|
|
|
return $this->owner->MyDefaultPageRows(); |
|
164
|
|
|
} |
|
165
|
|
|
return []; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
####################### |
|
169
|
|
|
### Import / Export Section |
|
170
|
|
|
####################### |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
####################### |
|
175
|
|
|
### CMS Edit Section |
|
176
|
|
|
####################### |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Update Fields |
|
180
|
|
|
* @return FieldList |
|
|
|
|
|
|
181
|
|
|
*/ |
|
182
|
|
|
public function updateCMSFields(FieldList $fields) |
|
183
|
|
|
{ |
|
184
|
|
|
$list = $this->owner->PageRows(); |
|
|
|
|
|
|
185
|
|
|
if($this->owner->canEdit() && $this->owner->exists() && $this->HasPageRows()) { |
|
186
|
|
|
$fields->addFieldsToTab( |
|
187
|
|
|
'Root.ContentBlocks', |
|
188
|
|
|
$this->owner->ContentBlocksFields() |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function ContentBlocksFields() |
|
194
|
|
|
{ |
|
195
|
|
|
$conf = GridFieldConfig_RelationEditor::create(100); |
|
196
|
|
|
$conf->addComponent(new GridFieldSortableRows('SortOrder')); |
|
197
|
|
|
// switch ($this->owner->ClassName) { |
|
|
|
|
|
|
198
|
|
|
// case 'HomePage': |
|
199
|
|
|
// // leave as is ... |
|
200
|
|
|
// $conf->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
|
|
|
|
|
201
|
|
|
// $conf->removeComponentsByType('GridFieldDeleteAction'); |
|
|
|
|
|
|
202
|
|
|
// $conf->removeComponentsByType('GridFieldAddNewButton'); |
|
|
|
|
|
|
203
|
|
|
// break; |
|
204
|
|
|
// default: |
|
205
|
|
|
// } |
|
206
|
|
|
// |
|
207
|
|
|
$conf->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(['Code', 'Title']); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
$pageRowField = GridField::create( |
|
210
|
|
|
'PageRows', |
|
211
|
|
|
'Content Blocks', |
|
212
|
|
|
$this->owner->PageRows(), |
|
213
|
|
|
$conf |
|
214
|
|
|
); |
|
215
|
|
|
$array = [$pageRowField]; |
|
216
|
|
|
$arrayRowList = []; |
|
217
|
|
|
foreach ($this->owner->DefaultPageRows() as $count => $className) { |
|
218
|
|
|
if (is_array($className)) { |
|
219
|
|
|
$className = $className['Parent']; |
|
220
|
|
|
} |
|
221
|
|
|
$humanCount = $count + 1; |
|
222
|
|
|
$arrayRowList[$className.'_'.$count] = $humanCount . ' - '.Injector::inst()->get($className)->singular_name().' ('.$className.')'; |
|
223
|
|
|
} |
|
224
|
|
|
if(count($arrayRowList)) { |
|
225
|
|
|
$array[] = LiteralField::create( |
|
226
|
|
|
'ListOfContentBlocks', |
|
227
|
|
|
'<h2>By default, this page type ('.$this->owner->singular_name().') has the following content blocks:</h2><p>'.implode('</p><p>', $arrayRowList).'</p>' |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $array; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function HasPageRows() |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
switch($this->owner->ClassName) { |
|
237
|
|
|
case 'ErrorPage': |
|
238
|
|
|
case 'RedirectorPage': |
|
239
|
|
|
case 'VirtualPage': |
|
240
|
|
|
return false; |
|
241
|
|
|
} |
|
242
|
|
|
if($this->owner->hasMethod('MyHasPageRows')) { |
|
243
|
|
|
return $this->owner->MyHasPageRows(); |
|
244
|
|
|
} |
|
245
|
|
|
return true; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.