|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ilateral\SilverStripe\Notifier; |
|
4
|
|
|
|
|
5
|
|
|
use ilateral\SilverStripe\Notifier\Model\Notification; |
|
6
|
|
|
use ilateral\SilverStripe\Notifier\Model\NotificationRule; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
use SilverStripe\ORM\DataExtension; |
|
9
|
|
|
|
|
10
|
|
|
class DataObjectExtension extends DataExtension |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Check if this object has been changed and |
|
14
|
|
|
* notify if needed |
|
15
|
|
|
*/ |
|
16
|
|
|
public function onAfterWrite() |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var DataObject */ |
|
19
|
|
|
$owner = $this->getOwner(); |
|
20
|
|
|
$changed_fields = $owner->getChangedFields(true, DataObject::CHANGE_VALUE); |
|
21
|
|
|
$new_values = []; |
|
22
|
|
|
|
|
23
|
|
|
foreach ($changed_fields as $key => $value) { |
|
24
|
|
|
$new_values[$key] = $value['after']; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$notifications = Notification::get() |
|
28
|
|
|
->filter('BaseClassName', $owner->ClassName); |
|
29
|
|
|
|
|
30
|
|
|
// if created, shortcut below logic |
|
31
|
|
|
if (in_array('ID', array_keys($changed_fields))) { |
|
32
|
|
|
Notifier::processNotifications( |
|
33
|
|
|
$notifications->filter(Notification::STATE_CREATED, true), |
|
34
|
|
|
$owner |
|
35
|
|
|
); |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// If no fields were changed, then no need to continue |
|
40
|
|
|
if (count(array_keys($changed_fields)) == 0) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Get a list of all relevent rules |
|
45
|
|
|
$rules = NotificationRule::get()->filter([ |
|
46
|
|
|
'Notification.BaseClassName' => $owner->ClassName, |
|
47
|
|
|
'FieldName' => array_keys($changed_fields), |
|
48
|
|
|
'Value:not' => null |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$rule_ids = $rules->filterByCallback( |
|
52
|
|
|
function($item, $list) use ($new_values) { |
|
|
|
|
|
|
53
|
|
|
if (in_array($item->FieldName, array_keys($new_values)) |
|
54
|
|
|
&& $new_values[$item->FieldName] == $item->Value) { |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
)->column('ID'); |
|
59
|
|
|
|
|
60
|
|
|
if (count($rule_ids) > 0) { |
|
61
|
|
|
$notifications = $notifications->filter('Rules.ID', $rule_ids); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$notifications = $notifications->filterByCallback( |
|
65
|
|
|
function($item, $list) use ($changed_fields) { |
|
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var Notification $item |
|
68
|
|
|
* @var DataList $list |
|
69
|
|
|
*/ |
|
70
|
|
|
if ($item->dbObject(Notification::STATE_UPDATED)->getValue()) { |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach ($item->Rules() as $rule) { |
|
75
|
|
|
/** @var NotificationRule $rule */ |
|
76
|
|
|
if (in_array($rule->FieldName, array_keys($changed_fields)) |
|
77
|
|
|
&& $rule->WasChanged == true) { |
|
|
|
|
|
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (in_array($rule->FieldName, array_keys($changed_fields)) |
|
82
|
|
|
&& isset($changed_fields[$rule->FieldName]['after']) |
|
83
|
|
|
&& $changed_fields[$rule->FieldName]['after'] == $rule->Value |
|
84
|
|
|
) { |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
Notifier::processNotifications($notifications, $owner); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Check if this object has been deleted and |
|
98
|
|
|
* notify if needed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function onAfterDelete() |
|
101
|
|
|
{ |
|
102
|
|
|
/** @var DataObject */ |
|
103
|
|
|
$owner = $this->getOwner(); |
|
104
|
|
|
|
|
105
|
|
|
$notifications = Notification::get()->filter( |
|
106
|
|
|
[ |
|
107
|
|
|
'BaseClassName' => $owner->ClassName, |
|
108
|
|
|
Notification::STATE_DELETED => true |
|
109
|
|
|
] |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
Notifier::processNotifications($notifications, $owner); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.