]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/Core/Mailing.php
Fix mailing preview; fixes #1800
[galette.git] / galette / lib / Galette / Core / Mailing.php
index cbe3ab14eb854d55ea0eba61bd298bc94cecc347..e2344014d58341e623a48eff892a3558d1337966 100644 (file)
 namespace Galette\Core;
 
 use Analog\Analog;
+use ArrayObject;
 use Galette\Entity\Adherent;
 use Galette\IO\File;
+use Laminas\Db\ResultSet\ResultSet;
 
 /**
  * Mailing features
@@ -68,6 +70,7 @@ use Galette\IO\File;
  * @property array $attachments
  * @property-read string $sender_name
  * @property-read string $sender_address
+ * @property integer $history_id
  */
 class Mailing extends GaletteMail
 {
@@ -179,17 +182,25 @@ class Mailing extends GaletteMail
     /**
      * Loads a mailing from history
      *
-     * @param ResultSet $rs  Mailing entry
-     * @param boolean   $new True if we create a 'new' mailing,
-     *                       false otherwise (from preview for example)
+     * @param ArrayObject $rs  Mailing entry
+     * @param boolean     $new True if we create a 'new' mailing,
+     *                         false otherwise (from preview for example)
      *
      * @return boolean
      */
-    public function loadFromHistory($rs, $new = true)
+    public function loadFromHistory(ArrayObject $rs, $new = true)
     {
         global $zdb;
 
-        $orig_recipients = unserialize($rs->mailing_recipients);
+        try {
+            $orig_recipients = unserialize($rs->mailing_recipients);
+        } catch (\Throwable $e) {
+            Analog::log(
+                'Unable to unserialize recipients for mailing ' . $rs->mailing_id,
+                Analog::ERROR
+            );
+            $orig_recipients = [];
+        }
 
         $_recipients = array();
         $mdeps = ['parent' => true];
@@ -200,6 +211,7 @@ class Mailing extends GaletteMail
         $this->setRecipients($_recipients);
         $this->subject = $rs->mailing_subject;
         $this->message = $rs->mailing_body;
+        $this->html = $this->message != strip_tags($this->message) ? true : false;
         if ($rs->mailing_sender_name !== null || $rs->mailing_sender_address !== null) {
             $this->setSender(
                 $rs->mailing_sender_name,
@@ -266,7 +278,7 @@ class Mailing extends GaletteMail
     /**
      * Apply final header to email and send it :-)
      *
-     * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
+     * @return int
      */
     public function send()
     {
@@ -284,7 +296,7 @@ class Mailing extends GaletteMail
      *
      * @param array $members Array of Adherent objects
      *
-     * @return void
+     * @return bool
      */
     public function setRecipients($members)
     {
@@ -306,7 +318,7 @@ class Mailing extends GaletteMail
                 }
             }
         }
-        parent::setRecipients($m);
+        return parent::setRecipients($m);
     }
 
     /**
@@ -323,7 +335,7 @@ class Mailing extends GaletteMail
         }
 
         if (!file_exists($this->tmp_path)) {
-            //directory does not exists, create it
+            //directory does not exist, create it
             mkdir($this->tmp_path);
         }
 
@@ -436,7 +448,7 @@ class Mailing extends GaletteMail
      * @param boolean $temp Remove only tmporary attachments,
      *                      to avoid history breaking
      *
-     * @return void
+     * @return void|false
      */
     public function removeAttachments($temp = false)
     {
@@ -500,9 +512,9 @@ class Mailing extends GaletteMail
     /**
      * Global getter method
      *
-     * @param string $name name of the property we want to retrive
+     * @param string $name name of the property we want to retrieve
      *
-     * @return false|object the called property
+     * @return mixed the called property
      */
     public function __get($name)
     {
@@ -511,32 +523,23 @@ class Mailing extends GaletteMail
             switch ($name) {
                 case 'alt_message':
                     return $this->cleanedHtml();
-                    break;
                 case 'step':
                     return $this->current_step;
-                    break;
                 case 'subject':
                     return $this->getSubject();
-                    break;
                 case 'message':
                     return $this->getMessage();
-                    break;
                 case 'wrapped_message':
                     return $this->getWrappedMessage();
-                    break;
                 case 'html':
                     return $this->isHTML();
-                    break;
                 case 'mail':
                 case '_mail':
                     return $this->getPhpMailer();
-                    break;
                 case 'errors':
                     return $this->getErrors();
-                    break;
                 case 'recipients':
                     return $this->mrecipients;
-                    break;
                 case 'tmp_path':
                     if (isset($this->tmp_path) && trim($this->tmp_path) !== '') {
                         return $this->tmp_path;
@@ -544,23 +547,20 @@ class Mailing extends GaletteMail
                         //no attachments
                         return false;
                     }
-                    break;
                 case 'attachments':
                     return $this->attachments;
-                    break;
                 case 'sender_name':
                     return $this->getSenderName();
-                    break;
                 case 'sender_address':
                     return $this->getSenderAddress();
-                    break;
+                case 'history_id':
+                    return $this->$name;
                 default:
                     Analog::log(
                         '[' . get_class($this) . 'Trying to get ' . $name,
                         Analog::DEBUG
                     );
                     return $this->$name;
-                    break;
             }
         } else {
             Analog::log(
@@ -575,9 +575,9 @@ class Mailing extends GaletteMail
      * Global isset method
      * Required for twig to access properties via __get
      *
-     * @param string $name name of the property we want to retrive
+     * @param string $name name of the property we want to retrieve
      *
-     * @return false|object the called property
+     * @return bool
      */
     public function __isset($name)
     {
@@ -610,7 +610,7 @@ class Mailing extends GaletteMail
      * Global setter method
      *
      * @param string $name  name of the property we want to assign a value to
-     * @param object $value a relevant value for the property
+     * @param mixed  $value a relevant value for the property
      *
      * @return void
      */
@@ -658,11 +658,9 @@ class Mailing extends GaletteMail
                 break;
             default:
                 Analog::log(
-                    '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',
+                    '[' . get_class($this) . '] Unable to set property `' . $name . '`',
                     Analog::WARNING
                 );
-                return false;
-                break;
         }
     }
 }