Completed
Pull Request — master (#37)
by Klochok
04:24
created

ObjectCombo::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    public $class_attribute = 'class';
19
20
    public $class_attribute_name = 'class';
21
22
    public $knownClasses = [
23
        'client' => ['alias' => '@client', 'combo' => ClientCombo::class],
24
        'device' => ['alias' => '@server', 'combo' => ServerCombo::class],
25
        'domain' => ['alias' => '@domain'],
26
        'zone' => ['alias' => '@zone'],
27
        'part' => ['alias' => '@part', 'combo' => PartCombo::class],
28
        'account' => ['alias' => '@account', 'combo' => AccountCombo::class],
29
        'plan' => ['alias' => '@plan', 'combo' => PlanCombo::class],
30
    ];
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function run()
36
    {
37
        return $this->render('ObjectCombo', [
38
            'model' => $this->model,
39
            'attribute' => $this->attribute,
40
            'classOptions' => $this->getClassOptions(),
41
            'classes' => $this->getClasses(),
42
            'class_attribute' => $this->model->{$this->class_attribute},
43
            'class_attribute_name' => $this->class_attribute_name,
44
        ]);
45
    }
46
47
    private function getClasses(): array
48
    {
49
        $objects = [];
50
        foreach ($this->knownClasses as $type => $options) {
51
            if (!Yii::getAlias($options['alias'], false)) {
52
                continue;
53
            }
54
            if ($options['combo']) {
55
                $options['comboOptions'] = get_class_vars($options['combo']);
56
            }
57
            $objects[$type] = $options;
58
        }
59
60
        return $objects;
61
    }
62
63
    private function getClassOptions(): array
64
    {
65
        $dropDownOptions = [];
66
        foreach ($this->getClasses() as $class => $options) {
67
            $dropDownOptions[$class] = Yii::t('hipanel', $this->getLabel($class));
68
        }
69
70
        return $dropDownOptions;
71
    }
72
73
    private function getLabel($class): string
74
    {
75
        return $class === 'device' ? 'Server' : ucfirst($class);
76
    }
77
}
78
79