BAOBAB
co_ll_location.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_class_dir().'/co_main_db_record.class.php');
29 
30 /***************************************************************************************************************************/
39  protected $_longitude;
40  protected $_latitude;
41 
42  /***********************************************************************************************************************/
43  /***********************/
53  protected function _default_setup() {
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  }
60 
61  /***********************/
69  protected function _build_parameter_array() {
70  $ret = parent::_build_parameter_array();
71 
72  $ret['longitude'] = $this->_longitude;
73  $ret['latitude'] = $this->_latitude;
74 
75  return $ret;
76  }
77 
78  /***********************/
82  protected function _km_per_degree() {
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  }
96 
97  /***********************/
101  protected function _fuzz_me() {
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  }
124 
125  /***********************************************************************************************************************/
126  /***********************/
130  public function __construct( $in_db_object = NULL,
131  $in_db_result = NULL,
132  $in_owner_id = NULL,
133  $in_tags_array = NULL,
134  $in_longitude = NULL,
135  $in_latitude = NULL,
136  $in_fuzz_factor = NULL,
137  $in_can_see_through_the_fuzz = NULL
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  }
159 
160  /***********************/
166  public function load_from_db( $in_db_result
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  }
191 
192  /***********************/
200  public function update_db() {
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  }
210 
211  /***********************/
217  public function set_longitude( $in_new_value
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  }
228 
229  /***********************/
235  public function set_latitude( $in_new_value
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  }
246 
247  /***********************/
253  public function fuzz_factor() {
254  return isset($this->context['fuzz_factor']) ? abs(floatval($this->context['fuzz_factor'])) : 0.0;
255  }
256 
257  /***********************/
261  public function is_fuzzy() {
262  return 0.0 < $this->fuzz_factor();
263  }
264 
265  /***********************/
271  public function set_fuzz_factor( $in_new_value
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  }
289 
290  /***********************/
296  public function raw_longitude() {
297  if ($this->i_can_see_clearly_now()) {
298  return $this->_longitude;
299  } else {
300  return $this->longitude();
301  }
302  }
303 
304  /***********************/
310  public function raw_latitude() {
311  if ($this->i_can_see_clearly_now()) {
312  return $this->_latitude;
313  } else {
314  return $this->latitude();
315  }
316  }
317 
318  /***********************/
324  public function longitude() {
325  return $this->_fuzz_me()['longitude'];
326  }
327 
328  /***********************/
334  public function latitude() {
335  return $this->_fuzz_me()['latitude'];
336  }
337 
338  /***********************/
344  public function set_can_see_through_the_fuzz( $in_id
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  }
370 
371  /***********************/
377  public function can_see_through_the_fuzz() {
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  }
392 
393  /***********************/
397  public function i_can_see_clearly_now() {
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  }
412 };
static get_accurate_distance( $lat1, $lon1, $lat2, $lon2)
Uses the Vincenty calculation to determine the distance (in Kilometers) between the two given lat/lon...
load_from_db( $in_db_result)
__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)
set_fuzz_factor( $in_new_value)
set_latitude( $in_new_value)
set_longitude( $in_new_value)
set_can_see_through_the_fuzz( $in_id)