Test Failed
Push — master ( 893e91...bda798 )
by Alexey
04:26
created

Category::getRoot()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

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