| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Helpers; |
| 4 |
|
| 5 |
use AcyMailing\Classes\ListClass; |
| 6 |
use AcyMailing\Classes\UserClass; |
| 7 |
use AcyMailing\Core\AcymObject; |
| 8 |
|
| 9 |
class RegacyHelper extends AcymObject |
| 10 |
{ |
| 11 |
private array $options = []; |
| 12 |
|
| 13 |
public string $label = ''; |
| 14 |
public array $lists = []; |
| 15 |
public string $listsHtml = ''; |
| 16 |
|
| 17 |
public function prepareLists(array $options): bool |
| 18 |
{ |
| 19 |
if (empty($options['baseOption'])) { |
| 20 |
$options['baseOption'] = 'regacy'; |
| 21 |
} |
| 22 |
$this->options = $options; |
| 23 |
|
| 24 |
$visibleLists = $this->config->get($options['baseOption'].'_lists'); |
| 25 |
if (empty($visibleLists)) return false; |
| 26 |
$visibleLists = explode(',', $visibleLists); |
| 27 |
acym_arrayToInteger($visibleLists); |
| 28 |
|
| 29 |
$listsClass = new ListClass(); |
| 30 |
$allLists = $listsClass->getAllWithoutManagement(true); |
| 31 |
|
| 32 |
$isAdmin = acym_isAdmin(); |
| 33 |
foreach ($visibleLists as $i => $oneListId) { |
| 34 |
if (in_array($oneListId, array_keys($allLists)) && $allLists[$oneListId]->active && ($allLists[$oneListId]->visible || $isAdmin)) continue; |
| 35 |
unset($visibleLists[$i]); |
| 36 |
} |
| 37 |
if (empty($visibleLists)) return false; |
| 38 |
|
| 39 |
|
| 40 |
$checkedLists = explode(',', $this->config->get($options['baseOption'].'_checkedlists')); |
| 41 |
acym_arrayToInteger($checkedLists); |
| 42 |
$userClass = new UserClass(); |
| 43 |
|
| 44 |
if ('wordpress' === ACYM_CMS) { |
| 45 |
$currentCMSId = acym_getVar('int', 'user_id', 0); |
| 46 |
if (empty($currentCMSId)) $currentCMSId = acym_currentUserId(); |
| 47 |
} else { |
| 48 |
if (acym_isAdmin()) { |
| 49 |
$currentCMSId = acym_getVar('int', 'id', 0); |
| 50 |
} else { |
| 51 |
$currentCMSId = acym_currentUserId(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (!empty($currentCMSId)) { |
| 56 |
$currentUser = $userClass->getOneByCMSId($currentCMSId); |
| 57 |
if (!empty($currentUser)) { |
| 58 |
$checkedLists = []; |
| 59 |
$currentSubscription = $userClass->getSubscriptionStatus($currentUser->id, $visibleLists); |
| 60 |
acym_trigger('onRegacyPrepareCheckedLists', [&$currentSubscription]); |
| 61 |
|
| 62 |
foreach ($currentSubscription as $listId => $oneSubsciption) { |
| 63 |
if ($oneSubsciption->status == '1') $checkedLists[] = $listId; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$autolist = explode(',', $this->config->get($options['baseOption'].'_autolists')); |
| 69 |
acym_arrayToInteger($autolist); |
| 70 |
if (!empty($autolist)) { |
| 71 |
$visibleLists = array_diff($visibleLists, $autolist); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
$this->label = $this->config->get($options['baseOption'].'_text'); |
| 76 |
if (empty($this->label)) $this->label = 'ACYM_SUBSCRIPTION'; |
| 77 |
$this->label = acym_translation($this->label); |
| 78 |
|
| 79 |
$this->lists = []; |
| 80 |
|
| 81 |
foreach ($visibleLists as $oneListId) { |
| 82 |
$this->lists[$oneListId] = [ |
| 83 |
'name' => !empty($allLists[$oneListId]->display_name) ? $allLists[$oneListId]->display_name : $allLists[$oneListId]->name, |
| 84 |
'checked' => in_array($oneListId, $checkedLists), |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
if ('joomla' === ACYM_CMS || !empty($options['formatted'])) { |
| 89 |
$this->formatResults(); |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
private function formatResults(): void |
| 96 |
{ |
| 97 |
$base = $this->options['baseOption']; |
| 98 |
$checkedListsOnAjaxUpdate = acym_getVar('array', $base.'_visible_lists_checked', []); |
| 99 |
$result = '<table class="acym__'.$base.'__lists" style="border:0">'; |
| 100 |
foreach ($this->lists as $id => $oneList) { |
| 101 |
$checked = $oneList['checked'] || in_array($id, $checkedListsOnAjaxUpdate) ? 'checked="checked"' : ''; |
| 102 |
$result .= '<tr style="border:0"> |
| 103 |
<td style="border:0"> |
| 104 |
<input type="checkbox" id="acym__'.$base.'__lists-'.intval( |
| 105 |
$id |
| 106 |
).'" class="acym_checkbox" name="'.$base.'_visible_lists_checked[]" '.$checked.' value="'.intval($id).'"/> |
| 107 |
</td> |
| 108 |
<td style="border:0; padding-left:10px;"> |
| 109 |
<label for="acym__'.$base.'__lists-'.intval($id).'" class="acym__'.$base.'__lists__label">'.acym_escape($oneList['name']).'</label> |
| 110 |
</td> |
| 111 |
</tr>'; |
| 112 |
} |
| 113 |
$result .= '</table>'; |
| 114 |
$result .= '<input type="hidden" value="'.implode(',', array_keys($this->lists)).'" name="'.$base.'_visible_lists" />'; |
| 115 |
$source = !empty($base) && $base != 'regacy' ? $base : ACYM_CMS; |
| 116 |
$result .= '<input type="hidden" value="'.$source.' registration form" name="acy_source" />'; |
| 117 |
$this->listsHtml = $result; |
| 118 |
} |
| 119 |
} |
| 120 |
|