Completed
Branch master (ebb499)
by Alexey
04:15
created

Param::indexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
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 View Code Duplication
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', 'onChange' => 'reloadForm'],
26
        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'],
27
        //Системные
28
        'date_create' => ['type' => 'dateTime']
29
    ];
30
31
    public static function indexes() {
32
        return [
33
            'ecommerce_itemOptionRelation' => [
34
                'type' => 'INDEX',
35
                'cols' => [
36
                    'item_param_item_id',
37
                    'item_param_item_option_id'
38
                ]
39
            ],
40
            'ecommerce_paramItemIndex' => [
41
                'type' => 'INDEX',
42
                'cols' => [
43
                    'item_param_item_id',
44
                ]
45
            ],
46
            'ecommerce_paramOptionIndex' => [
47
                'type' => 'INDEX',
48
                'cols' => [
49
                    'item_param_item_option_id'
50
                ]
51
            ],
52
        ];
53
    }
54
55
    public function realType() {
56
        if ($this->option) {
57
            $type = $this->option->type;
58
59
            if ($type == 'select') {
60
                return [
61
                    'type' => 'select',
62
                    'source' => 'relation',
63
                    'relation' => 'option:items',
64
                ];
65
            }
66
            return $type;
67
        }
68
        return 'text';
69
    }
70
71
    public static $dataManagers = [
72
73
        'manager' => [
74
            'name' => 'Параметры товара',
75
            'cols' => [
76
                'item_option_id',
77
                'item_id',
78
                'value',
79
            ],
80
        ],
81
    ];
82
    public static $forms = [
83
        'manager' => [
84
            'map' => [
85
                ['item_id', 'item_option_id'],
86
                ['value']
87
            ]
88
    ]];
89
90
    function name() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
        return $this->value;
92
    }
93
94
    public function value($default = '') {
95
        if ($this->option->type != 'select') {
96
            return $this->value;
97
        } elseif ($this->optionItem) {
98
            return $this->optionItem->value;
99
        }
100
        return $default;
101
    }
102
103
    public static function relations() {
104
        return [
105
            'file' => [
106
                'model' => 'Files\File',
107
                'col' => 'value'
108
            ],
109
            'option' => [
110
                'model' => 'Ecommerce\Item\Option',
111
                'col' => 'item_option_id'
112
            ],
113
            'item' => [
114
                'model' => 'Ecommerce\Item',
115
                'col' => 'item_id'
116
            ],
117
            'optionItem' => [
118
                'model' => 'Ecommerce\Item\Option\Item',
119
                'col' => 'value'
120
            ]
121
        ];
122
    }
123
124
}
125