Completed
Push — master ( d4133d...2ce86d )
by Alexey
04:49
created

Ecommerce::cartPayRecive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 14
rs 9.2
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
    App::$primary->view->customAsset('js', '/moduleAsset/Ecommerce/js/cart.js');
15
  }
16
17
  public function getPayTypeHandlers($forSelect = false) {
18
    if (!$forSelect) {
19
      return $this->getSnippets('payTypeHandler');
20
    }
21
    $handlers = ['' => 'Не выбрано'];
22
    foreach ($this->getSnippets('payTypeHandler') as $key => $handler) {
23
      if (empty($handler)) {
24
        continue;
25
      }
26
      $handlers[$key] = $handler['name'];
27
    }
28
    return $handlers;
29
  }
30
31
  public function cartPayRecive($data) {
32
    $cart = Ecommerce\Cart::get($data['pay']->data);
33
    if ($cart) {
34
      $payed = true;
35
      foreach ($cart->pays as $pay) {
36
        if ($pay->pay_status_id != 2) {
37
          $payed = false;
38
          break;
39
        }
40
      }
41
      $cart->payed = $payed;
42
      $cart->save();
43
    }
44
  }
45
46 View Code Duplication
  public function parseFields($data, $cart) {
47
    $fields = \Ecommerce\UserAdds\Field::getList();
48
    $name = '';
49
    foreach ($fields as $field) {
50
      if ($field->save && !empty($data[$field->id])) {
51
        $name .= htmlspecialchars($data[$field->id]) . ' ';
52
      }
53
    }
54
    $name = trim($name);
55
56
    $userAdds = Ecommerce\UserAdds::get([['user_id', $cart->user->id], ['name', $name]]);
57
    if (!$userAdds) {
58
      $userAdds = new Ecommerce\UserAdds();
59
      $userAdds->user_id = $cart->user->id;
60
      $userAdds->name = $name;
61
      $userAdds->save();
62
      foreach ($fields as $field) {
63
        if (!$field->save) {
64
          continue;
65
        }
66
        $userAddsValue = new Ecommerce\UserAdds\Value();
67
        $userAddsValue->value = htmlspecialchars($data[$field->id]);
68
        $userAddsValue->useradds_field_id = $field->id;
69
        $userAddsValue->useradds_id = $userAdds->id;
70
        $userAddsValue->save();
71
      }
72
    }
73
    $user = \Users\User::get($cart->user_id);
74
    foreach ($fields as $field) {
75
      $info = new \Ecommerce\Cart\Info();
76
      $info->name = $field->name;
77
      $info->value = htmlspecialchars($data[$field->id]);
78
      $info->useradds_field_id = $field->id;
79
      $info->cart_id = $cart->id;
80
      $info->save();
81
      $relations = [];
82
      if ($field->userfield) {
83
        if (strpos($field->userfield, ':')) {
84
          $path = explode(':', $field->userfield);
85
          if (!$user->{$path[0]}->{$path[1]}) {
86
            $user->{$path[0]}->{$path[1]} = $info->value;
87
            $relations[$path[0]] = $path[0];
88
          }
89
        } else {
90
          if (!$user->{$field->userfield}) {
91
            $user->{$field->userfield} = $info->value;
92
          }
93
        }
94
      }
95
      foreach ($relations as $rel) {
96
        $user->$rel->save();
97
      }
98
      $user->save();
99
    }
100
    return $userAdds;
101
  }
102
103 View Code Duplication
  public function parseDeliveryFields($data, $cart, $fields) {
104
    $name = '';
105
    foreach ($fields as $field) {
106
      if ($field->save && !empty($data[$field->id])) {
107
        $name .= htmlspecialchars($data[$field->id]) . ' ';
108
      }
109
    }
110
    $name = trim($name);
111
112
    $save = Ecommerce\Delivery\Save::get([['user_id', $cart->user->id], ['name', $name]]);
113
    if (!$save) {
114
      $save = new Ecommerce\Delivery\Save();
115
      $save->user_id = $cart->user->id;
116
      $save->name = $name;
117
      $save->save();
118
      foreach ($fields as $field) {
119
        if (!$field->save) {
120
          continue;
121
        }
122
        $saveValue = new Ecommerce\Delivery\Value();
123
        $saveValue->value = htmlspecialchars($data[$field->id]);
124
        $saveValue->delivery_field_id = $field->id;
125
        $saveValue->delivery_save_id = $save->id;
126
        $saveValue->save();
127
      }
128
    }
129
    $user = \Users\User::get($cart->user_id);
130
    foreach ($fields as $field) {
131
      $info = new \Ecommerce\Cart\DeliveryInfo();
132
      $info->name = $field->name;
133
      $info->value = htmlspecialchars($data[$field->id]);
134
      $info->delivery_field_id = $field->id;
135
      $info->cart_id = $cart->id;
136
      $info->save();
137
      $relations = [];
138
      if ($field->userfield) {
139
        if (strpos($field->userfield, ':')) {
140
          $path = explode(':', $field->userfield);
141
          if (!$user->{$path[0]}->{$path[1]}) {
142
            $user->{$path[0]}->{$path[1]} = $info->value;
143
            $relations[$path[0]] = $path[0];
144
          }
145
        } else {
146
          if (!$user->{$field->userfield}) {
147
            $user->{$field->userfield} = $info->value;
148
          }
149
        }
150
      }
151
      foreach ($relations as $rel) {
152
        $user->$rel->save();
153
      }
154
      $user->save();
155
    }
156
    return $save;
157
  }
