PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/sites/all/modules/civicrm/CRM/Utils/Geocode/Yahoo.php

https://github.com/timstephenson/NatureBridge
PHP | 187 lines | 90 code | 29 blank | 68 comment | 19 complexity | 11b0debd768d7efbcb512e1e3583e259 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * Class that uses geocoder.us to retrieve the lat/long of an address
  36. */
  37. class CRM_Utils_Geocode_Yahoo {
  38. /**
  39. * server to retrieve the lat/long
  40. *
  41. * @var string
  42. * @static
  43. */
  44. static protected $_server = 'api.local.yahoo.com';
  45. /**
  46. * uri of service
  47. *
  48. * @var string
  49. * @static
  50. */
  51. static protected $_uri = '/MapsService/V1/geocode';
  52. /**
  53. * function that takes an address array and gets the latitude / longitude
  54. * and postal code for this address. Note that at a later stage, we could
  55. * make this function also clean up the address into a more valid format
  56. *
  57. * @param array $values associative array of address data: country, street_address, city, state_province, postal code
  58. * @param boolean $stateName this parameter currently has no function
  59. *
  60. * @return boolean true if we modified the address, false otherwise
  61. * @static
  62. */
  63. static function format( &$values, $stateName = false ) {
  64. CRM_Utils_System::checkPHPVersion( 5, true );
  65. // we need a valid country, else we ignore
  66. if ( ! CRM_Utils_Array::value( 'country' , $values ) ) {
  67. return false;
  68. }
  69. $config = CRM_Core_Config::singleton( );
  70. $arg = array( );
  71. $arg[] = "appid=" . urlencode( $config->mapAPIKey );
  72. if ( CRM_Utils_Array::value( 'street_address', $values ) ) {
  73. $arg[] = "street=" . urlencode( $values['street_address'] );
  74. }
  75. $city = CRM_Utils_Array::value( 'city', $values );
  76. if ( $city ) {
  77. $arg[] = "city=" . urlencode( $city );
  78. }
  79. if ( CRM_Utils_Array::value( 'state_province', $values ) ) {
  80. if ( CRM_Utils_Array::value( 'state_province_id', $values ) ) {
  81. $stateProvince = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_StateProvince', $values['state_province_id'] );
  82. } else {
  83. if ( ! $stateName ) {
  84. $stateProvince = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_StateProvince', $values['state_province'], 'name', 'abbreviation' );
  85. } else {
  86. $stateProvince = $values['state_province'];
  87. }
  88. }
  89. // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
  90. if ( $stateProvince != $city ) {
  91. $arg[] = "state=" . urlencode( $stateProvince );
  92. }
  93. }
  94. if ( CRM_Utils_Array::value( 'country', $values ) ) {
  95. $arg[] = "country=" . urlencode( $values['country'] );
  96. }
  97. if ( CRM_Utils_Array::value( 'postal_code', $values ) ) {
  98. $arg[] = "zip=" . urlencode( $values['postal_code'] );
  99. }
  100. $args = implode( '&', $arg );
  101. $query = 'http://' . self::$_server . self::$_uri . '?' . $args;
  102. require_once 'HTTP/Request.php';
  103. $request = new HTTP_Request( $query );
  104. $request->sendRequest( );
  105. $string = $request->getResponseBody( );
  106. $xml = simplexml_load_string( $string );
  107. $ret = array( );
  108. $ret['precision'] = (string)$xml->Result['precision'];
  109. if ( is_a($xml->Result, 'SimpleXMLElement') ) {
  110. $result = array( ) ;
  111. $result = get_object_vars($xml->Result);
  112. foreach ( $result as $key => $val ) {
  113. if (is_scalar( $val ) &&
  114. strlen($val)) {
  115. $ret[(string)$key] = (string)$val;
  116. }
  117. }
  118. $values['geo_code_1'] = $ret['Latitude' ];
  119. $values['geo_code_2'] = $ret['Longitude'];
  120. if ( $ret['Zip'] ) {
  121. $current_pc = CRM_Utils_Array::value( 'postal_code', $values );
  122. $skip_zip = false;
  123. if ( $current_pc ) {
  124. $current_pc_suffix = CRM_Utils_Array::value( 'postal_code_suffix', $values );
  125. $current_pc_complete = $current_pc . $current_pc_suffix;
  126. $new_pc_complete = preg_replace("/[+-]/", '', $ret['Zip']);
  127. // if a postal code was already entered, don't change it, except to make it more precise
  128. if ( strpos( $new_pc_complete, $current_pc_complete ) !== 0) {
  129. $msg = ts( 'The Yahoo Geocoding system returned a different postal code (%1) than the one you entered (%2). If you want the Yahoo value, please delete the current postal code and save again.',
  130. array( 1 => $ret['Zip'],
  131. 2 => $current_pc . $current_pc_suffix ? "-$current_pc_suffix" : '' ) );
  132. CRM_Core_Session::setStatus( $msg );
  133. $skip_zip = true;
  134. }
  135. }
  136. if ( ! $skip_zip ) {
  137. $values['postal_code'] = $ret['Zip'];
  138. /* the following logic to split the string was borrowed from
  139. CRM/Core/BAO/Address.php -- CRM_Core_BAO_Address::fixAddress.
  140. This is actually the function that calls the geocoding
  141. script to begin with, but the zipcode business takes
  142. place before geocoding gets called.
  143. */
  144. if ( preg_match('/^(\d{4,5})[+-](\d{4})$/',
  145. $ret['Zip'],
  146. $match) ) {
  147. $values['postal_code'] = $match[1];
  148. $values['postal_code_suffix'] = $match[2];
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. // reset the geo code values if we did not get any good values
  155. $values['geo_code_1'] = $values['geo_code_2'] = 'null';
  156. return false;
  157. }
  158. }