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