Completed
Push — master ( 2db650...a2c379 )
by Klochok
04:08
created

AdvancedSearchActiveField   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 67
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A widget() 0 18 3
A getInputId() 0 4 2
B begin() 0 31 6
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use hipanel\helpers\ArrayHelper;
15
use hiqdev\combo\Combo;
16
use Yii;
17
use yii\helpers\Html;
18
use yii\widgets\ActiveField;
19
20
class AdvancedSearchActiveField extends ActiveField
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    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...
26
27
    public function widget($class, $config = [])
28
    {
29
        /* @var $class \yii\base\Widget */
30
        $config['class'] = $class;
31
        $config['model'] = $this->model;
32
        $config['attribute'] = $this->attribute;
33
        $config['view'] = $this->form->getView();
34
        $widget = Yii::createObject($config);
35
36
        if ($widget instanceof Combo) {
37
            $this->_inputId = $this->_inputId ?: ($this->getInputId() . '-' . mt_rand());
38
            $widget->inputOptions['id'] = $this->getInputId();
39
        }
40
41
        $this->parts['{input}'] = $widget->run();
42
43
        return $this;
44
    }
45
46
    protected function getInputId()
47
    {
48
        return $this->_inputId ?: parent::getInputId();
49
    }
50
51
    /**
52
     * Renders the opening tag of the field container.
53
     * @return string the rendering result.
54
     */
55
    public function begin()
56
    {
57
        if ($this->form->enableClientScript) {
58
            $clientOptions = $this->getClientOptions();
59
            if (!empty($clientOptions)) {
60
                $this->form->attributes[] = $clientOptions;
61
            }
62
        }
63
64
        $inputID = $this->getInputId();
65
        $attribute = Html::getAttributeName($this->attribute);
66
        $options = $this->options;
67
        $class = isset($options['class']) ? [$options['class']] : [];
68
        $class[] = "field-$inputID";
69
        if ($this->model->isAttributeRequired($attribute)) {
70
            $class[] = $this->form->requiredCssClass;
71
        }
72
        if ($this->model->hasErrors($attribute)) {
73
            $class[] = $this->form->errorCssClass;
74
        }
75
        $options['class'] = implode(' ', $class);
76
77
        $tag = ArrayHelper::remove($options, 'tag', 'div');
78
        // Added tooltip help
79
        $options['data'] = [
80
            'toggle' => 'tooltip',
81
            'title' => $this->model->getAttributeLabel($this->attribute),
82
        ];
83
84
        return Html::beginTag($tag, $options);
85
    }
86
}
87