1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// namespace SilverStripe\Framework\Model\Versioning |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A single line in a changeset |
7
|
|
|
* |
8
|
|
|
* @property string $Added |
9
|
|
|
* @property string $ObjectClass |
10
|
|
|
* @property int $ObjectID |
11
|
|
|
* @method ManyManyList ReferencedBy() List of explicit items that require this change |
12
|
|
|
* @method ManyManyList References() List of implicit items required by this change |
13
|
|
|
* @method ChangeSet ChangeSet() |
14
|
|
|
*/ |
15
|
|
|
class ChangeSetItem extends DataObject { |
16
|
|
|
|
17
|
|
|
const EXPLICITLY = 'explicitly'; |
18
|
|
|
|
19
|
|
|
const IMPLICITLY = 'implicitly'; |
20
|
|
|
|
21
|
|
|
/** Represents an object deleted */ |
22
|
|
|
const CHANGE_DELETED = 'deleted'; |
23
|
|
|
|
24
|
|
|
/** Represents an object which was modified */ |
25
|
|
|
const CHANGE_MODIFIED = 'modified'; |
26
|
|
|
|
27
|
|
|
/** Represents an object added */ |
28
|
|
|
const CHANGE_CREATED = 'created'; |
29
|
|
|
|
30
|
|
|
/** Represents an object which hasn't been changed directly, but owns a modified many_many relationship. */ |
31
|
|
|
//const CHANGE_MANYMANY = 'manymany'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Represents that an object has not yet been changed, but |
35
|
|
|
* should be included in this changeset as soon as any changes exist |
36
|
|
|
*/ |
37
|
|
|
const CHANGE_NONE = 'none'; |
38
|
|
|
|
39
|
|
|
private static $db = array( |
|
|
|
|
40
|
|
|
'VersionBefore' => 'Int', |
41
|
|
|
'VersionAfter' => 'Int', |
42
|
|
|
'Added' => "Enum('explicitly, implicitly', 'implicitly')" |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
private static $has_one = array( |
|
|
|
|
46
|
|
|
'ChangeSet' => 'ChangeSet', |
47
|
|
|
'Object' => 'DataObject', |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
private static $many_many = array( |
|
|
|
|
51
|
|
|
'ReferencedBy' => 'ChangeSetItem' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
55
|
|
|
'References' => 'ChangeSetItem.ReferencedBy' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
private static $indexes = array( |
|
|
|
|
59
|
|
|
'ObjectUniquePerChangeSet' => array( |
60
|
|
|
'type' => 'unique', |
61
|
|
|
'value' => '"ObjectID", "ObjectClass", "ChangeSetID"' |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the type of change: none, created, deleted, modified, manymany |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getChangeType() { |
71
|
|
|
// Get change versions |
72
|
|
|
if($this->VersionBefore || $this->VersionAfter) { |
73
|
|
|
$draftVersion = $this->VersionAfter; // After publishing draft was written to stage |
|
|
|
|
74
|
|
|
$liveVersion = $this->VersionBefore; // The live version before the publish |
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
$draftVersion = Versioned::get_versionnumber_by_stage( |
77
|
|
|
$this->ObjectClass, Versioned::DRAFT, $this->ObjectID, false |
78
|
|
|
); |
79
|
|
|
$liveVersion = Versioned::get_versionnumber_by_stage( |
80
|
|
|
$this->ObjectClass, Versioned::LIVE, $this->ObjectID, false |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Version comparisons |
85
|
|
|
if ($draftVersion == $liveVersion) { |
86
|
|
|
return self::CHANGE_NONE; |
87
|
|
|
} elseif (!$liveVersion) { |
88
|
|
|
return self::CHANGE_CREATED; |
89
|
|
|
} elseif (!$draftVersion) { |
90
|
|
|
return self::CHANGE_DELETED; |
91
|
|
|
} else { |
92
|
|
|
return self::CHANGE_MODIFIED; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Find version of this object in the given stage |
98
|
|
|
* |
99
|
|
|
* @param string $stage |
100
|
|
|
* @return Versioned|DataObject |
101
|
|
|
*/ |
102
|
|
|
private function getObjectInStage($stage) { |
103
|
|
|
return Versioned::get_by_stage($this->ObjectClass, $stage)->byID($this->ObjectID); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get all implicit objects for this change |
108
|
|
|
* |
109
|
|
|
* @return SS_List |
110
|
|
|
*/ |
111
|
|
|
public function findReferenced() { |
112
|
|
|
if($this->getChangeType() === ChangeSetItem::CHANGE_DELETED) { |
113
|
|
|
// If deleted from stage, need to look at live record |
114
|
|
|
return $this->getObjectInStage(Versioned::LIVE)->findOwners(false); |
115
|
|
|
} else { |
116
|
|
|
// If changed on stage, look at owned objects there |
117
|
|
|
return $this->getObjectInStage(Versioned::DRAFT)->findOwned()->filterByCallback(function ($owned) { |
118
|
|
|
/** @var Versioned|DataObject $owned */ |
119
|
|
|
return $owned->stagesDiffer(Versioned::DRAFT, Versioned::LIVE); |
|
|
|
|
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Publish this item, then close it. |
126
|
|
|
* |
127
|
|
|
* Note: Unlike Versioned::doPublish() and Versioned::doUnpublish, this action is not recursive. |
128
|
|
|
*/ |
129
|
|
|
public function publish() { |
130
|
|
|
// Logical checks prior to publish |
131
|
|
|
if(!$this->canPublish()) { |
132
|
|
|
throw new Exception("The current member does not have permission to publish this ChangeSetItem."); |
133
|
|
|
} |
134
|
|
|
if($this->VersionBefore || $this->VersionAfter) { |
135
|
|
|
throw new BadMethodCallException("This ChangeSetItem has already been published"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Record state changed |
139
|
|
|
$this->VersionAfter = Versioned::get_versionnumber_by_stage( |
|
|
|
|
140
|
|
|
$this->ObjectClass, Versioned::DRAFT, $this->ObjectID, false |
141
|
|
|
); |
142
|
|
|
$this->VersionBefore = Versioned::get_versionnumber_by_stage( |
|
|
|
|
143
|
|
|
$this->ObjectClass, Versioned::LIVE, $this->ObjectID, false |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
switch($this->getChangeType()) { |
147
|
|
|
case static::CHANGE_NONE: { |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
case static::CHANGE_DELETED: { |
151
|
|
|
// Non-recursive delete |
152
|
|
|
$object = $this->getObjectInStage(Versioned::LIVE); |
153
|
|
|
$object->deleteFromStage(Versioned::LIVE); |
154
|
|
|
break; |
155
|
|
|
} |
156
|
|
|
case static::CHANGE_MODIFIED: |
157
|
|
|
case static::CHANGE_CREATED: { |
158
|
|
|
// Non-recursive publish |
159
|
|
|
$object = $this->getObjectInStage(Versioned::DRAFT); |
160
|
|
|
$object->publishSingle(); |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->write(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** Reverts this item, then close it. **/ |
169
|
|
|
public function revert() { |
170
|
|
|
user_error('Not implemented', E_USER_ERROR); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function canView($member = null) { |
174
|
|
|
return $this->can(__FUNCTION__, $member); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function canEdit($member = null) { |
178
|
|
|
return $this->can(__FUNCTION__, $member); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function canCreate($member = null, $context = array()) { |
182
|
|
|
return $this->can(__FUNCTION__, $member, $context); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function canDelete($member = null) { |
186
|
|
|
return $this->can(__FUNCTION__, $member); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Check if the BeforeVersion of this changeset can be restored to draft |
191
|
|
|
* |
192
|
|
|
* @param Member $member |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function canRevert($member) { |
196
|
|
|
// Just get the best version as this object may not even exist on either stage anymore. |
197
|
|
|
/** @var Versioned|DataObject $object */ |
198
|
|
|
$object = Versioned::get_latest_version($this->ObjectClass, $this->ObjectID); |
199
|
|
|
if(!$object) { |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Check change type |
204
|
|
|
switch($this->getChangeType()) { |
205
|
|
|
case static::CHANGE_CREATED: { |
206
|
|
|
// Revert creation by deleting from stage |
207
|
|
|
if(!$object->canDelete($member)) { |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
break; |
211
|
|
|
} |
212
|
|
|
default: { |
213
|
|
|
// All other actions are typically editing draft stage |
214
|
|
|
if(!$object->canEdit($member)) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
break; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// If object can be published/unpublished let extensions deny |
222
|
|
|
return $this->can(__FUNCTION__, $member); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Check if this ChangeSetItem can be published |
227
|
|
|
* |
228
|
|
|
* @param Member $member |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
|
|
public function canPublish($member = null) { |
232
|
|
|
// Check canMethod to invoke on object |
233
|
|
|
switch($this->getChangeType()) { |
234
|
|
View Code Duplication |
case static::CHANGE_DELETED: { |
|
|
|
|
235
|
|
|
/** @var Versioned|DataObject $object */ |
236
|
|
|
$object = Versioned::get_by_stage($this->ObjectClass, Versioned::LIVE)->byID($this->ObjectID); |
237
|
|
|
if(!$object || !$object->canUnpublish($member)) { |
|
|
|
|
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
break; |
241
|
|
|
} |
242
|
|
View Code Duplication |
default: { |
|
|
|
|
243
|
|
|
/** @var Versioned|DataObject $object */ |
244
|
|
|
$object = Versioned::get_by_stage($this->ObjectClass, Versioned::DRAFT)->byID($this->ObjectID); |
245
|
|
|
if(!$object || !$object->canPublish($member)) { |
|
|
|
|
246
|
|
|
return false; |
247
|
|
|
} |
248
|
|
|
break; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// If object can be published/unpublished let extensions deny |
253
|
|
|
return $this->can(__FUNCTION__, $member); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Default permissions for this ChangeSetItem |
258
|
|
|
* |
259
|
|
|
* @param string $perm |
260
|
|
|
* @param Member $member |
261
|
|
|
* @param array $context |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
View Code Duplication |
public function can($perm, $member = null, $context = array()) { |
|
|
|
|
265
|
|
|
if(!$member) { |
266
|
|
|
$member = Member::currentUser(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// Allow extensions to bypass default permissions, but only if |
270
|
|
|
// each change can be individually published. |
271
|
|
|
$extended = $this->extendedCan($perm, $member, $context); |
|
|
|
|
272
|
|
|
if($extended !== null) { |
273
|
|
|
return $extended; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// Default permissions |
277
|
|
|
return (bool)Permission::checkMember($member, ChangeSet::config()->required_permission); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|