IconPicker::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace dominus77\iconpicker;
4
5
use yii\helpers\Html;
6
use yii\helpers\Json;
7
use yii\widgets\InputWidget;
8
9
/**
10
 * Class IconPicker
11
 * @package dominus77\iconpicker
12
 */
13
class IconPicker extends InputWidget
14
{
15
    /**
16
     * Options plugin
17
     * @var array
18
     * @see https://farbelous.github.io/fontawesome-iconpicker/
19
     */
20
    public $clientOptions = [];
21
22
    /**
23
     * @inheritdoc
24
     */
25 6
    public function run()
26
    {
27 6
        if ($this->hasModel()) {
28 3
            echo Html::activeTextInput($this->model, $this->attribute, $this->options);
29
        } else {
30 3
            echo Html::textInput($this->name, $this->value, $this->options);
31
        }
32 6
        $this->registerClientScript();
33 6
    }
34
35
    /**
36
     * Publish resource
37
     */
38 6
    protected function registerClientScript()
39
    {
40 6
        $js = [];
41 6
        $view = $this->getView();
42 6
        IconPickerAsset::register($view);
43 6
        $id = $this->options['id'];
44 6
        $options = Json::encode($this->clientOptions);
45 6
        $js[] = "$('#{$id}').iconpicker($options);";
46 6
        $view->registerJs(implode("\n", $js));
47 6
    }
48
}
49