Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

Category::relations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * 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 Materials;
13
14
class Category extends \Model {
15
16
    public static $objectName = 'Категория';
17
    public static $treeCategory = 'Materials\Material';
18
    public static $labels = [
19
        'name' => 'Название',
20
        'description' => 'Описание',
21
        'image_file_id' => 'Изображение',
22
        'parent_id' => 'Родительская категория',
23
        'alias' => 'Алиас',
24
        'viewer' => 'Тип категории по умолчанию',
25
        'template' => 'Шаблон категории по умолчанию',
26
        'material_viewer' => 'Тип страниц по умолчанию',
27
        'material_template' => 'Шаблон страниц по умолчанию',
28
    ];
29
    public static $cols = [
30
31
        'name' => ['type' => 'text'],
32
        'alias' => ['type' => 'text'],
33
        'image_file_id' => ['type' => 'image'],
34
        'description' => ['type' => 'html'],
35
        'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent', 'showCol' => 'name'],
36
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
37
        'material_viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsList', 'module' => 'Materials'],
38
        'material_template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesList', 'module' => 'Materials'],
39
        'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsCategoryList', 'module' => 'Materials'],
40
        'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesCategoryList', 'module' => 'Materials'],
41
        'tree_path' => ['type' => 'text'],
42
        'weight' => ['type' => 'number'],
43
    ];
44
    public static $dataManagers = [
45
        'manager' => [
46
            'options' => [
47
                'access' => [
48
                    'groups' => [
49
                        3
50
                    ]
51
                ]
52
            ],
53
            'cols' => [
54
                'name',
55
                'alias',
56
                'parent_id',
57
            ],
58
        ]
59
    ];
60
    public static $forms = [
61
        'manager' => [
62
            'options' => [
63
                'access' => [
64
                    'groups' => [
65
                        3
66
                    ]
67
                ]
68
            ],
69
            'map' => [
70
                ['name', 'parent_id'],
71
                ['alias', 'image_file_id'],
72
                ['viewer', 'template'],
73
                ['material_viewer', 'material_template'],
74
                ['description'],
75
            ]
76
        ]
77
    ];
78
79
    public function beforeDelete() {
80
        foreach ($this->childs as $child) {
81
            $child->delete();
82
        }
83
    }
84
85
    public function getRoot() {
86
        $treePath = array_values(array_filter(explode('/', $this->tree_path)));
87
        if (!empty($treePath[0])) {
88
            $category = Category::get($treePath[0]);
89
            if ($category) {
90
                return $category;
91
            }
92
        }
93
        return $this;
94
    }
95
96
    public function getHref() {
97
        $href = !empty(\App::$primary->config['defaultModule']) && \App::$primary->config['defaultModule'] == 'Materials' ? '/category' : '/materials/category';
98
        $treePath = array_filter(explode('/', $this->tree_path));
99
        if ($treePath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $treePath of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
100
            $categorys = Category::getList(['where' => ['id', implode(',', $treePath), 'IN']]);
101
            foreach ($categorys as $category) {
102
                $href .= "/{$category->alias}";
103
            }
104
        }
105
        return $href . "/" . ($this->alias ? $this->alias : $this->pk());
106
    }
107
108
    public static function relations() {
109
        return [
110
            'parent' => [
111
                'model' => 'Materials\Category',
112
                'col' => 'parent_id'
113
            ],
114
            'childs' => [
115
                'type' => 'many',
116
                'model' => 'Materials\Category',
117
                'col' => 'parent_id'
118
            ],
119
            'items' => [
120
                'type' => 'many',
121
                'model' => 'Materials\Material',
122
                'col' => 'category_id'
123
            ],
124
            'image' => [
125
                'model' => 'Files\File',
126
                'col' => 'image_file_id'
127
            ],
128
            'user' => [
129
                'model' => 'Users\User',
130
                'col' => 'user_id'
131
            ]
132
        ];
133
    }
134
135
    public function resolveTemplate($material = false) {
136
        $param = $material ? 'material_template' : 'template';
137
        if ($this->$param !== 'inherit') {
138
            return $this->$param;
139
        } elseif ($this->$param == 'inherit' && $this->parent) {
140
            return $this->parent->resolveTemplate($material);
141
        } else {
142
            return 'current';
143
        }
144
    }
145
146
    public function resolveViewer($material = false) {
147
        $param = $material ? 'material_viewer' : 'viewer';
148
        if ($this->$param !== 'inherit') {
149
            return $this->$param;
150
        } elseif ($this->$param == 'inherit' && $this->parent) {
151
            return $this->parent->resolveViewer($material);
152
        } else {
153
            return $material ? 'default' : 'category';
154
        }
155
    }
156
157
}
158