Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

AdvancedSearch::getView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
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\base\Model;
15
use Yii;
16
use yii\base\Widget;
17
use yii\helpers\ArrayHelper;
18
use yii\helpers\Html;
19
use yii\helpers\Inflector;
20
use yii\web\JsExpression;
21
use yii\widgets\ActiveForm;
22
23
/**
24
 * Advanced Search widget.
25
 *
26
 * Usage:
27
 * ```php
28
 * $form = AdvancedSearch::begin(['model' => $model]);
29
 * echo $form->field('domain');
30
 * $form->end();
31
 * ```
32
 */
33
class AdvancedSearch extends Widget
34
{
35
    /**
36
     * @var
37
     */
38
    protected $_view;
39
40
    /**
41
     * @var Model model
42
     */
43
    public $model;
44
45
    /**
46
     * @var string|array action url
47
     */
48
    public $action = ['index'];
49
50
    /**
51
     * @var string request method
52
     */
53
    public $method = 'GET';
54
55
    /**
56
     * @var array options passed to ActiveForm
57
     */
58
    public $formOptions = [
59
        'data-pjax' => true,
60
    ];
61
62
    /**
63
     * @var array Options that will be used for the search wrapping tag.
64
     * The following options have special effect:
65
     *  - `tag`: the tag name. Defaults to `div`
66
     *  - `displayNone`: whether to hide the form untill special button will be pressed. Defaults to true.
67
     */
68
    public $options = [];
69
70
    /**
71
     * @var ActiveForm form to be used
72
     */
73
    protected $_form;
74
75
    /**
76
     * @var array|false Options that will be used for the form submit button wrapping tag.
77
     * The following options have special effect:
78
     *  - `tag`: the tag name. Defaults to `div`
79
     *
80
     * Setting this property to `false` will prevent submit button render.
81
     */
82
    public $submitButtonWrapperOptions = [];
83
84
    /**
85
     * Renders the starting div.
86
     */
87
    public function init()
88
    {
89
        $this->registerMyJs();
90
        $display_none = '';
91
92
        if (ArrayHelper::remove($this->options, 'displayNone', true) === true) {
93
            $display_none = Yii::$app->request->get($this->model->formName())['search_form'] ? '' : 'display:none';
94
        }
95
96
        if ($this->submitButtonWrapperOptions !== false) {
97
            $this->submitButtonWrapperOptions = ArrayHelper::merge([
98
                'class' => 'col-md-12',
99
            ], $this->submitButtonWrapperOptions);
100
        }
101
102
        $tag = ArrayHelper::remove($this->options, 'tag', 'div');
103
        echo Html::beginTag($tag, ArrayHelper::merge([
104
            'id'    => $this->getDivId(),
105
            'class' => 'row',
106
            'style' => 'margin-bottom: 1rem; margin-top: 1rem; ' . $display_none,
107
        ], $this->options));
108
109
        $this->_form = ActiveForm::begin([
110
            'id'        => 'form-' . $this->getDivId(),
111
            'action'    => $this->action,
112
            'method'    => $this->method,
113
            'options'   => $this->formOptions,
114
            'fieldClass' => AdvancedSearchActiveField::class,
115
        ]);
116
        echo Html::hiddenInput(Html::getInputName($this->model, 'search_form'), 1);
117
    }
118
119
    public static function renderButton()
120
    {
121
        return Html::a(Yii::t('app', 'Advanced search'), '#', ['class' => 'btn btn-info btn-sm', 'id' => 'advancedsearch-button']);
122
    }
123
124
    public function run()
125
    {
126
        if ($this->submitButtonWrapperOptions !== false) {
127
            $tag = ArrayHelper::remove($this->submitButtonWrapperOptions, 'tag', 'div');
128
            echo Html::beginTag($tag, $this->submitButtonWrapperOptions);
129
            echo Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-sm btn-info']);
130
            echo ' &nbsp; ';
131
            echo Html::a(Yii::t('app', 'Clear'), $this->action, [
132
                'class' => 'btn btn-sm btn-default',
133
                'data-params' => [
134
                    'clear-filters' => true,
135
                    Yii::$app->request->csrfParam => Yii::$app->request->getCsrfToken(),
136
                ],
137
                'data-method' => 'POST',
138
            ]);
139
            echo Html::endTag('div');
140
        }
141
142
        $this->_form->end();
143
        echo Html::endTag('div');
144
    }
145
146
    public function field($attribute, $options = [])
147
    {
148
        return $this->_form->field($this->model, $attribute, $options);
149
    }
150
151
    public function registerMyJs()
152
    {
153
        $div_id = $this->getDivId();
154
        Yii::$app->getView()->registerJs(new JsExpression(<<<JS
155
$('#advancedsearch-button').click(function (event) {
156
    $('#${div_id}').toggle();
157
    event.preventDefault();
158
});
159
$('#search-form-ticket-pjax').on('pjax:end', function () {
160
    $.pjax.reload({container:'#ticket-grid-pjax', timeout: false});
161
});
162
JS
163
        ), \yii\web\View::POS_READY);
164
    }
165
166
    public function getDivId()
167
    {
168
        if ($this->getId(false) !== null) {
169
            $id = $this->getId(false);
170
        } else {
171
            $id = Inflector::camel2id($this->model->formName());
172
        }
173
        return 'advancedsearch-' . $id;
174
    }
175
176
    /**
177
     * @param mixed $view
178
     */
179
    public function setView($view)
180
    {
181
        $this->_view = $view;
182
    }
183
184
    /**
185
     * @return mixed
186
     */
187
    public function getView()
188
    {
189
        if ($this->_view === null) {
190
            $this->_view = Yii::$app->view;
191
        }
192
        return $this->_view;
193
    }
194
}
195