Automatically exported from code.google.com/p/planningalerts
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

117 строки
5.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. //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] . ")";
  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. $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. //Smarty template
  54. $smarty = new Smarty;
  55. $smarty->force_compile = true;
  56. $smarty->compile_dir = SMARTY_COMPILE_DIRECTORY;
  57. $smarty->assign("applications", $applications);
  58. $smarty->assign("base_url", BASE_URL);
  59. $smarty->assign("confirm_id", $user_results[$i][8]);
  60. //Get the email text
  61. $email_text = $smarty->fetch(SMARTY_TEMPLATE_DIRECTORY . 'alert_email_text.tpl');
  62. //Send the email
  63. if($email_text !=""){
  64. send_text_email($user_results[$i][1], EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS, "Planning applications near " . $user_results[$i][2], $email_text);
  65. $this->store_log("sent applications to " . $user_results[$i][1]);
  66. }else{
  67. $this->store_log("BLANK EMAIL TEXT !!! EMAIL NOT SENT");
  68. }
  69. //Update last sent
  70. $sql = "update user set last_sent = " . $db->quote(mysql_date(time())) . " where user_id = " . $user_results[$i][0];
  71. $db->query($sql);
  72. $this->store_log("Updating last checked date/time");
  73. }
  74. }
  75. }
  76. //Send the log
  77. send_text_email(LOG_EMAIL, "mailer@" . DOMAIN, "mailer@" . DOMAIN, "Planning mailer log", print_r($this->log, true));
  78. $this->store_log("Debug email sent to " . LOG_EMAIL);
  79. }
  80. function store_log($text){
  81. array_push($this->log, $text);
  82. print $text . "\n\n";
  83. }
  84. }
  85. ?>