Combu Server  3.1.1
PHP API Documentation
Newsletter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
5 define ("NEWSLETTER_STATUS_READY", 0);
6 define ("NEWSLETTER_STATUS_SENT", 1);
7 
13 class Newsletter extends DataClass {
14 
15  const TABLE_NAME = "Newsletter";
16 
17  public $Id = 0;
18  public $IdAccount = 0;
19  public $IdApp = 0;
20  public $Subject = "";
21  public $Body = "";
22  public $IsHtml = 1;
23  public $DateCreation = "";
24  public $DateSent = "";
25  public $Status = NEWSLETTER_STATUS_READY;
26  public $LogMessage = "";
27 
31  public function __construct($src = null, $stripSlashes = false) {
32  global $Database;
33  if ($src == null)
34  return;
35  if (is_array($src)) {
36  // Load by array
37  $this->_loadByRow($src, $stripSlashes);
38  } else if (is_numeric($src) && intval($src) > 0) {
39  // Load by Id
40  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
41  }
42  }
43 
54  public static function Load ($idApp = 0, $limit = null, $offset = null, &$count = null, $returnArray = false) {
55  $where = "";
56  if ($idApp > 0) {
57  $where = sprintf("IdApp = %d", $idApp);
58  }
59  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, NULL, $limit, $offset, $count);
60  }
61 
67  public function Save() {
68  global $Database;
69  if ($this->Id > 0) {
70  $query = sprintf("UPDATE %s SET Subject = '%s', Body = '%s', Status = %d, DateSent = %s, LogMessage = '%s' WHERE Id = %d",
71  self::GetTableName(__CLASS__),
72  $Database->Escape($this->Subject),
73  $Database->Escape($this->Body),
74  $this->Status,
75  $Database->EscapeDate($this->DateSent),
76  $Database->Escape($this->LogMessage),
77  $this->Id);
78  } else {
79  $this->DateCreation = date("Y-m-d H:i:s");
80  $query = sprintf("INSERT INTO %s (IdAccount, IdApp, Subject, Body, IsHtml, DateCreation, Status, LogMessage) VALUES (%d, %d, '%s', '%s', %d, %s, %d, '{}')",
81  self::GetTableName(__CLASS__),
82  $this->IdAccount,
83  $this->IdApp,
84  $Database->Escape($this->Subject),
85  $Database->Escape($this->Body),
86  $this->IsHtml,
87  $Database->EscapeDate($this->DateCreation),
88  $this->Status);
89  }
90  //echo "<pre>". htmlentities($query, ENT_QUOTES)."</pre>";
91  if ($Database->Query($query)) {
92  if ($this->Id < 1) {
93  $this->Id = $Database->InsertedId();
94  }
95  return TRUE;
96  }
97  return FALSE;
98  }
99 
105  public function Delete() {
106  if ($this->Id > 0 && $this->_Delete(self::GetTableName(__CLASS__), sprintf("Id = %d", $this->Id))) {
107  $this->_Delete(self::GetTableName(NewsletterLog::class), sprintf("IdNewsletter = %d", $this->Id));
108  return TRUE;
109  }
110  return FALSE;
111  }
112 
116  public static function Prune() {
117  self::TruncateClass(__CLASS__);
118  self::TruncateClass(NewsletterLog::class);
119  }
120 
126  public function Send() {
127  if ($this->Id > 0 && $this->Status != NEWSLETTER_STATUS_SENT) {
128  $this->DateSent = date("Y-m-d H:i:s");
129  $this->Status = NEWSLETTER_STATUS_SENT;
130  $sent = 0;
131  $skipped = 0;
132  $errors = 0;
133  $accounts = Account::LoadAny($this->IdApp);
134  foreach ($accounts as $account) {
135  if (empty($account->Email) || !$account->Enabled) {
136  $skipped++;
137  } else {
138  $recordLog = new NewsletterLog();
139  $recordLog->IdNewsletter = $this->Id;
140  $recordLog->IdAccount = $account->Id;
141  $mail = new Mail();
142  $mail->prepare($this->Subject, $this->Body, $account->Email, NEWSLETTER_SENDER_ADDRESS, NEWSLETTER_SENDER_NAME);
143  if ($mail->Send()) {
144  $sent++;
145  $recordLog->Sent = TRUE;
146  } else {
147  $errors++;
148  $recordLog->Sent = FALSE;
149  $recordLog->Message = $mail->ErrorInfo;
150  }
151  $recordLog->Save();
152  }
153  }
154  $this->LogMessage = json_encode(array( "sent" => $sent, "skipped" => $skipped, "errors" => $errors ));
155  $this->Save();
156  return TRUE;
157  }
158  return FALSE;
159  }
160 }
__construct($src=null, $stripSlashes=false)
Definition: Newsletter.php:31
const NEWSLETTER_STATUS_READY
Definition: Newsletter.php:5
static Load($idApp=0, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: Newsletter.php:54
Definition: Account.php:3
const NEWSLETTER_STATUS_SENT
Definition: Newsletter.php:6