Completed
Push — master ( 55a9f3...13acd2 )
by Alexey
05:06
created

Item::getPrice()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 17
rs 7.7777
c 1
b 0
f 1
cc 8
eloc 11
nc 4
nop 0
1
<?php
2
3
/**
4
 * Item
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
12
namespace Ecommerce;
13
14
class Item extends \Model
15
{
16
    public static $categoryModel = 'Ecommerce\Category';
17
    public static $objectName = 'Товар';
18
    public static $labels = [
19
        'name' => 'Название',
20
        'alias' => 'Алиас',
21
        'category_id' => 'Раздел',
22
        'description' => 'Описание',
23
        'item_type_id' => 'Тип товара',
24
        'image_file_id' => 'Изображение',
25
        'best' => 'Лучшее предложение',
26
        'options' => 'Параметры',
27
        'offers' => 'Торговые предложения',
28
    ];
29
    public static $cols = [
30
        'name' => ['type' => 'text'],
31
        'alias' => ['type' => 'text'],
32
        'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'],
33
        'description' => ['type' => 'html'],
34
        'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
35
        'image_file_id' => ['type' => 'image'],
36
        'best' => ['type' => 'bool'],
37
        'options' => ['type' => 'dataManager', 'relation' => 'options'],
38
        'offers' => ['type' => 'dataManager', 'relation' => 'offers'],
39
        'weight' => ['type' => 'number']
40
    ];
41
    public static $dataManagers = [
42
        'manager' => [
43
            'name' => 'Товары',
44
            'cols' => [
45
                'name',
46
                'category_id',
47
                'item_type_id',
48
                'best',
49
                'options',
50
                'offers',
51
            ],
52
            'categorys' => [
53
                'model' => 'Ecommerce\Category',
54
            ],
55
            'sortMode'=>true
56
        ]
57
    ];
58
    public static $forms = [
59
        'manager' => [
60
            'map' => [
61
                ['name', 'alias'],
62
                ['category_id', 'item_type_id'],
63
                ['best', 'image_file_id'],
64
                ['description'],
65
                ['options'],
66
                ['offers'],
67
            ]
68
    ]];
69
70
    public function beforeSave()
71
    {
72
73
        if ($this->id) {
74
            $this->search_index = $this->name . ' ';
75
            if ($this->category) {
76
                $this->search_index .= $this->category->name . ' ';
77
            }
78
            if ($this->options) {
79
                foreach ($this->options as $option) {
80
                    if ($option->item_option_searchable && $option->value) {
81
                        if ($option->item_option_type != 'select') {
82
                            $this->search_index .= $option->value . ' ';
83
                        } elseif (!empty($option->option->items[$option->value])) {
84
                            $option->option->items[$option->value]->value . ' ';
85
                        }
86
                    }
87
                }
88
            }
89
        }
90
    }
91
92
    public static function relations()
93
    {
94
95
        return [
96
            'category' => [
97
                'model' => 'Ecommerce\Category',
98
                'col' => 'category_id'
99
            ],
100
            'options' => [
101
                'type' => 'many',
102
                'model' => 'Ecommerce\Item\Param',
103
                'col' => 'item_id',
104
                //'resultKey' => 'code',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
105
                'resultKey' => 'item_option_id',
106
                'join' => [Item\Option::table(), Item\Option::index() . ' = ' . Item\Param::colPrefix() . Item\Option::index()]
107
            ],
108
            'offers' => [
109
                'type' => 'many',
110
                'model' => 'Ecommerce\Item\Offer',
111
                'col' => 'item_id',
112
            ],
113
            'type' => [
114
                'model' => 'Ecommerce\Item\Type',
115
                'col' => 'item_type_id',
116
            ],
117
            'image' => [
118
                'model' => 'Files\File',
119
                'col' => 'image_file_id'
120
            ]
121
        ];
122
    }
123
124
    public function getPrice()
125
    {
126
        $offers = $this->offers(['key' => false]);
127
        $curPrice = null;
128
129
        foreach ($offers[0]->prices as $price) {
130
            if (!$price->type) {
131
                $curPrice = $price;
132
            } elseif (
133
                    (!$price->type->roles && !$curPrice) ||
134
                    ($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false)
135
            ) {
136
                $curPrice = $price;
137
            }
138
        }
139
        return $curPrice;
140
    }
141
142
    public function name()
143
    {
144
        if (!empty(\App::$primary->ecommerce->config['item_option_as_name'])) {
145
            $param = Item\Param::get([['item_id', $this->id], ['item_option_id', \App::$primary->ecommerce->config['item_option_as_name']]]);
146
            if ($param && $param->value) {
147
                return $param->value;
148
            }
149
        }
150
        return $this->name;
151
    }
152
153
    public function beforeDelete()
154
    {
155
        if ($this->id) {
156
            if ($this->options) {
157
                foreach ($this->options as $option) {
158
                    $option->delete();
159
                }
160
            }
161
            if ($this->offers) {
162
                foreach ($this->offers as $offer) {
163
                    $offer->delete();
164
                }
165
            }
166
            if ($this->image) {
167
                $this->image->delete();
168
            }
169
        }
170
    }
171
172
}
173