5 require_once(CONST_BasePath.'/lib/ClassTypes.php');
8 * Detailed list of address parts for a single result
13 private $aAddressLines;
15 public function __construct(&$oDB, $iPlaceID, $sHousenumber, $mLangPref)
17 $this->iPlaceID = $iPlaceID;
19 if (is_array($mLangPref)) {
20 $mLangPref = $oDB->getArraySQL($oDB->getDBQuotedList($mLangPref));
23 if (!isset($sHousenumber)) {
28 $sSQL .= ' get_name_by_language(name,'.$mLangPref.') as localname';
29 $sSQL .= ' FROM get_addressdata('.$iPlaceID.','.$sHousenumber.')';
30 $sSQL .= ' ORDER BY rank_address DESC, isaddress DESC';
32 $this->aAddressLines = $oDB->getAll($sSQL);
35 private static function isAddress($aLine)
37 return $aLine['isaddress'] || $aLine['type'] == 'country_code';
40 public function getAddressDetails($bAll = false)
43 return $this->aAddressLines;
46 return array_filter($this->aAddressLines, array(__CLASS__, 'isAddress'));
49 public function getLocaleAddress()
54 foreach ($this->aAddressLines as $aLine) {
55 if ($aLine['isaddress'] && $sPrevResult != $aLine['localname']) {
56 $sPrevResult = $aLine['localname'];
57 $aParts[] = $sPrevResult;
61 return join(', ', $aParts);
64 public function getAddressNames($sCountry = null)
68 foreach ($this->aAddressLines as $aLine) {
69 if (!self::isAddress($aLine)) {
73 $sTypeLabel = ClassTypes\getLabelTag($aLine);
76 if (isset($aLine['localname']) && $aLine['localname']!=='') {
77 $sName = $aLine['localname'];
78 } elseif (isset($aLine['housenumber']) && $aLine['housenumber']!=='') {
79 $sName = $aLine['housenumber'];
83 $sTypeLabel = strtolower(str_replace(' ', '_', $sTypeLabel));
84 if (!isset($aAddress[$sTypeLabel])
85 || $aLine['class'] == 'place'
87 $aAddress[$sTypeLabel] = $sName;
96 * Annotates the given json with geocodejson address information fields.
98 * @param array $aJson Json hash to add the fields to.
100 * Geocodejson has the following fields:
101 * street, locality, postcode, city, district,
102 * county, state, country
104 * Postcode and housenumber are added by type, district is not used.
105 * All other fields are set according to address rank.
107 public function addGeocodeJsonAddressParts(&$aJson)
109 foreach (array_reverse($this->aAddressLines) as $aLine) {
110 if (!$aLine['isaddress']) {
114 if (!isset($aLine['localname']) || $aLine['localname'] == '') {
118 if ($aLine['type'] == 'postcode' || $aLine['type'] == 'postal_code') {
119 $aJson['postcode'] = $aLine['localname'];
120 } elseif ($aLine['type'] == 'house_number') {
121 $aJson['housenumber'] = $aLine['localname'];
124 if ($this->iPlaceID == $aLine['place_id']) {
128 $iRank = (int)$aLine['rank_address'];
130 if ($iRank > 25 && $iRank < 28) {
131 $aJson['street'] = $aLine['localname'];
132 } elseif ($iRank >= 22 && $iRank <= 25) {
133 $aJson['locality'] = $aLine['localname'];
134 } elseif ($iRank >= 17 && $iRank <= 21) {
135 $aJson['district'] = $aLine['localname'];
136 } elseif ($iRank >= 13 && $iRank <= 16) {
137 $aJson['city'] = $aLine['localname'];
138 } elseif ($iRank >= 10 && $iRank <= 12) {
139 $aJson['county'] = $aLine['localname'];
140 } elseif ($iRank >= 5 && $iRank <= 9) {
141 $aJson['state'] = $aLine['localname'];
142 } elseif ($iRank == 4) {
143 $aJson['country'] = $aLine['localname'];
148 public function getAdminLevels()
151 foreach (array_reverse($this->aAddressLines) as $aLine) {
152 if (self::isAddress($aLine)
153 && isset($aLine['admin_level'])
154 && $aLine['admin_level'] < 15
155 && !isset($aAddress['level'.$aLine['admin_level']])
157 $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
163 public function debugInfo()
165 return $this->aAddressLines;