]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Mailing.php
Merge branch 'hotfix/0.7.2.4' into develop
[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 if ( trim($member->email) != '' && self::isValidEmail($member->email) ) {
118 if ( !in_array($member, $this->_mrecipients) ) {
119 $this->_mrecipients[] = $member;
120 }
121 $m[$member->email] = $member->sname;
122 } else {
123 if ( !in_array($member, $this->_unreachables) ) {
124 $this->_unreachables[] = $member;
125 }
126 }
127 }
128 parent::setRecipients($m);
129 }
130
131 /**
132 * Global getter method
133 *
134 * @param string $name name of the property we want to retrive
135 *
136 * @return false|object the called property
137 */
138 public function __get($name)
139 {
140 global $log;
141 $forbidden = array('ordered');
142 if ( !in_array($name, $forbidden) ) {
143 switch($name) {
144 case 'alt_message':
145 return $this->cleanedHtml();
146 break;
147 case 'step':
148 return $this->current_step;
149 break;
150 case 'subject':
151 return $this->getSubject();
152 break;
153 case 'message':
154 return $this->getMessage();
155 break;
156 case 'html':
157 return $this->isHTML();
158 break;
159 case 'mail':
160 case '_mail':
161 return $this->getPhpMailer();
162 break;
163 case 'errors':
164 return $this->getErrors();
165 break;
166 case 'recipients':
167 return $this->_mrecipients;
168 break;
169 default:
170 $rname = '_' . $name;
171 $log->log(
172 '[' . get_class($this) . 'Trying to get ' . $name .
173 ' renamed: ' . $rname,
174 KLogger::DEBUG
175 );
176 return $this->$rname;
177 break;
178 }
179 } else {
180 $log->log(
181 '[' . get_class($this) . 'Unable to get ' . $name .
182 ' renamed: ' . $rname,
183 KLogger::ERR
184 );
185 return false;
186 }
187 }
188
189 /**
190 * Global setter method
191 *
192 * @param string $name name of the property we want to assign a value to
193 * @param object $value a relevant value for the property
194 *
195 * @return void
196 */
197 public function __set($name, $value)
198 {
199 global $log;
200 $rname = '_' . $name;
201
202 switch( $name ) {
203 case 'subject':
204 $this->setSubject($value);
205 break;
206 case 'message':
207 $this->setMessage($value);
208 break;
209 case 'html':
210 if ( is_bool($value) ) {
211 $this->isHTML($value);
212 } else {
213 $log->log(
214 '[' . get_class($this) . '] Value for field `' . $name .
215 '` should be boolean - (' . gettype($value) . ')' .
216 $value . ' given',
217 KLogger::WARN
218 );
219 }
220 break;
221 /** FIXME: remove... should no longer exists with phpMailer */
222 /*case 'message':
223 $this->$rname = (get_magic_quotes_gpc())
224 ? stripslashes($value)
225 : $value;
226 break;*/
227 case 'current_step':
228 if ( is_int($value)
229 && ( $value == self::STEP_START
230 || $value == self::STEP_PREVIEW
231 || $value == self::STEP_SEND
232 || $value == self::STEP_SENT )
233 ) {
234 $this->_current_step = (int)$value;
235 } else {
236 $log->log(
237 '[' . get_class($this) . '] Value for field `' . $name .
238 '` should be integer and know - (' . gettype($value) . ')' .
239 $value . ' given',
240 KLogger::WARN
241 );
242 }
243 break;
244 default:
245 $log->log(
246 '[' . get_class($this) . '] Unable to set proprety `' . $name . '`',
247 KLogger::WARN
248 );
249 break;
250 }
251 }
252 }