1 | <?php |
||
17 | class Store extends Base |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Store model instance |
||
22 | * @var \gplcart\core\models\Store $store |
||
23 | */ |
||
24 | protected $store; |
||
25 | |||
26 | /** |
||
27 | * @param StoreModel $store |
||
28 | */ |
||
29 | public function __construct(StoreModel $store) |
||
30 | { |
||
31 | parent::__construct(); |
||
32 | |||
33 | $this->store = $store; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Callback for "store-on" command |
||
38 | */ |
||
39 | public function cmdOnStore() |
||
40 | { |
||
41 | $this->setStatusStore(true); |
||
42 | $this->output(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Callback for "store-off" command |
||
47 | */ |
||
48 | public function cmdOffStore() |
||
49 | { |
||
50 | $this->setStatusStore(false); |
||
51 | $this->output(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Callback for "store-get" command |
||
56 | */ |
||
57 | public function cmdGetStore() |
||
58 | { |
||
59 | $result = $this->getListStore(); |
||
60 | $this->outputFormat($result); |
||
61 | $this->outputFormatTableStore($result); |
||
62 | $this->output(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Callback for "store-delete" command |
||
67 | */ |
||
68 | public function cmdDeleteStore() |
||
69 | { |
||
70 | $id = $this->getParam(0); |
||
71 | |||
72 | if (empty($id) || !is_numeric($id)) { |
||
73 | $this->errorExit($this->text('Invalid ID')); |
||
74 | } |
||
75 | |||
76 | if (!$this->store->delete($id)) { |
||
77 | $this->errorExit($this->text('An error occurred')); |
||
78 | } |
||
79 | |||
80 | $this->output(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Callback for "store-add" command |
||
85 | */ |
||
86 | public function cmdAddStore() |
||
87 | { |
||
88 | if ($this->getParam()) { |
||
89 | $this->submitAddStore(); |
||
90 | } else { |
||
91 | $this->wizardAddStore(); |
||
92 | } |
||
93 | |||
94 | $this->output(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Callback for "store-update" command |
||
99 | */ |
||
100 | public function cmdUpdateStore() |
||
101 | { |
||
102 | $params = $this->getParam(); |
||
103 | |||
104 | if (empty($params[0]) || count($params) < 2) { |
||
105 | $this->errorExit($this->text('Invalid command')); |
||
106 | } |
||
107 | |||
108 | if (!is_numeric($params[0])) { |
||
109 | $this->errorExit($this->text('Invalid ID')); |
||
110 | } |
||
111 | |||
112 | $this->setSubmitted(null, $this->getParam()); |
||
113 | $this->setSubmitted('update', $params[0]); |
||
114 | $this->setSubmittedJson('data'); |
||
115 | $this->validateComponent('store'); |
||
116 | $this->updateStore($params[0]); |
||
117 | $this->output(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Sets status for one or several stores |
||
122 | * @param bool $status |
||
123 | */ |
||
124 | protected function setStatusStore($status) |
||
125 | { |
||
126 | $id = $this->getParam(0); |
||
127 | $all = $this->getParam('all'); |
||
128 | |||
129 | if (!isset($id) && empty($all)) { |
||
130 | $this->errorExit($this->text('Invalid command')); |
||
131 | } |
||
132 | |||
133 | $result = false; |
||
134 | |||
135 | if (isset($id)) { |
||
136 | |||
137 | if (!is_numeric($id)) { |
||
138 | $this->errorExit($this->text('Invalid ID')); |
||
139 | } |
||
140 | |||
141 | $result = $this->store->update($id, array('status' => $status)); |
||
142 | |||
143 | } else if (!empty($all)) { |
||
144 | $updated = $count = 0; |
||
145 | foreach ((array) $this->store->getList() as $store) { |
||
146 | $count++; |
||
147 | $updated += (int) $this->store->update($store['store_id'], array('status' => $status)); |
||
148 | } |
||
149 | |||
150 | $result = ($count == $updated); |
||
151 | } |
||
152 | |||
153 | if (!$result) { |
||
154 | $this->errorExit($this->text('An error occurred')); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns an array of stores |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function getListStore() |
||
163 | { |
||
164 | $id = $this->getParam(0); |
||
165 | |||
166 | if (!isset($id)) { |
||
167 | return $this->store->getList(array('limit' => $this->getLimit())); |
||
168 | } |
||
169 | |||
170 | if (!is_numeric($id)) { |
||
171 | $this->errorExit($this->text('Invalid ID')); |
||
172 | } |
||
173 | |||
174 | $result = $this->store->get($id); |
||
175 | |||
176 | if (empty($result)) { |
||
177 | $this->errorExit($this->text('Invalid ID')); |
||
178 | } |
||
179 | |||
180 | return array($result); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Output table format |
||
185 | * @param array $items |
||
186 | */ |
||
187 | protected function outputFormatTableStore(array $items) |
||
188 | { |
||
189 | $header = array( |
||
190 | $this->text('ID'), |
||
191 | $this->text('Name'), |
||
192 | $this->text('Domain'), |
||
193 | $this->text('Path'), |
||
194 | $this->text('Enabled') |
||
195 | ); |
||
196 | |||
197 | $rows = array(); |
||
198 | |||
199 | foreach ($items as $item) { |
||
200 | $rows[] = array( |
||
201 | $item['store_id'], |
||
202 | $item['name'], |
||
203 | $item['domain'], |
||
204 | $item['basepath'], |
||
205 | empty($item['status']) ? $this->text('No') : $this->text('Yes') |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | $this->outputFormatTable($rows, $header); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Updates a store |
||
214 | * @param string $store_id |
||
215 | */ |
||
216 | protected function updateStore($store_id) |
||
222 | |||
223 | /** |
||
224 | * Add a new store at once |
||
225 | */ |
||
226 | protected function submitAddStore() |
||
227 | { |
||
228 | $this->setSubmitted(null, $this->getParam()); |
||
229 | $this->setSubmittedJson('data'); |
||
230 | $this->validateComponent('store'); |
||
231 | $this->addStore(); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Add a store |
||
236 | */ |
||
237 | protected function addStore() |
||
247 | |||
248 | /** |
||
249 | * Add a new store step by step |
||
250 | */ |
||
251 | protected function wizardAddStore() |
||
261 | } |
||
262 |