forked from flack/openpsa
/
city.php
107 lines (98 loc) · 3.08 KB
/
city.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
<?php
/**
* @package org.routamc.positioning
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* MidCOM wrapper class for city objects
*
* @package org.routamc.positioning
*/
class org_routamc_positioning_city_dba extends midcom_core_dbaobject
{
public $__midcom_class_name__ = __CLASS__;
public $__mgdschema_class_name__ = 'org_routamc_positioning_city';
/**
* Human-readable label for cases like Asgard navigation
*/
public function get_label()
{
return $this->city;
}
public function get_label_property()
{
return 'city';
}
/**
* @return org_routamc_positioning_country_dba Country the city is in
*/
function get_parent_guid_uncached()
{
if ($this->country)
{
$qb = org_routamc_positioning_country_dba::new_query_builder();
$qb->add_constraint('code', '=', $this->country);
$countries = $qb->execute();
if (count($countries) == 0)
{
debug_add("Could not load Country ID {$this->country} from the database, aborting.",
MIDCOM_LOG_INFO);
return null;
}
return $countries[0]->guid;
}
return null;
}
/**
* Don't save city if another city is in same place
*/
public function _on_creating()
{
$qb = org_routamc_positioning_city_dba::new_query_builder();
$qb->add_constraint('longitude', '=', (double)$this->longitude);
$qb->add_constraint('latitude', '=', (double)$this->latitude);
$qb->set_limit(1);
$matches = $qb->execute_unchecked();
if ( !empty($matches)
/* doublecheck */
&& $matches[0]->longitude === $this->longitude
&& $matches[0]->latitude === $this->latitude)
{
// We don't need to save duplicate entries
midcom_connection::set_error(MGD_ERR_DUPLICATE);
return false;
}
return true;
}
static function get_by_name($name)
{
if (empty($name))
{
return false;
}
// Seek by strict city name first
$qb = org_routamc_positioning_city_dba::new_query_builder();
$qb->add_constraint('city', 'LIKE', $name);
$qb->set_limit(1);
$matches = $qb->execute_unchecked();
if (count($matches) > 0)
{
return $matches[0];
}
// Strict name didn't match, seek by alternate names
$qb = org_routamc_positioning_city_dba::new_query_builder();
$qb->add_constraint('alternatenames', 'LIKE', "%{$name}%");
// Most likely we're interested in the biggest city that matches
$qb->add_order('population', 'DESC');
$qb->set_limit(1);
$matches = $qb->execute_unchecked();
if (count($matches) > 0)
{
return $matches[0];
}
return false;
}
}
?>