3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2021 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-2021 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
;
42 use Galette\Core\Login
;
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-2021 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->buildLists($select);
114 $this->proceedCount($select);
115 //add limits to retrieve only relevant rows
116 $this->filters
->setLimits($select);
117 $results = $this->zdb
->execute($select);
120 foreach ($results as $r) {
121 if ($r['mailing_sender'] !== null && $r['mailing_sender_name'] === null) {
122 $r['mailing_sender_name']
123 = Adherent
::getSName($this->zdb
, $r['mailing_sender']);
125 $body_resume = $r['mailing_body'];
126 if (strlen($body_resume) > 150) {
127 $body_resume = substr($body_resume, 0, 150);
128 $body_resume .= '[...]';
130 if (function_exists('tidy_parse_string')) {
131 //if tidy extension is present, we use it to clean a bit
132 $tidy_config = array(
134 'show-body-only' => true,
137 $tidy = tidy_parse_string($body_resume, $tidy_config, 'UTF8');
138 $tidy->cleanRepair();
139 $r['mailing_body_resume'] = tidy_get_output($tidy);
141 //if it is not... Well, let's serve the text as it.
142 $r['mailing_body_resume'] = $body_resume;
146 if (file_exists(GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
])) {
147 $rdi = new \
RecursiveDirectoryIterator(
148 GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
],
149 \FilesystemIterator
::SKIP_DOTS
151 $contents = new \
RecursiveIteratorIterator(
153 \RecursiveIteratorIterator
::CHILD_FIRST
155 foreach ($contents as $path) {
156 if ($path->isFile()) {
161 $r['attachments'] = $attachments;
165 } catch (Throwable
$e) {
167 'Unable to get history. | ' . $e->getMessage(),
175 * Builds users and actions lists
177 * @param \Laminas\Db\Sql\Select $select Original select
181 private function buildLists($select)
184 $select = $this->zdb
->select(self
::TABLE
);
185 $select->quantifier('DISTINCT')->columns(['mailing_sender']);
186 $select->order(['mailing_sender ASC']);
188 $results = $this->zdb
->execute($select);
191 foreach ($results as $result) {
192 $sender = $result->mailing_sender
;
193 if ($sender != null) {
194 $this->senders
[$sender] = Adherent
::getSName($this->zdb
, (int)$sender);
195 } elseif ($result->mailing_sender_name
!= null ||
$result->mailing_sender_address
!= null) {
196 $this->senders
[$result->mailing_sender_address
] = $result->mailing_sender_name
;
198 $this->senders
[-1] = _('Superadmin');
201 } catch (Throwable
$e) {
203 'Cannot list senders from mailing history! | ' . $e->getMessage(),
211 * Builds the order clause
213 * @return string SQL ORDER clause
215 protected function buildOrderClause()
219 switch ($this->filters
->orderby
) {
220 case MailingsList
::ORDERBY_DATE
:
221 $order[] = 'mailing_date ' . $this->filters
->ordered
;
223 case MailingsList
::ORDERBY_SENDER
:
224 $order[] = 'mailing_sender ' . $this->filters
->ordered
;
226 case MailingsList
::ORDERBY_SUBJECT
:
227 $order[] = 'mailing_subject ' . $this->filters
->ordered
;
229 case MailingsList
::ORDERBY_SENT
:
230 $order[] = 'mailing_sent ' . $this->filters
->ordered
;
238 * Builds where clause, for filtering on simple list mode
240 * @param Select $select Original select
242 * @return string SQL WHERE clause
244 private function buildWhereClause($select)
247 if ($this->filters
->start_date_filter
!= null) {
248 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
249 $select->where
->greaterThanOrEqualTo(
255 if ($this->filters
->end_date_filter
!= null) {
256 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
257 $select->where
->lessThanOrEqualTo(
263 if ($this->filters
->sender_filter
!= null && $this->filters
->sender_filter
!= '0') {
264 $sender = $this->filters
->sender_filter
;
265 if ($sender == '-1') {
266 $select->where('mailing_sender IS NULL');
268 $select->where
->equalTo(
275 switch ($this->filters
->sent_filter
) {
276 case self
::FILTER_SENT
:
277 $select->where('mailing_sent = true');
279 case self
::FILTER_NOT_SENT
:
280 $select->where('mailing_sent = false');
282 case self
::FILTER_DC_SENT
:
283 //nothing to do here.
288 if ($this->filters
->subject_filter
!= '') {
289 $token = $this->zdb
->platform
->quoteValue(
290 '%' . strtolower($this->filters
->subject_filter
) . '%'
294 'LOWER(mailing_subject) LIKE ' .
298 } catch (Throwable
$e) {
300 __METHOD__
. ' | ' . $e->getMessage(),
308 * Count history entries from the query
310 * @param Select $select Original select
314 private function proceedCount($select)
317 $countSelect = clone $select;
318 $countSelect->reset($countSelect::COLUMNS
);
319 $countSelect->reset($countSelect::JOINS
);
320 $countSelect->reset($countSelect::ORDER
);
321 $countSelect->columns(
323 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
327 $results = $this->zdb
->execute($countSelect);
328 $result = $results->current();
331 $this->count
= $result->$k;
332 if ($this->count
> 0) {
333 $this->filters
->setCounter($this->count
);
335 } catch (Throwable
$e) {
337 'Cannot count history | ' . $e->getMessage(),
345 * Load mailing from an existing one
347 * @param Db $zdb Database instance
348 * @param integer $id Model identifier
349 * @param GaletteMailing $mailing Mailing object
350 * @param boolean $new True if we create a 'new' mailing,
351 * false otherwise (from preview for example)
355 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
358 $select = $zdb->select(self
::TABLE
);
359 $select->where('mailing_id = ' . $id);
361 $results = $zdb->execute($select);
362 $result = $results->current();
364 return $mailing->loadFromHistory($result, $new);
365 } catch (Throwable
$e) {
367 'Unable to load mailing model #' . $id . ' | ' .
376 * Store a mailing in the history
378 * @param boolean $sent Defaults to false
382 public function storeMailing($sent = false)
384 if ($this->mailing
instanceof Mailing
) {
385 if ($this->mailing
->sender_name
!= null) {
386 $this->sender_name
= $this->mailing
->getSenderName();
387 $this->sender_address
= $this->mailing
->getSenderAddress();
389 $this->sender
= $this->login
->id
;
390 $this->subject
= $this->mailing
->subject
;
391 $this->message
= $this->mailing
->message
;
392 $this->recipients
= $this->mailing
->recipients
;
394 $this->date
= date('Y-m-d H:i:s');
395 if (!$this->mailing
->existsInHistory()) {
397 $this->mailing
->id
= $this->id
;
398 $this->mailing
->moveAttachments($this->id
);
400 if ($this->mailing
->tmp_path
!== false) {
401 //attachments are still in a temporary path, move them
402 $this->mailing
->moveAttachments($this->id ??
$this->mailing
->history_id
);
404 //existing stored mailing. Just update row.
410 '] Mailing should be an instance of Mailing',
417 * Update in the database
421 public function update()
424 $_recipients = array();
425 if ($this->recipients
!= null) {
426 foreach ($this->recipients
as $_r) {
427 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
431 $sender = ($this->sender
=== 0) ?
432 new Expression('NULL') : $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 $update = $this->zdb
->update(self
::TABLE
);
452 $update->set($values);
453 $update->where(self
::PK
. ' = ' . $this->mailing
->history_id
);
454 $this->zdb
->execute($update);
456 } catch (Throwable
$e) {
458 'An error occurend updating Mailing | ' . $e->getMessage(),
466 * Store in the database
470 public function store()
473 $_recipients = array();
474 if ($this->recipients
!= null) {
475 foreach ($this->recipients
as $_r) {
476 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
481 if ($this->sender
=== 0) {
482 $sender = new Expression('NULL');
484 $sender = $this->sender
;
486 $sender_name = ($this->sender_name
=== null) ?
487 new Expression('NULL') : $this->sender_name
;
488 $sender_address = ($this->sender_address
=== null) ?
489 new Expression('NULL') : $this->sender_address
;
492 'mailing_sender' => $sender,
493 'mailing_sender_name' => $sender_name,
494 'mailing_sender_address' => $sender_address,
495 'mailing_subject' => $this->subject
,
496 'mailing_body' => $this->message
,
497 'mailing_date' => $this->date
,
498 'mailing_recipients' => serialize($_recipients),
499 'mailing_sent' => ($this->sent
) ?
501 ($this->zdb
->isPostgres() ?
'false' : 0)
504 $insert = $this->zdb
->insert(self
::TABLE
);
505 $insert->values($values);
506 $this->zdb
->execute($insert);
508 $this->id
= $this->zdb
->getLastGeneratedValue($this);
510 } catch (Throwable
$e) {
512 'An error occurend storing Mailing | ' . $e->getMessage(),
520 * Remove specified entries
522 * @param integer|array $ids Mailing history entries identifiers
523 * @param History $hist History instance
527 public function removeEntries($ids, History
$hist)
530 if (is_numeric($ids)) {
531 //we've got only one identifier
537 if (is_array($list)) {
539 foreach ($list as $id) {
540 $mailing = new Mailing($this->preferences
, [], $id);
541 $mailing->removeAttachments();
544 $this->zdb
->connection
->beginTransaction();
547 $delete = $this->zdb
->delete(self
::TABLE
);
548 $delete->where
->in(self
::PK
, $list);
549 $this->zdb
->execute($delete);
552 $this->zdb
->connection
->commit();
554 //add an history entry
556 _T("Delete mailing entries")
560 } catch (Throwable
$e) {
561 $this->zdb
->connection
->rollBack();
563 'Unable to delete selected mailing history entries |' .
570 //not numeric and not an array: incorrect.
572 'Asking to remove mailing entries, but without ' .
573 'providing an array or a single numeric value.',
583 * @param boolean $prefixed Whether table name should be prefixed
587 protected function getTableName($prefixed = false)
589 if ($prefixed === true) {
590 return PREFIX_DB
. self
::TABLE
;
601 protected function getPk()
607 * Get count for current query
611 public function getCount()
621 public function getSendersList()
623 return $this->senders
;