| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Classes; |
| 4 |
|
| 5 |
use AcyMailing\Helpers\PaginationHelper; |
| 6 |
use AcyMailing\Core\AcymClass; |
| 7 |
|
| 8 |
class MailboxClass extends AcymClass |
| 9 |
{ |
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
parent::__construct(); |
| 13 |
|
| 14 |
$this->table = 'mailbox_action'; |
| 15 |
$this->pkey = 'id'; |
| 16 |
} |
| 17 |
|
| 18 |
public function getMatchingElements(array $settings = []): array |
| 19 |
{ |
| 20 |
$query = 'SELECT * FROM #__acym_mailbox_action'; |
| 21 |
$queryCount = 'SELECT COUNT(id) AS total, SUM(active) AS totalActive FROM #__acym_mailbox_action'; |
| 22 |
|
| 23 |
if (!empty($settings['search'])) { |
| 24 |
$filters[] = 'name LIKE '.acym_escapeDB('%'.$settings['search'].'%'); |
| 25 |
} |
| 26 |
|
| 27 |
if (!empty($filters)) { |
| 28 |
$queryCount .= ' WHERE ('.implode(') AND (', $filters).')'; |
| 29 |
} |
| 30 |
|
| 31 |
if (!empty($settings['status'])) { |
| 32 |
$filters[] = 'active = '.($settings['status'] == 'active' ? '1' : '0'); |
| 33 |
} |
| 34 |
|
| 35 |
if (!empty($filters)) { |
| 36 |
$query .= ' WHERE ('.implode(') AND (', $filters).')'; |
| 37 |
} |
| 38 |
|
| 39 |
if (!empty($settings['ordering']) && !empty($settings['ordering_sort_order'])) { |
| 40 |
$query .= ' ORDER BY '.acym_secureDBColumn($settings['ordering']).' '.acym_secureDBColumn(strtoupper($settings['ordering_sort_order'])); |
| 41 |
} else { |
| 42 |
$query .= ' ORDER BY id asc'; |
| 43 |
} |
| 44 |
|
| 45 |
if (empty($settings['offset']) || $settings['offset'] < 0) { |
| 46 |
$settings['offset'] = 0; |
| 47 |
} |
| 48 |
|
| 49 |
if (empty($settings['elementsPerPage']) || $settings['elementsPerPage'] < 1) { |
| 50 |
$pagination = new PaginationHelper(); |
| 51 |
$settings['elementsPerPage'] = $pagination->getListLimit(); |
| 52 |
} |
| 53 |
|
| 54 |
return [ |
| 55 |
'elements' => acym_loadObjectList($query, '', $settings['offset'], $settings['elementsPerPage']), |
| 56 |
'total' => acym_loadObject($queryCount), |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public function duplicate(array $mailboxIds): void |
| 61 |
{ |
| 62 |
if (empty($mailboxIds)) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$duplicateErrors = []; |
| 67 |
foreach ($mailboxIds as $mailboxId) { |
| 68 |
$mailbox = $this->getOneById($mailboxId); |
| 69 |
|
| 70 |
if (empty($mailbox)) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
$originalName = $mailbox->name ?? ''; |
| 75 |
unset($mailbox->id); |
| 76 |
$mailbox->active = 0; |
| 77 |
$mailbox->name .= '_copy'; |
| 78 |
$mailbox->conditions = json_encode($mailbox->conditions); |
| 79 |
$mailbox->actions = json_encode($mailbox->actions); |
| 80 |
|
| 81 |
if (empty($this->save($mailbox))) { |
| 82 |
$duplicateErrors[] = acym_translationSprintf('ACYM_ERROR_DUPLICATING_MAILBOX_ACTION', $originalName); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if (!empty($duplicateErrors)) { |
| 87 |
acym_enqueueMessage($duplicateErrors, 'error'); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function getOneById(int $id): ?object |
| 92 |
{ |
| 93 |
$mailbox = acym_loadObject('SELECT * FROM #__acym_mailbox_action WHERE `id` = '.intval($id)); |
| 94 |
|
| 95 |
if (empty($mailbox)) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
$mailbox->conditions = json_decode($mailbox->conditions, true); |
| 100 |
$mailbox->actions = json_decode($mailbox->actions, true); |
| 101 |
|
| 102 |
return $mailbox; |
| 103 |
} |
| 104 |
|
| 105 |
public function getAllActiveReadyWithActions(): array |
| 106 |
{ |
| 107 |
$mailboxes = acym_loadObjectList('SELECT * FROM #__acym_mailbox_action WHERE active = 1 AND nextdate < '.time().' AND actions IS NOT NULL ORDER BY id ASC'); |
| 108 |
|
| 109 |
if (empty($mailboxes)) { |
| 110 |
return []; |
| 111 |
} |
| 112 |
|
| 113 |
return array_map([$this, 'decodeMailbox'], $mailboxes); |
| 114 |
} |
| 115 |
|
| 116 |
private function decodeMailbox(object $mailbox): object |
| 117 |
{ |
| 118 |
$mailbox->conditions = json_decode($mailbox->conditions, true); |
| 119 |
$mailbox->actions = json_decode($mailbox->actions, true); |
| 120 |
|
| 121 |
return $mailbox; |
| 122 |
} |
| 123 |
} |
| 124 |
|