Completed
Push — master ( b93a9c...e7c2e1 )
by Alexey
05:14
created

Ecommerce::getCurCart()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 6
nop 1
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Ecommerce module
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 Ecommerce extends Module
12
{
13
    public function init()
14
    {
15
        App::$primary->view->customAsset('js', '/moduleAsset/Ecommerce/js/cart.js');
16
    }
17
18
    public function getPayTypeHandlers($forSelect = false)
19
    {
20
        if (!$forSelect) {
21
            return $this->getSnippets('payTypeHandler');
22
        }
23
        $handlers = ['' => 'Не выбрано'];
24
        foreach ($this->getSnippets('payTypeHandler') as $key => $handler) {
25
            if (empty($handler)) {
26
                continue;
27
            }
28
            $handlers[$key] = $handler['name'];
29
        }
30
        return $handlers;
31
    }
32
33
    public function cartPayRecive($data)
34
    {
35
        $cart = Ecommerce\Cart::get($data['pay']->data);
36
        if ($cart) {
37
            $payed = true;
38
            foreach ($cart->pays as $pay) {
39
                if ($pay->pay_status_id != 2) {
40
                    $payed = false;
41
                    break;
42
                }
43
            }
44
            $cart->payed = $payed;
45
            $cart->save();
46
        }
47
    }
48
49 View Code Duplication
    public function parseFields($data, $cart)
50
    {
51
        $fields = \Ecommerce\UserAdds\Field::getList();
52
        $name = '';
53
        foreach ($fields as $field) {
54
            if ($field->save && !empty($data[$field->id])) {
55
                $name .= htmlspecialchars($data[$field->id]) . ' ';
56
            }
57
        }
58
        $name = trim($name);
59
60
        $userAdds = Ecommerce\UserAdds::get([['user_id', $cart->user->id], ['name', $name]]);
61
        if (!$userAdds) {
62
            $userAdds = new Ecommerce\UserAdds();
63
            $userAdds->user_id = $cart->user->id;
64
            $userAdds->name = $name;
65
            $userAdds->save();
66
            foreach ($fields as $field) {
67
                if (!$field->save) {
68
                    continue;
69
                }
70
                $userAddsValue = new Ecommerce\UserAdds\Value();
71
                $userAddsValue->value = htmlspecialchars($data[$field->id]);
72
                $userAddsValue->useradds_field_id = $field->id;
73
                $userAddsValue->useradds_id = $userAdds->id;
74
                $userAddsValue->save();
75
            }
76
        }
77
        $user = \Users\User::get($cart->user_id);
78
        foreach ($fields as $field) {
79
            $info = new \Ecommerce\Cart\Info();
80
            $info->name = $field->name;
81
            $info->value = htmlspecialchars($data[$field->id]);
82
            $info->useradds_field_id = $field->id;
83
            $info->cart_id = $cart->id;
84
            $info->save();
85
            $relations = [];
86
            if ($field->userfield) {
87
                if (strpos($field->userfield, ':')) {
88
                    $path = explode(':', $field->userfield);
89
                    if (!$user->{$path[0]}->{$path[1]}) {
90
                        $user->{$path[0]}->{$path[1]} = $info->value;
91
                        $relations[$path[0]] = $path[0];
92
                    }
93
                } else {
94
                    if (!$user->{$field->userfield}) {
95
                        $user->{$field->userfield} = $info->value;
96
                    }
97
                }
98
            }
99
            foreach ($relations as $rel) {
100
                $user->$rel->save();
101
            }
102
            $user->save();
103
        }
104
        return $userAdds;
105
    }
106
107 View Code Duplication
    public function parseDeliveryFields($data, $cart, $fields)
108
    {
109
        $name = '';
110
        foreach ($fields as $field) {
111
            if ($field->save && !empty($data[$field->id])) {
112
                $name .= htmlspecialchars($data[$field->id]) . ' ';
113
            }
114
        }
115
        $name = trim($name);
116
117
        $save = Ecommerce\Delivery\Save::get([['user_id', $cart->user->id], ['name', $name]]);
118
        if (!$save) {
119
            $save = new Ecommerce\Delivery\Save();
120
            $save->user_id = $cart->user->id;
121
            $save->name = $name;
122
            $save->save();
123
            foreach ($fields as $field) {
124
                if (!$field->save) {
125
                    continue;
126
                }
127
                $saveValue = new Ecommerce\Delivery\Value();
128
                $saveValue->value = htmlspecialchars($data[$field->id]);
129
                $saveValue->delivery_field_id = $field->id;
130
                $saveValue->delivery_save_id = $save->id;
131
                $saveValue->save();
132
            }
133
        }
134
        $user = \Users\User::get($cart->user_id);
135
        foreach ($fields as $field) {
136
            $info = new \Ecommerce\Cart\DeliveryInfo();
137
            $info->name = $field->name;
138
            $info->value = htmlspecialchars($data[$field->id]);
139
            $info->delivery_field_id = $field->id;
140
            $info->cart_id = $cart->id;
141
            $info->save();
142
            $relations = [];
143
            if ($field->userfield) {
144
                if (strpos($field->userfield, ':')) {
145
                    $path = explode(':', $field->userfield);
146
                    if (!$user->{$path[0]}->{$path[1]}) {
147
                        $user->{$path[0]}->{$path[1]} = $info->value;
148
                        $relations[$path[0]] = $path[0];
149
                    }
150
                } else {
151
                    if (!$user->{$field->userfield}) {
152
                        $user->{$field->userfield} = $info->value;
153
                    }
154
                }
155
            }
156
            foreach ($relations as $rel) {
157
                $user->$rel->save();
158
            }
159
            $user->save();
160
        }
161
        return $save;
162
    }
163
164
    public function getCurCart($create = true)
1 ignored issue
show
Coding Style introduced by
getCurCart uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
165
    {
166
        $cart = false;
167
        if (!empty($_SESSION['cart']['cart_id'])) {
168
            $cart = Ecommerce\Cart::get((int) $_SESSION['cart']['cart_id']);
169
        }
170
        if (!$cart && $create) {
171
            $cart = new Ecommerce\Cart();
172
            $cart->cart_status_id = 1;
173
            $cart->user_id = Users\User::$cur->id;
174
            $userCard = \Ecommerce\Card\Item::get(\Users\User::$cur->id, 'user_id');
175
            if ($userCard) {
176
                $cart->card_item_id = $userCard->id;
177
            }
178
            $cart->save();
179
            $_SESSION['cart']['cart_id'] = $cart->id;
180
        }
181
        return $cart;
182
    }
183
184
    public function parseOptions($options = [])
185
    {
186
        $selectOptions = [
187
            'where' => !empty($options['where']) ? $options['where'] : [],
188
            'distinct' => false,
189
            'join' => [],
190
            'order' => [],
191
            'start' => isset($options['start']) ? (int) $options['start'] : 0,
192
            'key' => isset($options['key']) ? $options['key'] : null,
193
            'limit' => !empty($options['count']) ? (int) $options['count'] : 0,
194
        ];
195
        if (!empty($options['sort']) && is_array($options['sort'])) {
196
            foreach ($options['sort'] as $col => $direction) {
197
                switch ($col) {
198
                    case 'price':
199
                        $selectOptions['order'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
200
                        break;
201 View Code Duplication
                    case 'name':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
                        $selectOptions['order'][] = ['name', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
203
                        break;
204 View Code Duplication
                    case 'sales':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
                        $selectOptions['order'][] = ['sales', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
206
                        break;
207 View Code Duplication
                    case 'weight':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
                        $selectOptions['order'][] = ['weight', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
209
                        break;
210
                }
211
            }
212
        }
213
        $selectOptions['where'][] = ['deleted', 0];
214
        if (empty($this->config['view_empty_image'])) {
215
            $selectOptions['where'][] = ['image_file_id', 0, '!='];
216
        }
217
218
        $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner'];
219
220
        $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(),
221
            Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() .
222
            (empty($this->config['show_zero_price']) ? ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0' : ''),
223
            empty($this->config['show_without_price']) ? 'inner' : 'left'];
224
225
        $selectOptions['join'][] = [
226
            Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index()
227
        ];
228
229
        $selectOptions['where'][] = [
230
            [Ecommerce\Item\Offer\Price\Type::index(), NULL, 'is'],
231
            [
232
                [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'],
233
                [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'],
234
            ],
235
        ];
236
237
238
        if (!empty($this->config['view_filter'])) {
239
            if (!empty($this->config['view_filter']['options'])) {
240
                foreach ($this->config['view_filter']['options'] as $optionId => $optionValue) {
241
                    $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .
242
                        'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
243
                        'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"',
244
                        'inner', 'option' . $optionId];
245
                }
246
            }
247
        }
248
        //filters
249
        if (!empty($options['filters'])) {
250
            foreach ($options['filters'] as $col => $filter) {
251
                switch ($col) {
252
                    case 'price':
253 View Code Duplication
                        if (!empty($filter['min'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
                            $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>='];
255
                        }
256 View Code Duplication
                        if (!empty($filter['max'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
                            $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<='];
258
                        }
259
                        break;
260
                    case 'options':
261
                        foreach ($filter as $optionId => $optionValue) {
262
                            $optionId = (int) $optionId;
263
                            $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .
264
                                'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
265
                                'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = ' . \App::$cur->db->connection->pdo->quote($optionValue) . '',
266
                                'inner', 'option' . $optionId];
267
                        }
268
                        break;
269
                    case 'offerOptions':
270
                        //$selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = offer.' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'left', 'offer'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
271
                        foreach ($filter as $optionId => $optionValue) {
272
                            $optionId = (int) $optionId;
273
                            if (is_array($optionValue)) {
274
                                $optionValueArr = [];
275
                                foreach ($optionValue as $val) {
276
                                    $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val);
277
                                }
278
                                $qstr = 'IN (' . implode(',', $optionValueArr) . ')';
279
                            } else {
280
                                $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue);
281
                            }
282
                            $selectOptions['join'][] = [Ecommerce\Item\Offer\Param::table(), Ecommerce\Item\Offer::index() . ' = ' . 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer::index() . ' AND ' .
283
                                'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer\Option::index() . ' = "' . (int) $optionId . '" AND ' .
284
                                'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . 'value ' . $qstr,
285
                                'inner', 'offerOption' . $optionId];
286
                        }
287
                        break;
288
                }
289
            }
290
        }
291
        //parents
292
        if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) {
293
            $first = true;
294
            $where = [];
295
            foreach (explode(',', $options['parent']) as $categoryId) {
296
                if (!$categoryId) {
297
                    continue;
298
                }
299
                $category = \Ecommerce\Category::get($categoryId);
300
                $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR'];
301
                $first = false;
302
            }
303
            $selectOptions['where'][] = $where;
304
        } elseif (!empty($options['parent'])) {
305
            $category = \Ecommerce\Category::get($options['parent']);
306
            $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE'];
307
        }
308
309
        //search
310
        if (!empty($options['search'])) {
311
            $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']);
312
            $searchArr = [];
313
            foreach (explode(' ', $searchStr) as $part) {
314
                $part = trim($part);
315
                if ($part && strlen($part) > 2) {
316
                    $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE'];
317
                }
318
            }
319
            if (!empty($searchArr)) {
320
                $selectOptions['where'][] = $searchArr;
321
            }
322
        }
323
        if (empty($this->config['view_empty_warehouse'])) {
324
            $warehouseIds = [];
325 View Code Duplication
            if (class_exists('Geography\City\Data')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
326
                $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]);
327
                if ($warehouses && $warehouses->data) {
328
                    foreach (explode(',', $warehouses->data) as $id) {
329
                        $warehouseIds[$id] = $id;
330
                    }
331
                }
332
            }
333
            $selectOptions['where'][] = [
334
                '(
335
          (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) 
336
            FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw 
337
            WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . '
338
                ' . ($warehouseIds ? ' AND iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . '
339
            )
340
          -
341
          (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0)
342
            FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb
343
            inner JOIN ' . \App::$cur->db->table_prefix . \Ecommerce\Cart::table() . ' icc ON icc.' . \Ecommerce\Cart::index() . ' = iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index() . ' AND (
344
                (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) ||
345
                (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))
346
            )
347
            WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ')
348
          )',
349
                0,
350
                '>'
351
            ];
352
        }
353
354
355
356
357
358
359
        $selectOptions['group'] = Ecommerce\Item::index();
360
361
        return $selectOptions;
362
    }