158
159
  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...
160
    $cart = false;
161
    if (!empty($_SESSION['cart']['cart_id'])) {
162
      $cart = Ecommerce\Cart::get((int) $_SESSION['cart']['cart_id']);
163
    }
164
    if (!$cart && $create) {
165
      $cart = new Ecommerce\Cart();
166
      $cart->cart_status_id = 1;
167
      $cart->user_id = Users\User::$cur->id;
168
      $userCard = \Ecommerce\Card\Item::get(\Users\User::$cur->id, 'user_id');
169
      if ($userCard) {
170
        $cart->card_item_id = $userCard->id;
171
      }
172
      $cart->save();
173
      $_SESSION['cart']['cart_id'] = $cart->id;
174
    }
175
    return $cart;
176
  }
177
178
  public function parseOptions($options = []) {
179
    $selectOptions = [
180
        'where' => !empty($options['where']) ? $options['where'] : [],
181
        'distinct' => false,
182
        'join' => [],
183
        'order' => [],
184
        'start' => isset($options['start']) ? (int) $options['start'] : 0,
185
        'key' => isset($options['key']) ? $options['key'] : null,
186
        'limit' => !empty($options['count']) ? (int) $options['count'] : 0,
187
    ];
188
    if (!empty($options['sort']) && is_array($options['sort'])) {
189
      foreach ($options['sort'] as $col => $direction) {
190
        switch ($col) {
191
          case 'price':
192
            $selectOptions['order'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
193
            break;
194 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...
195
            $selectOptions['order'][] = ['name', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
196
            break;
197 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...
198
            $selectOptions['order'][] = ['sales', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
199
            break;
200 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...
201
            $selectOptions['order'][] = ['weight', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
202
            break;
203
        }
204
      }
205
    }
206
    $selectOptions['where'][] = ['deleted', 0];
207
    if (empty($this->config['view_empty_image'])) {
208
      $selectOptions['where'][] = ['image_file_id', 0, '!='];
209
    }
210
211
    $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner'];
212
213
    $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(),
214
        Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() .
215
        (empty($this->config['show_zero_price']) ? ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0' : ''),
216
        empty($this->config['show_without_price']) ? 'inner' : 'left'];
217
218
    $selectOptions['join'][] = [
219
        Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index()
220
    ];
221
222
    $selectOptions['where'][] = [
223
        [Ecommerce\Item\Offer\Price\Type::index(), NULL, 'is'],
224
        [
225
            [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'],
226
            [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'],
227
        ],
228
    ];
229
230
231
    if (!empty($this->config['view_filter'])) {
232
      if (!empty($this->config['view_filter']['options'])) {
233
        foreach ($this->config['view_filter']['options'] as $optionId => $optionValue) {
234
          $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .
235
              'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
236
              'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"',
237
              'inner', 'option' . $optionId];
238
        }
239
      }
240
    }
241
    //filters
242
    if (!empty($options['filters'])) {
243
      foreach ($options['filters'] as $col => $filter) {
244
        switch ($col) {
245
          case 'price':
246 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...
247
              $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>='];
248
            }
249 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...
250
              $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<='];
251
            }
252
            break;
253 View Code Duplication
          case 'options':
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
            foreach ($filter as $optionId => $optionValue) {
255
              $optionId = (int) $optionId;
256
              if (is_array($optionValue)) {
257
                $optionValueArr = [];
258
                foreach ($optionValue as $val) {
259
                  $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val);
260
                }
261
                $qstr = 'IN (' . implode(',', $optionValueArr) . ')';
262
              } else {
263
                $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue);
264
              }
265
              $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .
266
                  'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
267
                  'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value ' . $qstr . '',
268
                  'inner', 'option' . $optionId];
269
            }
270
            break;
271 View Code Duplication
          case 'offerOptions':
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...
272
            //$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...
273
            foreach ($filter as $optionId => $optionValue) {
274
              $optionId = (int) $optionId;
275
              if (is_array($optionValue)) {
276
                $optionValueArr = [];
277
                foreach ($optionValue as $val) {
278
                  $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val);
279
                }
280
                $qstr = 'IN (' . implode(',', $optionValueArr) . ')';
281
              } else {
282
                $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue);
283
              }
284
              $selectOptions['join'][] = [Ecommerce\Item\Offer\Param::table(), Ecommerce\Item\Offer::index() . ' = ' . 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer::index() . ' AND ' .
285
                  'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer\Option::index() . ' = "' . (int) $optionId . '" AND ' .
286
                  'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . 'value ' . $qstr,
287
                  'inner', 'offerOption' . $optionId];
288
            }
289
            break;
290
        }
291
      }
