BAOBAB
CO_LL_Location Class Reference
Inheritance diagram for CO_LL_Location:
Collaboration diagram for CO_LL_Location:

Public Member Functions

 __construct ( $in_db_object=NULL, $in_db_result=NULL, $in_owner_id=NULL, $in_tags_array=NULL, $in_longitude=NULL, $in_latitude=NULL, $in_fuzz_factor=NULL, $in_can_see_through_the_fuzz=NULL)
 
 load_from_db ( $in_db_result)
 
 update_db ()
 
 set_longitude ( $in_new_value)
 
 set_latitude ( $in_new_value)
 
 fuzz_factor ()
 
 is_fuzzy ()
 
 set_fuzz_factor ( $in_new_value)
 
 raw_longitude ()
 
 raw_latitude ()
 
 longitude ()
 
 latitude ()
 
 set_can_see_through_the_fuzz ( $in_id)
 
 can_see_through_the_fuzz ()
 
 i_can_see_clearly_now ()
 
- Public Member Functions inherited from CO_Main_DB_Record
 __construct ( $in_db_object=NULL, $in_db_result=NULL, $in_owner_id=NULL, $in_tags_array=NULL)
 
 set_owner_id ( $in_new_id)
 
 set_tags ( $in_tags_array)
 
 set_tag ( $in_tag_index, $in_tag_value)
 
 get_payload ()
 
 set_payload ( $in_payload)
 
 owner_id ()
 
 tags ()
 
- Public Member Functions inherited from A_CO_DB_Table_Base
 __construct ( $in_db_object=NULL, $in_db_result=NULL)
 
 set_batch_mode ()
 
 clear_batch_mode ()
 
 id ()
 
 lock ()
 
 locked ()
 
 danger_will_robinson_danger_clear_id ()
 
 user_can_read ()
 
 user_can_write ()
 
 set_read_security_id ($in_new_id)
 
 set_write_security_id ($in_new_id)
 
 set_name ($in_new_value)
 
 delete_from_db ()
 
 reload_from_db ()
 
 get_access_object ()
 
 get_lang ()
 
 set_lang ( $in_lang_id=NULL)
 

Protected Member Functions

 _default_setup ()
 
 _build_parameter_array ()
 
 _km_per_degree ()
 
 _fuzz_me ()
 
- Protected Member Functions inherited from A_CO_DB_Table_Base
 _badger_serialize ( $in_data)
 
 _badger_unserialize ( $in_data)
 
 _write_to_db ()
 
 _seppuku ()
 

Protected Attributes

 $_longitude
 
 $_latitude
 
- Protected Attributes inherited from CO_Main_DB_Record
 $_owner_id
 
 $_tags
 
- Protected Attributes inherited from A_CO_DB_Table_Base
 $_db_object
 This is the actual database object that "owns" this instance. It should not be exposed beyond this class or subclasses, thereof. More...
 
 $_id
 This is the within-table unique ID of this record. More...
 
 $_batch_mode
 If this is true, then the write_record call will not be made in update_db. It will be done when clear_batch_mode() is called, instead. More...
 

Additional Inherited Members

- Public Attributes inherited from A_CO_DB_Table_Base
 $class_description
 This is a description of the class (not the instance). More...
 
 $instance_description
 This is a description that describes the instance. More...
 
 $last_access
 This is a UNIX epoch date that describes the last modification. The default is UNIX Day Two (in case of UTC timezone issues). More...
 
 $name
 This is the "object_name" string field. More...
 
 $read_security_id
 This is a single integer, defining the security ID required to view the record. If it is 0, then it is "open.". More...
 
 $write_security_id
 This is a single integer, defining the required security token to modify the record. If it is 0, then any logged-in user can modify. More...
 
 $context
 This is a mixed associative array, containing fields for the object. More...
 
 $error
 If there is an error, it is contained here, in a LGV_Error instance. More...
 
- Static Public Attributes inherited from CO_Main_DB_Record
static $s_table_name = 'co_data_nodes'
 

Detailed Description

This is a specialization of the basic data class, implementing the long/lat fields (built into the table structure, but unused by base classes).

