1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class Ajde_Model_Revision extends Ajde_Model |
4
|
|
|
{ |
5
|
|
|
protected $_ignoreFieldInRevision = []; |
6
|
|
|
protected $_ignoreFieldInRevisionIfEmpty = []; |
7
|
|
|
|
8
|
|
|
const DEFAULT_LIMIT = 50; |
9
|
|
|
|
10
|
|
|
public function getRevisionsHtml($crud = null) |
11
|
|
|
{ |
12
|
|
|
if (!$this->getPK()) { |
13
|
|
|
return; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
$revisions = new RevisionCollection(); |
17
|
|
|
$revisions->addFilter(new Ajde_Filter_Where('model', Ajde_Filter::FILTER_EQUALS, $this->getModelName())); |
18
|
|
|
$revisions->addFilter(new Ajde_Filter_Where('foreignkey', Ajde_Filter::FILTER_EQUALS, $this->getPK())); |
19
|
|
|
$revisions->orderBy('time', 'DESC'); |
20
|
|
|
$revisions->limit(self::DEFAULT_LIMIT); |
21
|
|
|
|
22
|
|
|
$controller = Ajde_Controller::fromRoute(new Ajde_Core_Route('_core/crud:revisions')); |
23
|
|
|
$controller->setRevisions($revisions); |
|
|
|
|
24
|
|
|
$controller->setModel($this); |
|
|
|
|
25
|
|
|
$controller->setCrudInstance($crud); |
|
|
|
|
26
|
|
|
|
27
|
|
|
return $controller->invoke(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function getModelName() |
31
|
|
|
{ |
32
|
|
|
$modelNameCC = str_replace('Model', '', get_class($this)); |
33
|
|
|
|
34
|
|
|
return $this->_tableName = $this->fromCamelCase($modelNameCC); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function purgeRevisions() |
38
|
|
|
{ |
39
|
|
|
if (!$this->getPK()) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$revisions = new RevisionCollection(); |
44
|
|
|
$revisions->addFilter(new Ajde_Filter_Where('model', Ajde_Filter::FILTER_EQUALS, $this->getModelName())); |
45
|
|
|
$revisions->addFilter(new Ajde_Filter_Where('foreignkey', Ajde_Filter::FILTER_EQUALS, $this->getPK())); |
46
|
|
|
$revisions->deleteAll(); |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function save() |
52
|
|
|
{ |
53
|
|
|
// check all changed fields |
54
|
|
|
$modelName = get_class($this); |
55
|
|
|
$shadowModel = new $modelName(); |
56
|
|
|
/* @var $shadowModel Ajde_Model */ |
57
|
|
|
$shadowModel->loadByPK($this->getPK()); |
58
|
|
|
if ($shadowModel->_hasMeta) { |
59
|
|
|
$shadowModel->populateMeta(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// old values |
63
|
|
|
$oldValues = $shadowModel->values(); |
64
|
|
|
foreach ($oldValues as &$oldValue) { |
65
|
|
|
@$oldValue = (string) $oldValue; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// populate meta of current model, but don't override |
69
|
|
|
if ($this->_hasMeta) { |
70
|
|
|
$this->populateMeta(false, false); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// new values |
74
|
|
|
$newValues = $this->values(); |
75
|
|
|
foreach ($newValues as $k => &$newValue) { |
76
|
|
|
if ($k == 'meta_4') { |
77
|
|
|
// die('hier'); |
78
|
|
|
} |
79
|
|
|
@$newValue = (string) $newValue; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// ignore fields |
83
|
|
|
foreach ($this->_ignoreFieldInRevision as $ignoreField) { |
84
|
|
|
unset($oldValues[$ignoreField]); |
85
|
|
|
unset($newValues[$ignoreField]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// ignore fields |
89
|
|
|
foreach ($this->_ignoreFieldInRevisionIfEmpty as $ignoreField) { |
90
|
|
|
if (!isset($newValues[$ignoreField]) || empty($newValues[$ignoreField])) { |
91
|
|
|
unset($oldValues[$ignoreField]); |
92
|
|
|
unset($newValues[$ignoreField]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($diffs = array_diff_assoc($oldValues, $newValues)) { |
97
|
|
|
foreach ($diffs as $diffField => $diffValue) { |
98
|
|
|
$revision = new RevisionModel(); |
99
|
|
|
$revision->model = $this->getModelName(); |
|
|
|
|
100
|
|
|
$revision->foreignkey = $this->getPK(); |
|
|
|
|
101
|
|
|
$revision->user = UserModel::getLoggedIn(); |
|
|
|
|
102
|
|
|
$revision->field = $diffField; |
|
|
|
|
103
|
|
|
$revision->old = issetor($oldValues[$diffField]); |
|
|
|
|
104
|
|
|
$revision->new = issetor($newValues[$diffField]); |
|
|
|
|
105
|
|
|
$revision->insert(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return parent::save(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: