BAOBAB
co_place.class.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************************************************************/
26 defined( 'LGV_DBF_CATCHER' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
27 
28 require_once(CO_Config::db_classes_class_dir().'/co_ll_location.class.php');
29 
30 if ( !defined('LGV_CHAMELEON_UTILS_CATCHER') ) {
31  define('LGV_CHAMELEON_UTILS_CATCHER', 1);
32 }
33 
34 $utils_file = CO_Config::chameleon_main_class_dir().'/co_chameleon_utils.class.php';
35 require_once($utils_file);
36 
37 function test_is_assoc(array $arr) {
38  if (array() === $arr) return false;
39  return array_keys($arr) !== range(0, count($arr) - 1);
40 }
41 
42 /***************************************************************************************************************************/
46 class CO_Place extends CO_LL_Location {
47  var $address_elements = Array();
50  var $region_bias = NULL;
51 
52  /***********************************************************************************************************************/
53  /***********************/
59  protected function _get_address_element_labels() {
60  return Array(
69  );
70  }
71 
72  /***********************************************************************************************************************/
73  /***********************/
77  public function __construct( $in_db_object = NULL,
78  $in_db_result = NULL,
79  $in_owner_id = NULL,
80  $in_tags_array = NULL,
92  $in_longitude = NULL,
93  $in_latitude = NULL
94  ) {
95 
96  parent::__construct($in_db_object, $in_db_result, $in_owner_id, $in_tags_array, $in_longitude, $in_latitude);
97 
98  $this->class_description = "This is a 'Place' Class for General Addresses.";
99  $this->instance_description = isset($this->name) && $this->name ? "$this->name ($this->_longitude, $this->_latitude)" : "($this->_longitude, $this->_latitude)";
100 
101  $this->set_address_elements($this->tags(), true);
102 
103  $bias = (NULL != $this->region_bias) ? 'region='.$this->region_bias.'&' : '';
104 
105  $this->google_geocode_uri_prefix = 'https://maps.googleapis.com/maps/api/geocode/json?'.$bias.'key='.CO_Config::$google_api_key.'&address=';
106  $this->google_lookup_uri_prefix = 'https://maps.googleapis.com/maps/api/geocode/json?'.$bias.'key='.CO_Config::$google_api_key.'&latlng=';
107  }
108 
109  /***********************/
115  public function set_address_elements ( $in_tags,
127  $dont_save = false
128  ) {
129  $ret = false;
130 
131  $this->address_elements = Array();
132  $labels = $this->_get_address_element_labels();
133 
134  for ($i = 0; $i < count($labels); $i++) {
135  $tag_value = '';
136 
137  if (test_is_assoc($in_tags)) {
138  $tag_value = isset($in_tags[$labels[$i]]) ? $in_tags[$labels[$i]] : '';
139  } else {
140  $tag_value = isset($in_tags[$i]) ? $in_tags[$i] : '';
141  }
142 
143  $this->set_address_element($i, $tag_value, true);
144  }
145 
146  if (!$dont_save) {
147  $ret = $this->update_db();
148  }
149 
150  return $ret;
151  }
152 
153  /***********************/
159  public function set_address_element( $in_index,
160  $in_value,
161  $dont_save = false
162  ) {
163  $ret = false;
164 
165  $in_index = intval($in_index);
166  $labels = $this->_get_address_element_labels();
167 
168  if ((0 <= $in_index) && ($in_index < count($labels))) {
169  $key = $labels[$in_index];
170 
171  $in_value = strval($in_value);
172 
173  $this->address_elements[$key] = $in_value;
174  $this->_tags[$in_index] = $in_value;
175 
176  if (!$dont_save) {
177  $ret = $this->update_db();
178  }
179  }
180 
181  return $ret;
182  }
183 
184  /***********************/
190  public function set_address_element_by_key( $in_key,
191  $in_value,
192  $dont_save = false
193  ) {
194  $ret = false;
195 
196  $in_index = intval($in_index);
197  $labels = $this->_get_address_element_labels();
198 
199  for ($i = 0; $i < count($labels); $i++) {
200  if ($labels[$i] == $in_key) {
201  $ret = $this->set_address_element($i, $in_value, $dont_save);
202  break;
203  }
204  }
205 
206  return $ret;
207  }
208 
209  /***********************/
213  public function get_address_element_by_index( $in_index
214  ) {
215  $ret = NULL;
216 
217  $labels = $this->_get_address_element_labels();
218 
219  if ((0 <= $in_index) && ($in_index < count($labels))) {
220  $key = $labels[$in_index];
221  $ret = $this->address_elements[$key];
222  }
223 
224  return $ret;
225  }
226 
227  /***********************/
234  public function lookup_address() {
235  $uri = $this->google_geocode_uri_prefix.urlencode($this->get_readable_address(false));
236  $http_status = '';
237  $error_catcher = '';
238 
239  $resulting_json = json_decode(CO_Chameleon_Utils::call_curl($uri, false, $http_status, $error_catcher));
240  if (isset($resulting_json) && $resulting_json &&isset($resulting_json->results) && is_array($resulting_json->results) && count($resulting_json->results)) {
241  if (isset($resulting_json->results[0]->geometry) && isset($resulting_json->results[0]->geometry->location) && isset($resulting_json->results[0]->geometry->location->lng) && isset($resulting_json->results[0]->geometry->location->lat)) {
242  return Array( 'longitude' => floatval($resulting_json->results[0]->geometry->location->lng), 'latitude' => floatval($resulting_json->results[0]->geometry->location->lat));
243  }
244  }
245 
249 
250  return NULL;
251  }
252 
253  /***********************/
260  public function geocode_long_lat() {
261  $uri = $this->google_lookup_uri_prefix . urlencode($this->raw_latitude()) . ',' . urlencode($this->raw_longitude());
262  $http_status = '';
263  $error_catcher = '';
264 
265  $resulting_json = json_decode(CO_Chameleon_Utils::call_curl($uri, false, $http_status, $error_catcher));
266  if (isset($resulting_json) && $resulting_json &&isset($resulting_json->results) && is_array($resulting_json->results) && count($resulting_json->results)) {
267  if (isset($resulting_json->results[0]->address_components) && is_array($resulting_json->results[0]->address_components) && count($resulting_json->results[0]->address_components)) {
268  $address_components = $resulting_json->results[0]->address_components;
269 
270  $labels = $this->_get_address_element_labels();
271  $ret = Array($labels[0] => '', $labels[1] => '', $labels[3] => '', $labels[4] => '', $labels[5] => '', $labels[6] => '');
272 
273  if (isset($labels[7])) {
274  $ret[$labels[7]] = '';
275  }
276 
277  foreach ($address_components as $component) {
278  $int_key = $component->types[0];
279 
280  switch ($int_key) {
281  case 'premise':
282  if ($ret[$labels[0]]) {
283  $ret[$labels[0]] = ' '.$ret[$labels[0]];
284  }
285  $ret[$labels[0]] = strval($component->long_name).$ret[$labels[0]];
286  break;
287  case 'street_number':
288  if ($ret[$labels[1]]) {
289  $ret[$labels[1]] = ' '.$ret[$labels[1]];
290  }
291  $ret[$labels[1]] = strval($component->short_name).$ret[$labels[1]];
292  break;
293  case 'route':
294  if ($ret[$labels[1]]) {
295  $ret[$labels[1]] .= ' ';
296  }
297  $ret[$labels[1]] .= strval($component->long_name);
298  break;
299  case 'locality':
300  $ret[$labels[3]] = strval($component->long_name);
301  break;
302  case 'administrative_area_level_1':
303  $ret[$labels[5]] = strval($component->short_name);
304  break;
305  case 'administrative_area_level_2':
306  $ret[$labels[4]] = strval($component->short_name);
307  break;
308  case 'postal_code':
309  if ($ret[$labels[6]]) {
310  $ret[$labels[6]] = '-'.$ret[$labels[6]];
311  }
312  $ret[$labels[6]] = strval($component->short_name).$ret[$labels[6]];
313  break;
314  case 'postal_code_suffix':
315  if ($ret[$labels[6]]) {
316  $ret[$labels[6]] .= '-';
317  }
318  $ret[$labels[6]] .= strval($component->short_name);
319  break;
320  case 'country':
321  if (isset($labels[7])) {
322  $ret[$labels[7]] = strval($component->short_name);
323  }
324  break;
325  }
326  }
327 
328  return $ret;
329  }
330  }
331 
335 
336  return NULL;
337  }
338 
339  /***********************/
343  public function get_readable_address( $with_venue = true
344  ) {
345  $ret = '';
346 
347  $tag_key_array = $this->_get_address_element_labels();
348 
349  if (isset($tag_key_array) && is_array($tag_key_array) && count($tag_key_array)) {
350  if ($with_venue && isset($tag_key_array[0]) && isset($this->address_elements[$tag_key_array[0]])) {
351  $ret = $this->address_elements[$tag_key_array[0]];
352  }
353 
354  if ($with_venue && isset($tag_key_array[2]) && isset($this->address_elements[$tag_key_array[2]]) && $this->address_elements[$tag_key_array[2]]) {
355  $open_paren = false;
356 
357  if ($ret) {
358  $ret .= ' (';
359  $open_paren = true;
360  }
361 
362  $ret .= $this->address_elements[$tag_key_array[2]];
363 
364  if ($open_paren) {
365  $ret .= ')';
366  }
367  }
368 
369  if (isset($tag_key_array[1]) && isset($this->address_elements[$tag_key_array[1]]) && $this->address_elements[$tag_key_array[1]]) {
370  if ($ret) {
371  $ret .= ', ';
372  }
373 
374  $ret .= $this->address_elements[$tag_key_array[1]];
375  }
376 
377  if (isset($tag_key_array[3]) && isset($this->address_elements[$tag_key_array[3]]) && $this->address_elements[$tag_key_array[3]]) {
378  if ($ret) {
379  $ret .= ', ';
380  }
381 
382  $ret .= $this->address_elements[$tag_key_array[3]];
383  }
384 
385  if (isset($tag_key_array[5]) && isset($this->address_elements[$tag_key_array[5]]) && $this->address_elements[$tag_key_array[5]]) {
386  if ($ret) {
387  $ret .= ', ';
388  }
389 
390  $ret .= $this->address_elements[$tag_key_array[5]];
391  }
392 
393  if (isset($tag_key_array[6]) && isset($this->address_elements[$tag_key_array[6]]) && $this->address_elements[$tag_key_array[6]]) {
394  if ($ret) {
395  $ret .= ' ';
396  }
397 
398  $ret .= $this->address_elements[$tag_key_array[6]];
399  }
400 
401  if (isset($tag_key_array[7]) && isset($this->address_elements[$tag_key_array[7]]) && $this->address_elements[$tag_key_array[7]]) {
402  if ($ret) {
403  $ret .= ' ';
404  }
405 
406  $ret .= $this->address_elements[$tag_key_array[7]];
407  }
408  }
409 
410  return $ret;
411  }
412 
413  /***********************/
419  public function set_tags_from_address_elements() {
420  $new_tags = $this->tags;
421  $labels = $this->_get_address_element_labels();
422 
423  for ($i = 0; $i < count($labels); $i++) {
424  $key = $labels[$i];
425  $new_tags[$key] = $this->address_elements[$key];
426  }
427 
428  return $this->set_tags($new_tags);
429  }
430 
431  /***********************/
435  public function get_address_elements() {
436  $ret = [];
437  $labels = $this->_get_address_element_labels();
438 
439  for ($i = 0; $i < count($labels); $i++) {
440  $key = $labels[$i];
441  if (isset($this->address_elements[$key]) && trim($this->address_elements[$key])) {
442  $ret[$key] = trim($this->address_elements[$key]);
443  }
444  }
445 
446  return $ret;
447  }
448 };
if(!defined('LGV_ACCESS_CATCHER')) if(!defined( 'LGV_ANDISOL_CATCHER'))
test_is_assoc(array $arr)
if(!defined('LGV_CHAMELEON_UTILS_CATCHER')) $utils_file
get_address_element_by_index( $in_index)
__construct( $in_db_object=NULL, $in_db_result=NULL, $in_owner_id=NULL, $in_tags_array=NULL, $in_longitude=NULL, $in_latitude=NULL)
$region_bias
This can be set by subclasses in order to set a region bias.
$google_lookup_uri_prefix
This is the lookup URI for the Google reverse Geocode.
_get_address_element_labels()
$google_geocode_uri_prefix
This is the Geocode URI for the Google Geocode.
set_address_element_by_key( $in_key, $in_value, $dont_save=false)
set_address_element( $in_index, $in_value, $dont_save=false)
get_address_elements()
set_tags_from_address_elements()
set_address_elements( $in_tags, $dont_save=false)
get_readable_address( $with_venue=true)
$address_elements
These are the address elements we use for creating lookup addresses.
static $chameleon_co_place_tag_0
These apply to the CO_Place class. Only the first seven tags are used for US location information.
Definition: common.inc.php:48
static $co_place_error_code_failed_to_geocode
Definition: common.inc.php:57
static $co_place_error_code_failed_to_lookup
Definition: common.inc.php:58
static $co_place_error_name_failed_to_geocode
Definition: en.php:32
static $co_place_error_name_failed_to_lookup
Definition: en.php:35
static $co_place_error_desc_failed_to_lookup
Definition: en.php:36
static $co_place_error_desc_failed_to_geocode
Definition: en.php:33
static call_curl( $in_uri, $in_post=false, &$http_status=NULL, &$content_failure_note=NULL)
This is a function that returns the results of an HTTP call to a URI. It is a lot more secure than fi...
This class provides a general error report, with file, method and error information.
Definition: error.class.php:32