forked from rainlab/notify-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutionContextCondition.php
136 lines (113 loc) · 3.38 KB
/
ExecutionContextCondition.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php namespace RainLab\Notify\NotifyRules;
use Cms\Classes\Theme;
use RainLab\Notify\Classes\ConditionBase;
class ExecutionContextCondition extends ConditionBase
{
protected $operators = [
'is' => 'is',
'is_not' => 'is not',
];
public function getName()
{
return 'Event is triggered from environment';
}
public function getTitle()
{
return 'Execution context';
}
public function getText()
{
$host = $this->host;
$value = $host->value;
$attribute = $host->subcondition;
$subconditions = $this->getSubconditionOptions();
$result = array_get($subconditions, $attribute, 'Execution context');
$result .= ' <span class="operator">'.array_get($this->operators, $host->operator, $host->operator).'</span> ';
if ($attribute == 'locale' || $attribute == 'environment') {
$result .= strtolower($value) ?: '?';
}
elseif ($value) {
$options = $this->getValueOptions();
$result .= strtolower(array_get($options, $value));
}
else {
$result .= '?';
}
return $result;
}
/**
* Returns a title to use for grouping subconditions
* in the Create Condition drop-down menu
*/
public function getGroupingTitle()
{
return 'Execution context';
}
public function listSubconditions()
{
return array_flip($this->getSubconditionOptions());
}
public function initConfigData($host)
{
$host->operator = 'is';
}
public function setFormFields($fields)
{
$attribute = $fields->subcondition->value;
if ($attribute == 'locale' || $attribute == 'environment') {
$fields->value->type = 'text';
}
else {
$fields->value->type = 'dropdown';
}
}
public function getValueOptions()
{
$attribute = $this->host->subcondition;
$result = [];
if ($attribute == 'context') {
$result = [
'backend' => 'Back-end area',
'front' => 'Front-end website',
'console' => 'Command line interface',
];
}
if ($attribute == 'theme') {
foreach (Theme::all() as $theme) {
$result[$theme->getDirName()] = $theme->getDirName();
}
}
return $result;
}
public function getSubconditionOptions()
{
return [
'environment' => 'Application environment',
'context' => 'Request context',
'theme' => 'Active theme',
'locale' => 'Visitor locale',
];
}
public function getOperatorOptions()
{
return $this->operators;
}
/**
* Checks whether the condition is TRUE for specified parameters
* @param array $params
* @return bool
*/
public function isTrue(&$params)
{
$hostObj = $this->host;
$attribute = $hostObj->subcondition;
$conditionValue = $hostObj->value;
$conditionValue = trim(mb_strtolower($conditionValue));
if ($attribute == 'locale') {
return array_get($params, 'appLocale') == $conditionValue;
} else if ($attribute === 'environment') {
return $conditionValue === \App::environment();
}
return false;
}
}