Test Failed
Push — master ( 819d33...a2b805 )
by Alexey
05:27
created

Param::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 3
loc 3
rs 10
c 1
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\Offer;
13
14 View Code Duplication
class Param extends \Model {
15
16
    public static $objectName = 'Параметр товара';
17
    public static $labels = [
18
        'item_offer_option_id' => 'Параметр предложения',
19
        'item_offer_id' => 'Предложение',
20
        'value' => 'Значение',
21
    ];
22
    public static $cols = [
23
        //Основные параметры
24
        'item_offer_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'offer'],
25
        'item_offer_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_itemOfferOptionRelation' => [
34
                'type' => 'INDEX',
35
                'cols' => [
36
                    'item_offer_param_item_offer_id',
37
                    'item_offer_param_item_offer_option_id'
38
                ]
39
            ],
40
            'ecommerce_paramItemOfferIndex' => [
41
                'type' => 'INDEX',
42
                'cols' => [
43
                    'item_offer_param_item_offer_id',
44
                ]
45
            ],
46
            'ecommerce_paramOfferOptionIndex' => [
47
                'type' => 'INDEX',
48
                'cols' => [
49
                    'item_offer_param_item_offer_option_id'
50
                ]
51
            ],
52
        ];
53
    }
54
55
    public function realType() {
56
        if ($this->option && $this->option->type) {
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_offer_option_id',
77
                'item_offer_id',
78
                'value',
79
            ],
80
        ],
81
    ];
82
    public static $forms = [
83
        'manager' => [
84
            'map' => [
85
                ['item_offer_id', 'item_offer_option_id'],
86
                ['value']
87
            ]
88
    ]];
89
90
    /**
91
     * Return final param value
92
     * 
93
     * @param string $default
94
     * @return string
95
     */
96
    public function valueText($default = '') {
97
        if ($this->option->type == 'select' && $this->optionItem) {
98
            return $this->optionItem->value;
99
        } elseif ($this->option->type !== 'select' && $this->value) {
100
            return $this->value;
101
        }
102
        return $default;
103
    }
104
105
    public static function relations() {
106
        return [
107
            'file' => [
108
                'model' => 'Files\File',
109
                'col' => 'value'
110
            ],
111
            'option' => [
112
                'model' => 'Ecommerce\Item\Offer\Option',
113
                'col' => 'item_offer_option_id'
114
            ],
115
            'offer' => [
116
                'model' => 'Ecommerce\Item\Offer',
117
                'col' => 'item_offer_id'
118
            ],
119
            'optionItem' => [
120
                'model' => 'Ecommerce\Item\Offer\Option\Item',
121
                'col' => 'value'
122
            ]
123
        ];
124
    }
125
}