Completed
Pull Request — master (#37)
by Klochok
40:07 queued 17:47
created

ObjectCombo::getLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace hipanel\widgets\combo;
4
5
use hipanel\modules\client\widgets\combo\ClientCombo;
6
use hipanel\modules\finance\widgets\combo\PlanCombo;
7
use hipanel\modules\hosting\widgets\combo\AccountCombo;
8
use hipanel\modules\server\widgets\combo\ServerCombo;
9
use hipanel\modules\stock\widgets\combo\PartCombo;
10
use Yii;
11
use yii\bootstrap\InputWidget;
12
13
/**
14
 * Class BaseObjectSelector
15
 */
16
class ObjectCombo extends InputWidget
17
{
18
    /**
19
     * @var string
20
     */
21
    public $class_attribute = 'class';
22
23
    /**
24
     * @var string
25
     */
26
    public $class_attribute_name = 'class';
27
28
    /**
29
     * @var array
30
     */
31
    public $knownClasses = [
32
        'client' => ['alias' => '@client', 'combo' => ClientCombo::class],
33
        'device' => ['alias' => '@server', 'combo' => ServerCombo::class],
34
        'domain' => ['alias' => '@domain'],
35
        'zone' => ['alias' => '@zone'],
36
        'part' => ['alias' => '@part', 'combo' => PartCombo::class],
37
        'account' => ['alias' => '@account', 'combo' => AccountCombo::class],
38
        'plan' => ['alias' => '@plan', 'combo' => PlanCombo::class],
39
    ];
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function run()
45
    {
46
        return $this->render('ObjectCombo', [
47
            'model' => $this->model,
48
            'attribute' => $this->attribute,
49
            'classOptions' => $this->getClassOptions(),
50
            'classes' => $this->getClasses(),
51
            'class_attribute' => $this->model->{$this->class_attribute},
52
            'class_attribute_name' => $this->class_attribute_name,
53
        ]);
54
    }
55
56
    private function getClasses(): array
57
    {
58
        $classes = [];
59
        foreach ($this->knownClasses as $class => $options) {
60
            if (!Yii::getAlias($options['alias'], false)) {
61
                continue;
62
            }
63
            if ($options['combo']) {
64
                $options['comboOptions'] = get_class_vars($options['combo']);
65
            }
66
            $classes[$class] = $options;
67
        }
68
69
        return $classes;
70
    }
71
72
    private function getClassOptions(): array
73
    {
74
        $dropDownOptions = [];
75
        foreach ($this->getClasses() as $class => $options) {
76
            $dropDownOptions[$class] = Yii::t('hipanel', $this->getLabel($class));
77
        }
78
79
        return $dropDownOptions;
80
    }
81
82
    /**
83
     * @param string $class
84
     * @return string
85
     */
86
    private function getLabel($class): string
87
    {
88
        return $class === 'device' ? 'Server' : ucfirst($class);
89
    }
90
}
91
92