PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/cron/notify_admin_user/Sender.php

https://bitbucket.org/iarp/knowledgebase
PHP | 187 lines | 98 code | 42 blank | 47 comment | 1 complexity | f1d3615a9c443f4015415b2a83b59e4f MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. class Sender extends CronCommon
  3. {
  4. var $tbl_pref_custom = 'kb_';
  5. var $tables = array('entry'=>'entry',
  6. 'category'=>'category',
  7. 'entry_to_category'=>'entry_to_category'
  8. );
  9. var $custom_tables = array('user'=>'user', 'data_to_user_rule', 'data_to_user_value');
  10. var $debug = false;
  11. function isFreshArticles($last_sent_date) {
  12. $sql = "SELECT COUNT(*) AS num
  13. FROM {$this->tbl->entry} e
  14. WHERE (e.date_posted > '{$last_sent_date}'
  15. OR e.date_updated > '{$last_sent_date}')";
  16. //echo "<pre>"; print_r($sql); echo "</pre>";
  17. $result =& $this->db->Execute($sql) or die(db_error($sql));
  18. return $result->Fields('num');
  19. }
  20. function getArticlesSql($last_sent_date, $body_words = 100) {
  21. $body_words_sql = ($body_words != 'all')
  22. ? "SUBSTRING_INDEX(e.body,' ',$body_words)"
  23. : 'e.body';
  24. $sql = "
  25. SELECT
  26. e.id,
  27. e.title,
  28. {$body_words_sql} AS body,
  29. cat.id AS category_id,
  30. cat.name AS category_name,
  31. UNIX_TIMESTAMP(e.date_posted) AS ts_posted,
  32. UNIX_TIMESTAMP(e.date_updated) AS ts_updated,
  33. IF(e.date_updated > e.date_posted, 'updated', 'posted') as type
  34. FROM
  35. {$this->tbl->entry} e,
  36. {$this->tbl->category} cat,
  37. {$this->tbl->entry_to_category} e_to_cat
  38. WHERE 1
  39. AND e.id = e_to_cat.entry_id
  40. AND cat.id = e_to_cat.category_id
  41. AND (e.date_posted > '{$last_sent_date}'
  42. OR e.date_updated > '{$last_sent_date}')
  43. AND {$this->sql_params}
  44. ORDER BY e.date_updated";
  45. //echo "<pre>"; print_r($sql); echo "</pre>";
  46. return $sql;
  47. }
  48. function &getArticles($last_sent_date, $body_words = 100) {
  49. $sql = $this->getArticlesSql($last_sent_date);
  50. $result = &$this->db->Execute($sql) or die(db_error($sql));
  51. return $result;
  52. }
  53. function getSubscribersSql($category_id) {
  54. $sql = "
  55. SELECT
  56. u.*
  57. FROM
  58. {$this->tbl->data_to_user_rule} d,
  59. {$this->tbl->data_to_user_value} dv,
  60. {$this->tbl->user} u
  61. WHERE 1
  62. AND d.title = 'kb_category_to_admin'
  63. AND d.id = dv.rule_id
  64. AND dv.data_value IN ($category_id)
  65. AND dv.user_value = u.id";
  66. return $sql;
  67. }
  68. function getSubscribers($category_id) {
  69. $sql = $this->getSubscribersSql($category_id);
  70. $result =& $this->db->Execute($sql) or die(db_error($sql));
  71. return $result->GetArray();
  72. }
  73. function getLetter($record, $user = false) {
  74. $controller = &new KBClientController;
  75. $controller->mod_rewrite = false;
  76. $tpl = &new tplTemplatez(APP_EMAI_TMPL_DIR . 'updated_articles.html');
  77. $link = $controller->getLink('entry', $record['category_id'], $record['id']);
  78. $link = substr($link, strpos($link, '?'));
  79. $link = APP_CLIENT_PATH . 'index.php' . $link;
  80. $record['article_link'] = $link;
  81. $record['body'] = $this->getSummary($record['body']);
  82. $record['date'] = strftime('%d %b, %Y', $record['ts_posted']);
  83. $tpl->tplParse($record, 'row');
  84. $tpl->tplParse();
  85. return $tpl->tplPrint(1);
  86. }
  87. function send($letter, $data) {
  88. $mail = new PHPMailer();
  89. $mail->From = 'noreplay@';
  90. $mail->FromName = 'KB Mailer';
  91. $mail->Sender = 'KB Mailer';
  92. $mail->Subject = 'New or Updated articles';
  93. $mail->IsHTML(true);
  94. $mail->AddAddress($data['email']);
  95. $mail->Body =& $letter;
  96. echo "<pre>"; print_r($mail); echo "</pre>";
  97. //exit;
  98. //$sent = $mail->Send();
  99. return $sent;
  100. }
  101. /*
  102. // return last date sent or false if no time to send
  103. function isTimeToSend() {
  104. $sql = "SELECT sent_date FROM {$this->tbl->letter_time}
  105. WHERE 1
  106. AND FIND_IN_SET(DAYOFWEEK(CURRENT_DATE)-1, send_day)
  107. AND send_time <= CURRENT_TIME
  108. AND sent_date < CURRENT_DATE
  109. AND lang = '{$this->content_lang}'";
  110. $result =& $this->db->Execute($sql) or die(db_error($sql));
  111. $sent_date = $result->Fields('sent_date');
  112. return ($sent_date) ? $sent_date : false;
  113. }
  114. */
  115. /*
  116. function getArticlesRecords() {
  117. $data = array();
  118. $data['posted'] = array();
  119. $data['updated'] = array();
  120. $this->seSqlParams("AND (e.date_posted > '{$this->last_sent_date}'
  121. OR e.date_updated > '{$this->last_sent_date}')");
  122. $this->setSqlParamsOrder('e.date_updated');
  123. $sql = $this->getArticlesSql();
  124. $result =& $this->db->Execute($sql) or die(db_error($sql));
  125. while($row = $result->FetchRow()) {
  126. $data[$row['type']][][$row['id']] = $row;
  127. }
  128. return $data;
  129. }
  130. */
  131. /*
  132. $pause = ($i % SENDER_MAIL_ONCE) ? false : true;
  133. if($pause) {
  134. sleep(SENDER_SLEEP_TIME);
  135. }
  136. $i++;
  137. */
  138. }
  139. ?>