292
    }
293
    //parents
294
    if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) {
295
      $first = true;
296
      $where = [];
297
      foreach (explode(',', $options['parent']) as $categoryId) {
298
        if (!$categoryId) {
299
          continue;
300
        }
301
        $category = \Ecommerce\Category::get($categoryId);
302
        $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR'];
303
        $first = false;
304
      }
305
      $selectOptions['where'][] = $where;
306
    } elseif (!empty($options['parent'])) {
307
      $category = \Ecommerce\Category::get($options['parent']);
308
      $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE'];
309
    }
310
311
    //search
312
    if (!empty($options['search'])) {
313
      $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']);
314
      $searchArr = [];
315
      foreach (explode(' ', $searchStr) as $part) {
316
        $part = trim($part);
317
        if ($part && strlen($part) > 2) {
318
          $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE'];
319
        }
320
      }
321
      if (!empty($searchArr)) {
322
        $selectOptions['where'][] = $searchArr;
323
      }
324
    }
325
    if (empty($this->config['view_empty_warehouse'])) {
326
      $warehouseIds = [];
327 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...
328
        $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]);
329
        if ($warehouses && $warehouses->data) {
330
          foreach (explode(',', $warehouses->data) as $id) {
331
            $warehouseIds[$id] = $id;
332
          }
333
        }
334
      }
335
      $selectOptions['where'][] = [
336
          '(
337
          (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) 
338
            FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw 
339
            WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . '
340
                ' . ($warehouseIds ? ' AND iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . '
341
            )
342
          -
343
          (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0)
344
            FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb
345
            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 (
346
                (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) ||
347
                (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))
348
            )
349
            WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ')
350
          )',
351
          0,
352
          '>'
353
      ];
354
    }
355
356
357
358
359
360
361
    $selectOptions['group'] = Ecommerce\Item::index();
362
363
    return $selectOptions;
364
  }
365
366
  /**
367
   * Getting items params with params
368
   * 
369
   * @param array $params
370
   * @return array
371
   */
372
  public function getItemsParams($params = []) {
373
    $selectOptions = $this->parseOptions($params);
374
    $items = Ecommerce\Item::getList($selectOptions);
375
    $items = Ecommerce\Item\Param::getList([
376
                'where' => ['item_id', array_keys($items), 'IN'],
377
                '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']],
378
                'distinct' => \Ecommerce\Item\Option::index()
379
    ]);
380
    return $items;
381
  }
382
383
  /**
384
   * Getting items with params
385
   * 
386
   * @param array $params
387
   * @return array
388
   */
389
  public function getItems($params = []) {
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
    $selectOptions = $this->parseOptions($params);
403
    $selectOptions['distinct'] = \Ecommerce\Item::index();
404
    $counts = Ecommerce\Item::getCount($selectOptions);
405
    if (is_array($counts)) {
406
      $sum = 0;
407
      foreach ($counts as $count) {
408
        $sum +=$count['count'];
409
      }
410
      return $sum;
411
    }
412
    return $counts;
413
  }
414
415 View Code Duplication
  public function viewsCategoryList($inherit = true) {
416
    $return = [];
417
    if ($inherit) {
418
      $return['inherit'] = 'Как у родителя';
419
    }
420
    $return['itemList'] = 'Список товаров';
421
    $conf = App::$primary->view->template->config;
422
    if (!empty($conf['files']['modules']['Ecommerce'])) {
423
      foreach ($conf['files']['modules']['Ecommerce'] as $file) {
424
        if ($file['type'] == 'Category') {
425
          $return[$file['file']] = $file['name'];
426
        }
427
      }
428
    }
429
    return $return;
430
  }
