forked from Darkheir/SAModelVersioning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAModelVersioning.php
More file actions
404 lines (371 loc) · 11.4 KB
/
SAModelVersioning.php
File metadata and controls
404 lines (371 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
*
*/
class SAModelVersioning extends CActiveRecordBehavior
{
/**
* Params that can be setted when declaring the
* behavior.
* There's an additional param that can be used to
* specify the version table name: 'VersionTable'.
* If not setted it's value is {tableName}_version
*/
public $createdByField = "created_by";
public $createdAtField = "created_time";
public $versionCommentField = "version_comment";
public $versionField = "version";
public $removeVersioningOnDelete = true;
protected $_createdAt = "";
protected $_createdBy = "";
protected $_versionComment = "";
protected $_lastVersion;
protected $_versionTable;
public function afterSave($event)
{
Yii::app()->db->createCommand()->insert($this->versionTable, $this->versionedAttributes);
$version = Yii::app()->db->getLastInsertID();
Yii::app()->db->createCommand()->update($this->getOwner()->tableName(), array(
$this->versionField => $version,
), 'id=:id', array(':id' => $this->getOwner()->id)
);
$this->getOwner()->version = $version;
$this->_lastVersion = $version;
}
public function afterDelete($event)
{
if($this->removeVersioningOnDelete) {
$this->deleteVersioning(false);
}
}
/**
* Return the name of the version table for the model
* Default to {tableName}_version
* @return String return the name of the version table for the model
*/
public function getVersionTable()
{
if($this->_versionTable !== null) {
return $this->_versionTable;
} else {
return $this->getOwner()->tableName() . "_version";
}
}
public function setVersionTable($table)
{
$this->_versionTable = $this->setAttribute($table);
}
public function setVersionCreatedBy($createdBy)
{
$this->_createdBy = $this->setAttribute($createdBy);
}
public function getVersionCreatedBy()
{
return $this->_createdBy;
}
public function setVersionComment($versionComment)
{
$this->_versionComment = $this->setAttribute($versionComment);
}
public function getVersionComment()
{
return $this->_versionComment;
}
public function getVersionCreatedAt()
{
if($this->_createdAt !== null) {
return $this->_createdAt;
} else {
return time();
}
}
public function setVersionCreatedAt($versionCreatedAt)
{
$this->_createdAt = $versionCreatedAt;
}
/**
* @return Return if the model is at its last version
*/
public function isLastVersion()
{
return $this->getOwner()->version === $this->getLastVersionNumber();
}
/**
* @return int Return the version number of the model
*/
public function getVersion()
{
if ($this->getOwner()->version == null) {
return 0;
} else {
return $this->getOwner()->version;
}
}
/**
* @return int Return the last version number of the model
*/
public function getLastVersionNumber()
{
if($this->_lastVersion !== null) {
return $this->_lastVersion;
} else {
$lastVersion = Yii::app()->db->createCommand()
->select("MAX($this->versionField) as version_number")
->from($this->versionTable)
->where('id=:id', array(':id'=>$this->getOwner()->primaryKey))
->queryRow();
$this->_lastVersion = $lastVersion['version_number'];
return $this->_lastVersion;
}
}
/**
* Remove all the versioned data from the version table
* @param boolean $updateVersion Wheither or not the original model version must be resetted
*/
public function deleteVersioning($updateVersion = true)
{
Yii::app()->db->createCommand()->delete($this->versionTable, 'id=:id', array(
':id'=>$this->getOwner()->primaryKey
)
);
if($updateVersion) {
Yii::app()->db->createCommand()->update($this->getOwner()->tableName(), array(
$this->versionField => 1,
), 'id=:id', array(':id' => $this->getOwner()->id)
);
}
}
/**
* Return all the versions of the Active Record Object
* @return array List of the different versions of the object
*/
public function getAllVersions()
{
$allVersionsArray = Yii::app()->db->createCommand()
->select('*')
->from($this->versionTable)
->where('id=:id', array(':id'=>$this->getOwner()->primaryKey))
->order('version ASC')
->queryAll();
if(!empty($allVersionsArray)) {
return $this->populateActiveRecords($allVersionsArray);
} else {
return array();
}
}
/**
* Return the n last versions of the model.
* @param int $number Number of the last versions to return. Default to 1.
* @return array return an array containing the last versions or
* an empty array if no versions are available
*/
public function getLastVersions($number = 1)
{
$lastVersionsArray = Yii::app()->db->createCommand()
->select('*')
->from($this->versionTable)
->where('id=:id', array(':id'=>$this->getOwner()->primaryKey))
->order('version DESC')
->limit($number)
->queryAll();
if(!empty($lastVersionsArray)) {
return $this->populateActiveRecords($lastVersionsArray);
} else {
return array();
}
}
/**
* Return a version of the model or false if the version number doesn't exist
* @param int $versionNumber Number of the version to return
* @return mixed return an active record corresponding to the version number
* or false if it doesn't exist in the db
*/
public function getOneVersion($versionNumber)
{
$versionArray = Yii::app()->db->createCommand()
->select('*')
->from($this->versionTable)
->where(
"id=:id AND $this->versionField=:version",
array(
':id'=>$this->getOwner()->primaryKey,
':version'=>$versionNumber,
)
)
->queryRow();
if($versionArray) {
return $this->populateNewRecord($versionArray, get_class($this->getOwner()));
} else {
return false;
}
}
/**
* Convert the model to the given version
* @param int $versionNumber The version to convert to
* @return bool true if everything went fine, false otherwise
*/
public function toVersion($versionNumber)
{
$versionArray = Yii::app()->db->createCommand()
->select('*')
->from($this->versionTable)
->where(
"id=:id AND $this->versionField=:version",
array(
':id'=>$this->getOwner()->primaryKey,
':version'=>$versionNumber,
)
)
->queryRow();
if($versionArray) {
$this->populateActiveRecord($versionArray, $this->getOwner());
return true;
} else {
return false;
}
}
/**
* Compare 2 versions of the model
* the number of the 2 version
* @return mixed return an array containing the differences or false
* if a version hasn't been found in the db
*/
public function compareVersions($version1, $version2)
{
$versionsArray = Yii::app()->db->createCommand()
->select('*')
->from($this->versionTable)
->where(
array('and', 'id=:id', array('or', $this->versionField . '=:version1', $this->versionField . '=:version2')),
array(
':id'=>$this->getOwner()->primaryKey,
':version1' => $version1,
':version2' => $version2,
)
)
->order("$this->versionField ASC")
->queryAll();
if(!empty($versionsArray)&& count($versionsArray) == 2) {
//Watch attributes changing from one version to the other and put them in the array
//penser à unset les attributs de version (version, comment, created by, created at)
$differences = array();
foreach($versionsArray[0] as $index => $value) {
if(isset($versionsArray[1][$index]) && $value !== $versionsArray[1][$index]) {
$differences[$index] = array($versionsArray[0]['version'] => $value, $versionsArray[1]['version'] => $versionsArray[1][$index]);
}
}
$differences = $this->unsetVersionedAttributes($differences);
return $differences;
} else {
return false;
}
}
/**
* Compare the actual model to the given version
* @param int $versionNumber Version number to compare to
* @return mixed An array containing the differences or false if the version number
* doesn't exist in the db
*/
public function compareTo($versionNumber)
{
$thisVersion = $this->getOwner()->getAttributes(false);
$versionArray = Yii::app()->db->createCommand()
->select('*')
->from($this->versionTable)
->where(
"id=:id AND $this->versionField=:version",
array(
':id'=>$this->getOwner()->primaryKey,
':version'=>$versionNumber,
)
)
->queryRow();
if($versionArray) {
$differences = array();
$thisVersion = $this->unsetVersionedAttributes($this->getOwner()->getAttributes(false));
foreach($thisVersion as $index => $value) {
if(isset($versionArray[$index]) && $value !== $versionArray[$index]) {
$differences[$index] = array('actual' => $value, $versionArray[$this->versionField] => $versionArray[$index]);
}
}
return $differences;
} else {
return false;
}
}
/**
* Get the attributes to add to the version table:
* - the attributes from the model (also the not safe ones)
* - The version attributes (createdBy, ...)
* @return array an array containing the attributes
*/
protected function getVersionedAttributes()
{
$this->VersionCreatedAt = date('Y-m-d H:i:s', time());
$versionedAttributes = $this->getOwner()->getAttributes(false);
$versionedAttributes[$this->createdByField] = $this->versionCreatedBy;
$versionedAttributes[$this->createdAtField] = $this->VersionCreatedAt;
$versionedAttributes[$this->versionCommentField] = $this->versionComment;
//we don't save the actual version number in the version table since it'll be automatically incremented
unset($versionedAttributes[$this->versionField]);
return $versionedAttributes;
}
/**
* Unset the versionned attributes that could be returned from sql requests
* @param array $array the array to unset
*/
protected function unsetVersionedAttributes($array)
{
unset($array[$this->versionField]);
unset($array[$this->createdAtField]);
unset($array[$this->createdByField]);
unset($array[$this->versionCommentField]);
return $array;
}
protected function setAttribute($value)
{
if($value == null) {
return "";
} else {
return $value;
}
}
/**
* Create some Active Records from an array of their values
* @param array $values Array of the values returned from the db
* @return array return an array containing the CactiveRecords models
*/
protected function populateActiveRecords($values)
{
$className = get_class($this->getOwner());
$activeRecords = array();
foreach($values as $version) {
$activeRecords[] = $this->populateNewRecord($version, $className);
}
return $activeRecords;
}
/**
* Create a new active record object and fill it with the given value
* @param array $values the values that the active record object need to be filled with
* @param string $className Name of the class object to create
* @return CActiveRecord Return the newly created object populated
*/
protected function populateNewRecord($values, $className)
{
return $this->populateActiveRecord($values, new $className());
}
/**
* Populate the given Active Record with the given values.
* @param array $values The values to put in the Active record
* @param CActiveRecord $model the object to populate
* @return CActiveRecord return the populate active record
*/
protected function populateActiveRecord($values, $model)
{
$model->versionComment = $values[$this->versionCommentField];
$model->versionCreatedBy = $values[$this->createdByField];
$model->versionCreatedAt = $values[$this->createdAtField];
$model->setAttributes($values, false);
return $model;
}
}