-
Notifications
You must be signed in to change notification settings - Fork 9
/
CepInput.php
163 lines (138 loc) · 4.46 KB
/
CepInput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace yiibr\correios;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\widgets\InputWidget;
class CepInput extends InputWidget
{
/**
* @var array|string $action the form action URL. This parameter will be processed by [[Url::to()]].
*/
public $action = '';
/**
* @inheritdoc
*/
public $options = ['class' => 'form-control'];
/**
* @var string the css search icon class
*/
public $searchIcon = 'glyphicon glyphicon-search';
/**
* @var array $fields ID of html elements that will receive result of search
* ```php
* [
* 'location' => 'location_input_id',
* 'district' => 'district_input_id',
* 'city' => 'city_input_id',
* 'state' => 'state_input_id',
* ]
* ```
*/
public $fields = [
'location' => '',
'district' => '',
'city' => '',
'state' => '',
];
/**
* @var string name of query parameter
*/
public $queryParam = '_cep';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
CepAsset::register($this->getView());
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
$input = Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
$input = Html::textInput($this->name, $this->value, $this->options);
}
$this->renderSearch($input, $this->id);
$this->renderModal();
$this->registerJs();
}
/**
* Renders search input box
* @param string $input input tag
* @param string $id
*/
protected function renderSearch($input, $id = null)
{
echo Html::beginTag("div", ['class' => 'input-group cep-search', 'id' => $id]);
echo $input;
echo Html::beginTag("span", ['class' => 'input-group-btn']);
echo Html::beginTag("a", ['class' => 'btn btn-default']);
echo Html::tag("i", null, ['class' => $this->searchIcon]);
echo Html::endTag("a");
echo Html::endTag("span");
echo Html::endTag("div");
}
/**
* Renders a modal window
*/
protected function renderModal()
{
echo Html::beginTag('div', ['class' => 'fade modal', 'role' => 'dialog', 'tabindex' => '-1']);
echo Html::beginTag('div', ['class' => 'modal-dialog modal-lg']);
echo Html::beginTag('div', ['class' => 'modal-content']);
echo Html::beginTag('div', ['class' => 'modal-header']);
echo Html::button('×', ['class' => 'close', 'data-dismiss' => 'modal', 'aria-hidden' => true]);
echo "CEP";
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'modal-body']);
echo Html::label('Endereço');
$input = Html::textInput(null, null, ['class' => ArrayHelper::getValue($this->options, 'class')]);
$this->renderSearch($input);
$this->renderGrid();
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'modal-footer']);
echo Html::button('Fechar', ['class' => 'btn btn-default', 'data-dismiss' => 'modal', 'aria-hidden' => true]);
echo Html::endTag('div');
echo Html::endTag('div');
echo Html::endTag('div');
echo Html::endTag('div');
}
/**
* Renders address grid
*/
protected function renderGrid()
{
echo Html::Tag('div', null, ['class' => 'separator bottom']);
echo Html::beginTag('div', ['class' => 'row-fluid']);
echo Html::beginTag('table', ['class' => 'table table-bordered table-striped']);
echo Html::beginTag('thead');
echo Html::beginTag('tr');
echo Html::Tag('th', "CEP");
echo Html::Tag('th', "Endereço");
echo Html::Tag('th', "Bairro");
echo Html::Tag('th', "Cidade");
echo Html::Tag('th', "UF");
echo Html::endTag('tr');
echo Html::endTag('thead');
echo Html::beginTag('tbody');
echo Html::endTag('tbody');
echo Html::endTag('table');
echo Html::endTag('div');
}
protected function registerJs()
{
$id = $this->options['id'];
$options = Json::encode([
'action' => Url::to($this->action),
'fields' => $this->fields,
'queryParam' => $this->queryParam
]);
$this->getView()->registerJs("jQuery('#{$id}').cep($options);");
}
}