-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathGeoController.php
141 lines (117 loc) · 3.94 KB
/
GeoController.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
<?php
namespace Igaster\LaravelCities;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Response;
class GeoController extends Controller
{
// [Geo] Get an item by $id
public function item($id)
{
$geo = Geo::find($id);
$this->applyFilter($geo);
return Response::json($geo);
}
// [Collection] Get multiple items by ids (comma seperated string or array)
public function items($ids = [])
{
if (is_string($ids)) {
$ids = explode(',', $ids);
}
$items = Geo::getByIds($ids);
return Response::json($items);
}
// [Collection] Get children of $id
public function children($id)
{
return $this->applyFilter(Geo::find($id)->getChildren());
}
// [Geo] Get parent of $id
public function parent($id)
{
$geo = Geo::find($id)->getParent();
$this->applyFilter($geo);
return Response::json($geo);
}
// [Geo] Get country by $code (two letter code)
public function country($code)
{
$geo = Geo::getCountry($code);
$this->applyFilter($geo);
return Response::json($geo);
}
// [Collection] Get all countries
public function countries()
{
return $this->applyFilter(Geo::level(Geo::LEVEL_COUNTRY)->get());
}
// [Collection] Search for %$name% in 'name' and 'alternames'. Optional filter to children of $parent_id
public function search($name, $parent_id = null)
{
if ($parent_id) {
return $this->applyFilter(Geo::searchNames($name, Geo::find($parent_id)));
}
return $this->applyFilter(Geo::searchNames($name));
}
public function ancestors($id)
{
$current = Geo::find($id);
$ancestors = $current->ancenstors()->get()->sortBy('a1code')->values();
$ancestors->push($current);
$result = collect();
foreach ($ancestors as $i => $ancestor) {
if ($i === 0) {
$locations = Geo::getCountries();
} else {
$parent = $ancestor->getParent();
if (! $parent) {
continue;
}
$locations = $parent->getChildren();
}
$selected = $locations->firstWhere('id', $ancestor->id);
$selected && $selected->isSelected = true;
$result->push($locations);
if ($i == $ancestors->count() - 1 && $ancestor) {
$childrens = $ancestor->getChildren();
if ($childrens->count()) {
$result->push($childrens);
}
}
}
$result = $this->applyFilter($result);
return $result;
}
public function breadcrumbs($id)
{
$current = Geo::find($id);
$ancestors = $current->ancenstors()->get();
$ancestors->push($current);
$ancestors = $this->applyFilter($ancestors);
return $ancestors;
}
// Apply Filter from request to json representation of an item or a collection
// api/call?fields=field1,field2
protected function applyFilter($geo)
{
if (request()->has('fields')) {
if (get_class($geo) == Collection::class) {
foreach ($geo as $item) {
$this->applyFilter($item);
}
return $geo;
}
$fields = request()->input('fields');
if ($fields == 'all') {
$geo->fliterFields();
} else {
$fields = explode(',', $fields);
array_walk($fields, function (&$item) {
$item = strtolower(trim($item));
});
$geo->fliterFields($fields);
}
}
return $geo;
}
}