AdvancedSearchActiveField::textarea()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
use hipanel\helpers\ArrayHelper;
14
use hiqdev\assets\autosize\AutosizeAsset;
15
use hiqdev\combo\Combo;
16
use Yii;
17
use yii\helpers\Html;
18
use yii\widgets\ActiveField;
19
20
/**
21
 * Class AdvancedSearchActiveField.
22
 *
23
 * @author Dmytro Naumenko <[email protected]>
24
 */
25
class AdvancedSearchActiveField extends ActiveField
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    private $_inputId;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
31
32
    public function widget($class, $config = [])
33
    {
34
        /* @var $class \yii\base\Widget */
35
        $config['class'] = $class;
36
        $config['model'] = $this->model;
37
        $config['attribute'] = $this->attribute;
38
        $config['view'] = $this->form->getView();
39
        $widget = Yii::createObject($config);
40
        if ($widget instanceof Combo) {
41
            $this->_inputId = $this->_inputId ?: ($this->getInputId() . '-' . mt_rand());
42
            $widget->inputOptions['id'] = $this->getInputId();
43
            $widget->setPluginOptions(ArrayHelper::merge($widget->pluginOptions, [
44
                'select2Options' => [
45
                    'placeholder' => $this->model->getAttributeLabel($this->attribute),
46
                ],
47
            ]));
48
        }
49
        $this->parts['{input}'] = $widget->run();
50
51
        return $this;
52
    }
53
54
    public function textarea($options = [])
55
    {
56
        AutosizeAsset::activate($this->form->getView(), '[data-autosize]');
57
58
        return parent::textarea(ArrayHelper::merge([
59
            'data-autosize' => true, 'rows' => 1,
60
            'placeholder' => $this->model->getAttributeLabel($this->attribute),
61
            'style' => ['max-height' => '30vh'],
62
        ], $options));
63
    }
64
65
    protected function getInputId()
66
    {
67
        return $this->_inputId ?: parent::getInputId();
68
    }
69
70
    /**
71
     * Renders the opening tag of the field container.
72
     * @return string the rendering result
73
     */
74
    public function begin()
75
    {
76
        if ($this->form->enableClientScript) {
77
            $clientOptions = $this->getClientOptions();
78
            if (!empty($clientOptions)) {
79
                $this->form->attributes[] = $clientOptions;
80
            }
81
        }
82
83
        $inputID = $this->getInputId();
84
        $attribute = Html::getAttributeName($this->attribute);
85
        $options = $this->options;
86
        $class = isset($options['class']) ? [$options['class']] : [];
87
        $class[] = "field-$inputID";
88
        if ($this->model->isAttributeRequired($attribute)) {
89
            $class[] = $this->form->requiredCssClass;
90
        }
91
        if ($this->model->hasErrors($attribute)) {
92
            $class[] = $this->form->errorCssClass;
93
        }
94
        $options['class'] = implode(' ', $class);
95
96
        $tag = ArrayHelper::remove($options, 'tag', 'div');
97
        // Added tooltip help
98
        $options['data'] = [
99
            'toggle' => 'tooltip',
100
            'title' => $this->model->getAttributeLabel($this->attribute),
101
        ];
102
103
        return Html::beginTag($tag, $options);
104
    }
105
}
106