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.

application_mailer.php 5.3 KiB

18 年之前
18 年之前
18 年之前
18 年之前
18 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. require_once('tools_ini.php');
  3. require_once('application.php');
  4. require_once('DB.php');
  5. //initialise
  6. $application_mailer = new application_mailer();
  7. $application_mailer->run();
  8. //class
  9. class application_mailer {
  10. //Properties
  11. var $log = array();
  12. //Run
  13. function run (){
  14. $db = DB::connect(DB_CONNECTION_STRING);
  15. //Grab all the users
  16. $sql = "select user_id, email, postcode, bottom_left_x, bottom_left_y, top_right_x, top_right_y, alert_area_size, confirm_id
  17. from user
  18. where confirmed = 1 and last_sent < " . $db->quote(mysql_date(time() - (20 * 60 * 60)));
  19. $this->store_log("Grabbing users");
  20. $user_results = $db->getAll($sql);
  21. $this->store_log("Found " . sizeof($user_results) . " who havnt been checked since " . mysql_date(time() - (20 * 60 * 60)));
  22. if(sizeof($user_results) > 0){
  23. //Loop though users
  24. for ($i=0; $i < sizeof($user_results); $i++){
  25. $this->store_log("Processing user " . $i);
  26. //Find applications for that user
  27. $sql = "select council_reference, address,
  28. postcode, description, info_tinyurl,
  29. comment_tinyurl, map_url, full_name
  30. from application
  31. inner join authority on application.authority_id = authority.authority_id
  32. where date_scraped > " . $db->quote(mysql_date(time() - (24 * 60 * 60))) .
  33. " and (application.x > " . $user_results[$i][3] . " and application.x < " . $user_results[$i][5] . ")
  34. and (application.y > " . $user_results[$i][4] . " and application.y < " . $user_results[$i][6] . ") and (application.y <> 0 and application.y <> 0 )";
  35. $application_results = $db->getAll($sql);
  36. //Send email if we have any
  37. if(sizeof($application_results) > 0){
  38. $this->store_log("Found " . sizeof($application_results) . "applications for this user. w00t!");
  39. //Setup applications array (bit pikey this)
  40. $applications = array();
  41. for ($ii=0; $ii < sizeof($application_results); $ii++){
  42. print_r($application_results);
  43. $application = new application();
  44. $application->council_reference = $application_results[$ii][0];
  45. $application->address = $application_results[$ii][1];
  46. $application->postcode = $application_results[$ii][2];
  47. $application->description= $application_results[$ii][3];
  48. $application->info_tinyurl= $application_results[$ii][4];
  49. $application->comment_tinyurl= $application_results[$ii][5];
  50. $application->map_url = $application_results[$ii][6];
  51. $application->authority_name = $application_results[$ii][7];
  52. array_push($applications, $application);
  53. }
  54. //Smarty template
  55. $smarty = new Smarty;
  56. $smarty->force_compile = true;
  57. $smarty->compile_dir = SMARTY_COMPILE_DIRECTORY;
  58. $smarty->assign("applications", $applications);
  59. $smarty->assign("base_url", BASE_URL);
  60. $smarty->assign("confirm_id", $user_results[$i][8]);
  61. $smarty->assign("alert_area_size", $user_results[$i][7]);
  62. $smarty->assign("alert_postcode", $user_results[$i][2]);
  63. //Get the email text
  64. $email_text = $smarty->fetch(SMARTY_TEMPLATE_DIRECTORY . 'alert_email_text.tpl');
  65. //Send the email
  66. if($email_text !=""){
  67. send_text_email($user_results[$i][1], EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS, "Planning applications near " . strtoupper($user_results[$i][2]), $email_text);
  68. $this->store_log("sent applications to " . $user_results[$i][1]);
  69. }else{
  70. $this->store_log("BLANK EMAIL TEXT !!! EMAIL NOT SENT");
  71. }
  72. //Update last sent
  73. $sql = "update user set last_sent = " . $db->quote(mysql_date(time())) . " where user_id = " . $user_results[$i][0];
  74. $db->query($sql);
  75. $this->store_log("Updating last checked date/time");
  76. }
  77. }
  78. }
  79. //Send the log
  80. send_text_email(LOG_EMAIL, "mailer@" . DOMAIN, "mailer@" . DOMAIN, "Planning mailer log", print_r($this->log, true));
  81. $this->store_log("Debug email sent to " . LOG_EMAIL);
  82. }
  83. function store_log($text){
  84. array_push($this->log, $text);
  85. print $text . "\n\n";
  86. }
  87. }
  88. ?>