CRM-16288
[civicrm-core.git] / CRM / Contact / BAO / ProximityQuery.php
index e6fe770572b28021e4310958ef2001ecfd9e337e..e4d56c47a7aea54b2d722a984f04433e8d31b4df 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -44,15 +44,13 @@ class CRM_Contact_BAO_ProximityQuery {
    * All function arguments and return values measure distances in metres
    * and angles in degrees.  The ellipsoid model is from the WGS-84 datum.
    * Ka-Ping Yee, 2003-08-11
-
    * earth_radius_semimajor = 6378137.0;
    * earth_flattening = 1/298.257223563;
    * earth_radius_semiminor = $earth_radius_semimajor * (1 - $earth_flattening);
    * earth_eccentricity_sq = 2*$earth_flattening - pow($earth_flattening, 2);
-
    * This library is an implementation of UCB CS graduate student, Ka-Ping Yee (http://www.zesty.ca).
    * This version has been taken from Drupal's location module: http://drupal.org/project/location
-   **/
+   */
 
   static protected $_earthFlattening;
   static protected $_earthRadiusSemiMinor;
@@ -72,18 +70,19 @@ class CRM_Contact_BAO_ProximityQuery {
     }
   }
 
-  /**
+  /*
    * Latitudes in all of U. S.: from -7.2 (American Samoa) to 70.5 (Alaska).
    * Latitudes in continental U. S.: from 24.6 (Florida) to 49.0 (Washington).
    * Average latitude of all U. S. zipcodes: 37.9.
    */
 
-  /*
-    /**
-     * Estimate the Earth's radius at a given latitude.
-     * Default to an approximate average radius for the United States.
-     */
-
+  /**
+   * Estimate the Earth's radius at a given latitude.
+   * Default to an approximate average radius for the United States.
+   *
+   * @param float $latitude
+   * @return float
+   */
   public static function earthRadius($latitude) {
     $lat = deg2rad($latitude);
 
@@ -95,15 +94,21 @@ class CRM_Contact_BAO_ProximityQuery {
   /**
    * Convert longitude and latitude to earth-centered earth-fixed coordinates.
    * X axis is 0 long, 0 lat; Y axis is 90 deg E; Z axis is north pole.
+   *
+   * @param float $longitude
+   * @param float $latitude
+   * @param float $height
+   *
+   * @return array
    */
   public static function earthXYZ($longitude, $latitude, $height = 0) {
     $long = deg2rad($longitude);
     $lat = deg2rad($latitude);
 
     $cosLong = cos($long);
-    $cosLat  = cos($lat);
+    $cosLat = cos($lat);
     $sinLong = sin($long);
-    $sinLat  = sin($lat);
+    $sinLat = sin($lat);
 
     $radius = self::$_earthRadiusSemiMajor / sqrt(1 - self::$_earthEccentricitySQ * $sinLat * $sinLat);
 
@@ -116,6 +121,10 @@ class CRM_Contact_BAO_ProximityQuery {
 
   /**
    * Convert a given angle to earth-surface distance.
+   *
+   * @param float $angle
+   * @param float $latitude
+   * @return float
    */
   public static function earthArcLength($angle, $latitude) {
     return deg2rad($angle) * self::earthRadius($latitude);
@@ -123,18 +132,23 @@ class CRM_Contact_BAO_ProximityQuery {
 
   /**
    * Estimate the min and max longitudes within $distance of a given location.
+   *
+   * @param float $longitude
+   * @param float $latitude
+   * @param float $distance
+   * @return array
    */
   public static function earthLongitudeRange($longitude, $latitude, $distance) {
-    $long   = deg2rad($longitude);
-    $lat    = deg2rad($latitude);
+    $long = deg2rad($longitude);
+    $lat = deg2rad($latitude);
     $radius = self::earthRadius($latitude);
 
-    $angle   = $distance / $radius;
-    $diff    = asin(sin($angle) / cos($lat));
+    $angle = $distance / $radius;
+    $diff = asin(sin($angle) / cos($lat));
     $minLong = $long - $diff;
     $maxLong = $long + $diff;
 
-    if ($minLong < - pi()) {
+    if ($minLong < -pi()) {
       $minLong = $minLong + pi() * 2;
     }
 
@@ -142,26 +156,32 @@ class CRM_Contact_BAO_ProximityQuery {
       $maxLong = $maxLong - pi() * 2;
     }
 
-    return array(rad2deg($minLong),
+    return array(
+      rad2deg($minLong),
       rad2deg($maxLong),
     );
   }
 
   /**
    * Estimate the min and max latitudes within $distance of a given location.
+   *
+   * @param float $longitude
+   * @param float $latitude
+   * @param float $distance
+   * @return array
    */
   public static function earthLatitudeRange($longitude, $latitude, $distance) {
-    $long   = deg2rad($longitude);
-    $lat    = deg2rad($latitude);
+    $long = deg2rad($longitude);
+    $lat = deg2rad($latitude);
     $radius = self::earthRadius($latitude);
 
-    $angle      = $distance / $radius;
-    $minLat     = $lat - $angle;
-    $maxLat     = $lat + $angle;
+    $angle = $distance / $radius;
+    $minLat = $lat - $angle;
+    $maxLat = $lat + $angle;
     $rightangle = pi() / 2.0;
 
     // wrapped around the south pole
-    if ($minLat < - $rightangle) {
+    if ($minLat < -$rightangle) {
       $overshoot = -$minLat - $rightangle;
       $minLat = -$rightangle + $overshoot;
       if ($minLat > $maxLat) {
@@ -180,15 +200,16 @@ class CRM_Contact_BAO_ProximityQuery {
       $maxLat = $rightangle;
     }
 
-    return array(rad2deg($minLat),
+    return array(
+      rad2deg($minLat),
       rad2deg($maxLat),
     );
   }
 
   /**
-   * @param $latitude
-   * @param $longitude
-   * @param $distance
+   * @param float $latitude
+   * @param float $longitude
+   * @param float $distance
    * @param string $tablePrefix
    *
    * @return string
@@ -199,17 +220,8 @@ class CRM_Contact_BAO_ProximityQuery {
     $params = array();
     $clause = array();
 
-    list($minLongitude, $maxLongitude) =
-      self::earthLongitudeRange($longitude,
-        $latitude,
-        $distance
-      );
-    list($minLatitude, $maxLatitude) =
-      self::earthLatitudeRange(
-        $longitude,
-        $latitude,
-        $distance
-      );
+    list($minLongitude, $maxLongitude) = self::earthLongitudeRange($longitude, $latitude, $distance);
+    list($minLatitude, $maxLatitude) = self::earthLatitudeRange($longitude, $latitude, $distance);
 
     // DONT consider NAN values (which is returned by rad2deg php function)
     // for checking BETWEEN geo_code's criteria as it throws obvious 'NAN' field not found DB: Error
@@ -243,9 +255,10 @@ ACOS(
 
   /**
    * @param $query
-   * @param $values
+   * @param array $values
    *
    * @throws Exception
+   * @return void
    */
   public static function process(&$query, &$values) {
     list($name, $op, $distance, $grouping, $wildcard) = $values;
@@ -277,7 +290,7 @@ ACOS(
     }
 
     if (empty($proximityAddress)) {
-      return;
+      return NULL;
     }
 
     if (isset($proximityAddress['state_province_id'])) {
@@ -296,12 +309,11 @@ ACOS(
       }
     }
 
-    if (isset($proximityAddress['country_id'])) {
+    if (!empty($proximityAddress['country_id'])) {
       $proximityAddress['country'] = CRM_Core_PseudoConstant::country($proximityAddress['country_id']);
       $qill[] = $proximityAddress['country'];
     }
 
-
     if (
       isset($proximityAddress['distance_unit']) &&
       $proximityAddress['distance_unit'] == 'miles'
@@ -317,7 +329,7 @@ ACOS(
     $qill = ts('Proximity search to a distance of %1 from %2',
       array(
         1 => $qillUnits,
-        2 => implode(', ', $qill)
+        2 => implode(', ', $qill),
       )
     );
 
@@ -328,7 +340,7 @@ ACOS(
 
     $query->_tables['civicrm_address'] = $query->_whereTables['civicrm_address'] = 1;
 
-    require_once (str_replace('_', DIRECTORY_SEPARATOR, $fnName) . '.php');
+    require_once str_replace('_', DIRECTORY_SEPARATOR, $fnName) . '.php';
     $fnName::format($proximityAddress);
     if (
       !is_numeric(CRM_Utils_Array::value('geo_code_1', $proximityAddress)) ||
@@ -338,7 +350,7 @@ ACOS(
       $qill .= ': ' . ts('We could not geocode the destination address.');
       $query->_qill[$grouping][] = $qill;
       $query->_where[$grouping][] = ' (0) ';
-      return;
+      return NULL;
     }
 
     $query->_qill[$grouping][] = $qill;
@@ -348,11 +360,14 @@ ACOS(
       $distance
     );
 
-    return;
+    return NULL;
   }
 
   /**
-   * @param $input
+   * @param array $input
+   * retun void
+   *
+   * @return null
    */
   public static function fixInputParams(&$input) {
     foreach ($input as $param) {
@@ -375,8 +390,9 @@ ACOS(
             }
           }
         }
-        return;
+        return NULL;
       }
     }
   }
+
 }