1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package CLI |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\cli\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\models\Trigger as TriggerModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Base; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to triggers |
17
|
|
|
*/ |
18
|
|
|
class Trigger extends Base |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trigger model instance |
23
|
|
|
* @var \gplcart\core\models\Trigger $trigger |
24
|
|
|
*/ |
25
|
|
|
protected $trigger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param TriggerModel $trigger |
29
|
|
|
*/ |
30
|
|
|
public function __construct(TriggerModel $trigger) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->trigger = $trigger; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "trigger-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetTrigger() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListTrigger(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableTrigger($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "trigger-delete" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdDeleteTrigger() |
52
|
|
|
{ |
53
|
|
|
$id = $this->getParam(0); |
54
|
|
|
|
55
|
|
|
if (empty($id) || !is_numeric($id)) { |
56
|
|
|
$this->errorExit($this->text('Invalid ID')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($this->getParam('store')) { |
60
|
|
|
$deleted = $count = 0; |
61
|
|
|
foreach ($this->trigger->getList(array('store_id' => $id)) as $item) { |
62
|
|
|
$count++; |
63
|
|
|
$deleted += (int) $this->trigger->delete($item['trigger_id']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$result = ($count == $deleted); |
67
|
|
|
} else { |
68
|
|
|
$result = $this->trigger->delete($id); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!$result) { |
72
|
|
|
$this->errorExit($this->text('An error occurred')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->output(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Callback for "trigger-add" command |
80
|
|
|
*/ |
81
|
|
|
public function cmdAddTrigger() |
82
|
|
|
{ |
83
|
|
|
if ($this->getParam()) { |
84
|
|
|
$this->submitAddTrigger(); |
85
|
|
|
} else { |
86
|
|
|
$this->wizardAddTrigger(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->output(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Callback for "trigger-update" command |
94
|
|
|
*/ |
95
|
|
|
public function cmdUpdateTrigger() |
96
|
|
|
{ |
97
|
|
|
$params = $this->getParam(); |
98
|
|
|
|
99
|
|
|
if (empty($params[0]) || count($params) < 2) { |
100
|
|
|
$this->errorExit($this->text('Invalid command')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!is_numeric($params[0])) { |
104
|
|
|
$this->errorExit($this->text('Invalid ID')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->setSubmitted(null, $this->getParam()); |
108
|
|
|
$this->setSubmitted('data.conditions', $this->getSubmitted('conditions')); |
109
|
|
|
$this->setSubmittedList('data.conditions'); |
110
|
|
|
$this->setSubmitted('update', $params[0]); |
111
|
|
|
|
112
|
|
|
$this->validateComponent('trigger'); |
113
|
|
|
$this->updateTrigger($params[0]); |
114
|
|
|
$this->output(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns an array of triggers |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
protected function getListTrigger() |
122
|
|
|
{ |
123
|
|
|
$id = $this->getParam(0); |
124
|
|
|
$options = array('limit' => $this->getLimit()); |
125
|
|
|
|
126
|
|
|
if (!isset($id)) { |
127
|
|
|
return $this->trigger->getList($options); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!is_numeric($id)) { |
131
|
|
|
$this->errorExit($this->text('Invalid ID')); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!$this->getParam('store')) { |
135
|
|
|
$result = $this->trigger->get($id); |
136
|
|
|
if (empty($result)) { |
137
|
|
|
$this->errorExit($this->text('Invalid ID')); |
138
|
|
|
} |
139
|
|
|
return array($result); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$options['store_id'] = $id; |
143
|
|
|
return $this->trigger->getList($options); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Output table format |
148
|
|
|
* @param array $items |
149
|
|
|
*/ |
150
|
|
|
protected function outputFormatTableTrigger(array $items) |
151
|
|
|
{ |
152
|
|
|
$header = array( |
153
|
|
|
$this->text('ID'), |
154
|
|
|
$this->text('Name'), |
155
|
|
|
$this->text('Store'), |
156
|
|
|
$this->text('Weight'), |
157
|
|
|
$this->text('Enabled') |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$rows = array(); |
161
|
|
|
foreach ($items as $item) { |
162
|
|
|
$rows[] = array( |
163
|
|
|
$item['trigger_id'], |
164
|
|
|
$item['name'], |
165
|
|
|
$item['store_id'], |
166
|
|
|
$item['weight'], |
167
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->outputFormatTable($rows, $header); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Updates a trigger |
176
|
|
|
* @param string $trigger_id |
177
|
|
|
*/ |
178
|
|
|
protected function updateTrigger($trigger_id) |
179
|
|
|
{ |
180
|
|
|
if (!$this->isError() && !$this->trigger->update($trigger_id, $this->getSubmitted())) { |
181
|
|
|
$this->errorExit($this->text('Trigger has not been updated')); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Add a new trigger at once |
187
|
|
|
*/ |
188
|
|
|
protected function submitAddTrigger() |
189
|
|
|
{ |
190
|
|
|
$this->setSubmitted(null, $this->getParam()); |
191
|
|
|
$this->setSubmitted('data.conditions', $this->getSubmitted('conditions')); |
192
|
|
|
$this->setSubmittedList('data.conditions'); |
193
|
|
|
|
194
|
|
|
$this->validateComponent('trigger'); |
195
|
|
|
$this->addTrigger(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add a new trigger |
200
|
|
|
*/ |
201
|
|
|
protected function addTrigger() |
202
|
|
|
{ |
203
|
|
|
if (!$this->isError()) { |
204
|
|
|
$state_id = $this->trigger->add($this->getSubmitted()); |
205
|
|
|
if (empty($state_id)) { |
206
|
|
|
$this->errorExit($this->text('Trigger has not been added')); |
207
|
|
|
} |
208
|
|
|
$this->line($state_id); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Add a trigger step by step |
214
|
|
|
*/ |
215
|
|
|
protected function wizardAddTrigger() |
216
|
|
|
{ |
217
|
|
|
$this->validatePrompt('name', $this->text('Name'), 'trigger'); |
218
|
|
|
$this->validatePrompt('store_id', $this->text('Store'), 'trigger'); |
219
|
|
|
$this->validatePromptList('data.conditions', $this->text('Conditions'), 'trigger'); |
220
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'trigger', 0); |
221
|
|
|
$this->validatePrompt('weight', $this->text('Weight'), 'trigger', 0); |
222
|
|
|
$this->setSubmittedList('data.conditions'); |
223
|
|
|
|
224
|
|
|
$this->validateComponent('trigger'); |
225
|
|
|
$this->addTrigger(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|