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