forked from andkon13/yii2-klard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kladr.php
158 lines (139 loc) · 5.2 KB
/
Kladr.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
<?php
/**
* Created by PhpStorm.
* User: andkon
* Date: 28.06.16
* Time: 18:09
*/
namespace andkon\yii2kladr;
use andkon\yii2kladr\assets\KladrAsset;
use yii\helpers\Html;
use yii\widgets\InputWidget;
/**
* Class Widget
*
* @package common\components\kladr
*/
class Kladr extends InputWidget
{
/** область, регион */
const TYPE_REGION = 'region';
/** район */
const TYPE_DISTRICT = 'district';
/** населённый пункт */
const TYPE_CITY = 'city';
/** улица */
const TYPE_STREET = 'street';
/** строение */
const TYPE_BUILDING = 'building';
/** индекс */
const TYPE_ZIP = 'zip';
public $type;
public $select;
public $containerTag = 'span';
public $containerOptions = [];
protected $containerId;
static protected $inputs = [];
/** @inheritdoc */
public function init()
{
if (!$this->type) {
throw new \Exception('Need set type');
}
if (!$this->name) {
$this->name = Html::getInputName($this->model, $this->attribute);
$this->id = Html::getInputId($this->model, $this->attribute);
$this->value = $this->model->{$this->attribute};
}
KladrAsset::register($this->getView());
}
/** @inheritdoc */
public function run()
{
$this->containerId = KladrApi::KLADR_CACHE_PREFIX . \Yii::$app->getSecurity()->generateRandomString(10);
$this->containerOptions['id'] = $this->containerId;
echo Html::beginTag($this->containerTag, $this->containerOptions);
$name = explode(']', $this->name);
if ($pos = strpos($name[0], '_id')) {
$fakeName = substr($name[0], 0, $pos);
} else {
$fakeName = $name[0] . '_name';
}
if (isset($name[1])) {
$fakeName .= ']';
}
$fakeId = $this->id . '_kladr';
self::$inputs[$this->type] = [$this->id, $this->containerId . ' #' . $fakeId];
$options = array_merge($this->options, ['id' => $fakeId, 'data-kladr-id' => $this->value]);
$this->registryJsForInput($fakeId, $this->id);
$value = $this->value;
if ($this->value && !isset($this->options['value'])) {
switch ($this->type) {
case self::TYPE_BUILDING:
$obj = KladrApi::getBuilding($this->value);
break;
case self::TYPE_CITY:
$obj = KladrApi::getCity($this->value);
break;
case self::TYPE_STREET:
$obj = KladrApi::getStreet($this->value);
break;
default:
$obj = [];
break;
}
if (isset($obj[0], $obj[0]['name'])) {
$value = $obj[0]['name1'];
}
} else {
$value = (is_array($this->options) && array_key_exists('value', $this->options))
? $this->options['value'] : $value;
}
echo Html::textInput($fakeName, $value, $options);
$options = array_merge($this->options, ['id' => $this->id]);
echo Html::hiddenInput($this->name, $this->value, $options);
echo Html::endTag($this->containerTag);
}
/**
* @return KladrApi
*/
public static function getKladrApi()
{
return KladrApi::getInstanse();
}
/** @inheritdoc */
protected function registryJsForInput($fakeId, $id)
{
switch ($this->type) {
case self::TYPE_STREET:
$script = '$("#' . $this->containerId . ' #' . $fakeId . '")
.kladr({select: ' . $this->select . ', type: "' . $this->type . '", parentType: $.kladr.type.city,
parentInput:"#' . self::$inputs[self::TYPE_CITY][1] . '"})';
break;
case self::TYPE_BUILDING:
$script = '$("#' . $this->containerId . ' #' . $fakeId . '")
.kladr({select: ' . $this->select . ', type: "' . $this->type . '", type: "' . $this->type . '", parentType: $.kladr.type.street,
parentInput:"#' . self::$inputs[self::TYPE_STREET][1] . '"})';
break;
case self::TYPE_ZIP:
$zipJs = '$("#' . self::$inputs[self::TYPE_BUILDING][1] . '")
.kladr("select", function(obj){
if(obj.zip){
$("#' . self::$inputs[self::TYPE_ZIP][0] . '").val(obj.zip);
$("#' . self::$inputs[self::TYPE_ZIP][1] . '").val(obj.zip);
}
});';
$this->getView()->registerJs($zipJs);
$script = '$("#' . $this->containerId . ' #' . $fakeId . '")';
break;
default:
$script = '$("#' . $this->containerId . ' #' . $fakeId . '").kladr({select: ' . $this->select . ', type: "' . $this->type . '", type: "' . $this->type . '"})';
}
$script .= '.change(
function(event){
$("#' . $this->containerId . ' #' . $id . '").val($(event.target).attr("data-kladr-id"));
}
)';
$this->getView()->registerJs($script);
}
}