Completed
Push — master ( 8717df...fdbe7f )
by Alexey
05:44
created

Param   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 107
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B indexes() 0 24 1
A realType() 0 12 2
A value() 0 9 3
A relations() 0 21 1
1
<?php
2
3
/**
4
 * Item param
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\Item;
13
14
class Param extends \Model
15
{
16
    public static $objectName = 'Параметр товара';
17
    public static $labels = [
18
        'item_option_id' => 'Параметр',
19
        'item_id' => 'Товар',
20
        'value' => 'Значение',
21
    ];
22
    public static $cols = [
23
        //Основные параметры
24
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
25
        'item_option_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'option'],
26
        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'],
27
        //Системные
28
        'date_create' => ['type' => 'dateTime']
29
    ];
30
31
    public static function indexes()
32
    {
33
        return [
34
            'ecommerce_itemOptionRelation' => [
35
                'type' => 'INDEX',
36
                'cols' => [
37
                    'item_param_item_id',
38
                    'item_param_item_option_id'
39
                ]
40
            ],
41
            'ecommerce_paramItemIndex' => [
42
                'type' => 'INDEX',
43
                'cols' => [
44
                    'item_param_item_id',
45
                ]
46
            ],
47
            'ecommerce_paramOptionIndex' => [
48
                'type' => 'INDEX',
49
                'cols' => [
50
                    'item_param_item_option_id'
51
                ]
52
            ],
53
        ];
54
    }
55
56
    public function realType()
57
    {
58
        $type = $this->option->type;
59
        if ($type == 'select') {
60
            return [
61
                'type' => 'select',
62
                'source' => 'relation',
63
                'relation' => 'option:items',
64
            ];
65
        }
66
        return $type;
67
    }
68
69
    public static $dataManagers = [
70
71
        'manager' => [
72
            'name' => 'Параметры товара',
73
            'cols' => [
74
                'item_option_id',
75
                'item_id',
76
                'value',
77
            ],
78
        ],
79
    ];
80
    public static $forms = [
81
        'manager' => [
82
            'map' => [
83
                ['item_id', 'item_option_id'],
84
                ['value']
85
            ]
86
    ]];
87
88
    public function value($default = '')
89
    {
90
        if ($this->option->type != 'select') {
91
            return $this->value;
92
        } elseif ($this->optionItem) {
93
            return $this->optionItem->value;
94
        }
95
        return $default;
96
    }
97
98
    public static function relations()
99
    {
100
        return [
101
            'file' => [
102
                'model' => 'Files\File',
103
                'col' => 'value'
104
            ],
105
            'option' => [
106
                'model' => 'Ecommerce\Item\Option',
107
                'col' => 'item_option_id'
108
            ],
109
            'item' => [
110
                'model' => 'Ecommerce\Item',
111
                'col' => 'item_id'
112
            ],
113
            'optionItem' => [
114
                'model' => 'Ecommerce\Item\Option\Item',
115
                'col' => 'value'
116
            ]
117
        ];
118
    }
119
120
}
121