Completed
Push — master ( 025538...41b916 )
by Alexey
06:15 queued 23s
created

Category::beforeSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 4
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 7
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
    {
61
        return [
62
            'ecommerce_category_category_parent_id' => [
63
                'type' => 'INDEX',
64
                'cols' => [
65
                    'category_parent_id',
66
                ]
67
            ],
68
            'ecommerce_category_category_tree_path' => [
69
                'type' => 'INDEX',
70
                'cols' => [
71
                    'category_tree_path(255)'
72
                ]
73
            ],
74
        ];
75
    }
76
77
    public static function relations()
78
    {
79
        return [
80
            'items' => [
81
                'type' => 'many',
82
                'model' => 'Ecommerce\Item',
83
                'col' => 'category_id',
84
            ],
85
            'parent' => [
86
                'model' => 'Ecommerce\Category',
87
                'col' => 'parent_id'
88
            ],
89
            'options' => [
90
                'type' => 'relModel',
91
                'model' => 'Ecommerce\Item\Option',
92
                'relModel' => 'Ecommerce\Item\Option\Relation',
93
            ],
94
            'image' => [
95
                'model' => 'Files\File',
96
                'col' => 'image_file_id'
97
            ],
98
            'user' => [
99
                'model' => 'Users\User',
100
                'col' => 'user_id'
101
            ],
102
            'catalogs' => [
103
                'type' => 'many',
104
                'model' => 'Ecommerce\Category',
105
                'col' => 'parent_id',
106
            ]
107
        ];
108
    }
109
110
    public function beforeSave()
111
    {
112
        if ($this->id && $this->id == $this->parent_id) {
113
            $this->parent_id = 0;
114
            \Msg::add('Категория не может быть сама себе родителем');
115
        }
116
    }
117
118
    public function beforeDelete()
119
    {
120
        foreach ($this->catalogs as $category) {
121
            $category->delete();
122
        }
123
    }
124
125 View Code Duplication
    public function resolveTemplate()
126
    {
127
        if ($this->template !== 'inherit') {
128
            return $this->template;
129
        } elseif ($this->template == 'inherit' && $this->category) {
130
            return $this->category->resolveTemplate(true);
131
        } else {
132
            return 'current';
133
        }
134
    }
135
136 View Code Duplication
    public function resolveViewer()
137
    {
138
        if ($this->viewer !== 'inherit') {
139
            return $this->viewer;
140
        } elseif ($this->viewer == 'inherit' && $this->category) {
141
            return $this->category->resolveViewer(true);
142
        } else {
143
            return (!empty(\App::$cur->ecommerce->config['defaultCategoryView']) ? \App::$cur->ecommerce->config['defaultCategoryView'] : 'itemList');
144
        }
145
    }
146
147
}
148