363
364
    /**
365
     * Getting items params with params
366
     * 
367
     * @param array $params
368
     * @return array
369
     */
370
    public function getItemsParams($params = [])
371
    {
372
        $selectOptions = $this->parseOptions($params);
373
        $items = Ecommerce\Item::getList($selectOptions);
374
        $items = Ecommerce\Item\Param::getList([
375
                    'where' => ['item_id', array_keys($items), 'IN'],
376
                    'join' => [[Ecommerce\Item\Option::table(), Ecommerce\Item\Option::index() . ' = ' . \Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' and ' . \Ecommerce\Item\Option::colPrefix() . 'searchable = 1', 'inner']],
377
                    'distinct' => \Ecommerce\Item\Option::index()
378
        ]);
379
        return $items;
380
    }
381
382
    /**
383
     * Getting items with params
384
     * 
385
     * @param array $params
386
     * @return array
387
     */
388
    public function getItems($params = [])
389
    {
390
        $selectOptions = $this->parseOptions($params);
391
        $items = Ecommerce\Item::getList($selectOptions);
392
        return $items;
393
    }
394
395
    /**
396
     * Return count of items with params
397
     * 
398
     * @param array $params
399
     * @return int
400
     */
401
    public function getItemsCount($params = [])
402
    {
403
        $selectOptions = $this->parseOptions($params);
404
        $selectOptions['distinct'] = \Ecommerce\Item::index();
405
        $counts = Ecommerce\Item::getCount($selectOptions);
406
        if (is_array($counts)) {
407
            $sum = 0;
408
            foreach ($counts as $count) {
409
                $sum +=$count['count'];
410
            }
411
            return $sum;
412
        }
413
        return $counts;
414
    }
