-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautobox.php
56 lines (46 loc) · 1.88 KB
/
autobox.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
<?
require "Autoloader.php";
Autoloader::RegisterAutoloader();
require "run.php";
$REGIONCODE = null;
$AREACODE = null;
$CITYCODE = null;
$query = array();
$repo = new \Entities\KladrRepository();
$response = array();
/** init params */
if (isset($_REQUEST['REGIONCODE']))
$REGIONCODE = kladr_param_filter($_REQUEST['REGIONCODE']);
if (isset($_REQUEST['AREACODE']))
$AREACODE = kladr_param_filter($_REQUEST['AREACODE']);
if (isset($_REQUEST['CITYCODE']))
$CITYCODE = kladr_param_filter($_REQUEST['CITYCODE']);
/** select */
if ($REGIONCODE != null && $AREACODE == null && $CITYCODE == null) {
$response[] = autoBoxBuilderResponse($repo->getAreaList($REGIONCODE), 'AREACODE', '.wrap-area');
$citiesList = $repo->getCityList($REGIONCODE, "000", "000");
unset($citiesList[0]);
$response[] = autoBoxBuilderResponse($citiesList, 'CITYCODE', '.wrap-cities');
}
if ($REGIONCODE == null && $AREACODE != null && $CITYCODE == null) {
$parseCode = \Entities\KladrRepository::ParseCode($AREACODE);
$PLACECODE = ($parseCode['AREACODE'] == '000') ? '000' : '___';
$citiesList = $repo->getCityList($parseCode['REGIONCODE'], $parseCode['AREACODE'], $PLACECODE);
unset($citiesList[0]);
$response[] = autoBoxBuilderResponse($citiesList, 'CITYCODE', '.wrap-cities');
}
header('Content-Type: application/json');
echo json_encode($response);
function autoBoxBuilderResponse($items, $codeName, $wrapSelector)
{
$view = '<select name="%s">%s</select>';
$viewItem = '<option value="%s--%s" data-code="%s" >%s</option>';
$_ = '';
foreach ($items as $index => $item) {
$_ .= sprintf($viewItem, $codeName, $item->getCode($codeName, true), $item->getCode(), kladr_name_filter($item, true));
}
return array(
'wrapElement' => $wrapSelector,
'view' => sprintf($view, $codeName, $_)
);
}