/drupal/sites/all/modules/civicrm/CRM/Utils/Sunlight.php

https://github.com/michaelmcandrew/citylink · PHP · 146 lines · 92 code · 23 blank · 31 comment · 8 complexity · afec3906149f794e323bb13651a5fd9e MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 2.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. class CRM_Utils_Sunlight {
  34. static $_apiURL = 'http://api.sunlightlabs.com/';
  35. static $_apiKey = null;
  36. static function makeAPICall( $uri ) {
  37. require_once 'HTTP/Request.php';
  38. $params = array( 'method' => HTTP_REQUEST_METHOD_GET,
  39. 'allowRedirects' => false );
  40. $request =& new HTTP_Request( self::$_apiURL . $uri, $params );
  41. $result = $request->sendRequest( );
  42. if ( PEAR::isError( $result ) ) {
  43. CRM_Core_Error::fatal( $result->getMessage( ) );
  44. }
  45. if ( $request->getResponseCode( ) != 200 ) {
  46. CRM_Core_Error::fatal( ts( 'Invalid response code received from Sunlight servers: %1',
  47. array(1 => $request->getResponseCode())) );
  48. }
  49. $string = $request->getResponseBody( );
  50. return simplexml_load_string( $string );
  51. }
  52. static function getCityState( $zipcode ) {
  53. $key = self::$_apiKey;
  54. $uri = "places.getCityStateFromZip.php?zip={$zipcode}&apikey={$key}&output=xml";
  55. $xml = self::makeAPICall( $uri );
  56. return array( $xml->city, $xml->state );
  57. }
  58. static function getDetailedInfo( $peopleID ) {
  59. $key = self::$_apiKey;
  60. $uri = "people.getPersonInfo.php?id={$peopleID}&apikey={$key}&output=xml";
  61. $xml = self::makeAPICall( $uri );
  62. $result = array( );
  63. $fields = array( 'title' => 'title',
  64. 'firstname' => 'first_name',
  65. 'lastname' => 'last_name',
  66. 'gender' => 'gender',
  67. 'party' => 'party',
  68. 'congress_office' => 'address',
  69. 'phone' => 'phone',
  70. 'email' => 'email',
  71. 'congresspedia' => 'url',
  72. 'photo' => 'image_url',
  73. 'webform' => 'contact_url', );
  74. foreach ( $fields as $old => $new ) {
  75. $result[$new] = (string ) $xml->$old;
  76. }
  77. $result['image_url'] =
  78. 'http://sunlightlabs.com/widgets/popuppoliticians/resources/images/' .
  79. $result['image_url'];
  80. return $result;
  81. }
  82. static function getPeopleInfo( $uri ) {
  83. $xml = self::makeAPICall( $uri );
  84. $result = array( );
  85. foreach ( $xml->entity_id_list->entity_id as $key => $value ) {
  86. $result[] = self::getDetailedInfo( $value );
  87. }
  88. return $result;
  89. }
  90. static function getRepresentativeInfo( $city, $state ) {
  91. if ( ! $city ||
  92. ! $state ) {
  93. return null;
  94. }
  95. $key = self::$_apiKey;
  96. $city = urlencode( $city );
  97. $uri = "people.reps.getRepsFromCityState.php?city={$city}&state={$state}&apikey={$key}&output=xml";
  98. return self::getPeopleInfo( $uri );
  99. }
  100. static function getSenatorInfo( $state ) {
  101. if ( ! $state ) {
  102. return null;
  103. }
  104. $key = self::$_apiKey;
  105. $uri = "people.sens.getSensFromState.php?state={$state}&apikey={$key}&output=xml";
  106. return self::getPeopleInfo( $uri );
  107. }
  108. static function getInfo( $city, $state, $zipcode = null ) {
  109. if ( $zipcode ) {
  110. list( $city, $state ) = self::getCityState( $zipcode );
  111. }
  112. $reps = self::getRepresentativeInfo( $city, $state );
  113. $sens = self::getSenatorInfo( $state );
  114. $result = array( );
  115. if ( is_array( $reps ) ) {
  116. $result = array_merge( $result, $reps );
  117. }
  118. if ( is_array( $sens ) ) {
  119. $result = array_merge( $result, $sens );
  120. }
  121. return $result;
  122. }
  123. }