3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2023 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 2011-2023 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 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2011-08-27
37 namespace Galette\Core
;
40 use Laminas\Db\Sql\Select
;
43 use Galette\Entity\Adherent
;
44 use Galette\Filters\MailingsList
;
45 use Laminas\Db\Sql\Expression
;
51 * @name MailingHistory
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2011-2023 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.7dev - 2011-08-27
59 * @property MailingsList $filters
61 class MailingHistory
extends History
63 public const TABLE
= 'mailing_history';
64 public const PK
= 'mailing_id';
66 public const FILTER_DC_SENT
= 0;
67 public const FILTER_SENT
= 1;
68 public const FILTER_NOT_SENT
= 2;
70 private $mailing = null;
78 private $sender_address;
79 private $sent = false;
84 * @param Db $zdb Database
85 * @param Login $login Login
86 * @param Preferences $preferences Preferences
87 * @param MailingsList|null $filters Filtering
88 * @param Mailing|null $mailing Mailing
90 public function __construct(Db
$zdb, Login
$login, Preferences
$preferences, MailingsList
$filters = null, Mailing
$mailing = null)
92 parent
::__construct($zdb, $login, $preferences, $filters);
93 $this->mailing
= $mailing;
97 * Get the entire history list
101 public function getHistory()
104 $select = $this->zdb
->select($this->getTableName(), 'a');
106 array('b' => PREFIX_DB
. Adherent
::TABLE
),
107 'a.mailing_sender=b.' . Adherent
::PK
,
108 array('nom_adh', 'prenom_adh'),
111 $this->buildWhereClause($select);
112 $select->order($this->buildOrderClause());
113 $this->proceedCount($select);
114 //add limits to retrieve only relevant rows
115 $this->filters
->setLimits($select);
116 $results = $this->zdb
->execute($select);
119 foreach ($results as $r) {
120 if ($r['mailing_sender'] !== null && $r['mailing_sender_name'] === null) {
121 $r['mailing_sender_name']
122 = Adherent
::getSName($this->zdb
, $r['mailing_sender']);
126 if (file_exists(GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
])) {
127 $rdi = new \
RecursiveDirectoryIterator(
128 GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
],
129 \FilesystemIterator
::SKIP_DOTS
131 $contents = new \
RecursiveIteratorIterator(
133 \RecursiveIteratorIterator
::CHILD_FIRST
135 foreach ($contents as $path) {
136 if ($path->isFile()) {
141 $r['attachments'] = $attachments;
145 } catch (Throwable
$e) {
147 'Unable to get history. | ' . $e->getMessage(),
155 * Builds the order clause
157 * @return array SQL ORDER clauses
159 protected function buildOrderClause()
163 switch ($this->filters
->orderby
) {
164 case MailingsList
::ORDERBY_DATE
:
165 $order[] = 'mailing_date ' . $this->filters
->ordered
;
167 case MailingsList
::ORDERBY_SENDER
:
168 $order[] = 'mailing_sender ' . $this->filters
->ordered
;
170 case MailingsList
::ORDERBY_SUBJECT
:
171 $order[] = 'mailing_subject ' . $this->filters
->ordered
;
173 case MailingsList
::ORDERBY_SENT
:
174 $order[] = 'mailing_sent ' . $this->filters
->ordered
;
182 * Builds where clause, for filtering on simple list mode
184 * @param Select $select Original select
188 private function buildWhereClause($select)
191 if ($this->filters
->start_date_filter
!= null) {
192 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
193 $select->where
->greaterThanOrEqualTo(
199 if ($this->filters
->end_date_filter
!= null) {
200 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
201 $select->where
->lessThanOrEqualTo(
207 if ($this->filters
->sender_filter
!= null && $this->filters
->sender_filter
!= '0') {
208 $sender = $this->filters
->sender_filter
;
209 if ($sender == '-1') {
210 $select->where('mailing_sender IS NULL');
212 $select->where
->equalTo(
219 switch ($this->filters
->sent_filter
) {
220 case self
::FILTER_SENT
:
221 $select->where('mailing_sent = true');
223 case self
::FILTER_NOT_SENT
:
224 $select->where('mailing_sent = false');
226 case self
::FILTER_DC_SENT
:
227 //nothing to do here.
232 if ($this->filters
->subject_filter
!= '') {
233 $token = $this->zdb
->platform
->quoteValue(
234 '%' . strtolower($this->filters
->subject_filter
) . '%'
238 'LOWER(mailing_subject) LIKE ' .
242 } catch (Throwable
$e) {
244 __METHOD__
. ' | ' . $e->getMessage(),
252 * Count history entries from the query
254 * @param Select $select Original select
258 private function proceedCount($select)
261 $countSelect = clone $select;
262 $countSelect->reset($countSelect::COLUMNS
);
263 $countSelect->reset($countSelect::JOINS
);
264 $countSelect->reset($countSelect::ORDER
);
265 $countSelect->columns(
267 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
271 $results = $this->zdb
->execute($countSelect);
272 $result = $results->current();
275 $this->count
= $result->$k;
276 $this->filters
->setCounter($this->count
);
277 } catch (Throwable
$e) {
279 'Cannot count history | ' . $e->getMessage(),
287 * Load mailing from an existing one
289 * @param Db $zdb Database instance
290 * @param integer $id Model identifier
291 * @param Mailing $mailing Mailing object
292 * @param boolean $new True if we create a 'new' mailing,
293 * false otherwise (from preview for example)
297 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
300 $select = $zdb->select(self
::TABLE
);
301 $select->where(['mailing_id' => $id]);
303 $results = $zdb->execute($select);
304 /** @var ArrayObject $result */
305 $result = $results->current();
307 return $mailing->loadFromHistory($result, $new);
308 } catch (Throwable
$e) {
310 'Unable to load mailing model #' . $id . ' | ' .
319 * Store a mailing in the history
321 * @param boolean $sent Defaults to false
325 public function storeMailing($sent = false)
327 if ($this->mailing
instanceof Mailing
) {
328 if ($this->mailing
->sender_name
!= null) {
329 $this->sender_name
= $this->mailing
->getSenderName();
330 $this->sender_address
= $this->mailing
->getSenderAddress();
332 $this->sender
= $this->login
->id
;
333 $this->subject
= $this->mailing
->subject
;
334 $this->message
= $this->mailing
->message
;
335 $this->recipients
= $this->mailing
->recipients
;
337 $this->date
= date('Y-m-d H:i:s');
338 if (!$this->mailing
->existsInHistory()) {
340 $this->mailing
->id
= $this->id
;
341 $this->mailing
->moveAttachments($this->id
);
343 if ($this->mailing
->tmp_path
!== false) {
344 //attachments are still in a temporary path, move them
345 $this->mailing
->moveAttachments($this->id ??
$this->mailing
->history_id
);
347 //existing stored mailing. Just update row.
354 '] Mailing should be an instance of Mailing',
362 * Update in the database
366 public function update()
369 $_recipients = array();
370 if ($this->recipients
!= null) {
371 foreach ($this->recipients
as $_r) {
372 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
376 $sender = ($this->sender
=== 0) ?
377 new Expression('NULL') : $this->sender
;
378 $sender_name = ($this->sender_name
=== null) ?
379 new Expression('NULL') : $this->sender_name
;
380 $sender_address = ($this->sender_address
=== null) ?
381 new Expression('NULL') : $this->sender_address
;
384 'mailing_sender' => $sender,
385 'mailing_sender_name' => $sender_name,
386 'mailing_sender_address' => $sender_address,
387 'mailing_subject' => $this->subject
,
388 'mailing_body' => $this->message
,
389 'mailing_date' => $this->date
,
390 'mailing_recipients' => serialize($_recipients),
391 'mailing_sent' => ($this->sent
) ?
393 ($this->zdb
->isPostgres() ?
'false' : 0)
396 $update = $this->zdb
->update(self
::TABLE
);
397 $update->set($values);
398 $update->where([self
::PK
=> $this->mailing
->history_id
]);
399 $this->zdb
->execute($update);
401 } catch (Throwable
$e) {
403 'An error occurend updating Mailing | ' . $e->getMessage(),
411 * Store in the database
415 public function store()
418 $_recipients = array();
419 if ($this->recipients
!= null) {
420 foreach ($this->recipients
as $_r) {
421 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
426 if ($this->sender
=== 0) {
427 $sender = new Expression('NULL');
429 $sender = $this->sender
;
431 $sender_name = ($this->sender_name
=== null) ?
432 new Expression('NULL') : $this->sender_name
;
433 $sender_address = ($this->sender_address
=== null) ?
434 new Expression('NULL') : $this->sender_address
;
437 'mailing_sender' => $sender,
438 'mailing_sender_name' => $sender_name,
439 'mailing_sender_address' => $sender_address,
440 'mailing_subject' => $this->subject
,
441 'mailing_body' => $this->message
,
442 'mailing_date' => $this->date
,
443 'mailing_recipients' => serialize($_recipients),
444 'mailing_sent' => ($this->sent
) ?
446 ($this->zdb
->isPostgres() ?
'false' : 0)
449 $insert = $this->zdb
->insert(self
::TABLE
);
450 $insert->values($values);
451 $this->zdb
->execute($insert);
453 $this->id
= $this->zdb
->getLastGeneratedValue($this);
455 } catch (Throwable
$e) {
457 'An error occurend storing Mailing | ' . $e->getMessage(),
465 * Remove specified entries
467 * @param integer|array $ids Mailing history entries identifiers
468 * @param History $hist History instance
472 public function removeEntries($ids, History
$hist)
475 if (is_numeric($ids)) {
476 //we've got only one identifier
482 if (is_array($list)) {
484 foreach ($list as $id) {
485 $mailing = new Mailing($this->preferences
, [], $id);
486 $mailing->removeAttachments();
489 $this->zdb
->connection
->beginTransaction();
492 $delete = $this->zdb
->delete(self
::TABLE
);
493 $delete->where
->in(self
::PK
, $list);
494 $this->zdb
->execute($delete);
497 $this->zdb
->connection
->commit();
499 //add an history entry
501 _T("Delete mailing entries")
505 } catch (Throwable
$e) {
506 $this->zdb
->connection
->rollBack();
508 'Unable to delete selected mailing history entries |' .
515 //not numeric and not an array: incorrect.
517 'Asking to remove mailing entries, but without ' .
518 'providing an array or a single numeric value.',
528 * @param boolean $prefixed Whether table name should be prefixed
532 protected function getTableName($prefixed = false)
534 if ($prefixed === true) {
535 return PREFIX_DB
. self
::TABLE
;
546 protected function getPk()
552 * Get count for current query
556 public function getCount()