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 $this->_loadAttachments();
167 $this->_history_id
= $rs->mailing_id
;
172 * Copy attachments from another mailing
174 * @param int $id Original mailing id
178 private function _copyAttachments($id)
180 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
181 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
183 if ( file_exists($source_dir) ) {
184 if ( file_exists($dest_dir) ) {
185 throw new \
RuntimeException(
189 'Attachments directory already exists for mailing %s!'
195 //copy attachments from source mailing and populate attachments
196 $this->attachments
= array();
197 $files = glob($source_dir . '*.*');
198 foreach ( $files as $file ) {
199 $f = new File($source_dir);
200 $f->setFileName(str_replace($source_dir, '', $file));
201 $f->copyTo($dest_dir);
202 $this->attachments
[] = $f;
207 'No attachments in source directory',
214 * Apply final header to mail and send it :-)
216 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
218 public function send()
221 foreach ( $this->_mrecipients
as $member ) {
222 $m[$member->email
] = $member->sname
;
224 parent
::setRecipients($m);
225 return parent
::send();
229 * Set mailing recipients
231 * @param array $members Array of Adherent objects
235 public function setRecipients($members)
238 $this->_mrecipients
= array();
239 $this->_unreachables
= array();
241 foreach ($members as $member) {
242 $email = $member->email
;
243 if ( trim($email) != '' && self
::isValidEmail($email) ) {
244 if ( !in_array($member, $this->_mrecipients
) ) {
245 $this->_mrecipients
[] = $member;
247 $m[$email] = $member->sname
;
249 if ( !in_array($member, $this->_unreachables
) ) {
250 $this->_unreachables
[] = $member;
254 parent
::setRecipients($m);
258 * Store maling attachments
260 * @param array $files Array of uploaded files to store
262 * @return true|int error code
264 public function store($files)
266 if ( !file_exists($this->_tmp_path
) ) {
267 //directory does not exists, create it
268 mkdir($this->_tmp_path
);
271 if ( !is_dir($this->_tmp_path
) ) {
272 throw new \
RuntimeException(
273 $this->_tmp_path
. ' should be a directory!'
278 $attachment = new File($this->_tmp_path
);
279 $res = $attachment->store($files);
283 $this->attachments
[] = $attachment;
290 * Move attachments with final id once mailing has been stored
292 * @param int $id Mailing history id
296 public function moveAttachments($id)
298 if ( isset($this->_tmp_path
)
299 && trim($this->_tmp_path
) !== ''
301 foreach ( $this->attachments
as &$attachment ) {
302 $old_path = $attachment->getDestDir() . $attachment->getFileName();
303 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->_id
.'/' . $attachment->getFileName();
304 if ( !file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
305 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->_id
);
307 $moved = rename($old_path, $new_path);
309 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
312 rmdir($this->_tmp_path
);
313 $this->_tmp_path
= null;
318 * Remove specified attachment
320 * @param string $name Filename
324 public function removeAttachment($name)
327 if ( isset($this->_tmp_path
)
328 && trim($this->_tmp_path
) !== ''
329 && file_exists($this->_tmp_path
)
331 $to_remove = $this->_tmp_path
;
332 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
333 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
336 if ( $to_remove !== null ) {
337 $to_remove .= '/' . $name;
339 if ( !$this->attachments
) {
340 $this->_loadAttachments();
343 if ( file_exists($to_remove) ) {
345 foreach ( $this->attachments
as $att ) {
346 if ( $att->getFileName() == $name ) {
347 unset($this->attachments
[$i]);
358 'File %file does not exists and cannot be removed!'
364 throw new \
RuntimeException(
365 'Unable to get attachments path!'
371 * Remove mailing attachments
373 * @param boolean $temp Remove only tmporary attachments, to avoid history breaking
377 public function removeAttachments($temp = false)
380 if ( isset($this->_tmp_path
)
381 && trim($this->_tmp_path
) !== ''
382 && file_exists($this->_tmp_path
)
384 $to_remove = $this->_tmp_path
;
385 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
386 if ( $temp === true ) {
389 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
392 if ( $to_remove !== null ) {
393 $rdi = new \
RecursiveDirectoryIterator(
395 \FilesystemIterator
::SKIP_DOTS
397 $contents = new \
RecursiveIteratorIterator(
399 \RecursiveIteratorIterator
::CHILD_FIRST
401 foreach ( $contents as $path) {
402 if ( $path->isFile() ) {
403 unlink($path->getPathname());
405 rmdir($path->getPathname());
413 * Return textual error message
415 * @param int $code The error code
417 * @return string Localized message
419 public function getAttachmentErrorMessage($code)
421 $f = new File($this->_tmp_path
);
422 return $f->getErrorMessage($code);
426 * Does mailing already exists in history?
430 public function existsInHistory()
432 return isset($this->_history_id
);
436 * Global getter method
438 * @param string $name name of the property we want to retrive
440 * @return false|object the called property
442 public function __get($name)
444 $forbidden = array('ordered');
445 if ( !in_array($name, $forbidden) ) {
448 return $this->cleanedHtml();
451 return $this->current_step
;
454 return $this->getSubject();
457 return $this->getMessage();
460 return $this->isHTML();
464 return $this->getPhpMailer();
467 return $this->getErrors();
470 return $this->_mrecipients
;
473 if ( isset($this->_tmp_path
) && trim($this->_tmp_path
) !== '') {
474 return $this->_tmp_path
;
481 return $this->attachments
;
484 $rname = '_' . $name;
486 '[' . get_class($this) . 'Trying to get ' . $name .
487 ' renamed: ' . $rname,
490 return $this->$rname;
495 '[' . get_class($this) . 'Unable to get ' . $name .
496 ' renamed: ' . $rname,
504 * Global setter method
506 * @param string $name name of the property we want to assign a value to
507 * @param object $value a relevant value for the property
511 public function __set($name, $value)
513 $rname = '_' . $name;
517 $this->setSubject($value);
520 $this->setMessage($value);
523 if ( is_bool($value) ) {
524 $this->isHTML($value);
527 '[' . get_class($this) . '] Value for field `' . $name .
528 '` should be boolean - (' . gettype($value) . ')' .
536 && ( $value == self
::STEP_START
537 ||
$value == self
::STEP_PREVIEW
538 ||
$value == self
::STEP_SEND
539 ||
$value == self
::STEP_SENT
)
541 $this->_current_step
= (int)$value;
544 '[' . get_class($this) . '] Value for field `' . $name .
545 '` should be integer and know - (' . gettype($value) . ')' .
556 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',