| @@ -1,0 +1,264 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Dashboard; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\CampaignClass; | |
| 6 | +use AcyMailing\Classes\MailStatClass; | |
| 7 | +use AcyMailing\Classes\QueueClass; | |
| 8 | +use AcyMailing\Controllers\StatsController; | |
| 9 | +use AcyMailing\Classes\ListClass; | |
| 10 | +use AcyMailing\Classes\UserClass; | |
| 11 | + | |
| 12 | +trait Listing | |
| 13 | +{ | |
| 14 | + public function listing(): void | |
| 15 | + { | |
| 16 | + acym_setVar('layout', 'listing'); | |
| 17 | + | |
| 18 | + if ($this->migration() || $this->walkthrough()) { | |
| 19 | + return; | |
| 20 | + } | |
| 21 | + | |
| 22 | + $campaignClass = new CampaignClass(); | |
| 23 | + $mailStatClass = new MailStatClass(); | |
| 24 | + $statsController = new StatsController(); | |
| 25 | + $listsClass = new ListClass(); | |
| 26 | + $usersClass = new UserClass(); | |
| 27 | + | |
| 28 | + $numberOfUsers = $usersClass->getCountTotalUsers(); | |
| 29 | + | |
| 30 | + $data = [ | |
| 31 | + 'page_title' => true, | |
| 32 | + 'mail_filter_options' => [], | |
| 33 | + 'stats_export' => '', | |
| 34 | + 'selectedMailid' => '', | |
| 35 | + 'show_date_filters' => false, | |
| 36 | + 'campaignsScheduled' => $campaignClass->getCampaignForDashboard(), | |
| 37 | + 'sentMails' => $mailStatClass->getAllMailsForStats(), | |
| 38 | + 'userCreated' => $numberOfUsers > 1, | |
| 39 | + 'listCreated' => $listsClass->getOneList(), | |
| 40 | + 'totalSubscribers' => $numberOfUsers, | |
| 41 | + 'campaignCreated' => $campaignClass->getOneCampaign(), | |
| 42 | + 'campaignSent' => $mailStatClass->getOneSentEmail(), | |
| 43 | + 'mailStatsCheckedOnce' => $this->config->get('mail_stats_checked_once', 0), | |
| 44 | + 'notifications' => json_decode($this->config->get('dashboard_notif', '[]'), true), | |
| 45 | + 'engage_community' => [ | |
| 46 | + [ | |
| 47 | + 'title' => 'ACYM_START_SENDING', | |
| 48 | + 'text' => 'ACYM_WEBSITE_LINKED', | |
| 49 | + 'link' => 'configuration', | |
| 50 | + 'icon' => 'acymicon-cogs', | |
| 51 | + ], | |
| 52 | + [ | |
| 53 | + 'title' => 'ACYM_SUBSCRIPTION_FORM', | |
| 54 | + 'text' => 'ACYM_RAISE_SUBSCRIBER', | |
| 55 | + 'link' => 'forms', | |
| 56 | + 'icon' => 'acymicon-edit', | |
| 57 | + ], | |
| 58 | + [ | |
| 59 | + 'title' => 'ACYM_SET_WELCOME_EMAIL', | |
| 60 | + 'text' => 'ACYM_WELCOME_NEW_SUBSCRIBER', | |
| 61 | + 'link' => 'campaigns&task=welcome', | |
| 62 | + 'icon' => 'acymicon-mail-o', | |
| 63 | + ], | |
| 64 | + [ | |
| 65 | + 'title' => 'ACYM_OVERRIDE_WEBSITE', | |
| 66 | + 'text' => 'ACYM_HOMOGENOUS_COMMUNICATION', | |
| 67 | + 'link' => 'override', | |
| 68 | + 'icon' => 'acymicon-edit', | |
| 69 | + ], | |
| 70 | + [ | |
| 71 | + 'title' => 'ACYM_ADD_MORE_USERS', | |
| 72 | + 'text' => 'ACYM_IMPORT_USERS', | |
| 73 | + 'link' => 'users&task=import', | |
| 74 | + 'icon' => 'acymicon-user-plus', | |
| 75 | + ], | |
| 76 | + [ | |
| 77 | + 'title' => 'ACYM_TRIGGER_ACTION', | |
| 78 | + 'text' => 'ACYM_EXTERNAL_SERVICES_ZAPIER', | |
| 79 | + 'link' => '', | |
| 80 | + 'link_doc' => ACYM_DOCUMENTATION.'addons/zapier', | |
| 81 | + 'icon' => 'acymicon-arrows-h', | |
| 82 | + ], | |
| 83 | + [ | |
| 84 | + 'title' => 'ACYM_ADVENCED_SCENARIOS', | |
| 85 | + 'text' => 'ACYM_CREATE_SCENARIOS_AND_ACTIONS', | |
| 86 | + 'link' => 'scenarios', | |
| 87 | + 'icon' => 'acymicon-movie', | |
| 88 | + ], | |
| 89 | + [ | |
| 90 | + 'title' => 'ACYM_BUILD_MEANINGFUL_PROFILES', | |
| 91 | + 'text' => 'ACYM_CREATE_CUSTOM_FIELDS', | |
| 92 | + 'link' => 'fields', | |
| 93 | + 'icon' => 'acymicon-group', | |
| 94 | + ], | |
| 95 | + [ | |
| 96 | + 'title' => 'ACYM_TARGET_YOUR_AUDIENCE', | |
| 97 | + 'text' => 'ACYM_CREATE_DIFFERENT_LISTS', | |
| 98 | + 'link' => 'segments', | |
| 99 | + 'icon' => 'acymicon-user-check', | |
| 100 | + ], | |
| 101 | + [ | |
| 102 | + 'title' => 'ACYM_CHOOSE_YOUR_COMMUNICATION', | |
| 103 | + 'text' => 'ACYM_CHOOSE_AMONG_DIFFERENT_KIND_OF_EMAIL', | |
| 104 | + 'link' => 'campaigns', | |
| 105 | + 'icon' => 'acymicon-mobile', | |
| 106 | + ], | |
| 107 | + [ | |
| 108 | + 'title' => 'ACYM_SEND_RELATED_INFORMATION', | |
| 109 | + 'text' => 'ACYM_INSERT_SPECIFIC_CONTENT', | |
| 110 | + 'link' => 'plugins', | |
| 111 | + 'icon' => 'acymicon-edit', | |
| 112 | + ], | |
| 113 | + ], | |
| 114 | + ]; | |
| 115 | + | |
| 116 | + $this->doDisplayBeginnerSteps($data); | |
| 117 | + $this->getDashboardNotifications($data); | |
| 118 | + $this->getCurrentCampaigns($data); | |
| 119 | + $this->getLastTwoCampaigns($data); | |
| 120 | + $this->getMainLists($data); | |
| 121 | + | |
| 122 | + $statsController->prepareOpenTimeChart($data); | |
| 123 | + $statsController->preparecharts($data); | |
| 124 | + $statsController->prepareDefaultLineChart($data); | |
| 125 | + $statsController->prepareDefaultDevicesChart($data); | |
| 126 | + $statsController->prepareDefaultBrowsersChart($data); | |
| 127 | + | |
| 128 | + $this->prepareStatsView($data); | |
| 129 | + $this->getUsersCount($data); | |
| 130 | + $statsEvolution = json_decode($this->config->get('statsEvolution', '{}')); | |
| 131 | + $data['newSubscribers'] = $statsEvolution->totalNewSubscribersEvolution ?? 0; | |
| 132 | + parent::display($data); | |
| 133 | + } | |
| 134 | + | |
| 135 | + public function getDashboardNotifications(array &$data): void | |
| 136 | + { | |
| 137 | + $data['dashboardNotifications'] = []; | |
| 138 | + | |
| 139 | + if (empty($data['notifications'])) { | |
| 140 | + return; | |
| 141 | + } | |
| 142 | + | |
| 143 | + $notifications = array_map(fn($notif) => is_array($notif) ? (object)$notif : $notif, $data['notifications']); | |
| 144 | + $notifications = array_filter($notifications, fn($notif) => isset($notif->message, $notif->level, $notif->date)); | |
| 145 | + | |
| 146 | + foreach ($notifications as $notif) { | |
| 147 | + if ($notif->level === 'info') { | |
| 148 | + continue; | |
| 149 | + } | |
| 150 | + | |
| 151 | + $data['dashboardNotifications'][] = [ | |
| 152 | + 'level' => $notif->level, | |
| 153 | + 'message' => $notif->message, | |
| 154 | + 'date' => acym_date($notif->date ?? 'now', 'Y-m-d H:i:s', false), | |
| 155 | + 'removable' => !isset($notif->removable) || $notif->removable != 0, | |
| 156 | + 'name' => $notif->name, | |
| 157 | + ]; | |
| 158 | + } | |
| 159 | + } | |
| 160 | + | |
| 161 | + private function doDisplayBeginnerSteps(array $data): void | |
| 162 | + { | |
| 163 | + if ($this->config->get('install_date', time()) < strtotime('-14 days')) { | |
| 164 | + $this->config->saveConfig(['show_beginner_steps' => 0]); | |
| 165 | + } elseif (!$data['listCreated'] || $data['totalSubscribers'] <= 1 || !$data['campaignCreated'] || !$data['campaignSent'] || $data['mailStatsCheckedOnce'] != 1) { | |
| 166 | + $this->config->saveConfig(['show_beginner_steps' => 1]); | |
| 167 | + } else { | |
| 168 | + $this->config->saveConfig(['show_beginner_steps' => 0]); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + private function prepareStatsView(array &$data): void | |
| 173 | + { | |
| 174 | + $data['stats'] = [ | |
| 175 | + 'ACYM_SUBSCRIBERS' => [ | |
| 176 | + 'value' => $data['totalSubscribers'] ?? 0, | |
| 177 | + 'evolution' => $data['totalSubscribersEvolution'] ?? 0, | |
| 178 | + 'icon' => 'acymicon-group', | |
| 179 | + ], | |
| 180 | + 'ACYM_OPEN_RATE' => [ | |
| 181 | + 'value' => $data['mail']->percentageOpen ?? 0, | |
| 182 | + 'evolution' => $data['openRateEvolution'] ?? 0, | |
| 183 | + 'icon' => 'acymicon-mail-o-open', | |
| 184 | + ], | |
| 185 | + 'ACYM_CLICK_RATE' => [ | |
| 186 | + 'value' => $data['mail']->percentageClick ?? 0, | |
| 187 | + 'evolution' => $data['clickRateEvolution'] ?? 0, | |
| 188 | + 'icon' => 'acymicon-trigger', | |
| 189 | + ], | |
| 190 | + 'ACYM_BOUNCE_RATE' => [ | |
| 191 | + 'value' => $data['mail']->percentageBounce ?? 0, | |
| 192 | + 'evolution' => $data['bounceRateEvolution'] ?? 0, | |
| 193 | + 'icon' => 'acymicon-exclamation-triangle', | |
| 194 | + ], | |
| 195 | + ]; | |
| 196 | + } | |
| 197 | + | |
| 198 | + private function getCurrentCampaigns(array &$data): void | |
| 199 | + { | |
| 200 | + $queueClass = new QueueClass(); | |
| 201 | + $campaignsPerPage = 3; | |
| 202 | + $activeCampaigns = $queueClass->getMatchingCampaigns( | |
| 203 | + [ | |
| 204 | + 'status' => 'sending', | |
| 205 | + 'campaignsPerPage' => $campaignsPerPage, | |
| 206 | + 'offset' => 0, | |
| 207 | + ] | |
| 208 | + ); | |
| 209 | + $pausedCampaigns = $queueClass->getMatchingCampaigns( | |
| 210 | + [ | |
| 211 | + 'status' => 'paused', | |
| 212 | + 'campaignsPerPage' => $campaignsPerPage, | |
| 213 | + 'offset' => 0, | |
| 214 | + ] | |
| 215 | + ); | |
| 216 | + $data['campaigns'] = array_merge($pausedCampaigns['elements'], $activeCampaigns['elements']); | |
| 217 | + } | |
| 218 | + | |
| 219 | + private function getLastTwoCampaigns(array &$data): void | |
| 220 | + { | |
| 221 | + $campaignsPerPage = 2; | |
| 222 | + $campaignClass = new CampaignClass(); | |
| 223 | + $lastTwoCampaigns = $campaignClass->getMatchingElements( | |
| 224 | + [ | |
| 225 | + 'element_tab' => 'campaigns', | |
| 226 | + 'ordering' => 'creation_date', | |
| 227 | + 'status' => 'sent', | |
| 228 | + 'ordering_sort_order' => 'DESC', | |
| 229 | + 'elementsPerPage' => $campaignsPerPage, | |
| 230 | + ] | |
| 231 | + ); | |
| 232 | + | |
| 233 | + $data['recent_campaigns'] = $lastTwoCampaigns['elements']; | |
| 234 | + } | |
| 235 | + | |
| 236 | + private function getMainLists(array &$data): void | |
| 237 | + { | |
| 238 | + $listClass = new ListClass(); | |
| 239 | + $listPerPage = 5; | |
| 240 | + $mainLists = $listClass->getMatchingElements( | |
| 241 | + [ | |
| 242 | + 'ordering_sort_order' => 'ASC', | |
| 243 | + 'elementsPerPage' => $listPerPage, | |
| 244 | + 'offset' => 0, | |
| 245 | + 'status' => 'active', | |
| 246 | + ], | |
| 247 | + ); | |
| 248 | + | |
| 249 | + $data['main_lists'] = $mainLists['elements']; | |
| 250 | + } | |
| 251 | + | |
| 252 | + private function getUsersCount(array &$data): void | |
| 253 | + { | |
| 254 | + $userClass = new UserClass(); | |
| 255 | + $matchingUsers = $userClass->getMatchingElements( | |
| 256 | + [ | |
| 257 | + 'offset' => 0, | |
| 258 | + 'cms_username' => true, | |
| 259 | + ], | |
| 260 | + ); | |
| 261 | + | |
| 262 | + $data['subscribers'] = $matchingUsers['status']; | |
| 263 | + } | |
| 264 | +} | |