Merge pull request #6953 from joannechester/CRM-17381
[civicrm-core.git] / CRM / Utils / Sunlight.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Utils_Sunlight {
34 static $_apiURL = 'http://api.sunlightlabs.com/';
35 static $_apiKey = NULL;
36
37 /**
38 * @param $uri
39 *
40 * @return SimpleXMLElement
41 * @throws Exception
42 */
43 public static function makeAPICall($uri) {
44 require_once 'HTTP/Request.php';
45 $params = array(
46 'method' => HTTP_REQUEST_METHOD_GET,
47 'allowRedirects' => FALSE,
48 );
49
50 $request = new HTTP_Request(self::$_apiURL . $uri, $params);
51 $result = $request->sendRequest();
52 if (PEAR::isError($result)) {
53 CRM_Core_Error::fatal($result->getMessage());
54 }
55 if ($request->getResponseCode() != 200) {
56 CRM_Core_Error::fatal(ts('Invalid response code received from Sunlight servers: %1',
57 array(1 => $request->getResponseCode())
58 ));
59 }
60 $string = $request->getResponseBody();
61 return simplexml_load_string($string);
62 }
63
64 /**
65 * @param $zipcode
66 *
67 * @return array
68 */
69 public static function getCityState($zipcode) {
70 $key = self::$_apiKey;
71 $uri = "places.getCityStateFromZip.php?zip={$zipcode}&apikey={$key}&output=xml";
72 $xml = self::makeAPICall($uri);
73
74 return array($xml->city, $xml->state);
75 }
76
77 /**
78 * @param int $peopleID
79 *
80 * @return array
81 */
82 public static function getDetailedInfo($peopleID) {
83 $key = self::$_apiKey;
84 $uri = "people.getPersonInfo.php?id={$peopleID}&apikey={$key}&output=xml";
85 $xml = self::makeAPICall($uri);
86
87 $result = array();
88 $fields = array(
89 'title' => 'title',
90 'firstname' => 'first_name',
91 'lastname' => 'last_name',
92 'gender' => 'gender',
93 'party' => 'party',
94 'congress_office' => 'address',
95 'phone' => 'phone',
96 'email' => 'email',
97 'congresspedia' => 'url',
98 'photo' => 'image_url',
99 'webform' => 'contact_url',
100 );
101
102 foreach ($fields as $old => $new) {
103 $result[$new] = (string ) $xml->$old;
104 }
105
106 $result['image_url'] = 'http://sunlightlabs.com/widgets/popuppoliticians/resources/images/' . $result['image_url'];
107
108 return $result;
109 }
110
111 /**
112 * @param $uri
113 *
114 * @return array
115 */
116 public static function getPeopleInfo($uri) {
117 $xml = self::makeAPICall($uri);
118
119 $result = array();
120 foreach ($xml->entity_id_list->entity_id as $key => $value) {
121 $result[] = self::getDetailedInfo($value);
122 }
123 return $result;
124 }
125
126 /**
127 * @param $city
128 * @param $state
129 *
130 * @return array|null
131 */
132 public static function getRepresentativeInfo($city, $state) {
133 if (!$city ||
134 !$state
135 ) {
136 return NULL;
137 }
138 $key = self::$_apiKey;
139 $city = urlencode($city);
140 $uri = "people.reps.getRepsFromCityState.php?city={$city}&state={$state}&apikey={$key}&output=xml";
141 return self::getPeopleInfo($uri);
142 }
143
144 /**
145 * @param $state
146 *
147 * @return array|null
148 */
149 public static function getSenatorInfo($state) {
150 if (!$state) {
151 return NULL;
152 }
153
154 $key = self::$_apiKey;
155 $uri = "people.sens.getSensFromState.php?state={$state}&apikey={$key}&output=xml";
156 return self::getPeopleInfo($uri);
157 }
158
159 /**
160 * @param $city
161 * @param $state
162 * @param null $zipcode
163 *
164 * @return array
165 */
166 public static function getInfo($city, $state, $zipcode = NULL) {
167 if ($zipcode) {
168 list($city, $state) = self::getCityState($zipcode);
169 }
170
171 $reps = self::getRepresentativeInfo($city, $state);
172 $sens = self::getSenatorInfo($state);
173
174 $result = array();
175 if (is_array($reps)) {
176 $result = array_merge($result, $reps);
177 }
178 if (is_array($sens)) {
179 $result = array_merge($result, $sens);
180 }
181
182 return $result;
183 }
184
185 }