3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2018 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-2018 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-2018 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 relavant 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(),
210 * Builds the order clause
212 * @return string SQL ORDER clause
214 protected function buildOrderClause()
218 switch ($this->filters
->orderby
) {
219 case MailingsList
::ORDERBY_DATE
:
220 $order[] = 'mailing_date ' . $this->filters
->ordered
;
222 case MailingsList
::ORDERBY_SENDER
:
223 $order[] = 'mailing_sender ' . $this->filters
->ordered
;
225 case MailingsList
::ORDERBY_SUBJECT
:
226 $order[] = 'mailing_subject ' . $this->filters
->ordered
;
228 case MailingsList
::ORDERBY_SENT
:
229 $order[] = 'mailing_sent ' . $this->filters
->ordered
;
237 * Builds where clause, for filtering on simple list mode
239 * @param Select $select Original select
241 * @return string SQL WHERE clause
243 private function buildWhereClause($select)
246 if ($this->filters
->start_date_filter
!= null) {
247 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
248 $select->where
->greaterThanOrEqualTo(
254 if ($this->filters
->end_date_filter
!= null) {
255 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
256 $select->where
->lessThanOrEqualTo(
262 if ($this->filters
->sender_filter
!= null && $this->filters
->sender_filter
!= '0') {
263 $sender = $this->filters
->sender_filter
;
264 if ($sender == '-1') {
265 $select->where('mailing_sender IS NULL');
267 $select->where
->equalTo(
274 switch ($this->filters
->sent_filter
) {
275 case self
::FILTER_SENT
:
276 $select->where('mailing_sent = true');
278 case self
::FILTER_NOT_SENT
:
279 $select->where('mailing_sent = false');
281 case self
::FILTER_DC_SENT
:
282 //nothing to do here.
287 if ($this->filters
->subject_filter
!= '') {
288 $token = $this->zdb
->platform
->quoteValue(
289 '%' . strtolower($this->filters
->subject_filter
) . '%'
293 'LOWER(mailing_subject) LIKE ' .
297 } catch (Throwable
$e) {
299 __METHOD__
. ' | ' . $e->getMessage(),
306 * Count history entries from the query
308 * @param Select $select Original select
312 private function proceedCount($select)
315 $countSelect = clone $select;
316 $countSelect->reset($countSelect::COLUMNS
);
317 $countSelect->reset($countSelect::JOINS
);
318 $countSelect->reset($countSelect::ORDER
);
319 $countSelect->columns(
321 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
325 $results = $this->zdb
->execute($countSelect);
326 $result = $results->current();
329 $this->count
= $result->$k;
330 if ($this->count
> 0) {
331 $this->filters
->setCounter($this->count
);
333 } catch (Throwable
$e) {
335 'Cannot count history | ' . $e->getMessage(),
343 * Load mailing from an existing one
345 * @param Db $zdb Database instance
346 * @param integer $id Model identifier
347 * @param GaletteMailing $mailing Mailing object
348 * @param boolean $new True if we create a 'new' mailing,
349 * false otherwise (from preview for example)
353 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
356 $select = $zdb->select(self
::TABLE
);
357 $select->where('mailing_id = ' . $id);
359 $results = $zdb->execute($select);
360 $result = $results->current();
362 return $mailing->loadFromHistory($result, $new);
363 } catch (Throwable
$e) {
365 'Unable to load mailing model #' . $id . ' | ' .
374 * Store a mailing in the history
376 * @param boolean $sent Defaults to false
380 public function storeMailing($sent = false)
382 if ($this->mailing
instanceof Mailing
) {
383 if ($this->mailing
->sender_name
!= null) {
384 $this->sender_name
= $this->mailing
->getSenderName();
385 $this->sender_address
= $this->mailing
->getSenderAddress();
387 $this->sender
= $this->login
->id
;
388 $this->subject
= $this->mailing
->subject
;
389 $this->message
= $this->mailing
->message
;
390 $this->recipients
= $this->mailing
->recipients
;
392 $this->date
= date('Y-m-d H:i:s');
393 if (!$this->mailing
->existsInHistory()) {
395 $this->mailing
->id
= $this->id
;
396 $this->mailing
->moveAttachments($this->id
);
398 if ($this->mailing
->tmp_path
!== false) {
399 //attachments are still in a temporary path, move them
400 $this->mailing
->moveAttachments($this->id ??
$this->mailing
->history_id
);
402 //existing stored mailing. Just update row.
408 '] Mailing should be an instance of Mailing',
416 * Update in the database
420 public function update()
423 $_recipients = array();
424 if ($this->recipients
!= null) {
425 foreach ($this->recipients
as $_r) {
426 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
430 $sender = ($this->sender
=== 0) ?
431 new Expression('NULL') : $this->sender
;
432 $sender_name = ($this->sender_name
=== null) ?
433 new Expression('NULL') : $this->sender_name
;
434 $sender_address = ($this->sender_address
=== null) ?
435 new Expression('NULL') : $this->sender_address
;
438 'mailing_sender' => $sender,
439 'mailing_sender_name' => $sender_name,
440 'mailing_sender_address' => $sender_address,
441 'mailing_subject' => $this->subject
,
442 'mailing_body' => $this->message
,
443 'mailing_date' => $this->date
,
444 'mailing_recipients' => serialize($_recipients),
445 'mailing_sent' => ($this->sent
) ?
447 ($this->zdb
->isPostgres() ?
'false' : 0)
450 $update = $this->zdb
->update(self
::TABLE
);
451 $update->set($values);
452 $update->where(self
::PK
. ' = ' . $this->mailing
->history_id
);
453 $this->zdb
->execute($update);
455 } catch (Throwable
$e) {
457 'An error occurend updating Mailing | ' . $e->getMessage(),
465 * Store in the database
469 public function store()
472 $_recipients = array();
473 if ($this->recipients
!= null) {
474 foreach ($this->recipients
as $_r) {
475 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
480 if ($this->sender
=== 0) {
481 $sender = new Expression('NULL');
483 $sender = $this->sender
;
485 $sender_name = ($this->sender_name
=== null) ?
486 new Expression('NULL') : $this->sender_name
;
487 $sender_address = ($this->sender_address
=== null) ?
488 new Expression('NULL') : $this->sender_address
;
491 'mailing_sender' => $sender,
492 'mailing_sender_name' => $sender_name,
493 'mailing_sender_address' => $sender_address,
494 'mailing_subject' => $this->subject
,
495 'mailing_body' => $this->message
,
496 'mailing_date' => $this->date
,
497 'mailing_recipients' => serialize($_recipients),
498 'mailing_sent' => ($this->sent
) ?
500 ($this->zdb
->isPostgres() ?
'false' : 0)
503 $insert = $this->zdb
->insert(self
::TABLE
);
504 $insert->values($values);
505 $this->zdb
->execute($insert);
507 $this->id
= $this->zdb
->getLastGeneratedValue($this);
509 } catch (Throwable
$e) {
511 'An error occurend storing Mailing | ' . $e->getMessage(),
519 * Remove specified entries
521 * @param integer|array $ids Mailing history entries identifiers
522 * @param History $hist History instance
526 public function removeEntries($ids, History
$hist)
529 if (is_numeric($ids)) {
530 //we've got only one identifier
536 if (is_array($list)) {
538 foreach ($list as $id) {
539 $mailing = new Mailing($this->preferences
, [], $id);
540 $mailing->removeAttachments();
543 $this->zdb
->connection
->beginTransaction();
546 $delete = $this->zdb
->delete(self
::TABLE
);
547 $delete->where
->in(self
::PK
, $list);
548 $this->zdb
->execute($delete);
551 $this->zdb
->connection
->commit();
553 //add an history entry
555 _T("Delete mailing entries")
559 } catch (Throwable
$e) {
560 $this->zdb
->connection
->rollBack();
562 'Unable to delete selected mailing history entries |' .
569 //not numeric and not an array: incorrect.
571 'Asking to remove mailing entries, but without ' .
572 'providing an array or a single numeric value.',
582 * @param boolean $prefixed Whether table name should be prefixed
586 protected function getTableName($prefixed = false)
588 if ($prefixed === true) {
589 return PREFIX_DB
. self
::TABLE
;
600 protected function getPk()
606 * Get count for current query
610 public function getCount()
620 public function getSendersList()
622 return $this->senders
;