AdvancedSearch::setView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
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\base\Model;
14
use Yii;
15
use yii\base\Widget;
16
use yii\bootstrap\ActiveField;
17
use yii\bootstrap\ActiveForm;
18
use yii\helpers\ArrayHelper;
19
use yii\helpers\Html;
20
use yii\helpers\Inflector;
21
use yii\web\JsExpression;
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('hipanel', '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('hipanel', 'Search'), ['class' => 'btn btn-sm btn-info']);
130
            echo ' &nbsp; ';
131
            echo Html::a(Yii::t('hipanel', '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
    /**
147
     * @param string $attribute
148
     * @param array $options
149
     * @return ActiveField|AdvancedSearchActiveField
150
     */
151
    public function field($attribute, $options = []): AdvancedSearchActiveField
152
    {
153
        return $this->_form->field($this->model, $attribute, $options)
154
            ->textInput(['placeholder' => $this->model->getAttributeLabel($attribute)])
155
            ->label(false);
156
    }
157
158
    public function registerMyJs()
159
    {
160
        $div_id = $this->getDivId();
161
        Yii::$app->getView()->registerJs(new JsExpression(<<<JS
162
$('#advancedsearch-button').click(function (event) {
163
    $('#${div_id}').toggle();
164
    event.preventDefault();
165
});
166
$('#search-form-ticket-pjax').on('pjax:end', function () {
167
    $.pjax.reload({container:'#ticket-grid-pjax', timeout: false});
168
});
169
JS
170
        ), \yii\web\View::POS_READY);
171
    }
172
173
    public function getDivId()
174
    {
175
        if ($this->getId(false) !== null) {
176
            $id = $this->getId(false);
177
        } else {
178
            $id = Inflector::camel2id($this->model->formName());
179
        }
180
181
        return 'advancedsearch-' . $id;
182
    }
183
184
    /**
185
     * @param mixed $view
186
     */
187
    public function setView($view)
188
    {
189
        $this->_view = $view;
190
    }
191
192
    /**
193
     * @return mixed
194
     */
195
    public function getView()
196
    {
197
        if ($this->_view === null) {
198
            $this->_view = Yii::$app->view;
199
        }
200
201
        return $this->_view;
202
    }
203
}
204