-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbBehavior.php
99 lines (82 loc) · 2.23 KB
/
DbBehavior.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
<?php
namespace mekegi\ArLogger;
/**
* @since 24.07.13 11:01
* @author Arsen Abdusalamov
*/
class DbBehavior extends Behavior
{
public $logChangesClass = '\LogChanges';
public $logSessionClass = '\LogSession';
public $title = 'title';
public $add_info = 'add_info';
public $variable = 'variable';
public $old = 'old';
public $new = 'new';
public $fk_user = 'fk_user';
public $fk_log_session = 'fk_log_session';
/**
* @var LogSession
*/
protected static $_logSession;
/**
*
* @param type $title
* @param array $diff
*/
protected function logChanges($title, array $diff)
{
foreach ($diff as $key => $new) {
$oldVal = empty($this->_oldAttributes[$key]) ? null : $this->_oldAttributes[$key];
$this->logChange($title, $key, $oldVal, $new);
}
}
/**
*
* @param string $title
* @param string $variable
* @param string $old
* @param string $new
*/
protected function logChange($title, $variable, $old, $new)
{
if ($old == $new) {
return;
}
$logChange = new $this->logChangesClass;
if ($this->title) {
$logChange->{$this->title} = $title;
}
if ($this->add_info) {
$logChange->{$this->add_info} = implode("\n", $this->getBackTrace());
}
if ($this->variable) {
$logChange->{$this->variable} = $variable;
}
if ($this->old) {
$logChange->{$this->old} = (string) $old;
}
$logChange->setUser();
if ($this->new) {
$logChange->{$this->new} = (string) $new;
}
$logChange->setGeneric($this->owner);
if ($this->fk_log_session) {
$logChange->{$this->fk_log_session} = $this->getLogSession()->getPrimaryKey();
}
$logChange->save();
}
/**
* @since 26.09.13 16:47
* @author Arsen Abdusalamov
* @return LogSession
*/
protected function getLogSession()
{
if (empty(self::$_logSession)) {
self::$_logSession = new $this->logSessionClass;
self::$_logSession->save(0);
}
return self::$_logSession;
}
}