415
416 View Code Duplication
    public function viewsCategoryList($inherit = true)
417
    {
418
        $return = [];
419
        if ($inherit) {
420
            $return['inherit'] = 'Как у родителя';
421
        }
422
        $return['itemList'] = 'Список товаров';
423
        $conf = App::$primary->view->template->config;
424
        if (!empty($conf['files']['modules']['Ecommerce'])) {
425
            foreach ($conf['files']['modules']['Ecommerce'] as $file) {
426
                if ($file['type'] == 'Category') {
427
                    $return[$file['file']] = $file['name'];
428
                }
429
            }
430
        }
431
        return $return;
432
    }
433
434 View Code Duplication
    public function templatesCategoryList()
435
    {
436
        $return = [
437
            'inherit' => 'Как у родителя',
438
            'current' => 'Текущая тема'
439
        ];
440
441
        $conf = App::$primary->view->template->config;
442
443
        if (!empty($conf['files']['aditionTemplateFiels'])) {
444
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
445
                $return[$file['file']] = '- ' . $file['name'];
446
            }
447
        }
448
        return $return;
449
    }
450
451
    public function cartStatusDetector($event)
452
    {
453
        $cart = $event['eventObject'];
454
        if (!empty($cart->_changedParams['cart_cart_status_id'])) {
455
            $cart->date_status = date('Y-m-d H:i:s');
456
            $event = new Ecommerce\Cart\Event(['cart_id' => $cart->id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 5, 'info' => $cart->cart_status_id]);
457
            $event->save();
458
459
            $prev_status_id = $cart->_changedParams['cart_cart_status_id'];
460
            $now_status_id = $cart->cart_status_id;
461
462
            $status = Ecommerce\Cart\Status::getList(['where' => ['id', implode(',', [$prev_status_id, $now_status_id]), 'IN']]);
463
464
            $prefix = isset(App::$cur->ecommerce->config['orderPrefix']) ? $config = App::$cur->ecommerce->config['orderPrefix'] : '';
465
            \App::$cur->users->AddUserActivity($cart->user_id, 3, "Статус вашего заказа номер {$prefix}{$cart->id} изменился с {$status[$prev_status_id]->name} на {$status[$now_status_id]->name}");
466
467
            if ($cart->cart_status_id == 5) {
468
                Inji::$inst->event('ecommerceCartClosed', $cart);
469
            }
470
        }
471
        return $cart;
472
    }
