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.
 
 
 
 
 
 

209 lines
7.7 KiB

  1. <?php
  2. require_once ("config.php");
  3. require_once ("DB.php");
  4. class user{
  5. //Properties
  6. var $user_id = 0;
  7. var $email ="";
  8. var $postcode ="";
  9. var $last_sent;
  10. var $bottom_left_x;
  11. var $bottom_left_y;
  12. var $top_right_x;
  13. var $top_right_y;
  14. var $confirm_id;
  15. var $confirmed;
  16. var $alert_area_size;
  17. function save($send_confirmation_email = false){
  18. $db = DB::connect(DB_CONNECTION_STRING);
  19. //Check if it exists, the do an update or insert
  20. $exists = false;
  21. if ($this->user_id !=0){
  22. $sql = "select user_id from user where user_id = " . $this->user_id ;
  23. $results = $db->query($sql);
  24. if (sizeof($results) != 0){
  25. $exists = true;
  26. }
  27. }
  28. if (!$exists){
  29. $this->add();
  30. }else{
  31. $this->update();
  32. }
  33. //Send email
  34. if($send_confirmation_email == true){
  35. $smarty = new Smarty;
  36. $smarty->force_compile = true;
  37. $smarty->compile_dir = SMARTY_COMPILE_DIRECTORY;
  38. $smarty->assign("email", $this->email);
  39. $smarty->assign("postcode", $this->postcode);
  40. $smarty->assign("url", BASE_URL . "/confirmed.php?cid=" . $this->confirm_id);
  41. //Get the email text
  42. $email_text = $smarty->fetch('confirm_email_text.tpl');
  43. //Send the email
  44. send_text_email($this->email, EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS, "Please confirm your planning alert", $email_text);
  45. }
  46. }
  47. function update(){
  48. $db = DB::connect(DB_CONNECTION_STRING);
  49. $sql = "
  50. update user
  51. set email = " . $db->quote($this->email) . ",
  52. postcode = " . $db->quote($this->postcode) . ",
  53. last_sent = " . $db->quote($this->last_sent) . ",
  54. bottom_left_x = " . $db->quote($this->bottom_left_x) . ",
  55. bottom_left_y = " . $db->quote($this->bottom_left_y) . ",
  56. top_right_x = " . $db->quote($this->top_right_x) . ",
  57. top_right_y = " . $db->quote($this->top_right_y) . ",
  58. confirm_id = " . $db->quote($this->confirm_id) . ",
  59. confirmed = " . $db->quote($this->confirmed) . ",
  60. alert_area_size = " . $db->quote($this->alert_area_size) . "
  61. where user_id = " . $db->quote($this->user_id) . "
  62. ";
  63. $db->query($sql);
  64. }
  65. function add(){
  66. $db = DB::connect(DB_CONNECTION_STRING);
  67. $sql = "
  68. insert into user
  69. (
  70. email,
  71. postcode,
  72. last_sent,
  73. bottom_left_x,
  74. bottom_left_y,
  75. top_right_x,
  76. top_right_y,
  77. confirm_id,
  78. confirmed,
  79. alert_area_size
  80. )
  81. values(
  82. " . $db->quote($this->email) . ",
  83. " . $db->quote($this->postcode) . ",
  84. " . $db->quote($this->last_sent) . ",
  85. " . $db->quote($this->bottom_left_x) . ",
  86. " . $db->quote($this->bottom_left_y) . ",
  87. " . $db->quote($this->top_right_x) . ",
  88. " . $db->quote($this->top_right_y) . ",
  89. " . $db->quote($this->confirm_id) . ",
  90. " . $db->quote($this->confirmed) . ",
  91. " . $db->quote($this->alert_area_size) . "
  92. )
  93. ";
  94. $db->query($sql);
  95. }
  96. //Remove any other alerts for this email/postcode
  97. function remove_existing(){
  98. $db = DB::connect(DB_CONNECTION_STRING);
  99. $sql = "delete from user
  100. where postcode = " . $db->quote($this->postcode) . "
  101. and email = " . $db->quote($this->email) ."
  102. and user_id <> " . $db->quote($this->user_id);
  103. $db->query($sql);
  104. }
  105. function delete(){
  106. $db = DB::connect(DB_CONNECTION_STRING);
  107. $sql = "delete from user where user_id = " . $this->user_id;
  108. $db->query($sql);
  109. }
  110. function populate_new($email, $postcode, $alert_area_size){
  111. //Set email, postcode and size
  112. $this->email = $email;
  113. $this->postcode = $postcode;
  114. $this->alert_area_size = $alert_area_size;
  115. //cleanup postcode
  116. $this->postcode = str_replace(" ","", $this->postcode);
  117. $this->postcode = strtolower($this->postcode);
  118. //Get xy of the postcode
  119. $xy = postcode_to_location($postcode);
  120. //if we couldent find the XY, throw an exception
  121. if($xy == false){
  122. //throw new exception("Something bad happened when trying to convert postcode to X 'n Y");
  123. }
  124. //Get actual size of zone
  125. $area_size_meters = alert_size_to_meters($this->alert_area_size);
  126. //Work out bounding box + buffer area (assumes OSGB location == meters)
  127. $area_buffered_meters = $area_size_meters + (($area_size_meters/100) * ZONE_BUFFER_PERCENTAGE);
  128. $this->bottom_left_x = $xy[0] - ($area_buffered_meters/2);
  129. $this->bottom_left_y = $xy[1] - ($area_buffered_meters/2);
  130. $this->top_right_x = $xy[0] + ($area_buffered_meters/2);
  131. $this->top_right_y = $xy[1] + ($area_buffered_meters/2);
  132. //Make a confirmation ID for them (no unique check, ho hum)
  133. $this->confirm_id = substr(md5(rand(5,15) . time()), 0, 20);
  134. $this->confirmed = false;
  135. //Set last sent date to yesterday
  136. $this->last_sent = mysql_date(time() - (24 * 60 * 60));
  137. }
  138. function load_from_confirm_id($confirm_id){
  139. $success = false;
  140. $db = DB::connect(DB_CONNECTION_STRING);
  141. $sql = "select user_id, email, postcode, last_sent,
  142. bottom_left_x, bottom_left_y, top_right_x, top_right_y,
  143. confirm_id, confirmed, alert_area_size
  144. from user where confirm_id = " . $db->quote($confirm_id);
  145. $results = $db->getall($sql);
  146. if(sizeof($results) ==1){
  147. $success = true;
  148. $results = $results[0];
  149. $this->user_id = $results[0];
  150. $this->email = $results[1];
  151. $this->postcode = $results[2];
  152. $this->last_sent = $results[3];
  153. $this->bottom_left_x = $results[4];
  154. $this->bottom_left_y = $results[5];
  155. $this->top_right_x = $results[6];
  156. $this->top_right_y = $results[7];
  157. $this->confirm_id = $results[8];
  158. $this->confirmed = $results[9];
  159. $this->alert_area_size = $results[10];
  160. }
  161. return $success;
  162. }
  163. }
  164. ?>