1 | <?php |
||
32 | class AuditEntry extends ActiveRecord |
||
33 | { |
||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $autoSerialize = false; |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | 57 | */ |
|
42 | public static function tableName() |
||
46 | |||
47 | /** |
||
48 | * @param bool $initialise |
||
49 | * @return static |
||
50 | 113 | */ |
|
51 | public static function create($initialise = true) |
||
59 | |||
60 | /** |
||
61 | * Returns all linked AuditError instances |
||
62 | * (Called `linkedErrors()` to avoid confusion with the `getErrors()` method) |
||
63 | * @return ActiveQuery |
||
64 | 3 | */ |
|
65 | public function getLinkedErrors() |
||
69 | |||
70 | /** |
||
71 | * Returns all linked AuditTrail instances |
||
72 | * @return ActiveQuery |
||
73 | 3 | */ |
|
74 | public function getTrails() |
||
78 | |||
79 | /** |
||
80 | * Returns all linked AuditMail instances |
||
81 | * @return ActiveQuery |
||
82 | 3 | */ |
|
83 | public function getMails() |
||
87 | |||
88 | /** |
||
89 | * Returns all linked AuditJavascript instances |
||
90 | * @return ActiveQuery |
||
91 | 3 | */ |
|
92 | public function getJavascripts() |
||
96 | |||
97 | /** |
||
98 | * Returns all linked data records |
||
99 | * @return ActiveQuery |
||
100 | 1 | */ |
|
101 | public function getData() |
||
105 | |||
106 | /** |
||
107 | * Writes a number of associated data records in one go. |
||
108 | * @param $batchData |
||
109 | * @param bool $compact |
||
110 | * @throws \yii\db\Exception |
||
111 | 38 | */ |
|
112 | public function addBatchData($batchData, $compact = true) |
||
113 | 38 | { |
|
114 | 38 | $columns = ['entry_id', 'type', 'created', 'data']; |
|
115 | 38 | $rows = []; |
|
116 | 14 | $params = []; |
|
117 | $date = date('Y-m-d H:i:s'); |
||
118 | // Some database like postgres depend on the data being escaped correctly. |
||
119 | // PDO can take care of this if you define the field as a LOB (Large OBject), but unfortunately Yii does threat values |
||
120 | // for batch inserts the same way. This code adds a number of literals instead of the actual values |
||
121 | 14 | // so that they can be bound right before insert and still get escaped correctly |
|
122 | 14 | foreach ($batchData as $type => $data) { |
|
123 | 14 | $param = ':data_' . str_replace('/', '_', $type); |
|
124 | 14 | $rows[] = [$this->id, $type, $date, new Expression($param)]; |
|
125 | 14 | $params[$param] = [Helper::serialize($data, $compact), \PDO::PARAM_LOB]; |
|
126 | 14 | } |
|
127 | 38 | static::getDb()->createCommand()->batchInsert(AuditData::tableName(), $columns, $rows)->bindValues($params)->execute(); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param $type |
||
132 | * @param $data |
||
133 | * @param bool|true $compact |
||
134 | * @throws \yii\db\Exception |
||
135 | 2 | */ |
|
136 | public function addData($type, $data, $compact = true) |
||
137 | { |
||
138 | // Make sure to mark data as a large object so it gets escaped |
||
139 | 2 | $record = [ |
|
140 | 2 | 'entry_id' => $this->id, |
|
141 | 2 | 'type' => $type, |
|
142 | 2 | 'created' => date('Y-m-d H:i:s'), |
|
143 | 2 | 'data' => [Helper::serialize($data, $compact), \PDO::PARAM_LOB], |
|
144 | 2 | ]; |
|
145 | 2 | static::getDb()->createCommand()->insert(AuditData::tableName(), $record)->execute(); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Records the current application state into the instance. |
||
150 | 113 | */ |
|
151 | public function record() |
||
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | 68 | public function finalize() |
|
173 | { |
||
174 | 68 | $app = Yii::$app; |
|
175 | 24 | $request = $app->request; |
|
176 | |||
177 | 24 | if (!$this->user_id && $request instanceof \yii\web\Request) { |
|
178 | 24 | $this->user_id = Audit::getInstance()->getUserId(); |
|
179 | 24 | } |
|
180 | 24 | ||
181 | $this->duration = microtime(true) - YII_BEGIN_TIME; |
||
182 | 24 | $this->memory_max = memory_get_peak_usage(); |
|
183 | 24 | return $this->save(false, ['duration', 'memory_max', 'user_id']); |
|
184 | 24 | } |
|
185 | |||
186 | /** |
||
187 | * @return array |
||
188 | */ |
||
189 | public function attributeLabels() |
||
201 | |||
202 | /** |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function hasRelatedData() |
||
221 | |||
222 | } |
||
223 |