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.
 
 
 
 
 
 

142 lines
6.0 KiB

  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. var $application_count = 0;
  13. var $email_count = 0;
  14. //Run
  15. function run (){
  16. $db = DB::connect(DB_CONNECTION_STRING);
  17. //Grab all the users
  18. $sql = "select user_id, email, postcode, bottom_left_x, bottom_left_y, top_right_x, top_right_y, alert_area_size, confirm_id
  19. from user
  20. where confirmed = 1 and last_sent < " . $db->quote(mysql_date(time() - (20 * 60 * 60)));
  21. $this->store_log("Grabbing users");
  22. $user_results = $db->getAll($sql);
  23. $this->store_log("Found " . sizeof($user_results) . " who havnt been checked since " . mysql_date(time() - (20 * 60 * 60)));
  24. if(sizeof($user_results) > 0){
  25. //Loop though users
  26. for ($i=0; $i < sizeof($user_results); $i++){
  27. //Find applications for that user
  28. $sql = "select council_reference, address,
  29. postcode, description, info_tinyurl,
  30. comment_tinyurl, map_url, full_name
  31. from application
  32. inner join authority on application.authority_id = authority.authority_id
  33. where date_scraped > " . $db->quote(mysql_date(time() - (24 * 60 * 60))) .
  34. " and (application.x > " . $user_results[$i][3] . " and application.x < " . $user_results[$i][5] . ")
  35. and (application.y > " . $user_results[$i][4] . " and application.y < " . $user_results[$i][6] . ") and (application.y <> 0 and application.y <> 0 )";
  36. $application_results = $db->getAll($sql);
  37. //Send email if we have any
  38. if(sizeof($application_results) > 0){
  39. //Setup applications array (bit pikey this)
  40. $applications = array();
  41. for ($ii=0; $ii < sizeof($application_results); $ii++){
  42. $application = new application();
  43. $application->council_reference = $application_results[$ii][0];
  44. $application->address = $application_results[$ii][1];
  45. $application->postcode = $application_results[$ii][2];
  46. $application->description= $application_results[$ii][3];
  47. $application->info_tinyurl= $application_results[$ii][4];
  48. $application->comment_tinyurl= $application_results[$ii][5];
  49. $application->map_url = $application_results[$ii][6];
  50. $application->authority_name = $application_results[$ii][7];
  51. array_push($applications, $application);
  52. }
  53. $this->application_count += sizeof($applications);
  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->email_count +=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. $this->store_log("Sent " . $this->application_count . " applications to " . $this->email_count . " people!");
  80. //update the number of apps sent
  81. $sql = "select `key`, `value` from stats";
  82. $stats_results = $db->getAll($sql);
  83. $new_application_total = 0;
  84. $new_email_total = 0;
  85. for ($i=0; $i < sizeof($stats_results); $i++){
  86. if($stats_results[$i][0] == 'applications_sent'){
  87. $new_application_total = $stats_results[$i][1] + $this->application_count;
  88. $new_email_total = $stats_results[$i][1] + $this->email_count;
  89. }
  90. }
  91. //add stats to email
  92. $this->store_log("Total applications ever sent: " . $new_application_total);
  93. $this->store_log("Total emails ever sent: " . $new_email_total);
  94. //update stats in DB
  95. $sql = "update stats set `value` = " . $new_application_total . " where `key` = 'applications_sent'";
  96. $db->query($sql);
  97. $sql = "update stats set `value` = " . $new_email_total . " where `key` = 'emails_sent'";
  98. $db->query($sql);
  99. //Send the log
  100. send_text_email(LOG_EMAIL, "mailer@" . DOMAIN, "mailer@" . DOMAIN, "Planning mailer log", print_r($this->log, true));
  101. }
  102. function store_log($text){
  103. array_push($this->log, $text);
  104. print $text . "\n\n";
  105. }
  106. }
  107. ?>