Prevent sending of empty NLs.
[platal.git] / include / newsletter.inc.php
index 97f57eb..26f648f 100644 (file)
@@ -174,7 +174,7 @@ class NewsLetter
             }
         }
 
-        return new NLIssue($id, &$this);
+        return new NLIssue($id, $this);
     }
 
     /** Create a new, empty, pending newsletter issue
@@ -240,8 +240,8 @@ class NewsLetter
                              FROM  newsletter_issues
                             WHERE  nlid = {?} AND state = \'new\'',
                             $this->id);
-        if ($res->numRows()) {
-            $id = $res->fetchOneCell();
+        $id = $res->fetchOneCell();
+        if ($id != null) {
             return new NLIssue($id, $this);
         } else if ($create) {
             $id = $this->createPending();
@@ -666,6 +666,14 @@ class NLIssue
                                       WHERE  id = {?}',
                                       $this->id);
             if ($success) {
+                global $globals;
+                $mailer = new PlMailer('newsletter/notify_scheduled.mail.tpl');
+                $mailer->assign('group', $this->nl->group);
+                $mailer->assign('nl_title', $this->title_mail);
+                $mailer->assign('nl_id', $this->id());
+                $mailer->assign('base', $globals->baseurl);
+                $mailer->assign('send_before', $this->send_before);
+                $mailer->send();
                 $this->refresh();
             }
             return $success;
@@ -681,7 +689,7 @@ class NLIssue
     {
         if ($this->state == self::STATE_PENDING) {
             $success = XDB::execute('UPDATE  newsletter_issues
-                                        SET  send_before = NULL, state = \'new\'
+                                        SET  state = \'new\'
                                       WHERE  id = {?}', $this->id);
             if ($success) {
                 $this->refresh();
@@ -831,9 +839,9 @@ class NLIssue
     }
 
     /** Save an article
-     * @p &$a A reference to a NLArticle object (will be modified once saved)
+     * @p $a A reference to a NLArticle object (will be modified once saved)
      */
-    public function saveArticle(&$a)
+    public function saveArticle($a)
     {
         $this->fetchArticles();
 
@@ -927,7 +935,7 @@ class NLIssue
      * @p $page Smarty object
      * @return Either 'true' (if CSS was added to a page) or the raw CSS to add (when $page is null)
      */
-    public function css(&$page = null)
+    public function css($page = null)
     {
         if (!is_null($page)) {
             $page->addCssLink($this->nl->cssFile());
@@ -942,7 +950,7 @@ class NLIssue
      * @p $page Smarty object (using the $this->nl->tplFile() template)
      * @p $user User to use when rendering the template
      */
-    public function toText(&$page, $user)
+    public function toText($page, $user)
     {
         $this->fetchArticles();
 
@@ -959,7 +967,7 @@ class NLIssue
      * @p $page Smarty object (using the $this->nl->tplFile() template)
      * @p $user User to use when rendering the template
      */
-    public function toHtml(&$page, $user)
+    public function toHtml($page, $user)
     {
         $this->fetchArticles();
 
@@ -975,7 +983,7 @@ class NLIssue
     /** Set all 'common' data for the page (those which are required for both web and email rendering)
      * @p $smarty Smarty object (e.g page) which should be filled
      */
-    protected function assignData(&$smarty)
+    protected function assignData($smarty)
     {
         $this->fetchArticles();
 
@@ -985,7 +993,15 @@ class NLIssue
 
     // }}}
     // {{{ Mailing
-    
+
+    /** Check whether this issue is empty
+     * An issue is empty if the email has no title (or the default one), or no articles and an empty head.
+     */
+    public function isEmpty()
+    {
+        return $this->title_mail == '' || $this->title_mail == 'to be continued' || (count($this->arts) == 0 && strlen($this->head) == 0);
+    }
+
     /** Retrieve the 'Send before' date, in a clean format.
      */
     public function getSendBeforeDate()
@@ -1079,7 +1095,7 @@ class NLIssue
 
         $ufc = new PFC_And($this->getRecipientsUFC(), new UFC_NLSubscribed($this->nl->id, $this->id), new UFC_HasEmailRedirect());
         $emailsCount = 0;
-        $uf = new UserFilter($ufc);
+        $uf = new UserFilter($ufc, array(new UFO_IsAdmin(), new UFO_Uid()));
         $limit = new PlLimit(self::BATCH_SIZE);
 
         while (true) {