|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* INJI |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
6
|
|
|
* @link http://inji.ru/ |
|
7
|
|
|
* @copyright 2017 Alexey Krupskiy |
|
8
|
|
|
* @license https://github.com/injitools/Inji/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ecommerce; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @property int $id |
|
15
|
|
|
* @property string $name |
|
16
|
|
|
* @property int $weight |
|
17
|
|
|
* @property int $parent_id |
|
18
|
|
|
* @property int $icon_file_id |
|
19
|
|
|
* @property int $items_count |
|
20
|
|
|
* @property \Ecommerce\Catalog $parent |
|
21
|
|
|
* @property \Ecommerce\Catalog[] $childs |
|
22
|
|
|
* @property \Ecommerce\Catalog\Category[] $categories |
|
23
|
|
|
* @property \Files\File $icon |
|
24
|
|
|
*/ |
|
25
|
|
|
class Catalog extends \Model { |
|
26
|
|
|
static $cols = [ |
|
|
|
|
|
|
27
|
|
|
'name' => ['type' => 'text'], |
|
28
|
|
|
'weight' => ['type' => 'number'], |
|
29
|
|
|
'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent', 'extraValues' => ['0' => 'Нет родителя']], |
|
30
|
|
|
'icon_file_id' => ['type' => 'image'], |
|
31
|
|
|
'items_count' => ['type' => 'number'], |
|
32
|
|
|
'childsMgr' => ['type' => 'dataManager', 'relation' => 'childs'], |
|
33
|
|
|
'categoriesMgr' => ['type' => 'dataManager', 'relation' => 'categories'], |
|
34
|
|
|
]; |
|
35
|
|
|
static $labels = [ |
|
|
|
|
|
|
36
|
|
|
'name' => 'Название', |
|
37
|
|
|
'weight' => 'Вес соритровки', |
|
38
|
|
|
'parent_id' => 'Родитель', |
|
39
|
|
|
'icon_file_id' => 'Иконка', |
|
40
|
|
|
'childsMgr' => 'Дочерние каталоги', |
|
41
|
|
|
'categoriesMgr' => 'Категории товаров', |
|
42
|
|
|
]; |
|
43
|
|
|
static $dataManagers = [ |
|
|
|
|
|
|
44
|
|
|
'manager' => [ |
|
45
|
|
|
'filters' => ['name', 'parent_id'], |
|
46
|
|
|
'cols' => ['name', 'icon_file_id', 'childsMgr', 'categoriesMgr'], |
|
47
|
|
|
'sortMode' => true |
|
48
|
|
|
] |
|
49
|
|
|
]; |
|
50
|
|
|
static $forms = [ |
|
|
|
|
|
|
51
|
|
|
'manager' => [ |
|
52
|
|
|
'map' => [ |
|
53
|
|
|
['name', 'icon_file_id'], |
|
54
|
|
|
['categoriesMgr'], |
|
55
|
|
|
['childsMgr'], |
|
56
|
|
|
|
|
57
|
|
|
] |
|
58
|
|
|
] |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
public function calcItemsCount($save = true) { |
|
62
|
|
|
$count = 0; |
|
63
|
|
|
foreach ($this->categories as $category) { |
|
64
|
|
|
$count += $category->category->items_count; |
|
65
|
|
|
} |
|
66
|
|
|
$this->items_count = $count; |
|
67
|
|
|
if ($save) { |
|
68
|
|
|
$this->save(); |
|
69
|
|
|
if ($this->parent) { |
|
70
|
|
|
$this->parent->calcItemsCount(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
static function relations() { |
|
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
'parent' => [ |
|
78
|
|
|
'model' => 'Ecommerce\Catalog', |
|
79
|
|
|
'col' => 'parent_id' |
|
80
|
|
|
], |
|
81
|
|
|
'childs' => [ |
|
82
|
|
|
'type' => 'many', |
|
83
|
|
|
'col' => 'parent_id', |
|
84
|
|
|
'model' => 'Ecommerce\Catalog' |
|
85
|
|
|
], |
|
86
|
|
|
'categories' => [ |
|
87
|
|
|
'type' => 'many', |
|
88
|
|
|
'col' => 'catalog_id', |
|
89
|
|
|
'model' => 'Ecommerce\Catalog\Category' |
|
90
|
|
|
], |
|
91
|
|
|
'icon' => [ |
|
92
|
|
|
'col' => 'icon_file_id', |
|
93
|
|
|
'model' => 'Files\File' |
|
94
|
|
|
] |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.