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->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']);
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 (Throwable
$e) {
166 'Unable to get history. | ' . $e->getMessage(),
174 * Builds the order clause
176 * @return string SQL ORDER clause
178 protected function buildOrderClause()
182 switch ($this->filters
->orderby
) {
183 case MailingsList
::ORDERBY_DATE
:
184 $order[] = 'mailing_date ' . $this->filters
->ordered
;
186 case MailingsList
::ORDERBY_SENDER
:
187 $order[] = 'mailing_sender ' . $this->filters
->ordered
;
189 case MailingsList
::ORDERBY_SUBJECT
:
190 $order[] = 'mailing_subject ' . $this->filters
->ordered
;
192 case MailingsList
::ORDERBY_SENT
:
193 $order[] = 'mailing_sent ' . $this->filters
->ordered
;
201 * Builds where clause, for filtering on simple list mode
203 * @param Select $select Original select
205 * @return string SQL WHERE clause
207 private function buildWhereClause($select)
210 if ($this->filters
->start_date_filter
!= null) {
211 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
212 $select->where
->greaterThanOrEqualTo(
218 if ($this->filters
->end_date_filter
!= null) {
219 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
220 $select->where
->lessThanOrEqualTo(
226 if ($this->filters
->sender_filter
!= null && $this->filters
->sender_filter
!= '0') {
227 $sender = $this->filters
->sender_filter
;
228 if ($sender == '-1') {
229 $select->where('mailing_sender IS NULL');
231 $select->where
->equalTo(
238 switch ($this->filters
->sent_filter
) {
239 case self
::FILTER_SENT
:
240 $select->where('mailing_sent = true');
242 case self
::FILTER_NOT_SENT
:
243 $select->where('mailing_sent = false');
245 case self
::FILTER_DC_SENT
:
246 //nothing to do here.
251 if ($this->filters
->subject_filter
!= '') {
252 $token = $this->zdb
->platform
->quoteValue(
253 '%' . strtolower($this->filters
->subject_filter
) . '%'
257 'LOWER(mailing_subject) LIKE ' .
261 } catch (Throwable
$e) {
263 __METHOD__
. ' | ' . $e->getMessage(),
271 * Count history entries from the query
273 * @param Select $select Original select
277 private function proceedCount($select)
280 $countSelect = clone $select;
281 $countSelect->reset($countSelect::COLUMNS
);
282 $countSelect->reset($countSelect::JOINS
);
283 $countSelect->reset($countSelect::ORDER
);
284 $countSelect->columns(
286 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
290 $results = $this->zdb
->execute($countSelect);
291 $result = $results->current();
294 $this->count
= $result->$k;
295 if ($this->count
> 0) {
296 $this->filters
->setCounter($this->count
);
298 } catch (Throwable
$e) {
300 'Cannot count history | ' . $e->getMessage(),
308 * Load mailing from an existing one
310 * @param Db $zdb Database instance
311 * @param integer $id Model identifier
312 * @param GaletteMailing $mailing Mailing object
313 * @param boolean $new True if we create a 'new' mailing,
314 * false otherwise (from preview for example)
318 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
321 $select = $zdb->select(self
::TABLE
);
322 $select->where(['mailing_id' => $id]);
324 $results = $zdb->execute($select);
325 $result = $results->current();
327 return $mailing->loadFromHistory($result, $new);
328 } catch (Throwable
$e) {
330 'Unable to load mailing model #' . $id . ' | ' .
339 * Store a mailing in the history
341 * @param boolean $sent Defaults to false
345 public function storeMailing($sent = false)
347 if ($this->mailing
instanceof Mailing
) {
348 if ($this->mailing
->sender_name
!= null) {
349 $this->sender_name
= $this->mailing
->getSenderName();
350 $this->sender_address
= $this->mailing
->getSenderAddress();
352 $this->sender
= $this->login
->id
;
353 $this->subject
= $this->mailing
->subject
;
354 $this->message
= $this->mailing
->message
;
355 $this->recipients
= $this->mailing
->recipients
;
357 $this->date
= date('Y-m-d H:i:s');
358 if (!$this->mailing
->existsInHistory()) {
360 $this->mailing
->id
= $this->id
;
361 $this->mailing
->moveAttachments($this->id
);
363 if ($this->mailing
->tmp_path
!== false) {
364 //attachments are still in a temporary path, move them
365 $this->mailing
->moveAttachments($this->id ??
$this->mailing
->history_id
);
367 //existing stored mailing. Just update row.
373 '] Mailing should be an instance of Mailing',
380 * Update in the database
384 public function update()
387 $_recipients = array();
388 if ($this->recipients
!= null) {
389 foreach ($this->recipients
as $_r) {
390 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
394 $sender = ($this->sender
=== 0) ?
395 new Expression('NULL') : $this->sender
;
396 $sender_name = ($this->sender_name
=== null) ?
397 new Expression('NULL') : $this->sender_name
;
398 $sender_address = ($this->sender_address
=== null) ?
399 new Expression('NULL') : $this->sender_address
;
402 'mailing_sender' => $sender,
403 'mailing_sender_name' => $sender_name,
404 'mailing_sender_address' => $sender_address,
405 'mailing_subject' => $this->subject
,
406 'mailing_body' => $this->message
,
407 'mailing_date' => $this->date
,
408 'mailing_recipients' => serialize($_recipients),
409 'mailing_sent' => ($this->sent
) ?
411 ($this->zdb
->isPostgres() ?
'false' : 0)
414 $update = $this->zdb
->update(self
::TABLE
);
415 $update->set($values);
416 $update->where([self
::PK
=> $this->mailing
->history_id
]);
417 $this->zdb
->execute($update);
419 } catch (Throwable
$e) {
421 'An error occurend updating Mailing | ' . $e->getMessage(),
429 * Store in the database
433 public function store()
436 $_recipients = array();
437 if ($this->recipients
!= null) {
438 foreach ($this->recipients
as $_r) {
439 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
444 if ($this->sender
=== 0) {
445 $sender = new Expression('NULL');
447 $sender = $this->sender
;
449 $sender_name = ($this->sender_name
=== null) ?
450 new Expression('NULL') : $this->sender_name
;
451 $sender_address = ($this->sender_address
=== null) ?
452 new Expression('NULL') : $this->sender_address
;
455 'mailing_sender' => $sender,
456 'mailing_sender_name' => $sender_name,
457 'mailing_sender_address' => $sender_address,
458 'mailing_subject' => $this->subject
,
459 'mailing_body' => $this->message
,
460 'mailing_date' => $this->date
,
461 'mailing_recipients' => serialize($_recipients),
462 'mailing_sent' => ($this->sent
) ?
464 ($this->zdb
->isPostgres() ?
'false' : 0)
467 $insert = $this->zdb
->insert(self
::TABLE
);
468 $insert->values($values);
469 $this->zdb
->execute($insert);
471 $this->id
= $this->zdb
->getLastGeneratedValue($this);
473 } catch (Throwable
$e) {
475 'An error occurend storing Mailing | ' . $e->getMessage(),
483 * Remove specified entries
485 * @param integer|array $ids Mailing history entries identifiers
486 * @param History $hist History instance
490 public function removeEntries($ids, History
$hist)
493 if (is_numeric($ids)) {
494 //we've got only one identifier
500 if (is_array($list)) {
502 foreach ($list as $id) {
503 $mailing = new Mailing($this->preferences
, [], $id);
504 $mailing->removeAttachments();
507 $this->zdb
->connection
->beginTransaction();
510 $delete = $this->zdb
->delete(self
::TABLE
);
511 $delete->where
->in(self
::PK
, $list);
512 $this->zdb
->execute($delete);
515 $this->zdb
->connection
->commit();
517 //add an history entry
519 _T("Delete mailing entries")
523 } catch (Throwable
$e) {
524 $this->zdb
->connection
->rollBack();
526 'Unable to delete selected mailing history entries |' .
533 //not numeric and not an array: incorrect.
535 'Asking to remove mailing entries, but without ' .
536 'providing an array or a single numeric value.',
546 * @param boolean $prefixed Whether table name should be prefixed
550 protected function getTableName($prefixed = false)
552 if ($prefixed === true) {
553 return PREFIX_DB
. self
::TABLE
;
564 protected function getPk()
570 * Get count for current query
574 public function getCount()