It has a "fuzz factor" built in. This is because some organizations, for reasons of privacy or law, don't want exact locations disclosed.

You can define a "fuzz factor" of how many kilometers you want as a "fuzzy circle" around the point.

Definition at line 38 of file co_ll_location.class.php.

Constructor & Destructor Documentation

◆ __construct()

CO_LL_Location::__construct (   $in_db_object = NULL,
  $in_db_result = NULL,
  $in_owner_id = NULL,
  $in_tags_array = NULL,
  $in_longitude = NULL,
  $in_latitude = NULL,
  $in_fuzz_factor = NULL,
  $in_can_see_through_the_fuzz = NULL 
)

Constructor (Initializer)

Parameters
$in_db_objectThe database object for this instance.
$in_db_resultThe database row for this instance (associative array, with database keys).
$in_owner_idThe ID of the object (in the database) that "owns" this instance.
$in_tags_arrayAn array of strings, up to ten elements long, for the tags.
$in_longitudeAn initial longitude value.
$in_latitudeAn initial latitude value.
$in_fuzz_factorAn initial "fuzz factor" value.
$in_can_see_through_the_fuzzThis is a security token for being able to see the value as a raw value (unfuzzed).

Definition at line 130 of file co_ll_location.class.php.

138  {
139  parent::__construct($in_db_object, $in_db_result, $in_owner_id, $in_tags_array);
140 
141  if (NULL != $in_longitude) {
142  $this->_longitude = $in_longitude;
143  }
144 
145  if (NULL != $in_latitude) {
146  $this->_latitude = $in_latitude;
147  }
148 
149  if ((NULL != $in_fuzz_factor) && (0 != $in_fuzz_factor)) {
150  $this->context['fuzz_factor'] = $in_fuzz_factor;
151  } elseif (!isset($this->context['fuzz_factor']) || !$this->context['fuzz_factor']) {
152  $this->context['fuzz_factor'] = 0;
153  }
154 
155  if (NULL != $in_can_see_through_the_fuzz) {
156  $this->context['can_see_through_the_fuzz'] = $in_can_see_through_the_fuzz;
157  }
158  }

Member Function Documentation

◆ _build_parameter_array()

CO_LL_Location::_build_parameter_array ( )
protected

This builds up the basic section of the instance database record. It should be overloaded, and the parent called before adding new fields.

This method overloads (and calls) the base class method.

Returns
an associative array, in database record form.

Reimplemented from CO_Main_DB_Record.

Definition at line 69 of file co_ll_location.class.php.

69  {
70  $ret = parent::_build_parameter_array();
71 
72  $ret['longitude'] = $this->_longitude;
73  $ret['latitude'] = $this->_latitude;
74 
75  return $ret;
76  }

◆ _default_setup()

CO_LL_Location::_default_setup ( )
protected

This is called to populate the object fields for this class with default values. These use the SQL table tags.

This should be subclassed, and the parent should be called before applying specific instance properties.

This method overloads (and calls) the base class method.

Returns
An associative array, simulating a database read.

Reimplemented from CO_Main_DB_Record.

Definition at line 53 of file co_ll_location.class.php.

53  {
54  $default_setup = parent::_default_setup();
55  $default_setup['longitude'] = (NULL != $this->_longitude) ? $this->_longitude : 0;
56  $default_setup['latitude'] = (NULL != $this->_latitude) ? $this->_latitude : 0;
57 
58  return $default_setup;
59  }

◆ _fuzz_me()

CO_LL_Location::_fuzz_me ( )
protected
Returns
an associative array, with a "fuzzed" long/lat (if there is no fuzz factor, it is the raw long/lat).

Definition at line 101 of file co_ll_location.class.php.

