Completed
Push — master ( 8e3681...c9e152 )
by Alexey
04:35
created

Category::beforeSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 1
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
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
  public function beforeSave() {
120
    if ($this->id && $this->id == $this->parent_id) {
121
      $this->parent_id = 0;
122
      \Msg::add('Категория не может быть сама себе родителем');
123
    }
124
  }
125
126
  public function beforeDelete() {
127
    foreach ($this->catalogs as $category) {
128
      $category->delete();
129
    }
130
  }
131
132 View Code Duplication
  public function resolveTemplate() {
133
    if ($this->template !== 'inherit') {
134
      return $this->template;
135
    } elseif ($this->template == 'inherit' && $this->category) {
136
      return $this->category->resolveTemplate(true);
137
    } else {
138
      return (!empty(\App::$cur->ecommerce->config['defaultCategoryTemplate']) ? \App::$cur->ecommerce->config['defaultCategoryTemplate'] : 'current');
139
    }
140
  }
141
142 View Code Duplication
  public function resolveViewer() {
143
    if ($this->viewer !== 'inherit') {
144
      return $this->viewer;
145
    } elseif ($this->viewer == 'inherit' && $this->category) {
146
      return $this->category->resolveViewer(true);
147
    } else {
148
      return (!empty(\App::$cur->ecommerce->config['defaultCategoryView']) ? \App::$cur->ecommerce->config['defaultCategoryView'] : 'itemList');
149
    }
150
  }
151
152
}
153