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 class MailingHistory
extends History
61 public const TABLE
= 'mailing_history';
62 public const PK
= 'mailing_id';
64 public const FILTER_DC_SENT
= 0;
65 public const FILTER_SENT
= 1;
66 public const FILTER_NOT_SENT
= 2;
68 private $mailing = null;
76 private $sender_address;
77 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 if ($this->count
> 0) {
277 $this->filters
->setCounter($this->count
);
279 } catch (Throwable
$e) {
281 'Cannot count history | ' . $e->getMessage(),
289 * Load mailing from an existing one
291 * @param Db $zdb Database instance
292 * @param integer $id Model identifier
293 * @param Mailing $mailing Mailing object
294 * @param boolean $new True if we create a 'new' mailing,
295 * false otherwise (from preview for example)
299 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
302 $select = $zdb->select(self
::TABLE
);
303 $select->where(['mailing_id' => $id]);
305 $results = $zdb->execute($select);
306 /** @var ArrayObject $result */
307 $result = $results->current();
309 return $mailing->loadFromHistory($result, $new);
310 } catch (Throwable
$e) {
312 'Unable to load mailing model #' . $id . ' | ' .
321 * Store a mailing in the history
323 * @param boolean $sent Defaults to false
327 public function storeMailing($sent = false)
329 if ($this->mailing
instanceof Mailing
) {
330 if ($this->mailing
->sender_name
!= null) {
331 $this->sender_name
= $this->mailing
->getSenderName();
332 $this->sender_address
= $this->mailing
->getSenderAddress();
334 $this->sender
= $this->login
->id
;
335 $this->subject
= $this->mailing
->subject
;
336 $this->message
= $this->mailing
->message
;
337 $this->recipients
= $this->mailing
->recipients
;
339 $this->date
= date('Y-m-d H:i:s');
340 if (!$this->mailing
->existsInHistory()) {
342 $this->mailing
->id
= $this->id
;
343 $this->mailing
->moveAttachments($this->id
);
345 if ($this->mailing
->tmp_path
!== false) {
346 //attachments are still in a temporary path, move them
347 $this->mailing
->moveAttachments($this->id ??
$this->mailing
->history_id
);
349 //existing stored mailing. Just update row.
356 '] Mailing should be an instance of Mailing',
364 * Update in the database
368 public function update()
371 $_recipients = array();
372 if ($this->recipients
!= null) {
373 foreach ($this->recipients
as $_r) {
374 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
378 $sender = ($this->sender
=== 0) ?
379 new Expression('NULL') : $this->sender
;
380 $sender_name = ($this->sender_name
=== null) ?
381 new Expression('NULL') : $this->sender_name
;
382 $sender_address = ($this->sender_address
=== null) ?
383 new Expression('NULL') : $this->sender_address
;
386 'mailing_sender' => $sender,
387 'mailing_sender_name' => $sender_name,
388 'mailing_sender_address' => $sender_address,
389 'mailing_subject' => $this->subject
,
390 'mailing_body' => $this->message
,
391 'mailing_date' => $this->date
,
392 'mailing_recipients' => serialize($_recipients),
393 'mailing_sent' => ($this->sent
) ?
395 ($this->zdb
->isPostgres() ?
'false' : 0)
398 $update = $this->zdb
->update(self
::TABLE
);
399 $update->set($values);
400 $update->where([self
::PK
=> $this->mailing
->history_id
]);
401 $this->zdb
->execute($update);
403 } catch (Throwable
$e) {
405 'An error occurend updating Mailing | ' . $e->getMessage(),
413 * Store in the database
417 public function store()
420 $_recipients = array();
421 if ($this->recipients
!= null) {
422 foreach ($this->recipients
as $_r) {
423 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
428 if ($this->sender
=== 0) {
429 $sender = new Expression('NULL');
431 $sender = $this->sender
;
433 $sender_name = ($this->sender_name
=== null) ?
434 new Expression('NULL') : $this->sender_name
;
435 $sender_address = ($this->sender_address
=== null) ?
436 new Expression('NULL') : $this->sender_address
;
439 'mailing_sender' => $sender,
440 'mailing_sender_name' => $sender_name,
441 'mailing_sender_address' => $sender_address,
442 'mailing_subject' => $this->subject
,
443 'mailing_body' => $this->message
,
444 'mailing_date' => $this->date
,
445 'mailing_recipients' => serialize($_recipients),
446 'mailing_sent' => ($this->sent
) ?
448 ($this->zdb
->isPostgres() ?
'false' : 0)
451 $insert = $this->zdb
->insert(self
::TABLE
);
452 $insert->values($values);
453 $this->zdb
->execute($insert);
455 $this->id
= $this->zdb
->getLastGeneratedValue($this);
457 } catch (Throwable
$e) {
459 'An error occurend storing Mailing | ' . $e->getMessage(),
467 * Remove specified entries
469 * @param integer|array $ids Mailing history entries identifiers
470 * @param History $hist History instance
474 public function removeEntries($ids, History
$hist)
477 if (is_numeric($ids)) {
478 //we've got only one identifier
484 if (is_array($list)) {
486 foreach ($list as $id) {
487 $mailing = new Mailing($this->preferences
, [], $id);
488 $mailing->removeAttachments();
491 $this->zdb
->connection
->beginTransaction();
494 $delete = $this->zdb
->delete(self
::TABLE
);
495 $delete->where
->in(self
::PK
, $list);
496 $this->zdb
->execute($delete);
499 $this->zdb
->connection
->commit();
501 //add an history entry
503 _T("Delete mailing entries")
507 } catch (Throwable
$e) {
508 $this->zdb
->connection
->rollBack();
510 'Unable to delete selected mailing history entries |' .
517 //not numeric and not an array: incorrect.
519 'Asking to remove mailing entries, but without ' .
520 'providing an array or a single numeric value.',
530 * @param boolean $prefixed Whether table name should be prefixed
534 protected function getTableName($prefixed = false)
536 if ($prefixed === true) {
537 return PREFIX_DB
. self
::TABLE
;
548 protected function getPk()
554 * Get count for current query
558 public function getCount()