101  {
102  $ret = Array('longitude' => $this->_longitude, 'latitude' => $this->_latitude);
103 
104  $fuzz_factor = $this->fuzz_factor();
105  if (0 < $fuzz_factor) {
106  // The big number gives it lots of fuzz.
107  $long_offset = function_exists('random_int') ? random_int(0, 100000 * ($fuzz_factor)) / 100000.0 : rand(0, 100000 * ($fuzz_factor)) / 100000.0;
108  $lat_offset = function_exists('random_int') ? random_int(0, 100000 * ($fuzz_factor)) / 100000.0 : rand(0, 100000 * ($fuzz_factor)) / 100000.0;
109 
110  // Convert the fuziness to degrees.
111  $km_per_degree = $this->_km_per_degree();
112  $long_offset /= $km_per_degree;
113  $lat_offset /= $km_per_degree;
114 
115  // This determines the direction we go. We make each axis an independent rand().
116  $long_offset *= (rand(0, 9) < 5 ? -1.0 : 1.0);
117  $lat_offset *= (rand(0, 9) < 5 ? -1.0 : 1.0);
118 
119  $ret['longitude'] += $long_offset;
120  $ret['latitude'] += $lat_offset;
121  }
122  return $ret;
123  }

◆ _km_per_degree()

CO_LL_Location::_km_per_degree ( )
protected
Returns
a floating-point number, with the Km per degree long/lat (Changes for different latitudes, and is only good for the immediate vicinity).

Definition at line 82 of file co_ll_location.class.php.

82  {
83  // We do an average. Take four points in a "cross" around the center, then average them. We go out ten Km, so there's some distance.
84  $spot0 = Array('longitude' => $this->_longitude, 'latitude' => $this->_latitude);
85  $spot1 = Array('longitude' => $this->_longitude + 10.0, 'latitude' => $this->_latitude);
86  $spot2 = Array('longitude' => $this->_longitude - 10.0, 'latitude' => $this->_latitude);
87  $spot3 = Array('longitude' => $this->_longitude, 'latitude' => $this->_latitude + 10.0);
88  $spot4 = Array('longitude' => $this->_longitude, 'latitude' => $this->_latitude - 10.0);
89  $distance1 = abs(CO_Main_Data_DB::get_accurate_distance($spot0['latitude'], $spot0['longitude'], $spot1['latitude'], $spot1['longitude']));
90  $distance2 = abs(CO_Main_Data_DB::get_accurate_distance($spot0['latitude'], $spot0['longitude'], $spot2['latitude'], $spot2['longitude']));
91  $distance3 = abs(CO_Main_Data_DB::get_accurate_distance($spot0['latitude'], $spot0['longitude'], $spot3['latitude'], $spot3['longitude']));
92  $distance4 = abs(CO_Main_Data_DB::get_accurate_distance($spot0['latitude'], $spot0['longitude'], $spot4['latitude'], $spot4['longitude']));
93 
94  return ($distance1 + $distance2 + $distance3 + $distance4) / 40.0;
95  }
static get_accurate_distance( $lat1, $lon1, $lat2, $lon2)
Uses the Vincenty calculation to determine the distance (in Kilometers) between the two given lat/lon...

References CO_Main_Data_DB\get_accurate_distance().

Here is the call graph for this function:

◆ can_see_through_the_fuzz()

CO_LL_Location::can_see_through_the_fuzz ( )

Getter for a security ID token that can see past the fuzz factor.

Returns
an integer, with the ID (as long as we can read it). 0 if we don't have permission for the ID, or there is none.

Definition at line 377 of file co_ll_location.class.php.

377  {
378  $ret = 0;
379 
380  $ids = $this->get_access_object()->get_security_ids();
381 
382  $my_see_item = intval($this->write_security_id);
383 
384  $the_id = isset($this->context['can_see_through_the_fuzz']) ? intval($this->context['can_see_through_the_fuzz']) : 0;
385 
386  if (isset($ids) && is_array($ids) && count($ids)) {
387  $ret = in_array($the_id, $ids) ? $the_id : 0;
388  }
389 
390  return $ret;
391  }

◆ fuzz_factor()

CO_LL_Location::fuzz_factor ( )

Getter for fuzz factor.

Returns
the fuzz factor, as a float. If it is not set, then it is zero.

Definition at line 253 of file co_ll_location.class.php.

