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->_id
= $rs->mailing_id
;
165 $this->_loadAttachments();
166 $this->_history_id
= $rs->mailing_id
;
171 * Copy attachments from another mailing
173 * @param int $id Original mailing id
177 private function _copyAttachments($id)
179 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
180 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
182 if ( file_exists($source_dir) ) {
183 if ( file_exists($dest_dir) ) {
184 throw new \
RuntimeException(
188 'Attachments directory already exists for mailing %s!'
192 //copy attachments from source mailing and populate attachments
193 $this->attachments
= array();
194 $files = glob($source_dir . '*.*');
195 foreach ( $files as $file ) {
196 $f = new File($source_dir);
197 $f->setFileName(str_replace($source_dir, '', $file));
198 $f->copyTo($dest_dir);
199 $this->attachments
[] = $f;
204 'No attachments in source directory',
211 * Apply final header to mail and send it :-)
213 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
215 public function send()
218 foreach ( $this->_mrecipients
as $member ) {
219 $m[$member->email
] = $member->sname
;
221 parent
::setRecipients($m);
222 return parent
::send();
226 * Set mailing recipients
228 * @param array $members Array of Adherent objects
232 public function setRecipients($members)
235 $this->_mrecipients
= array();
236 $this->_unreachables
= array();
238 foreach ($members as $member) {
239 $email = $member->email
;
240 if ( trim($email) != '' && self
::isValidEmail($email) ) {
241 if ( !in_array($member, $this->_mrecipients
) ) {
242 $this->_mrecipients
[] = $member;
244 $m[$email] = $member->sname
;
246 if ( !in_array($member, $this->_unreachables
) ) {
247 $this->_unreachables
[] = $member;
251 parent
::setRecipients($m);
255 * Store maling attachments
257 * @param array $files Array of uploaded files to store
259 * @return true|int error code
261 public function store($files)
263 if ( !file_exists($this->_tmp_path
) ) {
264 //directory does not exists, create it
265 mkdir($this->_tmp_path
);
268 if ( !is_dir($this->_tmp_path
) ) {
269 throw new \
RuntimeException(
270 $this->_tmp_path
. ' should be a directory!'
275 $attachment = new File($this->_tmp_path
);
276 $res = $attachment->store($files);
280 $this->attachments
[] = $attachment;
287 * Move attachments with final id once mailing has been stored
289 * @param int $id Mailing history id
293 public function moveAttachments($id)
295 if ( isset($this->_tmp_path
)
296 && trim($this->_tmp_path
) !== ''
298 foreach ( $this->attachments
as &$attachment ) {
299 $old_path = $attachment->getDestDir() . $attachment->getFileName();
300 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->_id
.'/' . $attachment->getFileName();
301 if ( !file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
302 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->_id
);
304 $moved = rename($old_path, $new_path);
306 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
309 rmdir($this->_tmp_path
);
310 $this->_tmp_path
= null;
315 * Remove specified attachment
317 * @param string $name Filename
321 public function removeAttachment($name)
324 if ( isset($this->_tmp_path
)
325 && trim($this->_tmp_path
) !== ''
326 && file_exists($this->_tmp_path
)
328 $to_remove = $this->_tmp_path
;
329 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
330 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
333 if ( $to_remove !== null ) {
334 $to_remove .= '/' . $name;
336 if ( !$this->attachments
) {
337 $this->_loadAttachments();
340 if ( file_exists($to_remove) ) {
342 foreach ( $this->attachments
as $att ) {
343 if ( $att->getFileName() == $name ) {
344 unset($this->attachments
[$i]);
355 'File %file does not exists and cannot be removed!'
361 throw new \
RuntimeException(
362 'Unable to get attachments path!'
368 * Remove mailing attachments
370 * @param boolean $temp Remove only tmporary attachments, to avoid history breaking
374 public function removeAttachments($temp = false)
377 if ( isset($this->_tmp_path
)
378 && trim($this->_tmp_path
) !== ''
379 && file_exists($this->_tmp_path
)
381 $to_remove = $this->_tmp_path
;
382 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
383 if ( $temp === true ) {
386 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
389 if ( $to_remove !== null ) {
390 $rdi = new \
RecursiveDirectoryIterator(
392 \FilesystemIterator
::SKIP_DOTS
394 $contents = new \
RecursiveIteratorIterator(
396 \RecursiveIteratorIterator
::CHILD_FIRST
398 foreach ( $contents as $path) {
399 if ( $path->isFile() ) {
400 unlink($path->getPathname());
402 rmdir($path->getPathname());
410 * Return textual error message
412 * @param int $code The error code
414 * @return string Localized message
416 public function getAttachmentErrorMessage($code)
418 $f = new File($this->_tmp_path
);
419 return $f->getErrorMessage($code);
423 * Does mailing already exists in history?
427 public function existsInHistory()
429 return isset($this->_history_id
);
433 * Global getter method
435 * @param string $name name of the property we want to retrive
437 * @return false|object the called property
439 public function __get($name)
441 $forbidden = array('ordered');
442 if ( !in_array($name, $forbidden) ) {
445 return $this->cleanedHtml();
448 return $this->current_step
;
451 return $this->getSubject();
454 return $this->getMessage();
457 return $this->isHTML();
461 return $this->getPhpMailer();
464 return $this->getErrors();
467 return $this->_mrecipients
;
470 if ( isset($this->_tmp_path
) && trim($this->_tmp_path
) !== '') {
471 return $this->_tmp_path
;
478 return $this->attachments
;
481 $rname = '_' . $name;
483 '[' . get_class($this) . 'Trying to get ' . $name .
484 ' renamed: ' . $rname,
487 return $this->$rname;
492 '[' . get_class($this) . 'Unable to get ' . $name .
493 ' renamed: ' . $rname,
501 * Global setter method
503 * @param string $name name of the property we want to assign a value to
504 * @param object $value a relevant value for the property
508 public function __set($name, $value)
510 $rname = '_' . $name;
514 $this->setSubject($value);
517 $this->setMessage($value);
520 if ( is_bool($value) ) {
521 $this->isHTML($value);
524 '[' . get_class($this) . '] Value for field `' . $name .
525 '` should be boolean - (' . gettype($value) . ')' .
533 && ( $value == self
::STEP_START
534 ||
$value == self
::STEP_PREVIEW
535 ||
$value == self
::STEP_SEND
536 ||
$value == self
::STEP_SENT
)
538 $this->_current_step
= (int)$value;
541 '[' . get_class($this) . '] Value for field `' . $name .
542 '` should be integer and know - (' . gettype($value) . ')' .
553 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',