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
;
41 use Galette\Core\Login
;
42 use Galette\Entity\Adherent
;
43 use Galette\Filters\MailingsList
;
44 use Laminas\Db\Sql\Expression
;
50 * @name MailingHistory
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2011-2018 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.7dev - 2011-08-27
58 class MailingHistory
extends History
60 public const TABLE
= 'mailing_history';
61 public const PK
= 'mailing_id';
63 public const FILTER_DC_SENT
= 0;
64 public const FILTER_SENT
= 1;
65 public const FILTER_NOT_SENT
= 2;
67 private $mailing = null;
75 private $sender_address;
76 private $sent = false;
83 * @param Db $zdb Database
84 * @param Login $login Login
85 * @param Preferences $preferences Preferences
86 * @param MailingsList|null $filters Filtering
87 * @param Mailing|null $mailing Mailing
89 public function __construct(Db
$zdb, Login
$login, Preferences
$preferences, MailingsList
$filters = null, Mailing
$mailing = null)
91 parent
::__construct($zdb, $login, $preferences, $filters);
92 $this->mailing
= $mailing;
96 * Get the entire history list
100 public function getHistory()
103 $select = $this->zdb
->select($this->getTableName(), 'a');
105 array('b' => PREFIX_DB
. Adherent
::TABLE
),
106 'a.mailing_sender=b.' . Adherent
::PK
,
107 array('nom_adh', 'prenom_adh'),
110 $this->buildWhereClause($select);
111 $select->order(self
::buildOrderClause());
112 $this->buildLists($select);
113 $this->proceedCount($select);
114 //add limits to retrieve only relavant 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']);
124 $body_resume = $r['mailing_body'];
125 if (strlen($body_resume) > 150) {
126 $body_resume = substr($body_resume, 0, 150);
127 $body_resume .= '[...]';
129 if (function_exists('tidy_parse_string')) {
130 //if tidy extension is present, we use it to clean a bit
131 $tidy_config = array(
133 'show-body-only' => true,
136 $tidy = tidy_parse_string($body_resume, $tidy_config, 'UTF8');
137 $tidy->cleanRepair();
138 $r['mailing_body_resume'] = tidy_get_output($tidy);
140 //if it is not... Well, let's serve the text as it.
141 $r['mailing_body_resume'] = $body_resume;
145 if (file_exists(GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
])) {
146 $rdi = new \
RecursiveDirectoryIterator(
147 GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
],
148 \FilesystemIterator
::SKIP_DOTS
150 $contents = new \
RecursiveIteratorIterator(
152 \RecursiveIteratorIterator
::CHILD_FIRST
154 foreach ($contents as $path) {
155 if ($path->isFile()) {
160 $r['attachments'] = $attachments;
164 } catch (\Exception
$e) {
166 'Unable to get history. | ' . $e->getMessage(),
174 * Builds users and actions lists
176 * @param Select $select Original select
180 private function buildLists($select)
183 $select = $this->zdb
->select(self
::TABLE
);
184 $select->quantifier('DISTINCT')->columns(['mailing_sender']);
185 $select->order(['mailing_sender ASC']);
187 $results = $this->zdb
->execute($select);
190 foreach ($results as $result) {
191 $sender = $result->mailing_sender
;
192 if ($sender != null) {
193 $this->senders
[$sender] = Adherent
::getSName($this->zdb
, (int)$sender);
194 } elseif ($result->mailing_sender_name
!= null ||
$result->mailing_sender_address
!= null) {
195 $this->senders
[$result->mailing_sender_address
] = $result->mailing_sender_name
;
197 $this->senders
[-1] = _('Superadmin');
200 } catch (\Exception
$e) {
202 'Cannot list senders from mailing history! | ' . $e->getMessage(),
209 * Builds the order clause
211 * @return string SQL ORDER clause
213 protected function buildOrderClause()
217 switch ($this->filters
->orderby
) {
218 case MailingsList
::ORDERBY_DATE
:
219 $order[] = 'mailing_date ' . $this->filters
->ordered
;
221 case MailingsList
::ORDERBY_SENDER
:
222 $order[] = 'mailing_sender ' . $this->filters
->ordered
;
224 case MailingsList
::ORDERBY_SUBJECT
:
225 $order[] = 'mailing_subject ' . $this->filters
->ordered
;
227 case MailingsList
::ORDERBY_SENT
:
228 $order[] = 'mailing_sent ' . $this->filters
->ordered
;
236 * Builds where clause, for filtering on simple list mode
238 * @param Select $select Original select
240 * @return string SQL WHERE clause
242 private function buildWhereClause($select)
245 if ($this->filters
->start_date_filter
!= null) {
246 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
247 $select->where
->greaterThanOrEqualTo(
253 if ($this->filters
->end_date_filter
!= null) {
254 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
255 $select->where
->lessThanOrEqualTo(
261 if ($this->filters
->sender_filter
!= null && $this->filters
->sender_filter
!= '0') {
262 $sender = $this->filters
->sender_filter
;
263 if ($sender == '-1') {
264 $select->where('mailing_sender IS NULL');
266 $select->where
->equalTo(
273 switch ($this->filters
->sent_filter
) {
274 case self
::FILTER_SENT
:
275 $select->where('mailing_sent = true');
277 case self
::FILTER_NOT_SENT
:
278 $select->where('mailing_sent = false');
280 case self
::FILTER_DC_SENT
:
281 //nothing to do here.
286 if ($this->filters
->subject_filter
!= '') {
287 $token = $this->zdb
->platform
->quoteValue(
288 '%' . strtolower($this->filters
->subject_filter
) . '%'
292 'LOWER(mailing_subject) LIKE ' .
296 } catch (\Exception
$e) {
298 __METHOD__
. ' | ' . $e->getMessage(),
305 * Count history entries from the query
307 * @param Select $select Original select
311 private function proceedCount($select)
314 $countSelect = clone $select;
315 $countSelect->reset($countSelect::COLUMNS
);
316 $countSelect->reset($countSelect::JOINS
);
317 $countSelect->reset($countSelect::ORDER
);
318 $countSelect->columns(
320 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
324 $results = $this->zdb
->execute($countSelect);
325 $result = $results->current();
328 $this->count
= $result->$k;
329 if ($this->count
> 0) {
330 $this->filters
->setCounter($this->count
);
332 } catch (\Exception
$e) {
334 'Cannot count history | ' . $e->getMessage(),
342 * Load mailing from an existing one
344 * @param Db $zdb Database instance
345 * @param integer $id Model identifier
346 * @param GaletteMailing $mailing Mailing object
347 * @param boolean $new True if we create a 'new' mailing,
348 * false otherwise (from preview for example)
352 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
355 $select = $zdb->select(self
::TABLE
);
356 $select->where('mailing_id = ' . $id);
358 $results = $zdb->execute($select);
359 $result = $results->current();
361 return $mailing->loadFromHistory($result, $new);
362 } catch (\Exception
$e) {
364 'Unable to load mailing model #' . $id . ' | ' .
373 * Store a mailing in the history
375 * @param boolean $sent Defaults to false
379 public function storeMailing($sent = false)
381 if ($this->mailing
instanceof Mailing
) {
382 if ($this->mailing
->sender_name
!= null) {
383 $this->sender_name
= $this->mailing
->getSenderName();
384 $this->sender_address
= $this->mailing
->getSenderAddress();
386 $this->sender
= $this->login
->id
;
387 $this->subject
= $this->mailing
->subject
;
388 $this->message
= $this->mailing
->message
;
389 $this->recipients
= $this->mailing
->recipients
;
391 $this->date
= date('Y-m-d H:i:s');
392 if (!$this->mailing
->existsInHistory()) {
394 $this->mailing
->id
= $this->id
;
395 $this->mailing
->moveAttachments($this->id
);
397 if ($this->mailing
->tmp_path
!== false) {
398 //attachments are still in a temporary path, move them
399 $this->mailing
->moveAttachments($this->id ??
$this->mailing
->history_id
);
401 //existing stored mailing. Just update row.
407 '] Mailing should be an instance of Mailing',
415 * Update in the database
419 public function update()
422 $_recipients = array();
423 if ($this->recipients
!= null) {
424 foreach ($this->recipients
as $_r) {
425 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
429 $sender = ($this->sender
=== 0) ?
430 new Expression('NULL') : $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 $update = $this->zdb
->update(self
::TABLE
);
450 $update->set($values);
451 $update->where(self
::PK
. ' = ' . $this->mailing
->history_id
);
452 $this->zdb
->execute($update);
454 } catch (\Exception
$e) {
456 'An error occurend updating Mailing | ' . $e->getMessage(),
464 * Store in the database
468 public function store()
471 $_recipients = array();
472 if ($this->recipients
!= null) {
473 foreach ($this->recipients
as $_r) {
474 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
479 if ($this->sender
=== 0) {
480 $sender = new Expression('NULL');
482 $sender = $this->sender
;
484 $sender_name = ($this->sender_name
=== null) ?
485 new Expression('NULL') : $this->sender_name
;
486 $sender_address = ($this->sender_address
=== null) ?
487 new Expression('NULL') : $this->sender_address
;
490 'mailing_sender' => $sender,
491 'mailing_sender_name' => $sender_name,
492 'mailing_sender_address' => $sender_address,
493 'mailing_subject' => $this->subject
,
494 'mailing_body' => $this->message
,
495 'mailing_date' => $this->date
,
496 'mailing_recipients' => serialize($_recipients),
497 'mailing_sent' => ($this->sent
) ?
499 ($this->zdb
->isPostgres() ?
'false' : 0)
502 $insert = $this->zdb
->insert(self
::TABLE
);
503 $insert->values($values);
504 $this->zdb
->execute($insert);
506 if ($this->zdb
->isPostgres()) {
507 $this->id
= $this->zdb
->driver
->getLastGeneratedValue(
508 PREFIX_DB
. 'mailing_history_id_seq'
511 $this->id
= $this->zdb
->driver
->getLastGeneratedValue();
514 } catch (\Exception
$e) {
516 'An error occurend storing Mailing | ' . $e->getMessage(),
524 * Remove specified entries
526 * @param integer|array $ids Mailing history entries identifiers
530 public function removeEntries($ids)
532 global $hist, $preferences;
535 if (is_numeric($ids)) {
536 //we've got only one identifier
542 if (is_array($list)) {
544 foreach ($list as $id) {
545 $mailing = new Mailing($preferences, [], $id);
546 $mailing->removeAttachments();
549 $this->zdb
->connection
->beginTransaction();
552 $delete = $this->zdb
->delete(self
::TABLE
);
553 $delete->where
->in(self
::PK
, $list);
554 $this->zdb
->execute($delete);
557 $this->zdb
->connection
->commit();
559 //add an history entry
561 _T("Delete mailing entries")
565 } catch (\Exception
$e) {
566 $this->zdb
->connection
->rollBack();
568 'Unable to delete selected mailing history entries |' .
575 //not numeric and not an array: incorrect.
577 'Asking to remove mailing entries, but without ' .
578 'providing an array or a single numeric value.',
588 * @param boolean $prefixed Whether table name should be prefixed
592 protected function getTableName($prefixed = false)
594 if ($prefixed === true) {
595 return PREFIX_DB
. self
::TABLE
;
606 protected function getPk()
612 * Get count for current query
616 public function getCount()
626 public function getSendersList()
628 return $this->senders
;