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);
114 * Load mailing attachments
118 private function _loadAttachments()
121 if ( isset($this->_tmp_path
)
122 && trim($this->_tmp_path
) !== ''
124 $dir = $this->_tmp_path
;
126 $dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
129 $files = glob($dir . '*.*');
130 foreach ( $files as $file ) {
132 $f->setFileName(str_replace($dir, '', $file));
133 $this->attachments
[] = $f;
138 * Loads a mailing from history
140 * @param ResultSet $rs Mailing entry
144 public function loadFromHistory($rs)
146 $orig_recipients = unserialize($rs->mailing_recipients
);
148 $_recipients = array();
149 foreach ( $orig_recipients as $k=>$v ) {
150 $m = new Adherent($k);
153 $this->setRecipients($_recipients);
154 $this->subject
= $rs->mailing_subject
;
155 $this->message
= $rs->mailing_body
;
156 //if mailing has already been sent, generate a new id and copy attachments
157 if ( $rs->mailing_sent
) {
158 $this->_generateNewId();
159 $this->_copyAttachments($rs->mailing_id
);
161 $this->_history_id
= $rs->mailing_id
;
166 * Copy attachments from another mailing
168 * @param int $id Original mailing id
172 private function _copyAttachments($id)
174 $source_dir = GALETTE_ATTACHMENTS_PATH
. $id . '/';
175 $dest_dir = GALETTE_ATTACHMENTS_PATH
. $this->_id
. '/';
177 if ( file_exists($source_dir) ) {
178 if ( file_exists($dest_dir) ) {
179 throw new \
RuntimeException(
183 'Attachments directory already exists for mailing %s!'
187 //copy attachments from source mailing and populate attachments
188 $this->attachments
= array();
189 $files = glob($source_dir . '*.*');
190 foreach ( $files as $file ) {
191 $f = new File($source_dir);
192 $f->setFileName(str_replace($source_dir, '', $file));
193 $f->copyTo($dest_dir);
194 $this->attachments
[] = $f;
199 'No attachments in source directory',
206 * Apply final header to mail and send it :-)
208 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
210 public function send()
213 foreach ( $this->_mrecipients
as $member ) {
214 $m[$member->email
] = $member->sname
;
216 parent
::setRecipients($m);
217 return parent
::send();
221 * Set mailing recipients
223 * @param array $members Array of Adherent objects
227 public function setRecipients($members)
230 $this->_mrecipients
= array();
231 $this->_unreachables
= array();
233 foreach ($members as $member) {
234 $email = $member->email
;
235 if ( trim($email) != '' && self
::isValidEmail($email) ) {
236 if ( !in_array($member, $this->_mrecipients
) ) {
237 $this->_mrecipients
[] = $member;
239 $m[$email] = $member->sname
;
241 if ( !in_array($member, $this->_unreachables
) ) {
242 $this->_unreachables
[] = $member;
246 parent
::setRecipients($m);
250 * Store maling attachments
252 * @param array $files Array of uploaded files to store
254 * @return true|int error code
256 public function store($files)
258 $this->_tmp_path
= GALETTE_ATTACHMENTS_PATH
. '/' . $this->_id
;
260 if ( !file_exists($this->_tmp_path
) ) {
261 //directory does not exists, create it
262 mkdir($this->_tmp_path
);
265 if ( !is_dir($this->_tmp_path
) ) {
266 throw new \
RuntimeException(
267 $this->_tmp_path
. ' should be a directory!'
272 $attachment = new File($this->_tmp_path
);
273 $res = $attachment->store($files);
277 $this->attachments
[] = $attachment;
284 * Move attachments with final id once mailing has been stored
286 * @param int $id Mailing history id
290 public function moveAttachments($id)
292 if ( isset($this->_tmp_path
)
293 && trim($this->_tmp_path
) !== ''
295 foreach ( $this->attachments
as &$attachment ) {
296 $old_path = $attachment->getDestDir() . $attachment->getFileName();
297 $new_path = GALETTE_ATTACHMENTS_PATH
. $this->_id
.'/' . $attachment->getFileName();
298 if ( !file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
299 mkdir(GALETTE_ATTACHMENTS_PATH
. $this->_id
);
301 $moved = rename($old_path, $new_path);
303 $attachment->setDestDir(GALETTE_ATTACHMENTS_PATH
);
306 rmdir($this->_tmp_path
);
307 $this->_tmp_path
= null;
312 * Remove specified attachment
314 * @param string $name Filename
318 public function removeAttachment($name)
321 if ( isset($this->_tmp_path
)
322 && trim($this->_tmp_path
) !== ''
323 && file_exists($this->_tmp_path
)
325 $to_remove = $this->_tmp_path
;
326 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
327 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
330 if ( $to_remove !== null ) {
331 $to_remove .= '/' . $name;
333 if ( !$this->attachments
) {
334 $this->_loadAttachments();
337 if ( file_exists($to_remove) ) {
339 foreach ( $this->attachments
as $att ) {
340 if ( $att->getFileName() == $name ) {
341 unset($this->attachments
[$i]);
352 'File %file does not exists and cannot be removed!'
358 throw new \
RuntimeException(
359 'Unable to get attachments path!'
365 * Remove mailing attachments
367 * @param boolean $temp Remove only tmporary attachments, to avoid history breaking
371 public function removeAttachments($temp = false)
374 if ( isset($this->_tmp_path
)
375 && trim($this->_tmp_path
) !== ''
376 && file_exists($this->_tmp_path
)
378 $to_remove = $this->_tmp_path
;
379 } else if ( file_exists(GALETTE_ATTACHMENTS_PATH
. $this->_id
) ) {
380 if ( $temp === true ) {
383 $to_remove = GALETTE_ATTACHMENTS_PATH
. $this->_id
;
386 if ( $to_remove !== null ) {
387 $rdi = new \
RecursiveDirectoryIterator(
389 \FilesystemIterator
::SKIP_DOTS
391 $contents = new \
RecursiveIteratorIterator(
393 \RecursiveIteratorIterator
::CHILD_FIRST
395 foreach ( $contents as $path) {
396 if ( $path->isFile() ) {
397 unlink($path->getPathname());
399 rmdir($path->getPathname());
407 * Return textual error message
409 * @param int $code The error code
411 * @return string Localized message
413 public function getAttachmentErrorMessage($code)
415 $f = new File($this->_tmp_path
);
416 return $f->getErrorMessage($code);
420 * Does mailing already exists in history?
424 public function existsInHistory()
426 return isset($this->_history_id
);
430 * Global getter method
432 * @param string $name name of the property we want to retrive
434 * @return false|object the called property
436 public function __get($name)
438 $forbidden = array('ordered');
439 if ( !in_array($name, $forbidden) ) {
442 return $this->cleanedHtml();
445 return $this->current_step
;
448 return $this->getSubject();
451 return $this->getMessage();
454 return $this->isHTML();
458 return $this->getPhpMailer();
461 return $this->getErrors();
464 return $this->_mrecipients
;
467 if ( isset($this->_tmp_path
) && trim($this->_tmp_path
) !== '') {
468 return $this->_tmp_path
;
475 return $this->attachments
;
478 $rname = '_' . $name;
480 '[' . get_class($this) . 'Trying to get ' . $name .
481 ' renamed: ' . $rname,
484 return $this->$rname;
489 '[' . get_class($this) . 'Unable to get ' . $name .
490 ' renamed: ' . $rname,
498 * Global setter method
500 * @param string $name name of the property we want to assign a value to
501 * @param object $value a relevant value for the property
505 public function __set($name, $value)
507 $rname = '_' . $name;
511 $this->setSubject($value);
514 $this->setMessage($value);
517 if ( is_bool($value) ) {
518 $this->isHTML($value);
521 '[' . get_class($this) . '] Value for field `' . $name .
522 '` should be boolean - (' . gettype($value) . ')' .
530 && ( $value == self
::STEP_START
531 ||
$value == self
::STEP_PREVIEW
532 ||
$value == self
::STEP_SEND
533 ||
$value == self
::STEP_SENT
)
535 $this->_current_step
= (int)$value;
538 '[' . get_class($this) . '] Value for field `' . $name .
539 '` should be integer and know - (' . gettype($value) . ')' .
550 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',