431
432 View Code Duplication
  public function templatesCategoryList() {
433
    $return = [
434
        'inherit' => 'Как у родителя',
435
        'current' => 'Текущая тема'
436
    ];
437
438
    $conf = App::$primary->view->template->config;
439
440
    if (!empty($conf['files']['aditionTemplateFiels'])) {
441
      foreach ($conf['files']['aditionTemplateFiels'] as $file) {
442
        $return[$file['file']] = '- ' . $file['name'];
443
      }
444
    }
445
    return $return;
446
  }
447
448
  public function cartStatusDetector($event) {
449
    $cart = $event['eventObject'];
450
    if (!empty($cart->_changedParams['cart_cart_status_id'])) {
451
      $cart->date_status = date('Y-m-d H:i:s');
452
      $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]);
453
      $event->save();
454
455
      $prev_status_id = $cart->_changedParams['cart_cart_status_id'];
456
      $now_status_id = $cart->cart_status_id;
457
458
      $status = Ecommerce\Cart\Status::getList(['where' => ['id', implode(',', [$prev_status_id, $now_status_id]), 'IN']]);
459
460
      $prefix = isset(App::$cur->ecommerce->config['orderPrefix']) ? $config = App::$cur->ecommerce->config['orderPrefix'] : '';
461
      \App::$cur->users->AddUserActivity($cart->user_id, 3, "Статус вашего заказа номер {$prefix}{$cart->id} изменился с {$status[$prev_status_id]->name} на {$status[$now_status_id]->name}");
462
463
      if ($cart->cart_status_id == 5) {
464
        Inji::$inst->event('ecommerceCartClosed', $cart);
465
      }
466
    }
467
    return $cart;
468
  }
469
470
  public function cardTrigger($event) {
471
    $cart = $event['eventObject'];
472
    if ($cart->card) {
473
      $sum = 0;
474
      foreach ($cart->cartItems as $cartItem) {
475
        $sum += $cartItem->final_price * $cartItem->count;
476
      }
477
      $cardItemHistory = new Ecommerce\Card\Item\History();
478
      $cardItemHistory->amount = $sum;
479
      $cardItemHistory->card_item_id = $cart->card_item_id;
480
      $cardItemHistory->save();
481
      $cart->card->sum += $sum;
482
      $cart->card->save();
483
    }
484
    return $cart;
485
  }
486
487
  public function bonusTrigger($event) {
488
    $cart = $event['eventObject'];
489
    foreach ($cart->cartItems as $cartItem) {
490
      foreach ($cartItem->price->offer->bonuses as $bonus) {
491
        if ($bonus->limited && $bonus->left <= 0) {
492
          continue;
493
        } elseif ($bonus->limited && $bonus->left > 0) {
494
          $bonus->left -= 1;
495
          $bonus->save();
496
        }
497
        switch ($bonus->type) {
498
          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...
499
            $currency = \Money\Currency::get($bonus->value);
500
            $wallets = App::$cur->money->getUserWallets($cart->user->id);
501
            $wallets[$currency->id]->diff($bonus->count, 'Бонус за покупку');
502
            break;
503
        }
504
      }
505
    }
506
    return $cart;
507
  }
508
509 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...
510
    $map = [];
511
    $zeroItems = \Ecommerce\Item::getList(['where' => ['category_id', 0]]);
512
    foreach ($zeroItems as $item) {
513
      $map[] = [
514
          'name' => $item->name,
515
          'url' => [
516
              'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($item->getHref())
517
          ],
518
      ];
519
    }
520
521
    $categorys = \Ecommerce\Category::getList(['where' => ['parent_id', 0]]);
522
    $scan = function($category, $scan) {
523
      $map = [];
524
525
      foreach ($category->items as $item) {
526
        $map[] = [
527
            'name' => $item->name,
528
            'url' => [
529
                'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($item->getHref())
530
            ],
531
        ];
532
      }
533
      foreach ($category->catalogs as $child) {
534
        $map = array_merge($map, $scan($child, $scan));
535
      }
536
      return $map;
537
    };
538
    foreach ($categorys as $category) {
539
      $map = array_merge($map, $scan($category, $scan));
540
    }
541
    return $map;
542
  }
543
544
}
545