1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Craft; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Audit Log Element Type. |
7
|
|
|
* |
8
|
|
|
* Makes the log behave as an Element Type |
9
|
|
|
* |
10
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2015, Bob Olde Hampsink |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
* @link http://github.com/boboldehampsink |
15
|
|
|
*/ |
16
|
|
|
class AuditLogElementType extends BaseElementType |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The name of the Element Type. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getName() |
24
|
|
|
{ |
25
|
|
|
return Craft::t('Audit Log'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Return true so we have a status select menu. |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function hasStatuses() |
34
|
|
|
{ |
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Define statuses. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getStatuses() |
44
|
|
|
{ |
45
|
|
|
return array( |
46
|
|
|
AuditLogModel::CREATED => Craft::t('Created'), |
47
|
|
|
AuditLogModel::MODIFIED => Craft::t('Modified'), |
48
|
|
|
AuditLogModel::DELETED => Craft::t('Deleted'), |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Define available table column names. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function defineAvailableTableAttributes() |
58
|
|
|
{ |
59
|
|
|
// Define default attributes |
60
|
|
|
$attributes = array( |
61
|
|
|
'type' => array('label' => Craft::t('Type')), |
62
|
|
|
'user' => array('label' => Craft::t('User')), |
63
|
|
|
'origin' => array('label' => Craft::t('Origin')), |
64
|
|
|
'dateUpdated' => array('label' => Craft::t('Modified')), |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// Allow plugins to modify the attributes |
68
|
|
|
$pluginAttributes = craft()->plugins->call('defineAdditionalAuditLogTableAttributes', array(), true); |
69
|
|
|
foreach ($pluginAttributes as $thisPluginAttributes) { |
70
|
|
|
$attributes = array_merge($attributes, $thisPluginAttributes); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Set changes at last |
74
|
|
|
$attributes['changes'] = array('label' => Craft::t('Changes')); |
75
|
|
|
|
76
|
|
|
// Return the attributes |
77
|
|
|
return $attributes; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the default table attributes. |
82
|
|
|
* |
83
|
|
|
* @param string $source |
84
|
|
|
* |
85
|
|
|
* @return string[] |
86
|
|
|
*/ |
87
|
|
|
public function getDefaultTableAttributes($source = null) |
88
|
|
|
{ |
89
|
|
|
return array('type', 'user', 'origin', 'dateUpdated', 'changes'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return table attribute html. |
94
|
|
|
* |
95
|
|
|
* @param BaseElementModel $element |
96
|
|
|
* @param string $attribute |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getTableAttributeHtml(BaseElementModel $element, $attribute) |
101
|
|
|
{ |
102
|
|
|
// First give plugins a chance to set this |
103
|
|
|
$pluginAttributeHtml = craft()->plugins->callFirst('getAuditLogTableAttributeHtml', array($element, $attribute), true); |
104
|
|
|
|
105
|
|
|
// Check if that had a valid result |
106
|
|
|
if ($pluginAttributeHtml) { |
107
|
|
|
return $pluginAttributeHtml; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Modify custom attributes |
111
|
|
|
switch ($attribute) { |
112
|
|
|
|
113
|
|
|
// Format dates |
114
|
|
|
case 'dateCreated': |
115
|
|
|
case 'dateUpdated': |
116
|
|
|
return craft()->dateFormatter->formatDateTime($element->$attribute); |
117
|
|
|
|
118
|
|
|
// Return clickable user link |
119
|
|
|
case 'user': |
120
|
|
|
$user = $element->getUser(); |
121
|
|
|
|
122
|
|
|
return $user ? '<a href="'.$user->getCpEditUrl().'">'.$user.'</a>' : Craft::t('Guest'); |
123
|
|
|
|
124
|
|
|
// Return clickable event origin |
125
|
|
|
case 'origin': |
126
|
|
|
return '<a href="'.preg_replace('/'.craft()->config->get('cpTrigger').'\//', '', UrlHelper::getUrl($element->origin), 1).'">'.$element->origin.'</a>'; |
127
|
|
|
|
128
|
|
|
// Return view changes button |
129
|
|
|
case 'changes': |
130
|
|
|
return '<a class="btn" href="'.UrlHelper::getCpUrl('auditlog/'.$element->id).'">'.Craft::t('View').'</a>'; |
131
|
|
|
|
132
|
|
|
// Default behavior |
133
|
|
|
default: |
134
|
|
|
return $element->$attribute; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Define criteria. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function defineCriteriaAttributes() |
144
|
|
|
{ |
145
|
|
|
return array( |
146
|
|
|
'type' => AttributeType::String, |
147
|
|
|
'userId' => AttributeType::Number, |
148
|
|
|
'origin' => AttributeType::String, |
149
|
|
|
'modified' => AttributeType::DateTime, |
150
|
|
|
'before' => AttributeType::String, |
151
|
|
|
'after' => AttributeType::String, |
152
|
|
|
'status' => AttributeType::String, |
153
|
|
|
'from' => AttributeType::DateTime, |
154
|
|
|
'to' => AttributeType::DateTime, |
155
|
|
|
'order' => array(AttributeType::String, 'default' => 'auditlog.id desc'), |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Modify the elements query. |
161
|
|
|
* |
162
|
|
|
* @param DbCommand $query |
163
|
|
|
* @param ElementCriteriaModel $criteria |
164
|
|
|
*/ |
165
|
|
|
public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria) |
166
|
|
|
{ |
167
|
|
|
// Default query |
168
|
|
|
$query |
169
|
|
|
->select('auditlog.id, auditlog.type, auditlog.userId, auditlog.origin, auditlog.before, auditlog.after, auditlog.status, auditlog.dateCreated, auditlog.dateUpdated') |
170
|
|
|
->from('auditlog auditlog'); |
171
|
|
|
|
172
|
|
|
// Reset default element type query parts |
173
|
|
|
$query->setJoin(''); |
174
|
|
|
$query->setWhere('1=1'); |
175
|
|
|
$query->setGroup(''); |
176
|
|
|
unset($query->params[':locale']); |
177
|
|
|
unset($query->params[':elementsid1']); |
178
|
|
|
|
179
|
|
|
// Check for specific id |
180
|
|
|
if (!empty($criteria->id)) { |
181
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.id', $criteria->id, $query->params)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Check type |
185
|
|
|
if (!empty($criteria->type)) { |
186
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.type', $criteria->type, $query->params)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Check user id |
190
|
|
|
if (!empty($criteria->userId)) { |
191
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.userId', $criteria->userId, $query->params)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// Check origin |
195
|
|
|
if (!empty($criteria->origin)) { |
196
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.origin', $criteria->origin, $query->params)); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Check before |
200
|
|
|
if (!empty($criteria->before)) { |
201
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.before', $criteria->before, $query->params)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Check after |
205
|
|
|
if (!empty($criteria->after)) { |
206
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.after', $criteria->after, $query->params)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// Check for status |
210
|
|
|
if (!empty($criteria->status)) { |
211
|
|
|
$query->andWhere(DbHelper::parseParam('auditlog.status', $criteria->status, $query->params)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Dates |
215
|
|
|
$this->applyDateCriteria($criteria, $query); |
216
|
|
|
|
217
|
|
|
// Search |
218
|
|
|
$this->applySearchCriteria($criteria, $query); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Apply date criteria. |
223
|
|
|
* |
224
|
|
|
* @param ElementCriteriaModel $criteria |
225
|
|
|
* @param DbCommand $query |
226
|
|
|
*/ |
227
|
|
|
private function applyDateCriteria(ElementCriteriaModel $criteria, DbCommand $query) |
228
|
|
|
{ |
229
|
|
|
// Check for date modified |
230
|
|
|
if (!empty($criteria->modified)) { |
231
|
|
|
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', $criteria->modified, $query->params)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// Check for date from |
235
|
|
|
if (!empty($criteria->from)) { |
236
|
|
|
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', '>= '.DateTimeHelper::formatTimeForDb($criteria->from), $query->params)); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// Check for date to |
240
|
|
|
if (!empty($criteria->to)) { |
241
|
|
|
$criteria->to->add(new DateInterval('PT23H59M59S')); |
242
|
|
|
$query->andWhere(DbHelper::parseDateParam('auditlog.dateUpdated', '<= '.DateTimeHelper::formatTimeForDb($criteria->to), $query->params)); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Apply search criteria. |
248
|
|
|
* |
249
|
|
|
* @param ElementCriteriaModel $criteria |
250
|
|
|
* @param DbCommand $query |
251
|
|
|
*/ |
252
|
|
|
private function applySearchCriteria(ElementCriteriaModel $criteria, DbCommand $query) |
253
|
|
|
{ |
254
|
|
|
if (!empty($criteria->search)) { |
255
|
|
|
|
256
|
|
|
// Always perform a LIKE search |
257
|
|
|
$criteria->search = '*'.$criteria->search.'*'; |
258
|
|
|
|
259
|
|
|
// Build conditions |
260
|
|
|
$conditions = array( |
261
|
|
|
'or', |
262
|
|
|
DbHelper::parseParam('auditlog.origin', $criteria->search, $query->params), |
263
|
|
|
DbHelper::parseParam('auditlog.before', $criteria->search, $query->params), |
264
|
|
|
DbHelper::parseParam('auditlog.after', $criteria->search, $query->params), |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
// Add to query |
268
|
|
|
$query->andWhere($conditions, $query->params); |
269
|
|
|
|
270
|
|
|
// Don't perform search logics after this |
271
|
|
|
$criteria->search = null; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Create element from row. |
277
|
|
|
* |
278
|
|
|
* @param array $row |
279
|
|
|
* |
280
|
|
|
* @return AuditLogModel |
281
|
|
|
*/ |
282
|
|
|
public function populateElementModel($row) |
283
|
|
|
{ |
284
|
|
|
return AuditLogModel::populateModel($row); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Define the sources. |
289
|
|
|
* |
290
|
|
|
* @param string $context |
291
|
|
|
*/ |
292
|
|
|
public function getSources($context = null) |
293
|
|
|
{ |
294
|
|
|
// Get plugin settings |
295
|
|
|
$settings = craft()->plugins->getPlugin('AuditLog')->getSettings(); |
296
|
|
|
|
297
|
|
|
// Set default sources |
298
|
|
|
$sources = array( |
299
|
|
|
'*' => array( |
300
|
|
|
'label' => Craft::t('All logs'), |
301
|
|
|
), |
302
|
|
|
array('heading' => Craft::t('Elements')), |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
// Show sources for entries when enabled |
306
|
|
|
if (in_array(ElementType::Entry, $settings->enabled)) { |
307
|
|
|
$sources['entries'] = array( |
308
|
|
|
'label' => Craft::t('Entries'), |
309
|
|
|
'criteria' => array( |
310
|
|
|
'type' => ElementType::Entry, |
311
|
|
|
), |
312
|
|
|
); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
// Show sources for categories when enabled |
316
|
|
|
if (in_array(ElementType::Category, $settings->enabled)) { |
317
|
|
|
$sources['categories'] = array( |
318
|
|
|
'label' => Craft::t('Categories'), |
319
|
|
|
'criteria' => array( |
320
|
|
|
'type' => ElementType::Category, |
321
|
|
|
), |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
// Show sources for users when enabled |
326
|
|
|
if (in_array(ElementType::User, $settings->enabled)) { |
327
|
|
|
$sources['users'] = array( |
328
|
|
|
'label' => Craft::t('Users'), |
329
|
|
|
'criteria' => array( |
330
|
|
|
'type' => ElementType::User, |
331
|
|
|
), |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Get sources by hook |
336
|
|
|
$plugins = craft()->plugins->call('registerAuditLogSources'); |
337
|
|
|
if (count($plugins)) { |
338
|
|
|
$sources[] = array('heading' => Craft::t('Custom')); |
339
|
|
|
foreach ($plugins as $plugin) { |
340
|
|
|
|
341
|
|
|
// Add as own source |
342
|
|
|
$sources = array_merge($sources, $plugin); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
// Return sources |
347
|
|
|
return $sources; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Set sortable attributes. |
352
|
|
|
* |
353
|
|
|
* @return array |
354
|
|
|
*/ |
355
|
|
|
public function defineSortableAttributes() |
356
|
|
|
{ |
357
|
|
|
// Set modified first |
358
|
|
|
$attributes = array('dateUpdated' => Craft::t('Modified')); |
359
|
|
|
|
360
|
|
|
// Get table attributes |
361
|
|
|
$attributes = array_merge($attributes, parent::defineSortableAttributes()); |
362
|
|
|
|
363
|
|
|
// Unset unsortable attributes |
364
|
|
|
unset($attributes['user'], $attributes['changes']); |
365
|
|
|
|
366
|
|
|
// Allow plugins to modify the attributes |
367
|
|
|
craft()->plugins->call('modifyAuditLogSortableAttributes', array(&$attributes)); |
368
|
|
|
|
369
|
|
|
// Return attributes |
370
|
|
|
return $attributes; |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|