| @@ -1,0 +1,134 @@ | ||
| 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 | + // What we want to add to the registration form | |
| 14 | + public string $label = ''; | |
| 15 | + public array $lists = []; | |
| 16 | + public string $listsHtml = ''; | |
| 17 | + | |
| 18 | + public function prepareLists(array $options): bool | |
| 19 | + { | |
| 20 | + if (empty($options['baseOption'])) { | |
| 21 | + $options['baseOption'] = 'regacy'; | |
| 22 | + } | |
| 23 | + $this->options = $options; | |
| 24 | + | |
| 25 | + // 1 - Make sure we need to display some lists | |
| 26 | + $visibleLists = $this->config->get($options['baseOption'].'_lists'); | |
| 27 | + if (empty($visibleLists)) return false; | |
| 28 | + $visibleLists = explode(',', $visibleLists); | |
| 29 | + acym_arrayToInteger($visibleLists); | |
| 30 | + | |
| 31 | + $listsClass = new ListClass(); | |
| 32 | + $allLists = $listsClass->getAllWithoutManagement(true); | |
| 33 | + | |
| 34 | + // Display only published and visible lists, except if we're on the back-end | |
| 35 | + $isAdmin = acym_isAdmin(); | |
| 36 | + foreach ($visibleLists as $i => $oneListId) { | |
| 37 | + if (in_array($oneListId, array_keys($allLists)) && $allLists[$oneListId]->active && ($allLists[$oneListId]->visible || $isAdmin)) continue; | |
| 38 | + unset($visibleLists[$i]); | |
| 39 | + } | |
| 40 | + if (empty($visibleLists)) return false; | |
| 41 | + | |
| 42 | + | |
| 43 | + // 2 - Get the lists we should check by default | |
| 44 | + $checkedLists = explode(',', $this->config->get($options['baseOption'].'_checkedlists')); | |
| 45 | + acym_arrayToInteger($checkedLists); | |
| 46 | + $userClass = new UserClass(); | |
| 47 | + | |
| 48 | + if ('wordpress' === ACYM_CMS) { | |
| 49 | + // If editing a user, get its lists | |
| 50 | + $currentCMSId = acym_getVar('int', 'user_id', 0); | |
| 51 | + if (!empty($currentCMSId) && !current_user_can('edit_user', $currentCMSId)) { | |
| 52 | + $currentCMSId = 0; | |
| 53 | + } | |
| 54 | + if (empty($currentCMSId)) $currentCMSId = acym_currentUserId(); | |
| 55 | + } else { | |
| 56 | + if (acym_isAdmin()) { | |
| 57 | + // If editing a user, get its lists | |
| 58 | + $currentCMSId = acym_getVar('int', 'id', 0); | |
| 59 | + } else { | |
| 60 | + // If the user is logged in, take its subscription | |
| 61 | + $currentCMSId = acym_currentUserId(); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + if (!empty($currentCMSId)) { | |
| 66 | + $currentUser = $userClass->getOneByCMSId($currentCMSId); | |
| 67 | + if (!empty($currentUser)) { | |
| 68 | + $checkedLists = []; | |
| 69 | + $currentSubscription = $userClass->getSubscriptionStatus($currentUser->id, $visibleLists); | |
| 70 | + acym_trigger('onRegacyPrepareCheckedLists', [&$currentSubscription]); | |
| 71 | + | |
| 72 | + foreach ($currentSubscription as $listId => $oneSubsciption) { | |
| 73 | + if ($oneSubsciption->status == '1') $checkedLists[] = $listId; | |
| 74 | + } | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + // 3 - remove autolists from display | |
| 79 | + $autolist = explode(',', $this->config->get($options['baseOption'].'_autolists')); | |
| 80 | + acym_arrayToInteger($autolist); | |
| 81 | + if (!empty($autolist)) { | |
| 82 | + $visibleLists = array_diff($visibleLists, $autolist); | |
| 83 | + } | |
| 84 | + | |
| 85 | + | |
| 86 | + // 4 - Prepare the HTML block we'll insert in the form | |
| 87 | + $this->label = $this->config->get($options['baseOption'].'_text'); | |
| 88 | + if (empty($this->label)) $this->label = 'ACYM_SUBSCRIPTION'; | |
| 89 | + $this->label = acym_translation($this->label); | |
| 90 | + | |
| 91 | + $this->lists = []; | |
| 92 | + | |
| 93 | + foreach ($visibleLists as $oneListId) { | |
| 94 | + $this->lists[$oneListId] = [ | |
| 95 | + 'name' => !empty($allLists[$oneListId]->display_name) ? $allLists[$oneListId]->display_name : $allLists[$oneListId]->name, | |
| 96 | + 'checked' => in_array($oneListId, $checkedLists), | |
| 97 | + ]; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ('joomla' === ACYM_CMS || !empty($options['formatted'])) { | |
| 101 | + $this->formatResults(); | |
| 102 | + } | |
| 103 | + | |
| 104 | + return true; | |
| 105 | + } | |
| 106 | + | |
| 107 | + private function formatResults(): void | |
| 108 | + { | |
| 109 | + $base = $this->options['baseOption']; | |
| 110 | + $checkedListsOnAjaxUpdate = acym_getVar('array', $base.'_visible_lists_checked', []); | |
| 111 | + $result = '<table class="acym__'.$base.'__lists" style="border:0">'; | |
| 112 | + foreach ($this->lists as $id => $oneList) { | |
| 113 | + $result .= '<tr style="border:0"> | |
| 114 | + <td style="border:0"> | |
| 115 | + <input type="checkbox" id="acym__'.$base.'__lists-'.intval( | |
| 116 | + $id | |
| 117 | + ).'" class="acym_checkbox" name="'.$base.'_visible_lists_checked[]" '.checked( | |
| 118 | + $oneList['checked'] || in_array($id, $checkedListsOnAjaxUpdate), | |
| 119 | + true, | |
| 120 | + false | |
| 121 | + ).' value="'.intval($id).'"/> | |
| 122 | + </td> | |
| 123 | + <td style="border:0; padding-left:10px;"> | |
| 124 | + <label for="acym__'.$base.'__lists-'.intval($id).'" class="acym__'.$base.'__lists__label">'.esc_html($oneList['name']).'</label> | |
| 125 | + </td> | |
| 126 | + </tr>'; | |
| 127 | + } | |
| 128 | + $result .= '</table>'; | |
| 129 | + $result .= '<input type="hidden" value="'.implode(',', array_keys($this->lists)).'" name="'.$base.'_visible_lists" />'; | |
| 130 | + $source = !empty($base) && $base != 'regacy' ? $base : ACYM_CMS; | |
| 131 | + $result .= '<input type="hidden" value="'.$source.' registration form" name="acy_source" />'; | |
| 132 | + $this->listsHtml = $result; | |
| 133 | + } | |
| 134 | +} | |