Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Utils_Sunlight { | |
34 | static $_apiURL = 'http://api.sunlightlabs.com/'; | |
35 | static $_apiKey = NULL; | |
36 | ||
5bc392e6 EM |
37 | /** |
38 | * @param $uri | |
39 | * | |
40 | * @return SimpleXMLElement | |
41 | * @throws Exception | |
42 | */ | |
00be9182 | 43 | public static function makeAPICall($uri) { |
6a488035 TO |
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', | |
353ffa53 TO |
57 | array(1 => $request->getResponseCode()) |
58 | )); | |
6a488035 TO |
59 | } |
60 | $string = $request->getResponseBody(); | |
61 | return simplexml_load_string($string); | |
62 | } | |
63 | ||
5bc392e6 EM |
64 | /** |
65 | * @param $zipcode | |
66 | * | |
67 | * @return array | |
68 | */ | |
00be9182 | 69 | public static function getCityState($zipcode) { |
6a488035 TO |
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 | ||
5bc392e6 | 77 | /** |
100fef9d | 78 | * @param int $peopleID |
5bc392e6 EM |
79 | * |
80 | * @return array | |
81 | */ | |
00be9182 | 82 | public static function getDetailedInfo($peopleID) { |
6a488035 TO |
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 | ||
5bc392e6 EM |
111 | /** |
112 | * @param $uri | |
113 | * | |
114 | * @return array | |
115 | */ | |
00be9182 | 116 | public static function getPeopleInfo($uri) { |
6a488035 TO |
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 | ||
5bc392e6 EM |
126 | /** |
127 | * @param $city | |
128 | * @param $state | |
129 | * | |
130 | * @return array|null | |
131 | */ | |
00be9182 | 132 | public static function getRepresentativeInfo($city, $state) { |
6a488035 TO |
133 | if (!$city || |
134 | !$state | |
135 | ) { | |
136 | return NULL; | |
137 | } | |
353ffa53 | 138 | $key = self::$_apiKey; |
6a488035 | 139 | $city = urlencode($city); |
353ffa53 | 140 | $uri = "people.reps.getRepsFromCityState.php?city={$city}&state={$state}&apikey={$key}&output=xml"; |
6a488035 TO |
141 | return self::getPeopleInfo($uri); |
142 | } | |
143 | ||
5bc392e6 EM |
144 | /** |
145 | * @param $state | |
146 | * | |
147 | * @return array|null | |
148 | */ | |
00be9182 | 149 | public static function getSenatorInfo($state) { |
6a488035 TO |
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 | ||
5bc392e6 EM |
159 | /** |
160 | * @param $city | |
161 | * @param $state | |
162 | * @param null $zipcode | |
163 | * | |
164 | * @return array | |
165 | */ | |
00be9182 | 166 | public static function getInfo($city, $state, $zipcode = NULL) { |
6a488035 TO |
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 | } | |
96025800 | 184 | |
6a488035 | 185 | } |