Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
6.7 KiB

  1. <?php
  2. require_once ("config.php");
  3. require_once ("phpcoord.php");
  4. $api = new api;
  5. class api {
  6. //Properties
  7. var $warnings = array();
  8. var $applications;
  9. //Constructor
  10. function api() {
  11. if (isset($_GET['howto'])){
  12. redirect("apihowto.php");//this handles old links to this page
  13. exit;
  14. } else {
  15. if(isset($_GET['call'])){
  16. $this->setup();
  17. }else{
  18. $this->setup_old();
  19. }
  20. $this->bind();
  21. }
  22. }
  23. //setup
  24. function setup(){
  25. //get the call type
  26. $call = $_GET['call'];
  27. switch ($call) {
  28. case "postcode":
  29. if(!isset($_GET['area_size']) || !is_postcode($_GET['postcode'])){
  30. array_push($this->warnings, "No valid postcode specified");
  31. }
  32. if(!isset($_GET['area_size'])){
  33. array_push($this->warnings, "Area size specified");
  34. }
  35. //all good, get the data
  36. if(sizeof($this->warnings) == 0){
  37. $xy = postcode_to_location($_GET['postcode']);
  38. $easting = $xy[0];
  39. $northing = $xy[1];
  40. $this->applications = Applications::query($easting, $northing, alert_size_to_meters($_GET['area_size']));
  41. }
  42. break;
  43. case "point":
  44. //validation
  45. if(!isset($_GET['lat']) || !isset($_GET['lng'])){
  46. array_push($this->warnings, "No longitude or latitude was specified");
  47. }
  48. if(!isset($_GET['area_size'])){
  49. array_push($this->warnings, "Area size specified");
  50. }
  51. //all good, get the data
  52. if(sizeof($this->warnings) == 0){
  53. $latlng = new LatLng($_GET['lat'], $_GET['lng']);
  54. $xy = $latlng->toOSRef();
  55. $easting = $xy->easting;
  56. $northing = $xy->northing;
  57. $this->applications = Applications::query($easting, $northing, alert_size_to_meters($_GET['area_size']));
  58. }
  59. break;
  60. case "pointos":
  61. //validation
  62. if(!isset($_GET['easting']) || !isset($_GET['northing'])){
  63. array_push($this->warnings, "No easting or northing was specified");
  64. }
  65. if(!isset($_GET['area_size'])){
  66. array_push($this->warnings, "Area size specified");
  67. }
  68. //all good, get the data
  69. if(sizeof($this->warnings) == 0){
  70. $this->applications = Applications::query($_GET['easting'], $_GET['northing'], alert_size_to_meters($_GET['area_size']));
  71. }
  72. break;
  73. case "authority":
  74. //validation
  75. if(!isset($_GET['authority'])){
  76. array_push($this->warnings, "No authority name specified");
  77. }
  78. //all good, get the data
  79. if(sizeof($this->warnings) == 0){
  80. $this->applications = Applications::query_authority($_GET['authority']);
  81. }
  82. break;
  83. case "area":
  84. //validation
  85. if(!isset($_GET['bottom_left_lat']) || !isset($_GET['bottom_left_lng']) || !isset($_GET['top_right_lat']) || !isset($_GET['top_right_lng'])){
  86. array_push($this->warnings, "Bounding box was not specified");
  87. }
  88. //all good, get the data
  89. if(sizeof($this->warnings) == 0){
  90. $bottom_left_latlng = new LatLng($_GET['bottom_left_lat'], $_GET['bottom_left_lng']);
  91. $bottom_left_xy = $bottom_left_latlng->toOSRef();
  92. $top_right_latlng = new LatLng($_GET['top_right_lat'], $_GET['top_right_lng']);
  93. $top_right_xy = $top_right_latlng->toOSRef();
  94. $this->applications = Applications::query_area($bottom_left_xy->easting, $bottom_left_xy->northing, $top_right_xy->easting, $top_right_xy->northing);
  95. }
  96. break;
  97. case "areaos":
  98. //validation
  99. if(!isset($_GET['bottom_left_easting']) || !isset($_GET['bottom_left_northing']) || !isset($_GET['top_right_easting']) || !isset($_GET['top_right_northing'])){
  100. array_push($this->warnings, "Bounding box was not specified");
  101. }
  102. //all good, get the data
  103. if(sizeof($this->warnings) == 0){
  104. $this->applications = Applications::query_area($_GET['bottom_left_easting'], $_GET['bottom_left_northing'], $_GET['top_right_easting'], $_GET['top_right_easting']);
  105. }
  106. break;
  107. case "latest":
  108. $this->applications = Applications::query_latest(500);
  109. break;
  110. query_latest
  111. default:
  112. $this->warnings = "No call type specified";
  113. }
  114. }
  115. //setup old (maintains the original mini api)
  116. function setup_old (){
  117. //Grab the postcode and area size from the get string
  118. if (!isset($_GET['area_size'])){ //check is_numeric and isn't too big
  119. array_push($this->warnings, "No area size specified");
  120. }
  121. if (sizeof($this->warnings) == 0){
  122. //Get OS ref from postcode
  123. if (isset($_GET['postcode'])) {
  124. $xy = postcode_to_location($_GET['postcode']);
  125. $this->easting = $xy[0];
  126. $this->northing = $xy[1];
  127. } else {
  128. $latlng = new LatLng($_GET['lat'], $_GET['lng']);
  129. $xy = $latlng->toOSRef();
  130. $this->easting = $xy->easting;
  131. $this->northing = $xy->northing;
  132. }
  133. $this->area_size = $_GET['area_size'];
  134. $this->applications = Applications::query($this->easting, $this->northing, alert_size_to_meters($this->area_size));
  135. }
  136. }
  137. //Bind
  138. function bind () {
  139. //page vars
  140. $form_action = $_SERVER['PHP_SELF'];
  141. header("Content-Type: text/xml");
  142. //smarty
  143. $smarty = new Smarty;
  144. $smarty->force_compile = true;
  145. $smarty->compile_dir = SMARTY_COMPILE_DIRECTORY;
  146. $smarty->assign("warnings", $this->warnings);
  147. $smarty->assign("applications", $this->applications);
  148. //Render
  149. $smarty->display('rss.tpl');
  150. }
  151. //howto
  152. function howto() {
  153. $form_action = $_SERVER['PHP_SELF'];
  154. $smarty = new Smarty;
  155. $smarty->force_compile = true;
  156. $smarty->compile_dir = SMARTY_COMPILE_DIRECTORY;
  157. $smarty->assign("page_title","API");
  158. $smarty->assign("menu_item","api");
  159. $smarty->display('apihowto.tpl');
  160. }
  161. }
  162. ?>