253  {
254  return isset($this->context['fuzz_factor']) ? abs(floatval($this->context['fuzz_factor'])) : 0.0;
255  }

◆ i_can_see_clearly_now()

CO_LL_Location::i_can_see_clearly_now ( )
Returns
true, if current user has the ability to see the raw values.

Definition at line 397 of file co_ll_location.class.php.

397  {
398  $ret = !$this->is_fuzzy() || $this->user_can_write(); // If we aren't fuzzed, then, no problem. Peep away. Writers can see.
399 
400  if (!$ret && $this->get_access_object()->security_db_available()) { // Only logged-in users get to see clearly.
401  if (!$ret && isset($this->context['can_see_through_the_fuzz'])) {
402  $ids = $this->get_access_object()->get_security_ids();
403 
404  $my_see_item = intval($this->context['can_see_through_the_fuzz']);
405  if (isset($ids) && is_array($ids) && count($ids)) {
406  $ret = in_array($my_see_item, $ids);
407  }
408  }
409  }
410  return $ret;
411  }

◆ is_fuzzy()

CO_LL_Location::is_fuzzy ( )
Returns
true, if the instance has a "fuzz factor."

Definition at line 261 of file co_ll_location.class.php.

261  {
262  return 0.0 < $this->fuzz_factor();
263  }

◆ latitude()

CO_LL_Location::latitude ( )

This returns the longitude, with any "fuzz factor" applied.

Returns
The current latitude value.

Definition at line 334 of file co_ll_location.class.php.

334  {
335  return $this->_fuzz_me()['latitude'];
336  }

◆ load_from_db()

CO_LL_Location::load_from_db (   $in_db_result)

This function sets up this instance, according to the DB-formatted associative array passed in.

Returns
true, if the instance was able to set itself up to the provided array.
Parameters
$in_db_resultThis is an associative array, formatted as a database row response.

Reimplemented from CO_Main_DB_Record.

Reimplemented in CO_User_Collection, CO_US_Place_Collection, CO_Place_Collection, CO_KeyValue_CO_Collection, and CO_Collection.

Definition at line 166 of file co_ll_location.class.php.

167  {
168  $ret = parent::load_from_db($in_db_result);
169 
170  if ($ret) {
171  $this->class_description = 'A basic class for long/lat locations.';
172 
173  if ($this->_db_object) {
174  if (isset($in_db_result['longitude'])) {
175  $this->_longitude = doubleval($in_db_result['longitude']);
176  }
177 
178  if (isset($in_db_result['latitude'])) {
179  $this->_latitude = doubleval($in_db_result['latitude']);
180  }
181  }
182 
183  $ll_string = ((NULL != $this->_longitude) && (NULL != $this->_latitude)) ? "($this->_longitude, $this->_latitude)" : '';
184 
185  $this->class_description = "Generic longitude/latitude Class.";
186  $this->instance_description = isset($this->name) && $this->name ? "$this->name $ll_string" : $ll_string;
187  }
188 
189  return $ret;
190  }

◆ longitude()

CO_LL_Location::longitude ( )

This returns the longitude, with any "fuzz factor" applied.

Returns
The current longitude value.

Definition at line 324 of file co_ll_location.class.php.

324  {
325  return $this->_fuzz_me()['longitude'];
326  }

◆ raw_latitude()

CO_LL_Location::raw_latitude ( )

This returns the latitude. However, if the user is not logged in, or doesn't have read rights (which shouldn't happen, anyway), they will only get the "fuzzed" version.

Returns
The current longitude value.

Definition at line 310 of file co_ll_location.class.php.

310  {
311  if ($this->i_can_see_clearly_now()) {
312  return $this->_latitude;
313  } else {
314  return $this->latitude();
315  }
316  }

Referenced by CO_Place\geocode_long_lat().

Here is the caller graph for this function:

◆ raw_longitude()

CO_LL_Location::raw_longitude ( )

This returns the longitude. However, if the user is not logged in, or doesn't have read rights (which shouldn't happen, anyway), they will only get the "fuzzed" version.

Returns
The current longitude value.

