3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2013 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2013 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2009-03-07
38 namespace Galette\Core
;
40 use Analog\Analog
as Analog
;
41 use Galette\Entity\Adherent
;
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2009-2013 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7dev - 2009-03-07
56 class Mailing
extends GaletteMail
59 const STEP_PREVIEW
= 1;
63 const MIME_HTML
= 'text/html';
64 const MIME_TEXT
= 'text/plain';
65 const MIME_DEFAULT
= self
::MIME_TEXT
;
69 private $_unreachables;
70 private $_mrecipients;
71 private $_current_step;
81 * @param array $members An array of members
82 * @param int $id Identifier, defaults to null
84 public function __construct($members, $id = null)
89 $this->_generateNewId();
91 $this->_current_step
= self
::STEP_START
;
92 $this->_mime_type
= self
::MIME_DEFAULT
;
93 /** TODO: add a preference that propose default mime-type to use,
95 if ( $members !== null) {
96 //Check which members have a valid email adress and which have not
97 $this->setRecipients($members);
99 $this->_loadAttachments();
103 * Generate new mailing id
107 private function _generateNewId()
109 $pass = new Password();
110 $this->_id
= $pass->makeRandomPassword(30);
111 $this->_tmp_path
= GALETTE_ATTACHMENTS_PATH
. '/' . $this->_id
;
115 * Load mailing attachments
119 private function _loadAttachments()
122 if ( isset($this->_tmp_path
)
123 && trim($this->_tmp_path
) !== ''
125 $dir = $this->_tmp_path
;
127 $dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
130 $files = glob($dir . '*.*');
131 foreach ( $files as $file ) {
133 $f->setFileName(str_replace($dir, '', $file));
134 $this->attachments
[] = $f;
139 * Loads a mailing from history
141 * @param ResultSet $rs Mailing entry
142 * @param boolean $new True if we create a 'new' mailing,
143 * false otherwise (from preview for example)
147 public function loadFromHistory($rs, $new = true)
149 $orig_recipients = unserialize($rs->mailing_recipients
);
151 $_recipients = array();
152 foreach ( $orig_recipients as $k=>$v ) {
153 $m = new Adherent($k);
156 $this->setRecipients($_recipients);
157 $this->subject
= $rs->mailing_subject
;
158 $this->message
= $rs->mailing_body
;
159 //if mailing has already been sent, generate a new id and copy attachments
160 if ( $rs->mailing_sent
&& $new ) {
161 $this->_generateNewId();
162 $this->_copyAttachments($rs->mailing_id
);
164 $this->_tmp_path
= null;
165 $this->_id
= $rs->mailing_id
;
166 if ( !$this->attachments
) {
167 $this->_loadAttachments();
169 $this->_history_id
= $rs->mailing_id
;
174 * Copy attachments from another mailing
176 * @param int $id Original mailing id
180 private function _copyAttachments($id)
182 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
183 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
185 if ( file_exists($source_dir) ) {
186 if ( file_exists($dest_dir) ) {
187 throw new \
RuntimeException(
191 'Attachments directory already exists for mailing %s!'
197 //copy attachments from source mailing and populate attachments
198 $this->attachments
= array();
199 $files = glob($source_dir . '*.*');
200 foreach ( $files as $file ) {
201 $f = new File($source_dir);
202 $f->setFileName(str_replace($source_dir, '', $file));
203 $f->copyTo($dest_dir);
204 $this->attachments
[] = $f;
209 'No attachments in source directory',
216 * Apply final header to mail and send it :-)
218 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
220 public function send()
223 foreach ( $this->_mrecipients
as $member ) {
224 $m[$member->email
] = $member->sname
;
226 parent
::setRecipients($m);
227 return parent
::send();
231 * Set mailing recipients
233 * @param array $members Array of Adherent objects
237 public function setRecipients($members)
240 $this->_mrecipients
= array();
241 $this->_unreachables
= array();
243 foreach ($members as $member) {
244 $email = $member->email
;
245 if ( trim($email) != '' && self
::isValidEmail($email) ) {
246 if ( !in_array($member, $this->_mrecipients
) ) {
247 $this->_mrecipients
[] = $member;
249 $m[$email] = $member->sname
;
251 if ( !in_array($member, $this->_unreachables
) ) {
252 $this->_unreachables
[] = $member;
256 parent
::setRecipients($m);
260 * Store maling attachments
262 * @param array $files Array of uploaded files to store
264 * @return true|int error code
266 public function store($files)
268 if ( !file_exists($this->_tmp_path
) ) {
269 //directory does not exists, create it
270 mkdir($this->_tmp_path
);
273 if ( !is_dir($this->_tmp_path
) ) {
274 throw new \
RuntimeException(
275 $this->_tmp_path
. ' should be a directory!'
280 $attachment = new File($this->_tmp_path
);
281 $res = $attachment->store($files);
285 $this->attachments
[] = $attachment;
292 * Move attachments with final id once mailing has been stored
294 * @param int $id Mailing history id
298 public function moveAttachments($id)
300 if ( isset($this->_tmp_path
)
301 && trim($this->_tmp_path
) !== ''
303 foreach ( $this->attachments
as &$attachment ) {
304 $old_path = $attachment->getDestDir() . $attachment->getFileName();
305 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->_id
.'/' . $attachment->getFileName();
306 if ( !file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
307 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->_id
);
309 $moved = rename($old_path, $new_path);
311 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
314 rmdir($this->_tmp_path
);
315 $this->_tmp_path
= null;
320 * Remove specified attachment
322 * @param string $name Filename
326 public function removeAttachment($name)
329 if ( isset($this->_tmp_path
)
330 && trim($this->_tmp_path
) !== ''
331 && file_exists($this->_tmp_path
)
333 $to_remove = $this->_tmp_path
;
334 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
335 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
338 if ( $to_remove !== null ) {
339 $to_remove .= '/' . $name;
341 if ( !$this->attachments
) {
342 $this->_loadAttachments();
345 if ( file_exists($to_remove) ) {
347 foreach ( $this->attachments
as $att ) {
348 if ( $att->getFileName() == $name ) {
349 unset($this->attachments
[$i]);
360 'File %file does not exists and cannot be removed!'
366 throw new \
RuntimeException(
367 'Unable to get attachments path!'
373 * Remove mailing attachments
375 * @param boolean $temp Remove only tmporary attachments, to avoid history breaking
379 public function removeAttachments($temp = false)
382 if ( isset($this->_tmp_path
)
383 && trim($this->_tmp_path
) !== ''
384 && file_exists($this->_tmp_path
)
386 $to_remove = $this->_tmp_path
;
387 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
388 if ( $temp === true ) {
391 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
394 if ( $to_remove !== null ) {
395 $rdi = new \
RecursiveDirectoryIterator(
397 \FilesystemIterator
::SKIP_DOTS
399 $contents = new \
RecursiveIteratorIterator(
401 \RecursiveIteratorIterator
::CHILD_FIRST
403 foreach ( $contents as $path) {
404 if ( $path->isFile() ) {
405 unlink($path->getPathname());
407 rmdir($path->getPathname());
415 * Return textual error message
417 * @param int $code The error code
419 * @return string Localized message
421 public function getAttachmentErrorMessage($code)
423 $f = new File($this->_tmp_path
);
424 return $f->getErrorMessage($code);
428 * Does mailing already exists in history?
432 public function existsInHistory()
434 return isset($this->_history_id
);
438 * Global getter method
440 * @param string $name name of the property we want to retrive
442 * @return false|object the called property
444 public function __get($name)
446 $forbidden = array('ordered');
447 if ( !in_array($name, $forbidden) ) {
450 return $this->cleanedHtml();
453 return $this->current_step
;
456 return $this->getSubject();
459 return $this->getMessage();
462 return $this->isHTML();
466 return $this->getPhpMailer();
469 return $this->getErrors();
472 return $this->_mrecipients
;
475 if ( isset($this->_tmp_path
) && trim($this->_tmp_path
) !== '') {
476 return $this->_tmp_path
;
483 return $this->attachments
;
486 $rname = '_' . $name;
488 '[' . get_class($this) . 'Trying to get ' . $name .
489 ' renamed: ' . $rname,
492 return $this->$rname;
497 '[' . get_class($this) . 'Unable to get ' . $name .
498 ' renamed: ' . $rname,
506 * Global setter method
508 * @param string $name name of the property we want to assign a value to
509 * @param object $value a relevant value for the property
513 public function __set($name, $value)
515 $rname = '_' . $name;
519 $this->setSubject($value);
522 $this->setMessage($value);
525 if ( is_bool($value) ) {
526 $this->isHTML($value);
529 '[' . get_class($this) . '] Value for field `' . $name .
530 '` should be boolean - (' . gettype($value) . ')' .
538 && ( $value == self
::STEP_START
539 ||
$value == self
::STEP_PREVIEW
540 ||
$value == self
::STEP_SEND
541 ||
$value == self
::STEP_SENT
)
543 $this->_current_step
= (int)$value;
546 '[' . get_class($this) . '] Value for field `' . $name .
547 '` should be integer and know - (' . gettype($value) . ')' .
558 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',