/
Historizer.php
138 lines (125 loc) · 3.68 KB
/
Historizer.php
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
<?php
namespace albertborsos\yii2historizer;
use albertborsos\yii2lib\db\ActiveRecord;
use Yii;
use yii\helpers\Json;
/**
* This is the model class for table "ext_historizer_archives".
*
* @property integer $id
* @property string $model_class
* @property integer $model_id
* @property string $model_attributes
* @property integer $created_at
* @property integer $created_user
* @property integer $updated_at
* @property integer $updated_user
* @property string $status
*/
class Historizer extends ActiveRecord
{
const STATUS_ACTIVE = 'a';
const STATUS_INACTIVE = 'i';
const STATUS_DELETED = 'd';
/**
* @inheritdoc
*/
public static function tableName()
{
return 'ext_historizer_archives';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['model_class', 'model_id', 'model_attributes', 'status'], 'required'],
[['model_id', 'created_at', 'created_user', 'updated_at', 'updated_user'], 'integer'],
[['model_attributes'], 'string'],
[['model_class'], 'string', 'max' => 512],
[['status'], 'string', 'max' => 1]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'model_class' => 'Model Class',
'model_id' => 'Model ID',
'model_attributes' => 'Model Attributes',
'created_at' => 'Created At',
'created_user' => 'Created User',
'updated_at' => 'Updated At',
'updated_user' => 'Updated User',
'status' => 'Status',
];
}
public function beforeValidate()
{
if (parent::beforeValidate()){
$this->status = self::STATUS_ACTIVE;
return true;
}else{
return false;
}
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)){
$this->setOwnerAndTime();
return true;
}else{
return false;
}
}
public function beforeDelete()
{
if (parent::beforeDelete()){
return true;
}else{
return false;
}
}
/**
* @param \albertborsos\yii2lib\db\ActiveRecord $model
*/
public static function createArchive($model){
if (!$model->getIsNewRecord()){
$class = get_class($model); /** @var \albertborsos\yii2lib\db\ActiveRecord $class */
$oldModel = $class::findOne(['id' => $model->getPrimaryKey()]);
if (!self::attributesAreSame($model, $oldModel)){
$archive = new Historizer();
$archive->model_class = $class;
$archive->model_id = $model->getPrimaryKey();
$archive->model_attributes = Json::encode($oldModel->attributes);
if (!$archive->save()){
$archive->throwNewException('Archiválás nem sikerült!');
}
}else{
return false;
}
}else{
return false;
}
}
/**
* @param \albertborsos\yii2lib\db\ActiveRecord $model
* @param \albertborsos\yii2lib\db\ActiveRecord $oldModel
*/
public static function attributesAreSame($model, $oldModel){
$newAttributes = $model->attributes;
$oldAttributes = $oldModel->attributes;
foreach($newAttributes as $id => $value){
$new = preg_replace('/\s+/', ' ', $newAttributes[$id]);
$old = preg_replace('/\s+/', ' ', $oldAttributes[$id]);
if ($new != $old){
return false;
}
}
return true;
}
}