5 require_once(CONST_BasePath.'/lib/ClassTypes.php');
8 * Detailed list of address parts for a single result
12 private $aAddressLines;
14 public function __construct(&$oDB, $iPlaceID, $sHousenumber, $mLangPref)
16 if (is_array($mLangPref)) {
17 $mLangPref = $oDB->getArraySQL($oDB->getDBQuotedList($mLangPref));
20 if (!isset($sHousenumber)) {
25 $sSQL .= ' get_name_by_language(name,'.$mLangPref.') as localname';
26 $sSQL .= ' FROM get_addressdata('.$iPlaceID.','.$sHousenumber.')';
27 $sSQL .= ' ORDER BY rank_address DESC, isaddress DESC';
29 $this->aAddressLines = $oDB->getAll($sSQL);
32 private static function isAddress($aLine)
34 return $aLine['isaddress'] || $aLine['type'] == 'country_code';
37 public function getAddressDetails($bAll = false)
40 return $this->aAddressLines;
43 return array_filter($this->aAddressLines, array(__CLASS__, 'isAddress'));
46 public function getLocaleAddress()
51 foreach ($this->aAddressLines as $aLine) {
52 if ($aLine['isaddress'] && $sPrevResult != $aLine['localname']) {
53 $sPrevResult = $aLine['localname'];
54 $aParts[] = $sPrevResult;
58 return join(', ', $aParts);
61 public function getAddressNames()
66 foreach ($this->aAddressLines as $aLine) {
67 if (!self::isAddress($aLine)) {
72 $aTypeLabel = ClassTypes\getInfo($aLine);
74 if ($aTypeLabel === false) {
75 $aTypeLabel = ClassTypes\getFallbackInfo($aLine);
80 if (isset($aLine['localname']) && $aLine['localname']!=='') {
81 $sName = $aLine['localname'];
82 } elseif (isset($aLine['housenumber']) && $aLine['housenumber']!=='') {
83 $sName = $aLine['housenumber'];
87 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel']) ? $aTypeLabel['simplelabel'] : $aTypeLabel['label']);
88 $sTypeLabel = str_replace(' ', '_', $sTypeLabel);
89 if (!isset($aAddress[$sTypeLabel])
90 || isset($aFallback[$sTypeLabel])
91 || $aLine['class'] == 'place'
93 $aAddress[$sTypeLabel] = $sName;
95 $aFallback[$sTypeLabel] = $bFallback;
105 * Annotates the given json with geocodejson address information fields.
107 * @param array $aJson Json hash to add the fields to.
109 * Geocodejson has the following fields:
110 * street, locality, postcode, city, district,
111 * county, state, country
113 * Postcode and housenumber are added by type, district is not used.
114 * All other fields are set according to address rank.
116 public function addGeocodeJsonAddressParts(&$aJson)
118 foreach ($this->aAddressLines as $aLine) {
119 if (!$aLine['isaddress']) {
123 if (!isset($aLine['localname']) || $aLine['localname'] == '') {
127 $iRank = (int)$aLine['rank_address'];
129 if ($aLine['type'] == 'postcode' || $aLine['type'] == 'postal_code') {
130 $aJson['postcode'] = $aLine['localname'];
131 } else if ($aLine['type'] == 'house_number') {
132 $aJson['housenumber'] = $aLine['localname'];
133 } else if ($iRank > 25 && $iRank < 28) {
134 $aJson['street'] = $aLine['localname'];
135 } else if ($iRank >= 22 && $iRank <= 25) {
136 $aJson['locality'] = $aLine['localname'];
137 } else if ($iRank >= 17 && $iRank <= 21) {
138 $aJson['district'] = $aLine['localname'];
139 } else if ($iRank >= 13 && $iRank <= 16) {
140 $aJson['city'] = $aLine['localname'];
141 } else if ($iRank >= 10 && $iRank <= 12) {
142 $aJson['county'] = $aLine['localname'];
143 } else if ($iRank >= 5 && $iRank <= 9) {
144 $aJson['state'] = $aLine['localname'];
145 } else if ($iRank == 4) {
146 $aJson['country'] = $aLine['localname'];
151 public function getAdminLevels()
154 foreach (array_reverse($this->aAddressLines) as $aLine) {
155 if (self::isAddress($aLine)
156 && isset($aLine['admin_level'])
157 && $aLine['admin_level'] < 15
158 && !isset($aAddress['level'.$aLine['admin_level']])
160 $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
166 public function debugInfo()
168 return $this->aAddressLines;