-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathAuditTrail.php
116 lines (104 loc) · 2.85 KB
/
AuditTrail.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
<?php
namespace sammaye\audittrail;
use Yii;
use yii\db\ActiveRecord;
/**
* The followings are the available columns in table 'tbl_audit_trail':
* @var integer $id
* @var string $new_value
* @var string $old_value
* @var string $action
* @var string $model
* @var string $field
* @var string $stamp
* @var integer $user_id
* @var string $model_id
*/
class AuditTrail extends ActiveRecord
{
private $_message_category = 'audittrail';
/**
* @return string the associated database table name
*/
public static function tableName()
{
if(isset(Yii::$app->params['audittrail.table'])){
return Yii::$app->params['audittrail.table'];
}else{
return '{{%audit_trail}}';
}
}
/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb()
{
if (isset(Yii::$app->params['audittrail.db'])) {
return Yii::$app->get(Yii::$app->params['audittrail.db']);
} else {
return parent::getDb();
}
// return Yii::$app->get('dbUser');
}
public function init()
{
parent::init();
\Yii::$app->i18n->translations[$this->_message_category] = [
'class' => 'yii\i18n\PhpMessageSource',
];
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return [
'id' => Yii::t('audittrail','ID'),
'old_value' => Yii::t('audittrail','Old Value'),
'new_value' => Yii::t('audittrail','New Value'),
'action' => Yii::t('audittrail','Action'),
'model' => Yii::t('audittrail','Type'),
'field' => Yii::t('audittrail','Field'),
'stamp' => Yii::t('audittrail','Stamp'),
'user_id' => Yii::t('audittrail','User'),
'model_id' => Yii::t('audittrail','ID'),
];
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return [
[['action', 'model', 'stamp', 'model_id'], 'required'],
['action', 'string', 'max' => 255],
['model', 'string', 'max' => 255],
['field', 'string', 'max' => 255],
['model_id', 'string', 'max' => 255],
['user_id', 'string', 'max' => 255],
[['old_value', 'new_value'], 'safe']
];
}
public static function recently($query)
{
$query->orderBy(['[[stamp]]' => SORT_DESC]);
}
public function getUser()
{
if(isset(Yii::$app->params['audittrail.model']) && isset(Yii::$app->params['audittrail.model'])){
return $this->hasOne(Yii::$app->params['audittrail.model'], ['id' => 'user_id']);
}else{
return $this->hasOne('common\models\User', ['id' => 'user_id']);
}
}
public function getParent(){
$model_name =
(
isset(Yii::$app->params['audittrail.FQNPrefix']) &&
rtrim(Yii::$app->params['audittrail.FQNPrefix'], '\\') ?
rtrim(Yii::$app->params['audittrail.FQNPrefix'], '\\') . '\\' :
''
) . $this->model;
return new $model_name;
}
}