1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Ecommerce app controller |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
class ecommerceController extends Controller { |
|
|
|
|
12
|
|
|
|
13
|
|
|
public function submitReviewAction() { |
14
|
|
|
$result = new \Server\Result(); |
|
|
|
|
15
|
|
|
if (!empty($_POST['review']['item_id']) && !empty($_POST['review']['name']) && !empty($_POST['review']['text'])) { |
16
|
|
|
$item = Ecommerce\Item::get((int) $_POST['review']['item_id']); |
|
|
|
|
17
|
|
|
if (!$item) { |
18
|
|
|
$result->success = false; |
19
|
|
|
$result->content = ['errorText' => 'Товар не найден']; |
20
|
|
|
return $result->send(); |
21
|
|
|
} |
22
|
|
|
$review = new Ecommerce\Item\Review([ |
|
|
|
|
23
|
|
|
'item_id' => $item->id, |
24
|
|
|
'user_id' => \Users\User::$cur->id, |
25
|
|
|
'name' => htmlspecialchars($_POST['review']['name']), |
26
|
|
|
'rating' => (int) $_POST['review']['rating'], |
27
|
|
|
'text' => htmlspecialchars($_POST['review']['text']), |
28
|
|
|
'mail' => !empty($_POST['review']['email']) ? htmlspecialchars($_POST['review']['email']) : '', |
29
|
|
|
'file_id' => !empty($_FILES['review']['tmp_name']['file']) ? App::$cur->files->upload([ |
30
|
|
|
'tmp_name' => $_FILES['review']['tmp_name']['file'], |
31
|
|
|
'name' => $_FILES['review']['name']['file'] |
32
|
|
|
]) : 0 |
33
|
|
|
]); |
34
|
|
|
$review->save(); |
35
|
|
|
$result->successMsg = 'Отзыв успешно оставлен, он появится после модерации'; |
36
|
|
|
return $result->send(); |
37
|
|
|
} |
38
|
|
|
$result->success = false; |
39
|
|
|
$result->content = ['errorText' => 'Не все поля были заполнены']; |
40
|
|
|
return $result->send(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function buyCardAction() { |
44
|
|
|
$this->view->setTitle('Покупка карты'); |
45
|
|
|
$bread = []; |
46
|
|
|
$bread[] = ['text' => 'Покупка карты']; |
47
|
|
|
$user = Users\User::$cur; |
48
|
|
|
if (!empty($_POST) && !empty($_POST['card_id'])) { |
49
|
|
|
$error = false; |
50
|
|
|
$card = \Ecommerce\Card::get((int) $_POST['card_id']); |
|
|
|
|
51
|
|
|
if (!$card) { |
52
|
|
|
$error = true; |
53
|
|
|
\Inji\Msg::add('Такой карты не существует', 'danger'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!Users\User::$cur->id) { |
57
|
|
|
$user_id = $this->Users->registration($_POST, true); |
58
|
|
|
if (!$user_id) { |
59
|
|
|
$error = true; |
60
|
|
|
$user = null; |
61
|
|
|
} else { |
62
|
|
|
$user = Users\User::get($user_id); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$userCard = \Ecommerce\Card\Item::get([['card_id', $card->id], ['user_id', $user->id]]); |
|
|
|
|
66
|
|
|
if ($userCard) { |
67
|
|
|
$error = true; |
68
|
|
|
\Inji\Msg::add('У вас уже есть такая карта', 'danger'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$fields = \Ecommerce\UserAdds\Field::getList(); |
|
|
|
|
72
|
|
|
foreach ($fields as $field) { |
73
|
|
|
if (empty($_POST['userAdds']['fields'][$field->id]) && $field->required) { |
74
|
|
|
$error = 1; |
75
|
|
|
\Inji\Msg::add('Вы не указали: ' . $field->name); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
if (!$error) { |
79
|
|
|
$cardItem = new \Ecommerce\Card\Item(); |
80
|
|
|
$cardItem->card_id = $card->id; |
81
|
|
|
$cardItem->user_id = $user->id; |
82
|
|
|
$cardItem->save(); |
83
|
|
|
|
84
|
|
|
$cart = new \Ecommerce\Cart(); |
85
|
|
|
$cart->user_id = $user->user_id; |
86
|
|
|
$cart->cart_status_id = 2; |
87
|
|
|
$cart->comment = htmlspecialchars($_POST['comment']); |
88
|
|
|
$cart->date_status = date('Y-m-d H:i:s'); |
89
|
|
|
$cart->complete_data = date('Y-m-d H:i:s'); |
90
|
|
|
if (!empty($_SESSION['cart']['cart_id'])) { |
91
|
|
|
$cart->card_item_id = $cardItem->id; |
92
|
|
|
} |
93
|
|
|
$cart->save(); |
94
|
|
|
|
95
|
|
|
$this->module->parseFields($_POST['userAdds']['fields'], $cart); |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
$extra = new \Ecommerce\Cart\Extra(); |
|
|
|
|
99
|
|
|
$extra->name = $card->name; |
100
|
|
|
$extra->price = $card->price; |
101
|
|
|
$extra->count = 1; |
102
|
|
|
$extra->cart_id = $cart->id; |
103
|
|
|
$extra->info = 'card:' . $card->id . '|cardItem:' . $cardItem->id; |
104
|
|
|
$extra->save(); |
105
|
|
|
Tools::redirect('/ecommerce/cart/success'); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$this->view->page(['data' => compact('bread')]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function autoCompleteAction() { |
112
|
|
|
$return = Cache::get('itemsAutocomplete', [''], function () { |
|
|
|
|
113
|
|
|
$items = $this->ecommerce->getItems(['array' => true]); |
114
|
|
|
$return = []; |
115
|
|
|
foreach ($items as $item) { |
116
|
|
|
$return[] = ['name' => $item['item_name'], 'search' => $item['item_search_index']]; |
117
|
|
|
} |
118
|
|
|
return gzcompress(json_encode($return)); |
119
|
|
|
}, 4 * 60 * 60); |
120
|
|
|
echo gzuncompress($return); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function indexAction() { |
124
|
|
|
if (empty($this->module->config['catalogPresentPage'])) { |
125
|
|
|
Tools::redirect('/ecommerce/itemList'); |
126
|
|
|
} |
127
|
|
|
$this->view->page(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function itemListAction($category_id = 0) { |
131
|
|
|
//search |
132
|
|
|
if (!empty($_GET['search'])) { |
133
|
|
|
if (!empty($_GET['inCatalog'])) { |
134
|
|
|
$category_id = (int) $_GET['inCatalog']; |
135
|
|
|
} |
136
|
|
|
$search = $_GET['search']; |
137
|
|
|
} else { |
138
|
|
|
$search = ''; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
//sort |
142
|
|
|
if (!empty($_GET['sort'])) { |
143
|
|
|
$sort = $_GET['sort']; |
144
|
|
|
} elseif (!empty($this->ecommerce->config['defaultSort'])) { |
145
|
|
|
$sort = $this->ecommerce->config['defaultSort']; |
146
|
|
|
} else { |
147
|
|
|
$sort = ['name' => 'asc']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
//category |
151
|
|
|
$category = null; |
152
|
|
|
$categoryClass = 'Ecommerce\Category'; |
153
|
|
|
if (!empty($this->module->config['catalogReplace'])) { |
154
|
|
|
$categoryClass = 'Ecommerce\Catalog'; |
155
|
|
|
} |
156
|
|
|
if ($category_id) { |
157
|
|
|
|
158
|
|
|
if (is_numeric($category_id)) { |
159
|
|
|
$category = $categoryClass::get((int) $category_id); |
160
|
|
|
} |
161
|
|
|
if (!$category) { |
162
|
|
|
$category = $categoryClass::get((int) $category_id, 'alias'); |
163
|
|
|
} |
164
|
|
|
if ($category) { |
165
|
|
|
$category_id = $category->id; |
166
|
|
|
} else { |
167
|
|
|
$category_id = 0; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} else { |
171
|
|
|
$category_id = 0; |
172
|
|
|
} |
173
|
|
|
if ($category) { |
174
|
|
|
$category->views++; |
175
|
|
|
$category->save(); |
176
|
|
|
} |
177
|
|
|
$active = $category_id; |
178
|
|
|
if (!empty($_GET['categorys'])) { |
179
|
|
|
$categorysList = $_GET['categorys']; |
180
|
|
|
} elseif ($categoryClass === 'Ecommerce\Catalog' && $category) { |
181
|
|
|
$categorysList = array_keys($category->categories(['key' => 'category_id'])); |
182
|
|
|
} else { |
183
|
|
|
$categorysList = $category_id; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
//items pages |
187
|
|
|
$pages = new \Ui\Pages($_GET, ['count' => $this->ecommerce->getItemsCount([ |
|
|
|
|
188
|
|
|
'parent' => $categorysList, |
189
|
|
|
'search' => trim($search), |
190
|
|
|
'filters' => !empty($_GET['filters']) ? $_GET['filters'] : [] |
191
|
|
|
]), |
192
|
|
|
'limit' => !empty($this->Ecommerce->config['default_limit']) ? $this->Ecommerce->config['default_limit'] : 18, |
193
|
|
|
]); |
194
|
|
|
if (!empty(App::$cur->ecommerce->config['list_all']) && !empty($_GET['limit']) && $_GET['limit'] == 'all') { |
195
|
|
|
$pages->params['start'] = 0; |
196
|
|
|
$pages->params['limit'] = 0; |
197
|
|
|
$pages->params['pages'] = 1; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
//bread |
201
|
|
|
$bread = []; |
202
|
|
|
if (!$category || !$category->name) { |
203
|
|
|
if (!empty($_GET['filters']['best'])) { |
204
|
|
|
$bread[] = array('text' => 'Каталог', 'href' => '/ecommerce'); |
205
|
|
|
$bread[] = array('text' => 'Рекомендумые товары'); |
206
|
|
|
} else { |
207
|
|
|
$bread[] = array('text' => 'Каталог'); |
208
|
|
|
} |
209
|
|
|
$this->view->setTitle('Каталог'); |
210
|
|
|
} else { |
211
|
|
|
$bread[] = array('text' => 'Каталог', 'href' => '/ecommerce'); |
212
|
|
|
$categoryIds = array_values(array_filter(explode('/', $category->tree_path))); |
213
|
|
|
foreach ($categoryIds as $id) { |
214
|
|
|
$cat = Ecommerce\Category::get($id); |
|
|
|
|
215
|
|
|
$bread[] = array('text' => $cat->name, 'href' => '/ecommerce/itemList/' . $cat->id); |
216
|
|
|
} |
217
|
|
|
$bread[] = array('text' => $category->name); |
218
|
|
|
$this->view->setTitle($category->name); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
//items |
222
|
|
|
$items = $this->ecommerce->getItems([ |
223
|
|
|
'parent' => $categorysList, |
224
|
|
|
'start' => $pages->params['start'], |
225
|
|
|
'count' => $pages->params['limit'], |
226
|
|
|
'search' => trim($search), |
227
|
|
|
'sort' => $sort, |
228
|
|
|
'filters' => !empty($_GET['filters']) ? $_GET['filters'] : [] |
229
|
|
|
]); |
230
|
|
|
|
231
|
|
|
//params |
232
|
|
|
if (!empty(App::$cur->ecommerce->config['filtersByRel'])) { |
233
|
|
|
$categorysList = is_array($categorysList) ? $categorysList : explode(',', $categorysList); |
234
|
|
|
$categorysList = array_filter($categorysList); |
235
|
|
|
$opts = []; |
236
|
|
|
foreach ($categorysList as $categoryId) { |
237
|
|
|
$cat = \Ecommerce\Category::get($categoryId); |
238
|
|
|
if ($cat) { |
239
|
|
|
$opts = array_merge($opts, array_keys($cat->options(['key' => 'item_option_id']))); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
$opts = array_unique($opts); |
243
|
|
|
if ($opts) { |
|
|
|
|
244
|
|
|
$options = \Ecommerce\Item\Option::getList(['where' => [['item_option_searchable', 1], ['id', $opts, 'IN']], 'order' => ['weight', 'asc']]); |
|
|
|
|
245
|
|
|
} else { |
246
|
|
|
$options = []; |
247
|
|
|
} |
248
|
|
|
} elseif (empty(App::$cur->ecommerce->config['filtersInLast'])) { |
249
|
|
|
$options = \Ecommerce\Item\Option::getList(['where' => ['item_option_searchable', 1], 'order' => ['weight', 'asc']]); |
250
|
|
|
} else { |
251
|
|
|
$params = $this->ecommerce->getItemsParams([ |
252
|
|
|
'parent' => $categorysList, |
253
|
|
|
'search' => trim($search), |
254
|
|
|
'filters' => !empty($_GET['filters']) ? $_GET['filters'] : [] |
255
|
|
|
]); |
256
|
|
|
$ids = []; |
257
|
|
|
foreach ($params as $param) { |
258
|
|
|
$ids[] = $param->item_option_id; |
259
|
|
|
} |
260
|
|
|
if ($ids) { |
261
|
|
|
$options = \Ecommerce\Item\Option::getList(['where' => ['id', $ids, 'IN'], 'order' => ['weight', 'asc']]); |
262
|
|
|
} else { |
263
|
|
|
$options = []; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
//child categorys |
269
|
|
|
if ($category) { |
270
|
|
|
$categorys = $category->catalogs; |
271
|
|
|
} else { |
272
|
|
|
$categorys = \Ecommerce\Category::getList(['where' => ['parent_id', 0]]); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
//view content |
276
|
|
|
$this->view->page([ |
277
|
|
|
'page' => $category ? $category->resolveTemplate() : (!empty(App::$cur->ecommerce->config['defaultCategoryTemplate']) ? App::$cur->ecommerce->config['defaultCategoryTemplate'] : 'current'), |
278
|
|
|
'content' => $category ? $category->resolveViewer() : (!empty(App::$cur->ecommerce->config['defaultCategoryView']) ? App::$cur->ecommerce->config['defaultCategoryView'] : 'itemList'), |
279
|
|
|
'data' => compact('active', 'category', 'sort', 'search', 'pages', 'items', 'categorys', 'bread', 'options')]); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function favoritesAction() { |
283
|
|
|
$count = $this->module->getFavoriteCount(); |
284
|
|
|
//items pages |
285
|
|
|
$pages = new \Ui\Pages($_GET, [ |
286
|
|
|
'count' => $count, |
287
|
|
|
'limit' => !empty($this->Ecommerce->config['default_limit']) ? $this->Ecommerce->config['default_limit'] : 18, |
288
|
|
|
]); |
289
|
|
|
if (Users\User::$cur->id) { |
290
|
|
|
$favs = \Ecommerce\Favorite::getList(['where' => ['user_id', Users\User::$cur->id], 'key' => 'item_id', 'start' => $pages->params['start'], 'limit' => $pages->params['limit']]); |
|
|
|
|
291
|
|
|
$ids = array_keys($favs); |
292
|
|
|
} else { |
293
|
|
|
$favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : []; |
294
|
|
|
$ids = array_slice($favs, $pages->params['start'], $pages->params['limit']); |
295
|
|
|
} |
296
|
|
|
if ($ids) { |
|
|
|
|
297
|
|
|
//items |
298
|
|
|
$items = \Ecommerce\Item::getList(['where' => ['id', $ids, 'IN']]); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$bread = []; |
302
|
|
|
$bread[] = ['text' => 'Каталог', 'href' => '/ecommerce/itemList/']; |
303
|
|
|
$bread[] = ['text' => 'Избранное']; |
304
|
|
|
$this->view->setTitle('Избранное'); |
305
|
|
|
$this->view->page(['data' => compact('pages', 'items', 'bread')]); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function viewAction($id = '', $quick = 0) { |
309
|
|
|
$item = \Ecommerce\Item::get((int) $id); |
310
|
|
|
if (!$item) { |
311
|
|
|
Tools::redirect('/ecommerce/', 'Такой товар не найден'); |
312
|
|
|
} |
313
|
|
|
if(!$item->isVisible()){ |
314
|
|
|
Tools::redirect('/ecommerce/', 'Этот товар сейчас недоступен'); |
315
|
|
|
} |
316
|
|
|
$active = $item->category_id; |
317
|
|
|
$catalog = $item->category; |
318
|
|
|
$bread = []; |
319
|
|
|
$bread[] = ['text' => 'Каталог', 'href' => '/ecommerce']; |
320
|
|
|
if (!empty($this->module->config['catalogReplace'])) { |
321
|
|
|
$cat = \Ecommerce\Catalog::get(['where' => ['categories:category_id', $item->category_id]]); |
|
|
|
|
322
|
|
|
while ($cat) { |
323
|
|
|
$bread[] = ['text' => $cat->name, 'href' => '/ecommerce/itemList/' . $cat->id]; |
324
|
|
|
$cat = $cat->parent; |
325
|
|
|
} |
326
|
|
|
} else { |
327
|
|
|
$catalogIds = array_values(array_filter(explode('/', $item->tree_path))); |
328
|
|
|
foreach ($catalogIds as $id) { |
329
|
|
|
$cat = Ecommerce\Category::get($id); |
330
|
|
|
if ($cat) { |
331
|
|
|
$bread[] = ['text' => $cat->name, 'href' => '/ecommerce/itemList/' . $cat->id]; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
$bread[] = ['text' => $item->name()]; |
336
|
|
|
$this->view->setTitle($item->name()); |
337
|
|
|
$options = [ |
338
|
|
|
'data' => compact('item', 'active', 'catalog', 'bread', 'quick'), |
339
|
|
|
'content' => $item->view ? $item->view : 'view', |
340
|
|
|
]; |
341
|
|
|
if (isset($_GET['quickview'])) { |
342
|
|
|
$options['page'] = 'blank'; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$this->view->addMetaTag(['property' => 'og:title', 'content' => $item->name]); |
346
|
|
|
$this->view->addMetaTag(['property' => 'og:description', 'content' => strip_tags($item->description)]); |
347
|
|
|
if ($item->image) { |
348
|
|
|
$this->view->addMetaTag(['property' => 'og:image', 'content' => 'http://' . INJI_DOMAIN_NAME . $item->image->path]); |
349
|
|
|
} |
350
|
|
|
$this->view->addMetaTag(['property' => 'og:url', 'content' => 'http://' . INJI_DOMAIN_NAME . '/view/' . $item->id]); |
351
|
|
|
|
352
|
|
|
$viewHistory = !empty($_COOKIE['ecommerce_item_view_history']) ? json_decode($_COOKIE['ecommerce_item_view_history'], true) : []; |
353
|
|
|
array_unshift($viewHistory, $item->id); |
354
|
|
|
$viewHistory = array_unique($viewHistory); |
355
|
|
|
array_splice($viewHistory, 10); |
356
|
|
|
setcookie("ecommerce_item_view_history", json_encode($viewHistory), time() + 360000, "/"); |
357
|
|
|
|
358
|
|
|
if ($quick) { |
359
|
|
|
$this->view->content($options); |
360
|
|
|
} else { |
361
|
|
|
$this->view->page($options); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function toggleFavAction($itemId) { |
366
|
|
|
$result = new Server\Result(); |
367
|
|
|
$item = \Ecommerce\Item::get((int) $itemId); |
368
|
|
|
if (!$item) { |
369
|
|
|
$result->success = false; |
370
|
|
|
$result->content = 'Товар не найден'; |
371
|
|
|
return $result->send(); |
372
|
|
|
} |
373
|
|
|
if (Users\User::$cur->id) { |
374
|
|
|
$fav = \Ecommerce\Favorite::get([['user_id', Users\User::$cur->id], ['item_id', $item->id]]); |
375
|
|
|
if ($fav) { |
376
|
|
|
$fav->delete(); |
377
|
|
|
$result->content = ['count' => $this->module->getFavoriteCount(), 'newText' => 'В избранное']; |
378
|
|
|
$result->successMsg = 'Товар успешно убран из избранного'; |
379
|
|
|
return $result->send(); |
380
|
|
|
} else { |
381
|
|
|
$fav = new \Ecommerce\Favorite([ |
382
|
|
|
'user_id' => Users\User::$cur->id, |
383
|
|
|
'item_id' => $item->id |
384
|
|
|
]); |
385
|
|
|
$fav->save(); |
386
|
|
|
$result->content = ['count' => $this->module->getFavoriteCount(), 'newText' => 'Из избранного']; |
387
|
|
|
$result->successMsg = 'Товар успешно добавлен в избранное'; |
388
|
|
|
return $result->send(); |
389
|
|
|
} |
390
|
|
|
} else { |
391
|
|
|
$favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : []; |
392
|
|
|
if (in_array($item->id, $favs)) { |
393
|
|
|
unset($favs[array_search($item->id, $favs)]); |
394
|
|
|
setcookie("ecommerce_favitems", json_encode($favs), time() + 360000, "/"); |
395
|
|
|
$result->content = ['count' => $this->module->getFavoriteCount(), 'newText' => 'В избранное']; |
396
|
|
|
$result->successMsg = 'Товар успешно убран из избранного'; |
397
|
|
|
return $result->send(); |
398
|
|
|
} else { |
399
|
|
|
$favs[] = $item->id; |
400
|
|
|
setcookie("ecommerce_favitems", json_encode($favs), time() + 360000, "/"); |
401
|
|
|
$result->content = ['count' => $this->module->getFavoriteCount(), 'newText' => 'Из избранного']; |
402
|
|
|
$result->successMsg = 'Товар успешно добавлен в избранное'; |
403
|
|
|
return $result->send(); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths