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