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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2011-08-27
38 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 const TABLE
= 'mailing_history';
62 const PK
= 'mailing_id';
64 const FILTER_DC_SENT
= 0;
65 const FILTER_SENT
= 1;
66 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 HistoryList $filters Filtering
87 * @param Mailing $mailing Mailing
89 public function __construct(Db
$zdb, Login
$login, $filters = null, $mailing = null)
91 parent
::__construct($zdb, $login, $filters);
93 if ($mailing instanceof Mailing
) {
94 $this->mailing
= $mailing;
95 } elseif ($mailing !== null) {
98 '] Mailing should be either null or an instance of Mailing',
105 * Get the entire history list
109 public function getHistory()
112 $select = $this->zdb
->select($this->getTableName(), 'a');
114 array('b' => PREFIX_DB
. Adherent
::TABLE
),
115 'a.mailing_sender=b.' . Adherent
::PK
,
116 array('nom_adh', 'prenom_adh'),
119 $this->buildWhereClause($select);
120 $select->order(self
::buildOrderClause());
121 $this->buildLists($select);
122 $this->proceedCount($select);
123 //add limits to retrieve only relavant rows
124 $this->filters
->setLimits($select);
125 $results = $this->zdb
->execute($select);
128 foreach ($results as $r) {
129 if ($r['mailing_sender'] !== null && $r['mailing_sender_name'] === null) {
130 $r['mailing_sender_name']
131 = Adherent
::getSName($this->zdb
, $r['mailing_sender']);
133 $body_resume = $r['mailing_body'];
134 if (strlen($body_resume) > 150) {
135 $body_resume = substr($body_resume, 0, 150);
136 $body_resume .= '[...]';
138 if (function_exists('tidy_parse_string')) {
139 //if tidy extension is present, we use it to clean a bit
140 $tidy_config = array(
142 'show-body-only' => true,
145 $tidy = tidy_parse_string($body_resume, $tidy_config, 'UTF8');
146 $tidy->cleanRepair();
147 $r['mailing_body_resume'] = tidy_get_output($tidy);
149 //if it is not... Well, let's serve the text as it.
150 $r['mailing_body_resume'] = $body_resume;
154 if (file_exists(GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
])) {
155 $rdi = new \
RecursiveDirectoryIterator(
156 GALETTE_ATTACHMENTS_PATH
. $r[self
::PK
],
157 \FilesystemIterator
::SKIP_DOTS
159 $contents = new \
RecursiveIteratorIterator(
161 \RecursiveIteratorIterator
::CHILD_FIRST
163 foreach ($contents as $path) {
164 if ($path->isFile()) {
169 $r['attachments'] = $attachments;
173 } catch (\Exception
$e) {
175 'Unable to get history. | ' . $e->getMessage(),
183 * Builds users and actions lists
185 * @param Select $select Original select
189 private function buildLists($select)
192 $select = $this->zdb
->select(self
::TABLE
);
193 $select->quantifier('DISTINCT')->columns(['mailing_sender']);
194 $select->order(['mailing_sender ASC']);
196 $results = $this->zdb
->execute($select);
199 foreach ($results as $result) {
200 $sender = $result->mailing_sender
;
201 if ($sender != null) {
202 $this->senders
[$sender] = Adherent
::getSName($this->zdb
, (int)$sender);
203 } elseif ($result->mailing_sender_name
!= null ||
$result->mailing_sender_address
!= null) {
204 $this->senders
[$result->mailing_sender_address
] = $result->mailing_sender_name
;
206 $this->senders
[-1] = _('Superadmin');
209 } catch (\Exception
$e) {
211 'Cannot list senders from mailing history! | ' . $e->getMessage(),
218 * Builds the order clause
220 * @return string SQL ORDER clause
222 protected function buildOrderClause()
226 switch ($this->filters
->orderby
) {
227 case MailingsList
::ORDERBY_DATE
:
228 $order[] = 'mailing_date ' . $this->filters
->ordered
;
230 case MailingsList
::ORDERBY_SENDER
:
231 $order[] = 'mailing_sender ' . $this->filters
->ordered
;
233 case MailingsList
::ORDERBY_SUBJECT
:
234 $order[] = 'mailing_subject ' . $this->filters
->ordered
;
236 case MailingsList
::ORDERBY_SENT
:
237 $order[] = 'mailing_sent ' . $this->filters
->ordered
;
245 * Builds where clause, for filtering on simple list mode
247 * @param Select $select Original select
249 * @return string SQL WHERE clause
251 private function buildWhereClause($select)
254 if ($this->filters
->start_date_filter
!= null) {
255 $d = new \
DateTime($this->filters
->raw_start_date_filter
);
256 $select->where
->greaterThanOrEqualTo(
262 if ($this->filters
->end_date_filter
!= null) {
263 $d = new \
DateTime($this->filters
->raw_end_date_filter
);
264 $select->where
->lessThanOrEqualTo(
270 if ($this->filters
->sender_filter
!= null && $this->filters
->sender_filter
!= '0') {
271 $sender = $this->filters
->sender_filter
;
272 if ($sender == '-1') {
273 $select->where('mailing_sender IS NULL');
275 $select->where
->equalTo(
282 switch ($this->filters
->sent_filter
) {
283 case self
::FILTER_SENT
:
284 $select->where('mailing_sent = true');
286 case self
::FILTER_NOT_SENT
:
287 $select->where('mailing_sent = false');
289 case self
::FILTER_DC_SENT
:
290 //nothing to do here.
295 if ($this->filters
->subject_filter
!= '') {
296 $token = $this->zdb
->platform
->quoteValue(
297 '%' . strtolower($this->filters
->subject_filter
) . '%'
301 'LOWER(mailing_subject) LIKE ' .
305 } catch (\Exception
$e) {
307 __METHOD__
. ' | ' . $e->getMessage(),
314 * Count history entries from the query
316 * @param Select $select Original select
320 private function proceedCount($select)
323 $countSelect = clone $select;
324 $countSelect->reset($countSelect::COLUMNS
);
325 $countSelect->reset($countSelect::JOINS
);
326 $countSelect->reset($countSelect::ORDER
);
327 $countSelect->columns(
329 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
333 $results = $this->zdb
->execute($countSelect);
334 $result = $results->current();
337 $this->count
= $result->$k;
338 if ($this->count
> 0) {
339 $this->filters
->setCounter($this->count
);
341 } catch (\Exception
$e) {
343 'Cannot count history | ' . $e->getMessage(),
351 * Load mailing from an existing one
353 * @param Db $zdb Database instance
354 * @param integer $id Model identifier
355 * @param GaletteMailing $mailing Mailing object
356 * @param boolean $new True if we create a 'new' mailing,
357 * false otherwise (from preview for example)
361 public static function loadFrom(Db
$zdb, $id, $mailing, $new = true)
364 $select = $zdb->select(self
::TABLE
);
365 $select->where('mailing_id = ' . $id);
367 $results = $zdb->execute($select);
368 $result = $results->current();
370 return $mailing->loadFromHistory($result, $new);
371 } catch (\Exception
$e) {
373 'Unable to load mailing model #' . $id . ' | ' .
382 * Store a mailing in the history
384 * @param boolean $sent Defaults to false
388 public function storeMailing($sent = false)
390 if ($this->mailing
instanceof Mailing
) {
391 if ($this->mailing
->sender_name
!= null) {
392 $this->sender_name
= $this->mailing
->getSenderName();
393 $this->sender_address
= $this->mailing
->getSenderAddress();
395 $this->sender
= $this->login
->id
;
396 $this->subject
= $this->mailing
->subject
;
397 $this->message
= $this->mailing
->message
;
398 $this->recipients
= $this->mailing
->recipients
;
400 $this->date
= date('Y-m-d H:i:s');
401 if (!$this->mailing
->existsInHistory()) {
403 $this->mailing
->id
= $this->id
;
404 $this->mailing
->moveAttachments($this->id
);
406 if ($this->mailing
->tmp_path
!== false) {
407 //attachments are still in a temporary path, move them
408 $this->mailing
->moveAttachments($this->id
);
410 //existing stored mailing. Just update row.
416 '] Mailing should be an instance of Mailing',
424 * Update in the database
428 public function update()
431 $_recipients = array();
432 if ($this->recipients
!= null) {
433 foreach ($this->recipients
as $_r) {
434 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
438 $sender = ($this->sender
=== 0) ?
439 new Expression('NULL') : $this->sender
;
440 $sender_name = ($this->sender_name
=== null) ?
441 new Expression('NULL') : $this->sender_name
;
442 $sender_address = ($this->sender_address
=== null) ?
443 new Expression('NULL') : $this->sender_address
;
446 'mailing_sender' => $sender,
447 'mailing_sender_name' => $sender_name,
448 'mailing_sender_address' => $sender_address,
449 'mailing_subject' => $this->subject
,
450 'mailing_body' => $this->message
,
451 'mailing_date' => $this->date
,
452 'mailing_recipients' => serialize($_recipients),
453 'mailing_sent' => ($this->sent
) ?
455 ($this->zdb
->isPostgres() ?
'false' : 0)
458 $update = $this->zdb
->update(self
::TABLE
);
459 $update->set($values);
460 $update->where(self
::PK
. ' = ' . $this->mailing
->history_id
);
461 $this->zdb
->execute($update);
463 } catch (\Exception
$e) {
465 'An error occurend updating Mailing | ' . $e->getMessage(),
473 * Store in the database
477 public function store()
480 $_recipients = array();
481 if ($this->recipients
!= null) {
482 foreach ($this->recipients
as $_r) {
483 $_recipients[$_r->id
] = $_r->sname
. ' <' . $_r->email
. '>';
488 if ($this->sender
=== 0) {
489 $sender = new Expression('NULL');
491 $sender = $this->sender
;
493 $sender_name = ($this->sender_name
=== null) ?
494 new Expression('NULL') : $this->sender_name
;
495 $sender_address = ($this->sender_address
=== null) ?
496 new Expression('NULL') : $this->sender_address
;
499 'mailing_sender' => $sender,
500 'mailing_sender_name' => $sender_name,
501 'mailing_sender_address' => $sender_address,
502 'mailing_subject' => $this->subject
,
503 'mailing_body' => $this->message
,
504 'mailing_date' => $this->date
,
505 'mailing_recipients' => serialize($_recipients),
506 'mailing_sent' => ($this->sent
) ?
508 ($this->zdb
->isPostgres() ?
'false' : 0)
511 $insert = $this->zdb
->insert(self
::TABLE
);
512 $insert->values($values);
513 $this->zdb
->execute($insert);
515 if ($this->zdb
->isPostgres()) {
516 $this->id
= $this->zdb
->driver
->getLastGeneratedValue(
517 PREFIX_DB
. 'mailing_history_id_seq'
520 $this->id
= $this->zdb
->driver
->getLastGeneratedValue();
523 } catch (\Exception
$e) {
525 'An error occurend storing Mailing | ' . $e->getMessage(),
533 * Remove specified entries
535 * @param integer|array $ids Mailing history entries identifiers
539 public function removeEntries($ids)
541 global $hist, $preferences;
544 if (is_numeric($ids)) {
545 //we've got only one identifier
551 if (is_array($list)) {
553 foreach ($list as $id) {
554 $mailing = new Mailing($preferences, null, $id);
555 $mailing->removeAttachments();
558 $this->zdb
->connection
->beginTransaction();
561 $delete = $this->zdb
->delete(self
::TABLE
);
562 $delete->where
->in(self
::PK
, $list);
563 $this->zdb
->execute($delete);
566 $this->zdb
->connection
->commit();
568 //add an history entry
570 _T("Delete mailing entries")
574 } catch (\Exception
$e) {
575 $this->zdb
->connection
->rollBack();
577 'Unable to delete selected mailing history entries |' .
584 //not numeric and not an array: incorrect.
586 'Asking to remove mailing entries, but without ' .
587 'providing an array or a single numeric value.',
597 * @param boolean $prefixed Whether table name should be prefixed
601 protected function getTableName($prefixed = false)
603 if ($prefixed === true) {
604 return PREFIX_DB
. self
::TABLE
;
615 protected function getPk()
621 * Get count for current query
625 public function getCount()
635 public function getSendersList()
637 return $this->senders
;