Test Failed
Push — master ( 45bc78...882099 )
by Alexey
05:03
created

Category::calcItemsCount()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Item Category
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
 * @property int $id
15
 * @property int $parent_id
16
 * @property string $name
17
 * @property string $alias
18
 * @property string $viewer
19
 * @property string $template
20
 * @property string $description
21
 * @property int $image_file_id
22
 * @property int $icon_file_id
23
 * @property bool $options_inherit
24
 * @property bool $hidden
25
 * @property int $views
26
 * @property bool $imported
27
 * @property int $weight
28
 * @property int $user_id
29
 * @property int $items_count
30
 * @property string $tree_path
31
 * @property string $date_create
32
 *
33
 * @property-read \Ecommerce\Item[] $items
34
 * @property-read \Ecommerce\Category $parent
35
 * @property-read \Ecommerce\Item\Option[] $options
36
 * @property-read \Files\File $image
37
 * @property-read \Files\File $icon
38
 * @property-read \Ecommerce\Category[] $catalogs
39
 * @property-read \Users\User $user
40
 */
41
class Category extends \Model {
42
43
    public static $objectName = 'Категория магазина';
44
    public static $treeCategory = 'Ecommerce\Item';
45
    public static $cols = [
46
        //Основные параметры
47
        'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent'],
48
        'name' => ['type' => 'text'],
49
        'alias' => ['type' => 'text'],
50
        'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsCategoryList', 'module' => 'Ecommerce'],
51
        'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesCategoryList', 'module' => 'Ecommerce'],
52
        'description' => ['type' => 'html'],
53
        'image_file_id' => ['type' => 'image'],
54
        'icon_file_id' => ['type' => 'image'],
55
        'options_inherit' => ['type' => 'bool'],
56
        'hidden' => ['type' => 'bool'],
57
        //Системные
58
        'views' => ['type' => 'number', 'logging' => false],
59
        'imported' => ['type' => 'bool'],
60
        'weight' => ['type' => 'number'],
61
        'items_count' => ['type' => 'number'],
62
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
63
        'tree_path' => ['type' => 'text'],
64
        'date_create' => ['type' => 'dateTime'],
65
        //Менеджеры
66
        'options' => ['type' => 'dataManager', 'relation' => 'options'],
67
    ];
68
    public static $labels = [
69
        'name' => 'Название',
70
        'alias' => 'Алиас',
71
        'parent_id' => 'Родительская категория',
72
        'icon_file_id' => 'Иконка',
73
        'image_file_id' => 'Изображение',
74
        'description' => 'Описание',
75
        'options_inherit' => 'Наследовать набор свойств',
76
        'options' => 'Свойства товаров',
77
        'hidden' => 'Скрытая'
78
    ];
79
    public static $forms = [
80
        'manager' => [
81
            'map' => [
82
                ['name', 'alias'],
83
                ['parent_id', 'image_file_id', 'icon_file_id'],
84
                ['viewer', 'template'],
85
                ['options_inherit', 'hidden'],
86
                ['options'],
87
                ['description']
88
            ]
89
        ]
90
    ];
91
92
    public static function indexes() {
93
        return [
94
            'ecommerce_category_category_parent_id' => [
95
                'type' => 'INDEX',
96
                'cols' => [
97
                    'category_parent_id',
98
                ]
99
            ],
100
            'ecommerce_category_category_tree_path' => [
101
                'type' => 'INDEX',
102
                'cols' => [
103
                    'category_tree_path(255)'
104
                ]
105
            ],
106
        ];
107
    }
108
109
    public static function relations() {
110
        return [
111
            'items' => [
112
                'type' => 'many',
113
                'model' => 'Ecommerce\Item',
114
                'col' => 'category_id',
115
            ],
116
            'parent' => [
117
                'model' => 'Ecommerce\Category',
118
                'col' => 'parent_id'
119
            ],
120
            'options' => [
121
                'type' => 'relModel',
122
                'model' => 'Ecommerce\Item\Option',
123
                'relModel' => 'Ecommerce\Item\Option\Relation',
124
            ],
125
            'image' => [
126
                'model' => 'Files\File',
127
                'col' => 'image_file_id'
128
            ],
129
            'icon' => [
130
                'model' => 'Files\File',
131
                'col' => 'icon_file_id'
132
            ],
133
            'user' => [
134
                'model' => 'Users\User',
135
                'col' => 'user_id'
136
            ],
137
            'catalogs' => [
138
                'type' => 'many',
139
                'model' => 'Ecommerce\Category',
140
                'col' => 'parent_id',
141
            ]
142
        ];
143
    }
144
145
    public static $dataManagers = [
146
        'manager' => [
147
            'name' => 'Категории товаров',
148
            'cols' => [
149
                'name',
150
                'image_file_id',
151
                'parent_id',
152
            ],
153
            'sortMode' => true
154
        ]
155
    ];
156
157 View Code Duplication
    public function getRoot() {
158
        $treePath = array_values(array_filter(explode('/', $this->tree_path)));
159
        if (!empty($treePath[0])) {
160
            $category = Category::get($treePath[0]);
161
            if ($category) {
162
                return $category;
163
            }
164
        }
165
        return $this;
166
    }
167
168
    public function beforeSave() {
169
        if ($this->id && $this->id == $this->parent_id) {
170
            $this->parent_id = 0;
171
            \Msg::add('Категория не может быть сама себе родителем');
172
        }
173
    }
174
175
    public function beforeDelete() {
176
        foreach ($this->catalogs as $category) {
177
            $category->delete();
178
        }
179
    }
180
181 View Code Duplication
    public function resolveTemplate() {
182
        if ($this->template !== 'inherit') {
183
            return $this->template;
184
        } elseif ($this->template == 'inherit' && $this->category) {
185
            return $this->category->resolveTemplate(true);
186
        } else {
187
            return (!empty(\App::$cur->ecommerce->config['defaultCategoryTemplate']) ? \App::$cur->ecommerce->config['defaultCategoryTemplate'] : 'current');
188
        }
189
    }
190
191 View Code Duplication
    public function resolveViewer() {
192
        if ($this->viewer !== 'inherit') {
193
            return $this->viewer;
194
        } elseif ($this->viewer == 'inherit' && $this->category) {
195
            return $this->category->resolveViewer(true);
196
        } else {
197
            return (!empty(\App::$cur->ecommerce->config['defaultCategoryView']) ? \App::$cur->ecommerce->config['defaultCategoryView'] : 'itemList');
198
        }
199
    }
200
201
    public function calcItemsCount($save = true) {
202
        $count = \App::$cur->Ecommerce->getItemsCount(['parent' => $this->id]);
203
        $this->items_count = $count;
204
        if ($save) {
205
            $this->save();
206
            if ($this->parent) {
207
                $this->parent->calcItemsCount();
208
            }
209
            foreach (\Ecommerce\Catalog::getList(['categories:category_id', $this->id]) as $category) {
210
                $category->calcItemsCount();
211
            }
212
        }
213
        return $count;
214
    }
215
}