forked from rainlab/notify-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveDatabaseAction.php
118 lines (98 loc) · 3.2 KB
/
SaveDatabaseAction.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php namespace RainLab\Notify\NotifyRules;
use Ramsey\Uuid\Uuid;
use RainLab\Notify\Classes\ActionBase;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class SaveDatabaseAction extends ActionBase
{
protected $tableDefinitions;
/**
* Returns information about this event, including name and description.
*/
public function actionDetails()
{
return [
'name' => 'Store in database',
'description' => 'Log event data in the notifications activity log',
'icon' => 'icon-database'
];
}
public function defineFormFields()
{
return 'fields.yaml';
}
public function getText()
{
if ($this->host->related_object) {
$label = array_get($this->getRelatedObjectOptions(), $this->host->related_object);
return 'Log event in the '.$label.' log';
}
return parent::getText();
}
/**
* Triggers this action.
* @param array $params
* @return void
*/
public function triggerAction($params)
{
if (
(!$definition = array_get($this->tableDefinitions, $this->host->related_object)) ||
(!$param = array_get($definition, 'param')) ||
(!$value = array_get($params, $param))
) {
throw new ApplicationException('Error evaluating the save database action: the related object is not found in the action parameters.');
}
if (!$value instanceof EloquentModel) {
// @todo Perhaps value is an ID or a model array,
// look up model $definition[class] from ID ...
}
$rule = $this->host->notification_rule;
$relation = array_get($definition, 'relation');
$value->$relation()->create([
'id' => Uuid::uuid4()->toString(),
'event_type' => $rule->getEventClass(),
'icon' => $this->host->icon,
'type' => $this->host->type,
'body' => $this->host->body,
'data' => $this->getData($params),
'read_at' => null,
]);
}
/**
* Get the data for the notification.
*
* @param array $notifiable
* @return array
*/
protected function getData($params)
{
// This should check for params that cannot be jsonable.
return $params;
}
public function getRelatedObjectOptions()
{
$result = [];
foreach ($this->tableDefinitions as $key => $definition) {
$result[$key] = array_get($definition, 'label');
}
return $result;
}
public function getTableDefinitions()
{
return $this->tableDefinitions;
}
public function addTableDefinition($options)
{
if (!$className = array_get($options, 'class')) {
throw new ApplicationException('Missing class name from table definition.');
}
$options = array_merge([
'label' => 'Undefined table',
'class' => null,
'param' => null,
'relation' => 'notifications',
], $options);
$keyName = $className . '@' . array_get($options, 'relation');
$this->tableDefinitions[$keyName] = $options;
}
}