]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Mailing.php
Reminders should only concern active accounts, other minor improvements; refs #462
[galette.git] / galette / lib / Galette / Core / Mailing.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Mailing features
7 *
8 * PHP version 5
9 *
10 * Copyright © 2009-2012 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Core
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2012 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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2009-03-07
36 */
37
38 namespace Galette\Core;
39
40 use Galette\Common\KLogger as KLogger;
41
42 /**
43 * Mailing features
44 *
45 * @category Core
46 * @name Mailing
47 * @package Galette
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2009-2012 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since Available since 0.7dev - 2009-03-07
53 */
54 class Mailing extends GaletteMail
55 {
56 const STEP_START = 0;
57 const STEP_PREVIEW = 1;
58 const STEP_SEND = 2;
59 const STEP_SENT = 3;
60
61 const MIME_HTML = 'text/html';
62 const MIME_TEXT = 'text/plain';
63 const MIME_DEFAULT = self::MIME_TEXT;
64
65 private $_unreachables;
66 private $_mrecipients;
67 private $_current_step;
68
69 private $_mime_type;
70
71 /**
72 * Default constructor
73 *
74 * @param array $members An array of members
75 */
76 public function __construct($members)
77 {
78 $this->_current_step = self::STEP_START;
79 $this->_mime_type = self::MIME_DEFAULT;
80 /** TODO: add a preference that propose default mime-type to use,
81 then init it here */
82 if ( $members !== null) {
83 //Check which members have a valid email adress and which have not
84 $this->setRecipients($members);
85 }
86 }
87
88 /**
89 * Apply final header to mail and send it :-)
90 *
91 * @return GaletteMail::MAIL_ERROR|GaletteMail::MAIL_SENT
92 */
93 public function send()
94 {
95 $m = array();
96 foreach ( $this->_mrecipients as $member ) {
97 $m[$member->email] = $member->sname;
98 }
99 parent::setRecipients($m);
100 return parent::send();
101 }
102
103 /**
104 * Set mailing recipients
105 *
106 * @param <type> $members Array of Adherent objects
107 *
108 * @return void
109 */
110 public function setRecipients($members)
111 {
112 $m = array();
113 $this->_mrecipients = array();
114 $this->_unreachables = array();
115
116 foreach ($members as $member) {
117 $email = $member->email;
118 if ( trim($email) != '' && self::isValidEmail($email) ) {
119 if ( !in_array($member, $this->_mrecipients) ) {
120 $this->_mrecipients[] = $member;
121 }
122 $m[$email] = $member->sname;
123 } else {
124 if ( !in_array($member, $this->_unreachables) ) {
125 $this->_unreachables[] = $member;
126 }
127 }
128 }
129 parent::setRecipients($m);
130 }
131
132 /**
133 * Global getter method
134 *
135 * @param string $name name of the property we want to retrive
136 *
137 * @return false|object the called property
138 */
139 public function __get($name)
140 {
141 global $log;
142 $forbidden = array('ordered');
143 if ( !in_array($name, $forbidden) ) {
144 switch($name) {
145 case 'alt_message':
146 return $this->cleanedHtml();
147 break;
148 case 'step':
149 return $this->current_step;
150 break;
151 case 'subject':
152 return $this->getSubject();
153 break;
154 case 'message':
155 return $this->getMessage();
156 break;
157 case 'html':
158 return $this->isHTML();
159 break;
160 case 'mail':
161 case '_mail':
162 return $this->getPhpMailer();
163 break;
164 case 'errors':
165 return $this->getErrors();
166 break;
167 case 'recipients':
168 return $this->_mrecipients;
169 break;
170 default:
171 $rname = '_' . $name;
172 $log->log(
173 '[' . get_class($this) . 'Trying to get ' . $name .
174 ' renamed: ' . $rname,
175 KLogger::DEBUG
176 );
177 return $this->$rname;
178 break;
179 }
180 } else {
181 $log->log(
182 '[' . get_class($this) . 'Unable to get ' . $name .
183 ' renamed: ' . $rname,
184 KLogger::ERR
185 );
186 return false;
187 }
188 }
189
190 /**
191 * Global setter method
192 *
193 * @param string $name name of the property we want to assign a value to
194 * @param object $value a relevant value for the property
195 *
196 * @return void
197 */
198 public function __set($name, $value)
199 {
200 global $log;
201 $rname = '_' . $name;
202
203 switch( $name ) {
204 case 'subject':
205 $this->setSubject($value);
206 break;
207 case 'message':
208 $this->setMessage($value);
209 break;
210 case 'html':
211 if ( is_bool($value) ) {
212 $this->isHTML($value);
213 } else {
214 $log->log(
215 '[' . get_class($this) . '] Value for field `' . $name .
216 '` should be boolean - (' . gettype($value) . ')' .
217 $value . ' given',
218 KLogger::WARN
219 );
220 }
221 break;
222 /** FIXME: remove... should no longer exists with phpMailer */
223 /*case 'message':
224 $this->$rname = (get_magic_quotes_gpc())
225 ? stripslashes($value)
226 : $value;
227 break;*/
228 case 'current_step':
229 if ( is_int($value)
230 && ( $value == self::STEP_START
231 || $value == self::STEP_PREVIEW
232 || $value == self::STEP_SEND
233 || $value == self::STEP_SENT )
234 ) {
235 $this->_current_step = (int)$value;
236 } else {
237 $log->log(
238 '[' . get_class($this) . '] Value for field `' . $name .
239 '` should be integer and know - (' . gettype($value) . ')' .
240 $value . ' given',
241 KLogger::WARN
242 );
243 }
244 break;
245 default:
246 $log->log(
247 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',
248 KLogger::WARN
249 );
250 break;
251 }
252 }
253 }
254 ?>