473
474
    public function cardTrigger($event)
475
    {
476
        $cart = $event['eventObject'];
477
        if ($cart->card) {
478
            $sum = 0;
479
            foreach ($cart->cartItems as $cartItem) {
480
                $sum += $cartItem->final_price * $cartItem->count;
481
            }
482
            $cardItemHistory = new Ecommerce\Card\Item\History();
483
            $cardItemHistory->amount = $sum;
484
            $cardItemHistory->card_item_id = $cart->card_item_id;
485
            $cardItemHistory->save();
486
            $cart->card->sum += $sum;
487
            $cart->card->save();
488
        }
489
        return $cart;
490
    }
491
492
    public function bonusTrigger($event)
493
    {
494
        $cart = $event['eventObject'];
495
        foreach ($cart->cartItems as $cartItem) {
496
            foreach ($cartItem->price->offer->bonuses as $bonus) {
497
                if ($bonus->limited && $bonus->left <= 0) {
498
                    continue;
499
                } elseif ($bonus->limited && $bonus->left > 0) {
500
                    $bonus->left -= 1;
501
                    $bonus->save();
502
                }
503
                switch ($bonus->type) {
504
                    case'currency':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
505
                        $currency = \Money\Currency::get($bonus->value);
506
                        $wallets = App::$cur->money->getUserWallets($cart->user->id);
507
                        $wallets[$currency->id]->diff($bonus->count, 'Бонус за покупку');
508
                        break;
509
                }
510
            }
511
        }
512
        return $cart;
513
    }
514
515 View Code Duplication
    function sitemap()
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
sitemap uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
516
    {
517
        $map = [];
518
        $zeroItems = \Ecommerce\Item::getList(['where' => ['category_id', 0]]);
519
        foreach ($zeroItems as $item) {
520
            $map[] = [
521
                'name' => $item->name,
522
                'url' => [
523
                    'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($item->getHref())
524
                ],
525
            ];
526
        }
527
528
        $categorys = \Ecommerce\Category::getList(['where' => ['parent_id', 0]]);
529
        $scan = function($category, $scan) {
530
            $map = [];
531
532
            foreach ($category->items as $item) {
533
                $map[] = [
534
                    'name' => $item->name,
535
                    'url' => [
536
                        'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($item->getHref())
537
                    ],
538
                ];
539
            }
540
            foreach ($category->catalogs as $child) {
541
                $map = array_merge($map, $scan($child, $scan));
542
            }
543
            return $map;
544
        };
545
        foreach ($categorys as $category) {
546
            $map = array_merge($map, $scan($category, $scan));
547
        }
548
        return $map;
549
    }
550
551
}
552