|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// namespace SilverStripe\Framework\Model\Versioning |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* The ChangeSet model tracks several VersionedAndStaged objects for later publication as a single |
|
7
|
|
|
* atomic action |
|
8
|
|
|
* |
|
9
|
|
|
* @method HasManyList Changes() |
|
10
|
|
|
* @method Member Owner() |
|
11
|
|
|
* @property string $Name |
|
12
|
|
|
* @property string $State |
|
13
|
|
|
* |
|
14
|
|
|
* @package framework |
|
15
|
|
|
* @subpackage model |
|
16
|
|
|
*/ |
|
17
|
|
|
class ChangeSet extends DataObject { |
|
18
|
|
|
|
|
19
|
|
|
private static $singular_name = 'Campaign'; |
|
20
|
|
|
|
|
21
|
|
|
private static $plural_name = 'Campaigns'; |
|
22
|
|
|
|
|
23
|
|
|
/** An active changeset */ |
|
24
|
|
|
const STATE_OPEN = 'open'; |
|
25
|
|
|
|
|
26
|
|
|
/** A changeset which is reverted and closed */ |
|
27
|
|
|
const STATE_REVERTED = 'reverted'; |
|
28
|
|
|
|
|
29
|
|
|
/** A changeset which is published and closed */ |
|
30
|
|
|
const STATE_PUBLISHED = 'published'; |
|
31
|
|
|
|
|
32
|
|
|
private static $db = array( |
|
33
|
|
|
'Name' => 'Varchar', |
|
34
|
|
|
'State' => "Enum('open,published,reverted','open')", |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
private static $has_many = array( |
|
38
|
|
|
'Changes' => 'ChangeSetItem', |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
private static $defaults = array( |
|
42
|
|
|
'State' => 'open' |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
private static $has_one = array( |
|
46
|
|
|
'Owner' => 'Member', |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
private static $casting = array( |
|
50
|
|
|
'Description' => 'Text', |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* List of classes to set apart in description |
|
55
|
|
|
* |
|
56
|
|
|
* @config |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private static $important_classes = array( |
|
60
|
|
|
'SiteTree', |
|
61
|
|
|
'File', |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Default permission to require for publishers. |
|
66
|
|
|
* Publishers must either be able to use the campaign admin, or have all admin access. |
|
67
|
|
|
* |
|
68
|
|
|
* Also used as default permission for ChangeSetItem default permission. |
|
69
|
|
|
* |
|
70
|
|
|
* @config |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
private static $required_permission = array('CMS_ACCESS_CampaignAdmin', 'CMS_ACCESS_LeftAndMain'); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Publish this changeset, then closes it. |
|
77
|
|
|
* |
|
78
|
|
|
* @throws Exception |
|
79
|
|
|
*/ |
|
80
|
|
|
public function publish() { |
|
81
|
|
|
// Logical checks prior to publish |
|
82
|
|
|
if($this->State !== static::STATE_OPEN) { |
|
83
|
|
|
throw new BadMethodCallException( |
|
84
|
|
|
"ChangeSet can't be published if it has been already published or reverted." |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
if(!$this->isSynced()) { |
|
88
|
|
|
throw new ValidationException( |
|
89
|
|
|
"ChangeSet does not include all necessary changes and cannot be published." |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
if(!$this->canPublish()) { |
|
93
|
|
|
throw new Exception("The current member does not have permission to publish this ChangeSet."); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
DB::get_conn()->withTransaction(function(){ |
|
97
|
|
|
foreach($this->Changes() as $change) { |
|
98
|
|
|
/** @var ChangeSetItem $change */ |
|
99
|
|
|
$change->publish(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->State = static::STATE_PUBLISHED; |
|
103
|
|
|
$this->write(); |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Add a new change to this changeset. Will automatically include all owned |
|
109
|
|
|
* changes as those are dependencies of this item. |
|
110
|
|
|
* |
|
111
|
|
|
* @param DataObject $object |
|
112
|
|
|
*/ |
|
113
|
|
|
public function addObject(DataObject $object) { |
|
114
|
|
|
if(!$this->isInDB()) { |
|
115
|
|
|
throw new BadMethodCallException("ChangeSet must be saved before adding items"); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$references = [ |
|
119
|
|
|
'ObjectID' => $object->ID, |
|
120
|
|
|
'ObjectClass' => $object->ClassName |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
// Get existing item in case already added |
|
124
|
|
|
$item = $this->Changes()->filter($references)->first(); |
|
125
|
|
|
|
|
126
|
|
|
if (!$item) { |
|
127
|
|
|
$item = new ChangeSetItem($references); |
|
128
|
|
|
$this->Changes()->add($item); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$item->ReferencedBy()->removeAll(); |
|
132
|
|
|
|
|
133
|
|
|
$item->Added = ChangeSetItem::EXPLICITLY; |
|
|
|
|
|
|
134
|
|
|
$item->write(); |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$this->sync(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Remove an item from this changeset. Will automatically remove all changes |
|
142
|
|
|
* which own (and thus depend on) the removed item. |
|
143
|
|
|
* |
|
144
|
|
|
* @param DataObject $object |
|
145
|
|
|
*/ |
|
146
|
|
|
public function removeObject(DataObject $object) { |
|
147
|
|
|
$item = ChangeSetItem::get()->filter([ |
|
148
|
|
|
'ObjectID' => $object->ID, |
|
149
|
|
|
'ObjectClass' => $object->ClassName, |
|
150
|
|
|
'ChangeSetID' => $this->ID |
|
151
|
|
|
])->first(); |
|
152
|
|
|
|
|
153
|
|
|
if ($item) { |
|
154
|
|
|
// TODO: Handle case of implicit added item being removed. |
|
155
|
|
|
|
|
156
|
|
|
$item->delete(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->sync(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function implicitKey($item) { |
|
163
|
|
|
if ($item instanceof ChangeSetItem) return $item->ObjectClass.'.'.$item->ObjectID; |
|
164
|
|
|
return $item->ClassName.'.'.$item->ID; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
protected function calculateImplicit() { |
|
168
|
|
|
/** @var string[][] $explicit List of all items that have been explicitly added to this ChangeSet */ |
|
169
|
|
|
$explicit = array(); |
|
170
|
|
|
|
|
171
|
|
|
/** @var string[][] $referenced List of all items that are "referenced" by items in $explicit */ |
|
172
|
|
|
$referenced = array(); |
|
173
|
|
|
|
|
174
|
|
|
/** @var string[][] $references List of which explicit items reference each thing in referenced */ |
|
175
|
|
|
$references = array(); |
|
176
|
|
|
|
|
177
|
|
|
foreach ($this->Changes()->filter(['Added' => ChangeSetItem::EXPLICITLY]) as $item) { |
|
178
|
|
|
$explicitKey = $this->implicitKey($item); |
|
179
|
|
|
$explicit[$explicitKey] = true; |
|
180
|
|
|
|
|
181
|
|
|
foreach ($item->findReferenced() as $referee) { |
|
182
|
|
|
$key = $this->implicitKey($referee); |
|
183
|
|
|
|
|
184
|
|
|
$referenced[$key] = [ |
|
185
|
|
|
'ObjectID' => $referee->ID, |
|
186
|
|
|
'ObjectClass' => $referee->ClassName |
|
187
|
|
|
]; |
|
188
|
|
|
|
|
189
|
|
|
$references[$key][] = $item->ID; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** @var string[][] $explicit List of all items that are either in $explicit, $referenced or both */ |
|
194
|
|
|
$all = array_merge($referenced, $explicit); |
|
195
|
|
|
|
|
196
|
|
|
/** @var string[][] $implicit Anything that is in $all, but not in $explicit, is an implicit inclusion */ |
|
197
|
|
|
$implicit = array_diff_key($all, $explicit); |
|
198
|
|
|
|
|
199
|
|
|
foreach($implicit as $key => $object) { |
|
200
|
|
|
$implicit[$key]['ReferencedBy'] = $references[$key]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $implicit; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Add implicit changes that should be included in this changeset |
|
208
|
|
|
* |
|
209
|
|
|
* When an item is created or changed, all it's owned items which have |
|
210
|
|
|
* changes are implicitly added |
|
211
|
|
|
* |
|
212
|
|
|
* When an item is deleted, it's owner (even if that owner does not have changes) |
|
213
|
|
|
* is implicitly added |
|
214
|
|
|
*/ |
|
215
|
|
|
public function sync() { |
|
216
|
|
|
// Start a transaction (if we can) |
|
217
|
|
|
DB::get_conn()->withTransaction(function() { |
|
218
|
|
|
|
|
219
|
|
|
// Get the implicitly included items for this ChangeSet |
|
220
|
|
|
$implicit = $this->calculateImplicit(); |
|
221
|
|
|
|
|
222
|
|
|
// Adjust the existing implicit ChangeSetItems for this ChangeSet |
|
223
|
|
|
foreach ($this->Changes()->filter(['Added' => ChangeSetItem::IMPLICITLY]) as $item) { |
|
224
|
|
|
$objectKey = $this->implicitKey($item); |
|
225
|
|
|
|
|
226
|
|
|
// If a ChangeSetItem exists, but isn't in $implicit, it's no longer required, so delete it |
|
227
|
|
|
if (!array_key_exists($objectKey, $implicit)) { |
|
228
|
|
|
$item->delete(); |
|
229
|
|
|
} |
|
230
|
|
|
// Otherwise it is required, so update ReferencedBy and remove from $implicit |
|
231
|
|
|
else { |
|
232
|
|
|
$item->ReferencedBy()->setByIDList($implicit[$objectKey]['ReferencedBy']); |
|
233
|
|
|
unset($implicit[$objectKey]); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
// Now $implicit is all those items that are implicitly included, but don't currently have a ChangeSetItem. |
|
238
|
|
|
// So create new ChangeSetItems to match |
|
239
|
|
|
|
|
240
|
|
|
foreach ($implicit as $key => $props) { |
|
241
|
|
|
$item = new ChangeSetItem($props); |
|
242
|
|
|
$item->Added = ChangeSetItem::IMPLICITLY; |
|
243
|
|
|
$item->ChangeSetID = $this->ID; |
|
|
|
|
|
|
244
|
|
|
$item->ReferencedBy()->setByIDList($props['ReferencedBy']); |
|
245
|
|
|
$item->write(); |
|
246
|
|
|
} |
|
247
|
|
|
}); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** Verify that any objects in this changeset include all owned changes */ |
|
251
|
|
|
public function isSynced() { |
|
252
|
|
|
$implicit = $this->calculateImplicit(); |
|
253
|
|
|
|
|
254
|
|
|
// Check the existing implicit ChangeSetItems for this ChangeSet |
|
255
|
|
|
|
|
256
|
|
|
foreach ($this->Changes()->filter(['Added' => ChangeSetItem::IMPLICITLY]) as $item) { |
|
257
|
|
|
$objectKey = $this->implicitKey($item); |
|
258
|
|
|
|
|
259
|
|
|
// If a ChangeSetItem exists, but isn't in $implicit -> validation failure |
|
260
|
|
|
if (!array_key_exists($objectKey, $implicit)) return false; |
|
261
|
|
|
// Exists, remove from $implicit |
|
262
|
|
|
unset($implicit[$objectKey]); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// If there's anything left in $implicit -> validation failure |
|
266
|
|
|
return empty($implicit); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function canView($member = null) { |
|
270
|
|
|
return $this->can(__FUNCTION__, $member); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
public function canEdit($member = null) { |
|
274
|
|
|
return $this->can(__FUNCTION__, $member); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function canCreate($member = null, $context = array()) { |
|
278
|
|
|
return $this->can(__FUNCTION__, $member, $context); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
public function canDelete($member = null) { |
|
282
|
|
|
return $this->can(__FUNCTION__, $member); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Check if this item is allowed to be published |
|
287
|
|
|
* |
|
288
|
|
|
* @param Member $member |
|
289
|
|
|
* @return bool |
|
290
|
|
|
*/ |
|
291
|
|
|
public function canPublish($member = null) { |
|
292
|
|
|
// All changes must be publishable |
|
293
|
|
|
foreach($this->Changes() as $change) { |
|
294
|
|
|
/** @var ChangeSetItem $change */ |
|
295
|
|
|
if(!$change->canPublish($member)) { |
|
296
|
|
|
return false; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
// Default permission |
|
301
|
|
|
return $this->can(__FUNCTION__, $member); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Check if this changeset (if published) can be reverted |
|
306
|
|
|
* |
|
307
|
|
|
* @param Member $member |
|
308
|
|
|
* @return bool |
|
309
|
|
|
*/ |
|
310
|
|
|
public function canRevert($member = null) { |
|
311
|
|
|
// All changes must be publishable |
|
312
|
|
|
foreach($this->Changes() as $change) { |
|
313
|
|
|
/** @var ChangeSetItem $change */ |
|
314
|
|
|
if(!$change->canRevert($member)) { |
|
|
|
|
|
|
315
|
|
|
return false; |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
// Default permission |
|
320
|
|
|
return $this->can(__FUNCTION__, $member); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Default permissions for this changeset |
|
325
|
|
|
* |
|
326
|
|
|
* @param string $perm |
|
327
|
|
|
* @param Member $member |
|
328
|
|
|
* @param array $context |
|
329
|
|
|
* @return bool |
|
330
|
|
|
*/ |
|
331
|
|
|
public function can($perm, $member = null, $context = array()) { |
|
332
|
|
|
if(!$member) { |
|
333
|
|
|
$member = Member::currentUser(); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
// Allow extensions to bypass default permissions, but only if |
|
337
|
|
|
// each change can be individually published. |
|
338
|
|
|
$extended = $this->extendedCan($perm, $member, $context); |
|
339
|
|
|
if($extended !== null) { |
|
340
|
|
|
return $extended; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
// Default permissions |
|
344
|
|
|
return (bool)Permission::checkMember($member, $this->config()->required_permission); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
public function getCMSFields() { |
|
348
|
|
|
$fields = new FieldList(); |
|
349
|
|
|
$fields->merge([ |
|
350
|
|
|
TextField::create('Name'), |
|
351
|
|
|
ReadonlyField::create('State') |
|
352
|
|
|
]); |
|
353
|
|
|
$this->extend('updateCMSFields', $fields); |
|
354
|
|
|
return $fields; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Gets summary of items in changeset |
|
359
|
|
|
* |
|
360
|
|
|
* @return string |
|
361
|
|
|
*/ |
|
362
|
|
|
public function getDescription() { |
|
363
|
|
|
// Initialise list of items to count |
|
364
|
|
|
$counted = []; |
|
365
|
|
|
$countedOther = 0; |
|
366
|
|
|
foreach($this->config()->important_classes as $type) { |
|
367
|
|
|
if(class_exists($type)) { |
|
368
|
|
|
$counted[$type] = 0; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
// Check each change item |
|
373
|
|
|
/** @var ChangeSetItem $change */ |
|
374
|
|
|
foreach($this->Changes() as $change) { |
|
375
|
|
|
$found = false; |
|
376
|
|
|
foreach($counted as $class => $num) { |
|
377
|
|
|
if(is_a($change->ObjectClass, $class, true)) { |
|
378
|
|
|
$counted[$class]++; |
|
379
|
|
|
$found = true; |
|
380
|
|
|
break; |
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
if(!$found) { |
|
384
|
|
|
$countedOther++; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
// Describe set based on this output |
|
389
|
|
|
$counted = array_filter($counted); |
|
390
|
|
|
|
|
391
|
|
|
// Empty state |
|
392
|
|
|
if(empty($counted) && empty($countedOther)) { |
|
393
|
|
|
return ''; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
// Put all parts together |
|
397
|
|
|
$parts = []; |
|
398
|
|
|
foreach($counted as $class => $count) { |
|
399
|
|
|
$parts[] = DataObject::singleton($class)->i18n_pluralise($count); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
// Describe non-important items |
|
403
|
|
|
if($countedOther) { |
|
404
|
|
|
if ($counted) { |
|
|
|
|
|
|
405
|
|
|
$parts[] = i18n::pluralise( |
|
406
|
|
|
_t('ChangeSet.DESCRIPTION_OTHER_ITEM', 'other item'), |
|
407
|
|
|
_t('ChangeSet.DESCRIPTION_OTHER_ITEMS', 'other items'), |
|
408
|
|
|
$countedOther |
|
409
|
|
|
); |
|
410
|
|
|
} else { |
|
411
|
|
|
$parts[] = i18n::pluralise( |
|
412
|
|
|
_t('ChangeSet.DESCRIPTION_ITEM', 'item'), |
|
413
|
|
|
_t('ChangeSet.DESCRIPTION_ITEMS', 'items'), |
|
414
|
|
|
$countedOther |
|
415
|
|
|
); |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
// Figure out how to join everything together |
|
420
|
|
|
if(empty($parts)) { |
|
421
|
|
|
return ''; |
|
422
|
|
|
} |
|
423
|
|
|
if(count($parts) === 1) { |
|
424
|
|
|
return $parts[0]; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
// Non-comma list |
|
428
|
|
|
if(count($parts) === 2) { |
|
429
|
|
|
return _t( |
|
430
|
|
|
'ChangeSet.DESCRIPTION_AND', |
|
431
|
|
|
'{first} and {second}', |
|
432
|
|
|
[ |
|
433
|
|
|
'first' => $parts[0], |
|
434
|
|
|
'second' => $parts[1], |
|
435
|
|
|
] |
|
436
|
|
|
); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
// First item |
|
440
|
|
|
$string = _t( |
|
441
|
|
|
'ChangeSet.DESCRIPTION_LIST_FIRST', |
|
442
|
|
|
'{item}', |
|
443
|
|
|
['item' => $parts[0]] |
|
444
|
|
|
); |
|
445
|
|
|
|
|
446
|
|
|
// Middle items |
|
447
|
|
|
for($i = 1; $i < count($parts) - 1; $i++) { |
|
448
|
|
|
$string = _t( |
|
449
|
|
|
'ChangeSet.DESCRIPTION_LIST_MID', |
|
450
|
|
|
'{list}, {item}', |
|
451
|
|
|
[ |
|
452
|
|
|
'list' => $string, |
|
453
|
|
|
'item' => $parts[$i] |
|
454
|
|
|
] |
|
455
|
|
|
); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
// Oxford comma |
|
459
|
|
|
$string = _t( |
|
460
|
|
|
'ChangeSet.DESCRIPTION_LIST_LAST', |
|
461
|
|
|
'{list}, and {item}', |
|
462
|
|
|
[ |
|
463
|
|
|
'list' => $string, |
|
464
|
|
|
'item' => end($parts) |
|
465
|
|
|
] |
|
466
|
|
|
); |
|
467
|
|
|
return $string; |
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.