Definition at line 296 of file co_ll_location.class.php.

296  {
297  if ($this->i_can_see_clearly_now()) {
298  return $this->_longitude;
299  } else {
300  return $this->longitude();
301  }
302  }

Referenced by CO_Place\geocode_long_lat().

Here is the caller graph for this function:

◆ set_can_see_through_the_fuzz()

CO_LL_Location::set_can_see_through_the_fuzz (   $in_id)

Setter for a security ID token that can see past the fuzz factor.

Returns
true, if the save was successful.
Parameters
$in_idThe ID to set. If 0 or NULL, the value is removed.

Definition at line 344 of file co_ll_location.class.php.

345  {
346  $ret = false;
347 
348  if ($this->user_can_write()) {
349  $in_id = intval($in_id);
350 
351  if (0 == $in_id) {
352  unset($this->context['can_see_through_the_fuzz']);
353  } else {
354  $ids = $this->get_access_object()->get_security_ids();
355 
356  $in_id = (in_array($in_id, $ids) || $this->get_access_object()->god_mode()) ? $in_id : 0;
357 
358  if ($in_id) {
359  $this->context['can_see_through_the_fuzz'] = $in_id;
360  } else {
361  unset($this->context['can_see_through_the_fuzz']);
362  }
363  }
364 
365  $ret = $this->update_db();
366  }
367 
368  return $ret;
369  }

◆ set_fuzz_factor()

CO_LL_Location::set_fuzz_factor (   $in_new_value)

Setter for fuzz factor.

Returns
true, if the save was successful.
Parameters
$in_new_valueThe new value must be a positive floating-point value over (and including) 0. If zero, the factor is deleted.

Definition at line 271 of file co_ll_location.class.php.

272  {
273  $ret = false;
274 
275  $in_new_value = abs(floatval($in_new_value));
276 
277  if ($this->user_can_write()) {
278  if (0 == $in_new_value) {
279  unset($this->context['fuzz_factor']);
280  } else {
281  $this->context['fuzz_factor'] = $in_new_value;
282  }
283 
284  $ret = $this->update_db();
285  }
286 
287  return $ret;
288  }

◆ set_latitude()

CO_LL_Location::set_latitude (   $in_new_value)

Setter for latitude.

Returns
true, if the save was successful.

Definition at line 235 of file co_ll_location.class.php.

236  {
237  $ret = false;
238 
239  if (isset($in_new_value) && $this->user_can_write()) {
240  $this->_latitude = floatval($in_new_value);
241  $ret = $this->update_db();
242  }
243 
244  return $ret;
245  }

◆ set_longitude()

CO_LL_Location::set_longitude (   $in_new_value)

Setter for longitude.

Returns
true, if the save was successful.

Definition at line 217 of file co_ll_location.class.php.

218  {
219  $ret = false;
220 
221  if (isset($in_new_value) && $this->user_can_write()) {
222  $this->_longitude = floatval($in_new_value);
223  $ret = $this->update_db();
224  }
225 
226  return $ret;
227  }

◆ update_db()

CO_LL_Location::update_db ( )

This is a "trigger" to update the database with the current instance state.

This checks to make sure the user has write permission before saving.

Returns
true, if a DB update was successful.

Reimplemented from A_CO_DB_Table_Base.

Definition at line 200 of file co_ll_location.class.php.

200  {
201  $ret = parent::update_db();
202  if ($ret) {
203  $ll_string = ((NULL != $this->_longitude) && (NULL != $this->_latitude)) ? "($this->_longitude, $this->_latitude)" : '';
204 
205  $this->instance_description = isset($this->name) && $this->name ? "$this->name $ll_string" : $ll_string;
206  }
207 
208  return $ret;
209  }

Referenced by CO_Place\set_address_element(), and CO_Place\set_address_elements().

Here is the caller graph for this function:

Member Data Documentation

◆ $_latitude

CO_LL_Location::$_latitude
protected

Definition at line 40 of file co_ll_location.class.php.

◆ $_longitude

CO_LL_Location::$_longitude
protected

Definition at line 39 of file co_ll_location.class.php.