PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/RP_WHOs_707/trunk/system/application/libraries/ApiData.php

https://github.com/holsinger/openfloor
PHP | 418 lines | 268 code | 49 blank | 101 comment | 25 complexity | 495424996a21941afcca3c878c5f868f MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class ApiData {
  3. var $error = false;
  4. var $debugLevel = 0;
  5. var $zip = 0;
  6. var $state = '';
  7. var $city = '';
  8. var $county = '';
  9. var $districtsArray = array();
  10. var $cacheData = true;
  11. function __construct ($debug=0) {
  12. $this->debugLevel = $debug;
  13. $this->CI=& get_instance();
  14. }
  15. //sets
  16. //gets
  17. /**
  18. * this function returns the data in the requested format
  19. *
  20. * @param string outputType (html|xml|...)
  21. */
  22. function getData ($outputType = 'html') {
  23. //get an array of rep ids and make sure we have the state
  24. $repIdArray = array();
  25. $senIdArray = array();
  26. if ($this->zip) {
  27. //get data starting with zip
  28. $this->getDistrictsFromZip ();
  29. if (!$this->state) $this->state = $this->getStateFromZip ();
  30. foreach ($this->districtsArray as $key => $value) array_push($repIdArray,$this->getRepFromDistrict ($value));
  31. } else if ($this->city && $this->state) {
  32. $repIdArray = $this->getRepsFromCityState ();
  33. }
  34. //now get state senitors
  35. $senIdArray = $this->getSensFromState ();
  36. $out = "<h2>Senators</h2>";
  37. foreach ($senIdArray as $id) $out .= $this->buildHTMLLink ($id);
  38. $out .= '<div style="clear:both;">';
  39. $out .= "<h2>Representatives</h2>";
  40. //vardump ($repIdArray);
  41. foreach ($repIdArray as $id) $out .= $this->buildHTMLLink ($id);
  42. $out .= '<div style="clear:both;">';
  43. return $out;
  44. }
  45. /**
  46. * this function will return html to diplay
  47. *
  48. * @param string id a sunlight id
  49. */
  50. function buildHTMLLink ($id) {
  51. $resultsArray = $this->getPersonalInfo ($id);
  52. //exit(var_dump($resultsArray));
  53. $html ="";
  54. $html .= "<div class='who_can' style='float:left;padding:5px;'>";
  55. $html .= "<img src='http://sunlightlabs.com/widgets/popuppoliticians/resources/images/{$resultsArray['photo']}'>";
  56. $html .= "<br />";
  57. $html .= "<a href=".$resultsArray['URL']." target='_blank'>".$resultsArray['firstname']." ".$resultsArray['lastname']."</a>";
  58. $html .= "<br />";
  59. $html .= anchor ('/your_government/politician/'.$id,"What is ".$resultsArray['firstname']." ".$resultsArray['lastname']."up to?");
  60. $html .= "</div>";
  61. return $html;
  62. }
  63. function whatUp ($id) {
  64. $html = '';
  65. $resultsArray = $this->getPersonalInfo ($id);
  66. $html .= "<h3>{$resultsArray['firstname']} {$resultsArray['lastname']}</h3>";
  67. $html .= "<img src='http://sunlightlabs.com/widgets/popuppoliticians/resources/images/{$resultsArray['photo']}'>";
  68. $html .= "<br />";
  69. $html .= "<h3>Videos</h3>";
  70. $html .= '<!-- ++Begin Video Bar Wizard Generated Code++ -->
  71. <div id="videoBar-player">
  72. <span style="color:#676767;font-size:11px;margin:10px;padding:4px;">Loading...</span>
  73. </div>
  74. <div id="videoBar-bar">
  75. <span style="color:#676767;font-size:11px;margin:10px;padding:4px;">Loading...</span>
  76. </div>
  77. <script src="http://www.google.com/uds/api?file=uds.js&v=1.0&source=uds-vbw&key=ABQIAAAAkKcqFPODFw9EgKwKbb0vmxQMMgp4bRk8oDwCMuUVC3eAkH6yhxRey5JriXQU4hISBErCZHOSAAHL4w"
  78. type="text/javascript"></script>
  79. <style type="text/css">
  80. @import url("http://www.google.com/uds/css/gsearch.css");
  81. </style>
  82. <!-- Video Bar Code and Stylesheet -->
  83. <script type="text/javascript">
  84. window._uds_vbw_donotrepair = true;
  85. </script>
  86. <script src="http://www.google.com/uds/solutions/videobar/gsvideobar.js?mode=new"
  87. type="text/javascript"></script>
  88. <style type="text/css">
  89. @import url("http://www.google.com/uds/solutions/videobar/gsvideobar.css");
  90. </style>
  91. <style type="text/css">
  92. #videoBar-player .playerInnerBox_gsvb .player_gsvb {
  93. width : 320px;
  94. height : 260px;
  95. }
  96. </style>
  97. <script type="text/javascript">
  98. function LoadVideoBar() {
  99. var videoBar;
  100. var options = {
  101. largeResultSet : !true,
  102. horizontal : true,
  103. autoExecuteList : {
  104. cycleTime : GSvideoBar.CYCLE_TIME_MEDIUM,
  105. cycleMode : GSvideoBar.CYCLE_MODE_RANDOM,
  106. executeList : ["'.$resultsArray['firstname'].' '.$resultsArray['lastname'].'"]
  107. }
  108. }
  109. videoBar = new GSvideoBar(document.getElementById("videoBar-bar"),
  110. document.getElementById("videoBar-player"),
  111. options);
  112. }
  113. // arrange for this function to be called during body.onload
  114. // event processing
  115. GSearch.setOnLoadCallback(LoadVideoBar);
  116. </script>
  117. <!-- ++End Video Bar Wizard Generated Code++ -->';
  118. $html .= '<h3>Blog Posts</h3>';
  119. $html .= '<!-- ++Begin Blog Bar Wizard Generated Code++ -->
  120. <!--
  121. // Created with a Google AJAX Search Wizard
  122. // http://code.google.com/apis/ajaxsearch/wizards.html
  123. -->
  124. <!--
  125. // The Following div element will end up holding the actual blogbar.
  126. // You can place this anywhere on your page.
  127. -->
  128. <div id="blogBar-bar">
  129. <span style="color:#676767;font-size:11px;margin:10px;padding:4px;">Loading...</span>
  130. </div>
  131. <!-- Ajax Search Api and Stylesheet
  132. // Note: If you are already using the AJAX Search API, then do not include it
  133. // or its stylesheet again
  134. //
  135. // The Key Embedded in the following script tag is designed to work with
  136. // the following site:
  137. // http://politic20.com
  138. -->
  139. <script src="http://www.google.com/uds/api?file=uds.js&v=1.0&source=uds-blbw&key=ABQIAAAAkKcqFPODFw9EgKwKbb0vmxQMMgp4bRk8oDwCMuUVC3eAkH6yhxRey5JriXQU4hISBErCZHOSAAHL4w"
  140. type="text/javascript"></script>
  141. <style type="text/css">
  142. @import url("http://www.google.com/uds/css/gsearch.css");
  143. </style>
  144. <!-- Blog Bar Code and Stylesheet -->
  145. <script src="http://www.google.com/uds/solutions/blogbar/gsblogbar.js?mode=new"
  146. type="text/javascript"></script>
  147. <style type="text/css">
  148. @import url("http://www.google.com/uds/solutions/blogbar/gsblogbar.css");
  149. </style>
  150. <script type="text/javascript">
  151. function LoadBlogBar() {
  152. var blogBar;
  153. var options = {
  154. largeResultSet : false,
  155. title : "Blogs",
  156. horizontal : true,
  157. orderBy : GSearch.ORDER_BY_DATE,
  158. autoExecuteList : {
  159. executeList : ["'.$resultsArray['firstname'].' '.$resultsArray['lastname'].'"]
  160. }
  161. }
  162. blogBar = new GSblogBar(document.getElementById("blogBar-bar"), options);
  163. }
  164. // arrange for this function to be called during body.onload
  165. // event processing
  166. GSearch.setOnLoadCallback(LoadBlogBar);
  167. </script>
  168. <!-- ++End Blog Bar Wizard Generated Code++ -->';
  169. $html .= "<h3>Tracked Events</h3>";
  170. $html .= "<script src='http://www.govtrack.us/embed/events.xpd?monitors=p:{$resultsArray["GovTrack_ID"]}&options=&count=8' type='text/javascript'/>";
  171. $html .= "<h3>Votes</h3>";
  172. $html .= "<script src='http://www.govtrack.us/embed/events.xpd?monitors=pv:{$resultsArray["GovTrack_ID"]}&options=&count=8' type='text/javascript'/>";
  173. return $html;
  174. }
  175. function getPersonalInfo ($id) {
  176. $resultsArray = $this->callAPI ('http://api.sunlightlabs.com/people.getPersonInfo.php?id='.$id,'getPersonInfo');
  177. return $resultsArray;
  178. }
  179. /**
  180. *
  181. * this function calls the sunlight api and returns
  182. * the data as an array
  183. * @param string value possible zip value
  184. * @return array of districts
  185. * @author James
  186. *
  187. */
  188. function getDistrictsFromZip ($value = false) {
  189. $resultsArray = array();
  190. //where we getting the zip from
  191. if ($value) $zip = $value;
  192. else $zip = $this->zip;
  193. //sunlight api url
  194. $url='http://api.sunlightlabs.com/places.getDistrictsFromZip.php?zip='.$zip;
  195. //parse results
  196. $resultsArray = $this->callAPI ($url,'getDistrictsFromZip');
  197. //set state
  198. $this->state = $resultsArray['districts'][0]['state'];
  199. //add to districts array
  200. foreach ($resultsArray['districts'] as $value) array_push($this->districtsArray,$value['district']);
  201. return $this->districtsArray;
  202. }
  203. /**
  204. * this function calls the sunlight api and returns
  205. * the data id
  206. * @param string value district number
  207. * @return array of representitives
  208. * @author James
  209. *
  210. */
  211. function getRepFromDistrict ($value = false) {
  212. $resultsArray = array();
  213. //sunlight api url
  214. $url='http://api.sunlightlabs.com/people.reps.getRepFromDistrict.php?state='.$this->state.'&district='.$value;
  215. //parse results
  216. $resultsArray = $this->callAPI ($url,'getRepFromDistrict');
  217. //exit(var_dump($resultsArray));
  218. return $resultsArray['entity_id'];
  219. }
  220. /**
  221. * this function calls the sunlight api and returns
  222. * the data as an array
  223. * @param string value city string
  224. * @param string value2 state string
  225. * @return array of representitives
  226. * @author James
  227. *
  228. */
  229. function getRepsFromCityState ($value = false,$value2=false) {
  230. $resultsArray = array();
  231. $returnArray = array();
  232. //where we getting the city state from
  233. if ($value) $city = $value;
  234. else $city = $this->city;
  235. if ($value2) $state = $value2;
  236. else $state = $this->state;
  237. //sunlight api url
  238. $url='http://api.sunlightlabs.com/people.reps.getRepsFromCityState.php?city='.$city.'&state='.$state;
  239. //parse results
  240. $resultsArray = $this->callAPI ($url,'getRepsFromCityState');
  241. //exit(var_dump($resultsArray));
  242. //set return array
  243. $returnArray = $resultsArray['entity_ids'];
  244. return $returnArray;
  245. }
  246. /**
  247. * this function calls the sunlight api and returns
  248. * the data as an array
  249. * @param string value state string
  250. * @return array of senators
  251. * @author James
  252. *
  253. */
  254. function getSensFromState ($value = false) {
  255. $resultsArray = array();
  256. $returnArray = array();
  257. //where we getting the state from
  258. if ($value) $state = $value;
  259. else $state = $this->state;
  260. //sunlight api url
  261. $url='http://api.sunlightlabs.com/people.sens.getSensFromState.php?state='.$state;
  262. //parse results
  263. $resultsArray = $this->callAPI ($url,'getSensFromState');
  264. //exit(var_dump($resultsArray));
  265. //set return array
  266. $returnArray = $resultsArray['entity_ids'];
  267. return $returnArray;
  268. }
  269. /**
  270. * this function will handle calling api and error checking
  271. *
  272. * @param string url the url of the api call
  273. * @param string debugName the name to pass as a debug title
  274. * @return array associative array of results
  275. * @author James Kleinschnitz
  276. */
  277. function callAPI ($url,$debugName='') {
  278. //get api contents decose with json to an array
  279. $cachedFileName = './system/cache/' . str_replace(array(':', '/', '?', '&'), '', substr($url, 28, strlen($url)));
  280. #TODO check if file past cache limit
  281. if (file_exists($cachedFileName)) {
  282. $resultsArray = json_decode(file_get_contents($cachedFileName),true);
  283. } else {
  284. $contents = curl_get_contents($url);
  285. $resultsArray = json_decode($contents,true);
  286. //if we want to cache the data write the data to a file
  287. if ($this->cacheData) {
  288. // delete the file if it already exists
  289. if (file_exists($cachedFileName)) unlink($cachedFileName);
  290. // recreate file and recache data
  291. if ($file = fopen($cachedFileName, 'wb')) {
  292. fwrite($file, $contents);
  293. fclose($file);
  294. }
  295. }
  296. }
  297. //do some kind of error checking here and return false
  298. if ( $this->debugLevel > 0 ) {
  299. echo "<b style='color:#FF0000;'>".$debugName.":</b><br />";
  300. foreach ($resultsArray as $key => $val) {
  301. if ( is_array($val) ) {
  302. echo $key."<br />";
  303. foreach ($val as $index => $value) echo " $index => $value <br />";
  304. } else echo "$key => $val <br />";
  305. }
  306. echo "<br />";
  307. }
  308. return $resultsArray;
  309. }
  310. /**
  311. * this function return and array of districts based on a city state pair
  312. *
  313. * @param string city a specific city value
  314. * @param string state a specific state value
  315. * @return array of districts
  316. * @author James Kleinschnitz
  317. */
  318. /*
  319. function getDistrictsFromCityState ($city = false, $state = false) {
  320. $zipArray = getZipsFromCityState ();
  321. //build the district array
  322. foreach ($zipArray as $key => $value) array_merge($this->districtsArray,$this->getDistrictsFromZip ($value));
  323. }*/
  324. function getAllNames()
  325. {
  326. $cachedPoliticians = './system/cache/politicians';
  327. $politicians = array('' => '');
  328. if (file_exists($cachedPoliticians)) {
  329. $politicians = unserialize(file_get_contents($cachedPoliticians));
  330. } else {
  331. $resultsArray = array();
  332. $parties = array('D', 'R', 'I');
  333. foreach ($parties as $v) {
  334. $url = "http://api.sunlightlabs.com/people.getDataCondition.php?party=$v";
  335. $array = $this->callAPI ($url,'getDataCondition');
  336. $resultsArray = array_merge($resultsArray, $array['entity_ids']);
  337. }
  338. foreach ($resultsArray as $k => $v) {
  339. $url = "http://api.sunlightlabs.com/people.getPersonInfo.php?id=".$v;
  340. $result = $this->callAPI ($url,'getPersonInfo');
  341. #$url = "http://api.sunlightlabs.com/people.getDataItem.php?id=$v&code=firstname";
  342. #$result = $this->callAPI ($url,'getDataItem');
  343. $firstname = $result['firstname'];
  344. //$url = "http://api.sunlightlabs.com/people.getDataItem.php?id=$v&code=lastname";
  345. //$result = $this->callAPI ($url,'getDataItem');
  346. $lastname = $result['lastname'];
  347. $entity_id = $result['entity_id'];
  348. $politicians[$entity_id] = "$lastname, $firstname";
  349. }
  350. asort($politicians);
  351. if ($file = fopen($cachedPoliticians, 'wb')) {
  352. fwrite($file, serialize($politicians));
  353. fclose($file);
  354. }
  355. }
  356. return $politicians;
  357. }
  358. }
  359. ?>