TreeInput::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace devgroup\JsTreeWidget\widgets;
4
5
use DevGroup\Metronic\widgets\InputWidget;
6
use Yii;
7
use yii\helpers\ArrayHelper;
8
use yii\helpers\Html;
9
10
class TreeInput extends InputWidget
11
{
12
    public $treeConfig = [];
13
14
    public $multiple = false;
15
16
    public $selectIcon = 'fa fa-folder-o';
17
    public $selectText;
18
19
    public $clickToOpen = true;
20
    public $search = false;
21
22
    public function init()
23
    {
24
        parent::init();
25
        if ($this->selectText === null) {
26
            $this->selectText = Yii::t('jstw', 'Select');
27
        }
28
    }
29
30
    public function run()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
31
    {
32
        $id = 'input_tree__' . $this->getId();
33
        $this->options['id'] = $id;
34
35
36
        if ($this->hasModel()) {
37
            $input = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
38
            $value = Html::getAttributeValue($this->model, $this->attribute);
39
        } else {
40
            $input = Html::hiddenInput($this->name, $this->value, $this->options);
41
            $value = $this->value;
42
        }
43
        if (is_array($value)) {
44
            $value = implode(',', $value);
45
        }
46
        $this->treeConfig['selectedNodes'] = $value;
47
48
        $this->treeConfig['id'] = $id . '__tree';
49
        $oldOptions = isset($this->treeConfig['options']) ? $this->treeConfig['options'] : [];
50
        $this->treeConfig['options'] = ArrayHelper::merge($oldOptions, [
51
            'core' => [
52
                'multiple' => $this->multiple,
53
                'dblclick_toggle' => false,
54
            ],
55
        ]);
56
        $this->treeConfig['plugins'] = [
57
            'wholerow',
58
            'contextmenu',
59
            'dnd',
60
            'types',
61
        ];
62
63
        if ($this->multiple) {
64
            $this->treeConfig['plugins'][] = 'checkbox';
65
        }
66
        if ($this->search) {
67
            $this->treeConfig['plugins'][] = 'search';
68
            $this->treeConfig['options']['search'] = [
69
                'show_only_matches' => true,
70
            ];
71
        }
72
73
        return $this->render(
74
            'tree-input',
75
            [
76
                'id' => $id,
77
                'treeConfig' => $this->treeConfig,
78
                'multiple' => $this->multiple,
79
                'selectIcon' => $this->selectIcon,
80
                'selectText' => $this->selectText,
81
                'input' => $input,
82
                'clickToOpen' => $this->clickToOpen,
83
                'search' => $this->search,
84
            ]
85
        );
86
    }
87
}
88