| @@ -1,0 +1,1265 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Campaigns; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\CampaignClass; | |
| 6 | +use AcyMailing\Classes\MailArchiveClass; | |
| 7 | +use AcyMailing\Classes\MailClass; | |
| 8 | +use AcyMailing\Classes\ListClass; | |
| 9 | +use AcyMailing\Classes\QueueClass; | |
| 10 | +use AcyMailing\Classes\SegmentClass; | |
| 11 | +use AcyMailing\Classes\TagClass; | |
| 12 | +use AcyMailing\Classes\UserClass; | |
| 13 | +use AcyMailing\Controllers\MailsController; | |
| 14 | +use AcyMailing\Controllers\SegmentsController; | |
| 15 | +use AcyMailing\Helpers\EditorHelper; | |
| 16 | +use AcyMailing\Helpers\EntitySelectHelper; | |
| 17 | +use AcyMailing\Helpers\PaginationHelper; | |
| 18 | +use AcyMailing\Helpers\PluginHelper; | |
| 19 | +use AcyMailing\Helpers\WorkflowHelper; | |
| 20 | +use AcyMailing\Types\UploadFileType; | |
| 21 | +use stdClass; | |
| 22 | + | |
| 23 | +trait Edition | |
| 24 | +{ | |
| 25 | + public function newEmail(): void | |
| 26 | + { | |
| 27 | + acym_setVar('layout', 'new_email'); | |
| 28 | + $mailClass = new MailClass(); | |
| 29 | + | |
| 30 | + $data = [ | |
| 31 | + 'selectedType' => acym_getVar('string', 'email_type', ''), | |
| 32 | + 'menuClass' => $this->menuClass, | |
| 33 | + ]; | |
| 34 | + | |
| 35 | + $listClass = new ListClass(); | |
| 36 | + if (acym_isAdmin()) { | |
| 37 | + $returnUrl = urlencode(base64_encode(acym_completeLink('campaigns'))); | |
| 38 | + $favoriteTemplate = $mailClass->getFavoriteTemplateId(); | |
| 39 | + $welcomeUnsub = '&list_id={dataid}&type_editor=acyEditor&return='.$returnUrl; | |
| 40 | + | |
| 41 | + if (empty($favoriteTemplate)) { | |
| 42 | + $data = array_merge( | |
| 43 | + $data, | |
| 44 | + [ | |
| 45 | + 'lists' => $listClass->getAllForSelect(), | |
| 46 | + 'campaign_link' => acym_completeLink('campaigns&task=edit&step=chooseTemplate&campaign_type=now'), | |
| 47 | + 'campaign_test_link' => acym_completeLink('campaigns&task=edit&step=chooseTemplate&campaign_type=now&abtest=1'), | |
| 48 | + 'campaign_auto_link' => acym_completeLink('campaigns&task=edit&step=chooseTemplate&campaign_type=auto'), | |
| 49 | + 'followup_link' => acym_completeLink('campaigns&task=edit&step=followupTrigger'), | |
| 50 | + 'campaign_scheduled_link' => acym_completeLink('campaigns&task=edit&step=chooseTemplate&campaign_type=scheduled'), | |
| 51 | + 'welcome_email_link' => acym_completeLink('mails&task=edit&type='.MailClass::TYPE_WELCOME.$welcomeUnsub), | |
| 52 | + 'unsubscribe_email_link' => acym_completeLink('mails&task=edit&type='.MailClass::TYPE_UNSUBSCRIBE.$welcomeUnsub), | |
| 53 | + ] | |
| 54 | + ); | |
| 55 | + } else { | |
| 56 | + $data = array_merge( | |
| 57 | + $data, | |
| 58 | + [ | |
| 59 | + 'lists' => $listClass->getAllForSelect(), | |
| 60 | + 'campaign_link' => acym_completeLink('campaigns&task=edit&step=editEmail&from='.$favoriteTemplate.'&campaign_type=now'), | |
| 61 | + 'campaign_test_link' => acym_completeLink('campaigns&task=edit&step=editEmail&from='.$favoriteTemplate.'&campaign_type=now&abtest=1'), | |
| 62 | + 'campaign_auto_link' => acym_completeLink('campaigns&task=edit&step=editEmail&from='.$favoriteTemplate.'&campaign_type=auto'), | |
| 63 | + 'followup_link' => acym_completeLink('campaigns&task=edit&step=followupTrigger'), | |
| 64 | + 'campaign_scheduled_link' => acym_completeLink('campaigns&task=edit&step=editEmail&from='.$favoriteTemplate.'&campaign_type=scheduled'), | |
| 65 | + 'welcome_email_link' => acym_completeLink('mails&task=edit&type='.MailClass::TYPE_WELCOME.'&from='.$favoriteTemplate.$welcomeUnsub), | |
| 66 | + 'unsubscribe_email_link' => acym_completeLink('mails&task=edit&type='.MailClass::TYPE_UNSUBSCRIBE.'&from='.$favoriteTemplate.$welcomeUnsub), | |
| 67 | + ] | |
| 68 | + ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + $data['extraBlocks'] = []; | |
| 72 | + acym_trigger('getNewEmailsTypeBlock', [&$data['extraBlocks']]); | |
| 73 | + $data['isNewslettersTab'] = !in_array( | |
| 74 | + $data['selectedType'], | |
| 75 | + array_merge( | |
| 76 | + [ | |
| 77 | + MailClass::TYPE_WELCOME, | |
| 78 | + MailClass::TYPE_UNSUBSCRIBE, | |
| 79 | + ], | |
| 80 | + array_column($data['extraBlocks'], 'email_type') | |
| 81 | + ) | |
| 82 | + ); | |
| 83 | + } else { | |
| 84 | + global $Itemid; | |
| 85 | + $itemId = empty($Itemid) ? '' : '&Itemid='.$Itemid; | |
| 86 | + $returnUrl = urlencode(base64_encode(acym_frontendLink('frontcampaigns'.$itemId))); | |
| 87 | + | |
| 88 | + $welcomeUnsub = '&list_id={dataid}&type_editor=acyEditor&return='.$returnUrl.$itemId; | |
| 89 | + $data = array_merge( | |
| 90 | + $data, | |
| 91 | + [ | |
| 92 | + 'lists' => $listClass->getAllForSelect(true, acym_currentUserId()), | |
| 93 | + 'campaign_link' => acym_frontendLink('frontcampaigns&task=edit&step=chooseTemplate&campaign_type=now'.$itemId.'&'.acym_getFormToken()), | |
| 94 | + 'campaign_scheduled_link' => acym_frontendLink('frontcampaigns&task=edit&step=chooseTemplate&campaign_type=scheduled'.$itemId.'&'.acym_getFormToken()), | |
| 95 | + 'welcome_email_link' => acym_frontendLink('frontmails&task=edit&type='.MailClass::TYPE_WELCOME.$welcomeUnsub.'&'.acym_getFormToken()), | |
| 96 | + 'unsubscribe_email_link' => acym_frontendLink('frontmails&task=edit&type='.MailClass::TYPE_UNSUBSCRIBE.$welcomeUnsub.'&'.acym_getFormToken()), | |
| 97 | + ] | |
| 98 | + ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + parent::display($data); | |
| 102 | + } | |
| 103 | + | |
| 104 | + private function prepareSegmentDisplay(array &$data, ?array $sendingParams): void | |
| 105 | + { | |
| 106 | + $data['menuClass'] = $this->menuClass; | |
| 107 | + $data['displaySegmentTab'] = !empty($sendingParams) && array_key_exists('segment', $sendingParams); | |
| 108 | + } | |
| 109 | + | |
| 110 | + public function chooseTemplate(): void | |
| 111 | + { | |
| 112 | + acym_setVar('layout', 'choose_email'); | |
| 113 | + acym_setVar('step', 'chooseTemplate'); | |
| 114 | + $pagination = new PaginationHelper(); | |
| 115 | + | |
| 116 | + // Get filters data | |
| 117 | + $campaignId = acym_getVar('int', 'campaignId', 0); | |
| 118 | + $campaignClass = new CampaignClass(); | |
| 119 | + $searchFilter = $this->getVarFiltersListing('string', 'mailchoose_search', ''); | |
| 120 | + $tagFilter = $this->getVarFiltersListing('string', 'mailchoose_tag', ''); | |
| 121 | + $ordering = $this->getVarFiltersListing('cmd', 'mailchoose_ordering', 'creation_date'); | |
| 122 | + $orderingSortOrder = $this->getVarFiltersListing('cmd', 'mailchoose_ordering_sort_order', 'DESC'); | |
| 123 | + $campaign = $campaignClass->getOneByIdWithMail($campaignId); | |
| 124 | + $campaignType = $this->getVarFiltersListing('string', 'campaign_type', 'now'); | |
| 125 | + $abTest = acym_getVar('bool', 'abtest', false); | |
| 126 | + | |
| 127 | + $this->setTaskListing($campaignType); | |
| 128 | + | |
| 129 | + if (!empty($campaign)) { | |
| 130 | + if (!$campaignClass->hasUserAccess($campaign->id)) { | |
| 131 | + die('Access denied for this campaign'); | |
| 132 | + } | |
| 133 | + $this->breadcrumb[$campaign->name] = ''; | |
| 134 | + } else { | |
| 135 | + $this->breadcrumb[acym_translation('ACYM_NEW_CAMPAIGN')] = ''; | |
| 136 | + } | |
| 137 | + | |
| 138 | + // Get pagination data | |
| 139 | + $mailsPerPage = $pagination->getListLimit(); | |
| 140 | + $page = $this->getVarFiltersListing('int', 'mailchoose_pagination_page', 1); | |
| 141 | + | |
| 142 | + $mailClass = new MailClass(); | |
| 143 | + $matchingMails = $mailClass->getMatchingElements( | |
| 144 | + [ | |
| 145 | + 'ordering' => $ordering, | |
| 146 | + 'ordering_sort_order' => $orderingSortOrder, | |
| 147 | + 'search' => $searchFilter, | |
| 148 | + 'elementsPerPage' => $mailsPerPage, | |
| 149 | + 'offset' => ($page - 1) * $mailsPerPage, | |
| 150 | + 'tag' => $tagFilter, | |
| 151 | + 'creator_id' => $this->setFrontEndParamsForTemplateChoose(), | |
| 152 | + 'gettingTemplates' => true, | |
| 153 | + ] | |
| 154 | + ); | |
| 155 | + | |
| 156 | + // Prepare the pagination | |
| 157 | + $pagination->setStatus($matchingMails['total']->total, $page, $mailsPerPage); | |
| 158 | + | |
| 159 | + $tagClass = new TagClass(); | |
| 160 | + | |
| 161 | + $data = [ | |
| 162 | + 'allMails' => $matchingMails['elements'], | |
| 163 | + 'allTags' => $tagClass->getAllTagsByType(TagClass::TYPE_MAIL), | |
| 164 | + 'pagination' => $pagination, | |
| 165 | + 'search' => $searchFilter, | |
| 166 | + 'tag' => $tagFilter, | |
| 167 | + 'ordering' => $ordering, | |
| 168 | + 'campaignID' => $campaignId, | |
| 169 | + 'campaign_type' => $campaignType, | |
| 170 | + 'abtest' => $abTest, | |
| 171 | + ]; | |
| 172 | + $this->prepareListingClasses($data); | |
| 173 | + $this->prepareSegmentDisplay($data, empty($campaign->sending_params) ? [] : $campaign->sending_params); | |
| 174 | + | |
| 175 | + $data['menuClass'] = $this->menuClass; | |
| 176 | + | |
| 177 | + parent::display($data); | |
| 178 | + } | |
| 179 | + | |
| 180 | + protected function setFrontEndParamsForTemplateChoose(): int | |
| 181 | + { | |
| 182 | + return 0; | |
| 183 | + } | |
| 184 | + | |
| 185 | + private function prepareEditCampaign(array &$data): void | |
| 186 | + { | |
| 187 | + $campaignId = acym_getVar('int', 'campaignId', 0); | |
| 188 | + $mailId = acym_getVar('int', 'from', 0); | |
| 189 | + $mailClass = new MailClass(); | |
| 190 | + $data['mailClass'] = $mailClass; | |
| 191 | + | |
| 192 | + if ($mailId > 0 && !$mailClass->hasUserAccess($mailId)) { | |
| 193 | + die('Access denied for this email'); | |
| 194 | + } | |
| 195 | + // Check autosave only if mail already saved in campaign and not just selected template (from parameter) | |
| 196 | + $checkAutosave = empty($mailId); | |
| 197 | + $editLink = 'campaigns&task=edit&step=editEmail'; | |
| 198 | + | |
| 199 | + if (empty($campaignId)) { | |
| 200 | + $data['mailInformation'] = new stdClass(); | |
| 201 | + $data['mailInformation']->id = 0; | |
| 202 | + $data['mailInformation']->name = ''; | |
| 203 | + $data['mailInformation']->tags = []; | |
| 204 | + $data['mailInformation']->subject = ''; | |
| 205 | + $data['mailInformation']->preheader = ''; | |
| 206 | + $data['mailInformation']->body = ''; | |
| 207 | + $data['mailInformation']->settings = null; | |
| 208 | + $data['mailInformation']->links_language = ''; | |
| 209 | + $data['mailInformation']->visible = 1; | |
| 210 | + $data['mailInformation']->mail_settings = null; | |
| 211 | + | |
| 212 | + $editLink .= '&from='.$mailId; | |
| 213 | + } else { | |
| 214 | + $campaignClass = new CampaignClass(); | |
| 215 | + if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 216 | + die('Access denied for this campaign'); | |
| 217 | + } | |
| 218 | + | |
| 219 | + if (!empty($mailId)) { | |
| 220 | + $campaignClass->resetAbTestVersion($campaignId); | |
| 221 | + } | |
| 222 | + | |
| 223 | + $data['mailInformation'] = $campaignClass->getOneByIdWithMail($campaignId); | |
| 224 | + if (empty($mailId)) { | |
| 225 | + $mailId = $data['mailInformation']->mail_id; | |
| 226 | + } | |
| 227 | + $editLink .= '&campaignId='.$campaignId; | |
| 228 | + | |
| 229 | + if (in_array($data['mailInformation']->sending_type, ['birthday', 'woocommerce_cart'])) { | |
| 230 | + $this->setTaskListing($data['mailInformation']->sending_type); | |
| 231 | + } | |
| 232 | + } | |
| 233 | + | |
| 234 | + if ($mailId == -1 || (empty($campaignId) && empty($mailId))) { | |
| 235 | + $data['mailInformation']->name = ''; | |
| 236 | + $data['mailInformation']->tags = []; | |
| 237 | + $data['mailInformation']->subject = ''; | |
| 238 | + $data['mailInformation']->preheader = ''; | |
| 239 | + $data['mailInformation']->body = ''; | |
| 240 | + $data['mailInformation']->settings = null; | |
| 241 | + $data['mailInformation']->attachments = []; | |
| 242 | + $data['mailInformation']->stylesheet = ''; | |
| 243 | + $data['mailInformation']->headers = ''; | |
| 244 | + $data['mailInformation']->from_email = ''; | |
| 245 | + $data['mailInformation']->from_name = ''; | |
| 246 | + $data['mailInformation']->reply_to_email = ''; | |
| 247 | + $data['mailInformation']->reply_to_name = ''; | |
| 248 | + $data['mailInformation']->mail_settings = null; | |
| 249 | + $data['typeEditor'] = 'acyEditor'; | |
| 250 | + } elseif (!empty($mailId)) { | |
| 251 | + $mail = $mailClass->getOneById($mailId); | |
| 252 | + if (!acym_isAdmin() && ACYM_CMS === 'joomla' && acym_isPluginActive('sef')) { | |
| 253 | + $mail->body = str_replace(['url("', '")'], ["url('", "')"], $mail->body); | |
| 254 | + } | |
| 255 | + $data['mailInformation']->tags = $mail->tags; | |
| 256 | + $data['mailInformation']->subject = $mail->subject; | |
| 257 | + $data['mailInformation']->preheader = $mail->preheader; | |
| 258 | + $data['mailInformation']->body = $mail->body; | |
| 259 | + $data['mailInformation']->settings = $mail->settings; | |
| 260 | + $data['mailInformation']->stylesheet = $mail->stylesheet; | |
| 261 | + $data['mailInformation']->headers = $mail->headers; | |
| 262 | + $data['mailInformation']->attachments = empty($mail->attachments) ? [] : json_decode($mail->attachments, true); | |
| 263 | + $data['mailInformation']->links_language = $mail->links_language; | |
| 264 | + | |
| 265 | + $data['mailInformation']->from_email = $mail->from_email; | |
| 266 | + $data['mailInformation']->from_name = $mail->from_name; | |
| 267 | + $data['mailInformation']->reply_to_email = $mail->reply_to_email; | |
| 268 | + $data['mailInformation']->reply_to_name = $mail->reply_to_name; | |
| 269 | + $data['mailInformation']->mail_settings = $mail->mail_settings; | |
| 270 | + | |
| 271 | + if ($checkAutosave) { | |
| 272 | + $data['mailInformation']->autosave = $mail->autosave; | |
| 273 | + } | |
| 274 | + } | |
| 275 | + $data['mailId'] = $mailId; | |
| 276 | + $data['campaignID'] = $data['mailInformation']->id; | |
| 277 | + | |
| 278 | + $pluginHelper = new PluginHelper(); | |
| 279 | + if (empty($data['mailInformation']->body)) { | |
| 280 | + $data['mailInformation']->body = ''; | |
| 281 | + } | |
| 282 | + $pluginHelper->cleanHtml($data['mailInformation']->body); | |
| 283 | + | |
| 284 | + $editLink .= '&type_editor='.$data['typeEditor']; | |
| 285 | + $this->breadcrumb[empty($data['mailInformation']->name) ? acym_translation('ACYM_NEW_CAMPAIGN') : $data['mailInformation']->name] = acym_completeLink( | |
| 286 | + $editLink | |
| 287 | + ); | |
| 288 | + } | |
| 289 | + | |
| 290 | + private function prepareEditor(array &$data): void | |
| 291 | + { | |
| 292 | + $data['editor'] = new EditorHelper(); | |
| 293 | + $data['editor']->content = $data['mailInformation']->body; | |
| 294 | + $data['editor']->autoSave = !empty($data['mailInformation']->autosave) ? $data['mailInformation']->autosave : ''; | |
| 295 | + if (!empty($data['mailInformation']->settings)) { | |
| 296 | + $data['editor']->settings = $data['mailInformation']->settings; | |
| 297 | + } | |
| 298 | + | |
| 299 | + if (!empty($data['mailInformation']->stylesheet)) { | |
| 300 | + $data['editor']->stylesheet = $data['mailInformation']->stylesheet; | |
| 301 | + } | |
| 302 | + | |
| 303 | + if (empty($data['typeEditor']) && strpos($data['editor']->content, 'acym__wysid__template') !== false) { | |
| 304 | + $data['typeEditor'] = 'acyEditor'; | |
| 305 | + } | |
| 306 | + | |
| 307 | + $data['editor']->editor = $data['typeEditor']; | |
| 308 | + if ($data['editor']->editor != 'acyEditor' || empty($data['editor']->editor)) { | |
| 309 | + if (!isset($data['mailInformation']->stylesheet)) { | |
| 310 | + $data['mailInformation']->stylesheet = ''; | |
| 311 | + } | |
| 312 | + $data['needDisplayStylesheet'] = $data['mailInformation']->stylesheet; | |
| 313 | + } else { | |
| 314 | + $data['needDisplayStylesheet'] = ''; | |
| 315 | + } | |
| 316 | + | |
| 317 | + $data['editor']->mailId = empty($data['mailId']) ? 0 : $data['mailId']; | |
| 318 | + | |
| 319 | + if ($data['editor']->isDragAndDrop()) { | |
| 320 | + $this->loadScripts['edit_email'][] = 'editor-wysid'; | |
| 321 | + $this->loadScripts['edit_email']['vue-applications'] = ['custom_view']; | |
| 322 | + } else { | |
| 323 | + $this->loadScripts['edit_email'][] = 'dtextPicker'; | |
| 324 | + } | |
| 325 | + } | |
| 326 | + | |
| 327 | + public function prepareMaxUpload(array &$data): void | |
| 328 | + { | |
| 329 | + $maxupload = ini_get('upload_max_filesize'); | |
| 330 | + $maxpost = ini_get('post_max_size'); | |
| 331 | + $data['maxupload'] = acym_bytes($maxupload) > acym_bytes($maxpost) ? $maxpost : $maxupload; | |
| 332 | + } | |
| 333 | + | |
| 334 | + private function prepareAbTest(array &$data, bool $editor = true): void | |
| 335 | + { | |
| 336 | + $data['abtest'] = false; | |
| 337 | + | |
| 338 | + } | |
| 339 | + | |
| 340 | + private function prepareMultilingual(array &$data, bool $editor = true): void | |
| 341 | + { | |
| 342 | + $data['multilingual'] = 0; | |
| 343 | + | |
| 344 | + } | |
| 345 | + | |
| 346 | + private function prepareAllMailsForAbtest(array &$data): void | |
| 347 | + { | |
| 348 | + $mailClass = new MailClass(); | |
| 349 | + | |
| 350 | + $mails = $mailClass->getParentAndChildMails($data['mailId']); | |
| 351 | + | |
| 352 | + if (empty($mails)) { | |
| 353 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_LOAD_ABTEST_MAILS'), 'error'); | |
| 354 | + | |
| 355 | + return; | |
| 356 | + } | |
| 357 | + | |
| 358 | + foreach ($mails as $key => $oneMail) { | |
| 359 | + $mails[$key] = $this->prepareMailDataSummary($data, (int)$oneMail->id); | |
| 360 | + } | |
| 361 | + | |
| 362 | + $data['abtest_mails'] = $mails; | |
| 363 | + } | |
| 364 | + | |
| 365 | + private function prepareAllMailsForMultilingual(array &$data): void | |
| 366 | + { | |
| 367 | + $mailClass = new MailClass(); | |
| 368 | + | |
| 369 | + $mails = $mailClass->getMultilingualMails($data['mailId']); | |
| 370 | + | |
| 371 | + if (empty($mails)) { | |
| 372 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_LOAD_MULTILINGUAL_MAILS'), 'error'); | |
| 373 | + | |
| 374 | + return; | |
| 375 | + } | |
| 376 | + | |
| 377 | + foreach ($mails as $key => $oneMail) { | |
| 378 | + $mails[$key] = $this->prepareMailDataSummary($data, (int)$oneMail->id); | |
| 379 | + } | |
| 380 | + | |
| 381 | + $data['multilingual_mails'] = $mails; | |
| 382 | + } | |
| 383 | + | |
| 384 | + public function editEmail(): void | |
| 385 | + { | |
| 386 | + acym_setVar('layout', 'edit_email'); | |
| 387 | + acym_setVar('numberattachment', '0'); | |
| 388 | + acym_setVar('step', 'editEmail'); | |
| 389 | + | |
| 390 | + $tagClass = new TagClass(); | |
| 391 | + | |
| 392 | + $data = [ | |
| 393 | + 'containerClass' => $this->stepContainerClass, | |
| 394 | + 'social_icons' => $this->config->get('social_icons', '{}'), | |
| 395 | + 'allTags' => $tagClass->getAllTagsByType(TagClass::TYPE_MAIL), | |
| 396 | + 'campaign_type' => acym_getVar('string', 'campaign_type', 'now'), | |
| 397 | + 'typeEditor' => acym_getVar('string', 'type_editor', ''), | |
| 398 | + 'uploadFileType' => new UploadFileType(), | |
| 399 | + ]; | |
| 400 | + | |
| 401 | + $this->prepareEditCampaign($data); | |
| 402 | + $this->prepareEditor($data); | |
| 403 | + $this->prepareMaxUpload($data); | |
| 404 | + $this->prepareAbTest($data); | |
| 405 | + $this->prepareMultilingual($data); | |
| 406 | + $this->prepareListingClasses($data); | |
| 407 | + $this->prepareSegmentDisplay($data, empty($data['mailInformation']->sending_params) ? [] : $data['mailInformation']->sending_params); | |
| 408 | + | |
| 409 | + $data['before-save'] = ''; | |
| 410 | + | |
| 411 | + if ($data['editor']->editor === 'acyEditor') { | |
| 412 | + $data['before-save'] = 'wysidSwitch'; | |
| 413 | + } | |
| 414 | + | |
| 415 | + $data['menuClass'] = $this->menuClass; | |
| 416 | + | |
| 417 | + parent::display($data); | |
| 418 | + } | |
| 419 | + | |
| 420 | + public function recipients(): void | |
| 421 | + { | |
| 422 | + acym_setVar('layout', 'recipients'); | |
| 423 | + acym_setVar('step', 'recipients'); | |
| 424 | + | |
| 425 | + $campaignId = acym_getVar('int', 'campaignId'); | |
| 426 | + $campaignClass = new CampaignClass(); | |
| 427 | + $mailClass = new MailClass(); | |
| 428 | + | |
| 429 | + if (empty($campaignId) || !$campaignClass->hasUserAccess($campaignId)) { | |
| 430 | + die('Access denied for this campaign'); | |
| 431 | + } | |
| 432 | + | |
| 433 | + $currentCampaign = $campaignClass->getOneByIdWithMail($campaignId); | |
| 434 | + $this->breadcrumb[$currentCampaign->name] = acym_completeLink('campaigns&task=edit&step=recipients&campaignId='.$campaignId); | |
| 435 | + | |
| 436 | + $campaign = [ | |
| 437 | + 'campaignInformation' => $campaignId, | |
| 438 | + 'currentCampaign' => $currentCampaign, | |
| 439 | + 'containerClass' => $this->stepContainerClass, | |
| 440 | + 'entitySelectHelper' => new EntitySelectHelper(), | |
| 441 | + ]; | |
| 442 | + | |
| 443 | + $campaignLists = $mailClass->getAllListsByMailId($currentCampaign->mail_id); | |
| 444 | + $campaign['campaignListsId'] = array_keys($campaignLists); | |
| 445 | + acym_arrayToInteger($campaign['campaignListsId']); | |
| 446 | + $campaign['campaignListsSelected'] = json_encode($campaign['campaignListsId']); | |
| 447 | + | |
| 448 | + $this->prepareListingClasses($campaign); | |
| 449 | + $this->prepareSegmentDisplay($campaign, $campaign['currentCampaign']->sending_params); | |
| 450 | + | |
| 451 | + parent::display($campaign); | |
| 452 | + } | |
| 453 | + | |
| 454 | + public function segment(): void | |
| 455 | + { | |
| 456 | + acym_setVar('layout', 'segment'); | |
| 457 | + acym_setVar('step', 'segment'); | |
| 458 | + | |
| 459 | + $campaignClass = new CampaignClass(); | |
| 460 | + $mailClass = new MailClass(); | |
| 461 | + $campaignId = acym_getVar('int', 'campaignId'); | |
| 462 | + | |
| 463 | + if (empty($campaignId)) { | |
| 464 | + acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_NOT_FOUND'), 'error'); | |
| 465 | + $this->listing(); | |
| 466 | + | |
| 467 | + return; | |
| 468 | + } | |
| 469 | + | |
| 470 | + $campaign = $campaignClass->getOneById($campaignId); | |
| 471 | + | |
| 472 | + $mail = $mailClass->getOneById($campaign->mail_id); | |
| 473 | + $data = [ | |
| 474 | + 'campaign' => $campaign, | |
| 475 | + 'containerClass' => $this->stepContainerClass, | |
| 476 | + 'displaySegmentTab' => true, | |
| 477 | + 'workflowHelper' => new WorkflowHelper(), | |
| 478 | + ]; | |
| 479 | + | |
| 480 | + | |
| 481 | + $this->breadcrumb[$mail->name] = acym_completeLink(acym_completeLink('campaigns&task=edit&step=recipients&campaignId='.$campaign->id)); | |
| 482 | + parent::display($data); | |
| 483 | + } | |
| 484 | + | |
| 485 | + public function sendSettings(): void | |
| 486 | + { | |
| 487 | + acym_setVar('layout', 'send_settings'); | |
| 488 | + acym_setVar('step', 'sendSettings'); | |
| 489 | + $campaignId = acym_getVar('int', 'campaignId'); | |
| 490 | + $campaignClass = new CampaignClass(); | |
| 491 | + $campaignInformation = empty($campaignId) ? null : $campaignClass->getOneById($campaignId); | |
| 492 | + | |
| 493 | + if (is_null($campaignInformation)) { | |
| 494 | + acym_enqueueMessage(acym_translation('ACYM_CANT_GET_CAMPAIGN_INFORMATION'), 'error'); | |
| 495 | + $this->listing(); | |
| 496 | + | |
| 497 | + return; | |
| 498 | + } | |
| 499 | + | |
| 500 | + if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 501 | + die('Access denied for this campaign'); | |
| 502 | + } | |
| 503 | + | |
| 504 | + //To know if we create or modify the campaign | |
| 505 | + $from = acym_getVar('string', 'from'); | |
| 506 | + | |
| 507 | + $campaignClass = new CampaignClass(); | |
| 508 | + $currentCampaign = $campaignClass->getOneByIdWithMail($campaignId); | |
| 509 | + $this->breadcrumb[$currentCampaign->name] = acym_completeLink('campaigns&task=edit&step=sendSettings&campaignId='.$campaignId); | |
| 510 | + | |
| 511 | + if (!empty($currentCampaign->sent) && empty($currentCampaign->active)) { | |
| 512 | + $currentCampaign->sending_date = ''; | |
| 513 | + } | |
| 514 | + | |
| 515 | + $data = [ | |
| 516 | + 'nbSubscribers' => $campaignClass->countUsersCampaign($campaignId, true), | |
| 517 | + 'from' => $from, | |
| 518 | + 'suggestedDate' => acym_date('1534771620', 'j M Y H:i'), | |
| 519 | + 'campaignClass' => $campaignClass, | |
| 520 | + ]; | |
| 521 | + | |
| 522 | + $data['currentCampaign'] = $currentCampaign; | |
| 523 | + $data['currentCampaign']->bcc = $currentCampaign->bcc ?? ''; | |
| 524 | + $data['currentCampaign']->bounce_email = $currentCampaign->bounce_email ?? ''; | |
| 525 | + $data['currentCampaign']->send_now = $currentCampaign->sending_type === CampaignClass::SENDING_TYPE_NOW; | |
| 526 | + $data['currentCampaign']->send_scheduled = $currentCampaign->sending_type === CampaignClass::SENDING_TYPE_SCHEDULED; | |
| 527 | + $data['currentCampaign']->send_auto = $currentCampaign->sending_type === CampaignClass::SENDING_TYPE_AUTO; | |
| 528 | + | |
| 529 | + // Handle special emails | |
| 530 | + $data['currentCampaign']->send_specific = []; | |
| 531 | + if (!in_array($currentCampaign->sending_type, CampaignClass::SENDING_TYPES)) { | |
| 532 | + acym_trigger( | |
| 533 | + 'getCampaignSpecificSendSettings', | |
| 534 | + [ | |
| 535 | + $currentCampaign->sending_type, | |
| 536 | + $currentCampaign->sending_params, | |
| 537 | + &$data['currentCampaign']->send_specific, | |
| 538 | + ] | |
| 539 | + ); | |
| 540 | + } | |
| 541 | + | |
| 542 | + $data['senderInformations'] = new stdClass(); | |
| 543 | + $data['senderInformations']->from_name = empty($currentCampaign->from_name) ? '' : $currentCampaign->from_name; | |
| 544 | + $data['senderInformations']->from_email = empty($currentCampaign->from_email) ? '' : $currentCampaign->from_email; | |
| 545 | + $data['senderInformations']->reply_to_name = empty($currentCampaign->reply_to_name) ? '' : $currentCampaign->reply_to_name; | |
| 546 | + $data['senderInformations']->reply_to_email = empty($currentCampaign->reply_to_email) ? '' : $currentCampaign->reply_to_email; | |
| 547 | + $data['senderInformations']->bounce_email = empty($currentCampaign->bounce_email) ? '' : $currentCampaign->bounce_email; | |
| 548 | + | |
| 549 | + $data['config_values'] = new stdClass(); | |
| 550 | + $data['config_values']->from_name = $this->config->get('from_name'); | |
| 551 | + $data['config_values']->from_email = $this->config->get('from_email'); | |
| 552 | + $data['config_values']->reply_to_name = $this->config->get('replyto_name'); | |
| 553 | + $data['config_values']->reply_to_email = $this->config->get('replyto_email'); | |
| 554 | + $data['config_values']->bounce_email = $this->config->get('bounce_email'); | |
| 555 | + | |
| 556 | + $triggers = []; | |
| 557 | + | |
| 558 | + acym_trigger('onAcymDeclareTriggers', [&$triggers, &$currentCampaign->sending_params], 'plgAcymTime'); | |
| 559 | + $triggers = $triggers['classic']; | |
| 560 | + | |
| 561 | + $data['triggers_select'] = []; | |
| 562 | + $data['triggers_display'] = []; | |
| 563 | + | |
| 564 | + foreach ($triggers as $key => $trigger) { | |
| 565 | + $data['triggers_select'][$key] = $trigger->name; | |
| 566 | + $data['triggers_display'][$key] = $trigger; | |
| 567 | + } | |
| 568 | + | |
| 569 | + if (!empty($data['currentCampaign']->sending_params) && empty($data['currentCampaign']->sending_params['trigger_type'])) { | |
| 570 | + foreach (array_keys($triggers) as $oneTrigger) { | |
| 571 | + if (!empty($data['currentCampaign']->sending_params[$oneTrigger])) { | |
| 572 | + $data['currentCampaign']->sending_params['trigger_type'] = $oneTrigger; | |
| 573 | + } | |
| 574 | + } | |
| 575 | + } | |
| 576 | + | |
| 577 | + $data['containerClass'] = $this->stepContainerClass; | |
| 578 | + if (acym_isMultilingual()) { | |
| 579 | + $data['langChoice'] = []; | |
| 580 | + } else { | |
| 581 | + $languages = acym_getLanguages(true, true); | |
| 582 | + $data['langChoice'] = count($languages) < 2 | |
| 583 | + ? [] | |
| 584 | + : [ | |
| 585 | + 'links' => $data['currentCampaign']->links_language, | |
| 586 | + 'name' => 'senderInformation[links_language]', | |
| 587 | + ]; | |
| 588 | + } | |
| 589 | + | |
| 590 | + $this->prepareListingClasses($data); | |
| 591 | + $this->prepareSegmentDisplay($data, $data['currentCampaign']->sending_params); | |
| 592 | + $this->prepareMultilingualOption($data); | |
| 593 | + | |
| 594 | + parent::display($data); | |
| 595 | + } | |
| 596 | + | |
| 597 | + public function saveEditEmail(bool $ajax = false): int | |
| 598 | + { | |
| 599 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 600 | + | |
| 601 | + $campaignClass = new CampaignClass(); | |
| 602 | + $mailClass = new MailClass(); | |
| 603 | + $formData = acym_getVar('array', 'mail', []); | |
| 604 | + $versions = acym_getVar('array', 'versions', [], 'REQUEST', ACYM_ALLOWRAW); | |
| 605 | + $versionType = acym_getVar('string', 'version_type', ''); | |
| 606 | + $allowedFields = acym_getColumns('mail'); | |
| 607 | + $campaignId = acym_getVar('int', 'campaignId', 0); | |
| 608 | + $campaignType = acym_getVar('string', 'campaign_type', 'now'); | |
| 609 | + | |
| 610 | + $types = [ | |
| 611 | + 'now' => CampaignClass::SENDING_TYPE_NOW, | |
| 612 | + 'auto' => CampaignClass::SENDING_TYPE_AUTO, | |
| 613 | + 'scheduled' => CampaignClass::SENDING_TYPE_SCHEDULED, | |
| 614 | + ]; | |
| 615 | + | |
| 616 | + acym_trigger('getCampaignTypes', [&$types]); | |
| 617 | + | |
| 618 | + if (empty($campaignId)) { | |
| 619 | + $mail = new stdClass(); | |
| 620 | + $mail->creation_date = acym_date('now', 'Y-m-d H:i:s', false); | |
| 621 | + $mail->type = MailClass::TYPE_STANDARD; | |
| 622 | + | |
| 623 | + $campaign = new stdClass(); | |
| 624 | + $campaign->draft = 1; | |
| 625 | + $campaign->active = 0; | |
| 626 | + $campaign->sending_type = $types[$campaignType] ?? CampaignClass::SENDING_TYPE_NOW; | |
| 627 | + $campaign->sent = 0; | |
| 628 | + $campaign->sending_params = []; | |
| 629 | + } else { | |
| 630 | + if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 631 | + die('Access denied for this campaign'); | |
| 632 | + } | |
| 633 | + | |
| 634 | + $campaign = $campaignClass->getOneById($campaignId); | |
| 635 | + $mail = $mailClass->getOneById($campaign->mail_id); | |
| 636 | + $mailArchiveClass = new MailArchiveClass(); | |
| 637 | + $archive = $mailArchiveClass->getOneByMailId($mail->id); | |
| 638 | + if (!empty($archive)) { | |
| 639 | + $mailArchiveClass->delete([$archive->id]); | |
| 640 | + } | |
| 641 | + } | |
| 642 | + $campaign->visible = acym_getVar('int', 'visible', 1); | |
| 643 | + | |
| 644 | + // Get the name and subject | |
| 645 | + foreach ($formData as $name => $data) { | |
| 646 | + if (!in_array($name, $allowedFields)) { | |
| 647 | + continue; | |
| 648 | + } | |
| 649 | + $mail->{acym_secureDBColumn($name)} = $data; | |
| 650 | + } | |
| 651 | + | |
| 652 | + // Name is mandatory. If empty copy subject (can't be an empty field) | |
| 653 | + if (empty($mail->name)) $mail->name = empty($mail->subject) ? acym_translation('ACYM_CAMPAIGN_NAME') : $mail->subject; | |
| 654 | + | |
| 655 | + if (empty($mail->subject)) $mail->subject = acym_translation('ACYM_EMAIL_SUBJECT'); | |
| 656 | + | |
| 657 | + $mail->body = acym_getVar('string', 'editor_content', '', 'REQUEST', ACYM_ALLOWRAW); | |
| 658 | + $mail->settings = acym_getVar('string', 'editor_settings', '', 'REQUEST', ACYM_ALLOWRAW); | |
| 659 | + $mail->stylesheet = acym_getVar('string', 'editor_stylesheet', '', 'REQUEST', ACYM_ALLOWRAW); | |
| 660 | + $mail->headers = acym_getVar('string', 'editor_headers', '', 'REQUEST', ACYM_ALLOWRAW); | |
| 661 | + $mail->from_email = acym_getVar('string', 'from_email', '', 'REQUEST'); | |
| 662 | + $mail->from_name = acym_getVar('string', 'from_name', '', 'REQUEST'); | |
| 663 | + $mail->reply_to_email = acym_getVar('string', 'reply_to_email', '', 'REQUEST'); | |
| 664 | + $mail->reply_to_name = acym_getVar('string', 'reply_to_name', '', 'REQUEST'); | |
| 665 | + $mail->drag_editor = strpos($mail->body, 'acym__wysid__template') === false ? 0 : 1; | |
| 666 | + $mail->attachments = empty($mail->attachments) ? [] : json_decode($mail->attachments, true); | |
| 667 | + | |
| 668 | + $mainColors = acym_getVar('string', 'main_colors', '', 'REQUEST', ACYM_ALLOWRAW); | |
| 669 | + if (!empty($mail->mail_settings)) { | |
| 670 | + $mailSettings = json_decode($mail->mail_settings, false); | |
| 671 | + } else { | |
| 672 | + $mailSettings = new stdClass(); | |
| 673 | + } | |
| 674 | + $mailSettings->mainColors = $mainColors; | |
| 675 | + $mail->mail_settings = json_encode($mailSettings); | |
| 676 | + | |
| 677 | + $mail->tags = acym_getVar('array', 'template_tags', []); | |
| 678 | + | |
| 679 | + $mailController = new MailsController(); | |
| 680 | + $mailController->setAttachmentToMail($mail); | |
| 681 | + | |
| 682 | + if (!empty($versions)) { | |
| 683 | + if (!empty($versions['main']['subject'])) $mail->subject = $versions['main']['subject']; | |
| 684 | + if (!empty($versions['main']['preview'])) $mail->preheader = $versions['main']['preview']; | |
| 685 | + if (!empty($versions['main']['content'])) $mail->body = $versions['main']['content']; | |
| 686 | + if (!empty($versions['main']['settings'])) $mail->settings = $versions['main']['settings']; | |
| 687 | + if (!empty($versions['main']['stylesheet'])) $mail->stylesheet = $versions['main']['stylesheet']; | |
| 688 | + | |
| 689 | + if ($versionType === 'multilingual') { | |
| 690 | + $mail->links_language = $this->config->get('multilingual_default'); | |
| 691 | + } | |
| 692 | + | |
| 693 | + unset($versions['main']); | |
| 694 | + } | |
| 695 | + | |
| 696 | + $mailID = $mailClass->save($mail); | |
| 697 | + if (!empty($mailID)) { | |
| 698 | + if (acym_getVar('string', 'nextstep', '') === 'listing') { | |
| 699 | + acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); | |
| 700 | + } | |
| 701 | + } else { | |
| 702 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 703 | + if (!empty($mailClass->errors)) { | |
| 704 | + acym_enqueueMessage($mailClass->errors, 'error'); | |
| 705 | + } | |
| 706 | + | |
| 707 | + if (!$ajax) { | |
| 708 | + $this->listing(); | |
| 709 | + } | |
| 710 | + | |
| 711 | + return 0; | |
| 712 | + } | |
| 713 | + | |
| 714 | + if (!empty($versions) && in_array($versionType, ['multilingual', 'abtest'])) { | |
| 715 | + $abTestSendingParams = empty($campaign->sending_params['abtest']) ? [] : $campaign->sending_params['abtest']; | |
| 716 | + foreach ($versions as $code => $version) { | |
| 717 | + if (empty($version['subject'])) { | |
| 718 | + if ($versionType === 'multilingual') { | |
| 719 | + $mailClass->delete([$mailClass->getTranslationId($mailID, $code)]); | |
| 720 | + } elseif (!empty($abTestSendingParams[$code])) { | |
| 721 | + $mailClass->delete([$abTestSendingParams[$code]]); | |
| 722 | + } | |
| 723 | + continue; | |
| 724 | + } | |
| 725 | + | |
| 726 | + unset($mail->id); | |
| 727 | + $versionId = null; | |
| 728 | + if ($versionType === 'multilingual') { | |
| 729 | + $versionId = $mailClass->getTranslationId($mailID, $code); | |
| 730 | + } elseif (!empty($abTestSendingParams[$code])) { | |
| 731 | + $versionId = $abTestSendingParams[$code]; | |
| 732 | + } | |
| 733 | + if (!empty($versionId)) { | |
| 734 | + $mail->id = $versionId; | |
| 735 | + } | |
| 736 | + | |
| 737 | + $mail->subject = $version['subject']; | |
| 738 | + $mail->preheader = $version['preview']; | |
| 739 | + $mail->body = $version['content']; | |
| 740 | + $mail->parent_id = $mailID; | |
| 741 | + $mail->settings = $version['settings']; | |
| 742 | + $mail->stylesheet = $version['stylesheet']; | |
| 743 | + | |
| 744 | + if ($versionType === 'multilingual') { | |
| 745 | + $mail->links_language = $code; | |
| 746 | + $mail->language = $code; | |
| 747 | + } | |
| 748 | + | |
| 749 | + $versionMailId = $mailClass->save($mail); | |
| 750 | + $abTestSendingParams[$code] = $versionMailId; | |
| 751 | + } | |
| 752 | + | |
| 753 | + if ($versionType === 'abtest') { | |
| 754 | + $campaign->sending_params['abtest'] = $abTestSendingParams; | |
| 755 | + } | |
| 756 | + } | |
| 757 | + | |
| 758 | + $campaign->mail_id = $mailID; | |
| 759 | + $campaign->id = $campaignClass->save($campaign); | |
| 760 | + | |
| 761 | + if (!$ajax) { | |
| 762 | + acym_setVar('campaignId', $campaign->id); | |
| 763 | + $this->edit(); | |
| 764 | + } | |
| 765 | + | |
| 766 | + return (int)$campaign->id; | |
| 767 | + } | |
| 768 | + | |
| 769 | + public function saveRecipients(): void | |
| 770 | + { | |
| 771 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 772 | + | |
| 773 | + $allLists = json_decode(acym_getVar('string', 'acym__entity_select__selected')); | |
| 774 | + $allListsUnselected = json_decode(acym_getVar('string', 'acym__entity_select__unselected')); | |
| 775 | + if (empty($allLists)) { | |
| 776 | + $allLists = []; | |
| 777 | + } | |
| 778 | + if (empty($allListsUnselected)) { | |
| 779 | + $allListsUnselected = []; | |
| 780 | + } | |
| 781 | + $campaignId = acym_getVar('int', 'campaignId'); | |
| 782 | + $addSegmentStep = acym_getVar('int', 'add_segment_step'); | |
| 783 | + | |
| 784 | + $campaignClass = new CampaignClass(); | |
| 785 | + | |
| 786 | + if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 787 | + die('Access denied for this campaign'); | |
| 788 | + } | |
| 789 | + | |
| 790 | + $currentCampaign = $campaignClass->getOneByIdWithMail($campaignId); | |
| 791 | + | |
| 792 | + if ((!$currentCampaign->sent || $currentCampaign->active) && !empty($currentCampaign->mail_id)) { | |
| 793 | + $campaignClass->manageListsToCampaign($allLists, $currentCampaign->mail_id, $allListsUnselected); | |
| 794 | + if (acym_getVar('string', 'nextstep', '') === 'listing') { | |
| 795 | + acym_enqueueMessage(acym_translationSprintf('ACYM_LIST_IS_SAVED', $currentCampaign->name)); | |
| 796 | + } | |
| 797 | + } | |
| 798 | + | |
| 799 | + if (!empty($addSegmentStep)) { | |
| 800 | + if (!isset($currentCampaign->sending_params['segment'])) { | |
| 801 | + $currentCampaign->sending_params['segment'] = []; | |
| 802 | + } | |
| 803 | + $campaignClass->save($currentCampaign); | |
| 804 | + acym_setVar('nextstep', 'segment'); | |
| 805 | + $this->segment(); | |
| 806 | + | |
| 807 | + return; | |
| 808 | + } | |
| 809 | + | |
| 810 | + if (isset($currentCampaign->sending_params['segment'])) unset($currentCampaign->sending_params['segment']); | |
| 811 | + $campaignClass->save($currentCampaign); | |
| 812 | + | |
| 813 | + acym_setVar('campaignId', $currentCampaign->id); | |
| 814 | + | |
| 815 | + $this->edit(); | |
| 816 | + } | |
| 817 | + | |
| 818 | + public function saveSegment(): void | |
| 819 | + { | |
| 820 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 821 | + | |
| 822 | + if (!acym_isAdmin() || !acym_isAllowed('campaigns')) { | |
| 823 | + die('Access denied for segments'); | |
| 824 | + } | |
| 825 | + | |
| 826 | + $segmentSelected = acym_getVar('int', 'segment_selected', 0); | |
| 827 | + $filters = acym_getVar('array', 'acym_action', []); | |
| 828 | + if (acym_conditionsContainRestrictedFilter($filters) && !acym_hasAdminPermissions()) { | |
| 829 | + die('Access denied for segments'); | |
| 830 | + } | |
| 831 | + $campaignId = acym_getVar('int', 'campaignId', 0); | |
| 832 | + | |
| 833 | + if (empty($campaignId)) { | |
| 834 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 835 | + $this->listing(); | |
| 836 | + | |
| 837 | + return; | |
| 838 | + } | |
| 839 | + | |
| 840 | + $segmentSavedId = acym_getVar('int', 'saved_segment_id', 0); | |
| 841 | + | |
| 842 | + if (!empty($segmentSavedId)) { | |
| 843 | + $segmentSelected = $segmentSavedId; | |
| 844 | + } | |
| 845 | + | |
| 846 | + $campaignClass = new CampaignClass(); | |
| 847 | + $campaign = $campaignClass->getOneById($campaignId); | |
| 848 | + | |
| 849 | + if (empty($campaign)) { | |
| 850 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 851 | + $this->listing(); | |
| 852 | + | |
| 853 | + return; | |
| 854 | + } | |
| 855 | + | |
| 856 | + if (empty($segmentSelected) && empty($filters)) { | |
| 857 | + $campaign->sending_params['segment'] = []; | |
| 858 | + } elseif (!empty($segmentSelected)) { | |
| 859 | + $campaign->sending_params['segment'] = ['segment_id' => $segmentSelected]; | |
| 860 | + } else { | |
| 861 | + $campaign->sending_params['segment'] = ['filters' => (object)$filters['filters']]; | |
| 862 | + } | |
| 863 | + | |
| 864 | + $campaign->sending_params['segment']['invert'] = acym_getVar('string', 'invert', 0); | |
| 865 | + | |
| 866 | + $campaignClass->save($campaign); | |
| 867 | + | |
| 868 | + $this->edit(); | |
| 869 | + } | |
| 870 | + | |
| 871 | + public function saveSendSettings(): void | |
| 872 | + { | |
| 873 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 874 | + | |
| 875 | + $campaignClass = new CampaignClass(); | |
| 876 | + $mailClass = new MailClass(); | |
| 877 | + $campaignId = acym_getVar('int', 'campaignId'); | |
| 878 | + | |
| 879 | + if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 880 | + die('Access denied for this campaign'); | |
| 881 | + } | |
| 882 | + | |
| 883 | + $senderInformation = acym_getVar('', 'senderInformation'); | |
| 884 | + $sendingDate = acym_getVar('string', 'sendingDate'); | |
| 885 | + $sendingType = acym_getVar('string', 'sending_type', CampaignClass::SENDING_TYPE_NOW); | |
| 886 | + $sendingParams = acym_getVar('array', 'sending_params', []); | |
| 887 | + $specificSendingParams = []; | |
| 888 | + $isScheduled = CampaignClass::SENDING_TYPE_SCHEDULED === $sendingType; | |
| 889 | + | |
| 890 | + $currentCampaign = $campaignClass->getOneById($campaignId); | |
| 891 | + | |
| 892 | + if (empty($currentCampaign)) { | |
| 893 | + acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_DOESNT_EXISTS'), 'error'); | |
| 894 | + | |
| 895 | + $this->listing(); | |
| 896 | + | |
| 897 | + return; | |
| 898 | + } | |
| 899 | + | |
| 900 | + if (CampaignClass::SENDING_TYPE_AUTO === $sendingType) { | |
| 901 | + $triggerType = acym_getVar('string', 'acym_triggers', ''); | |
| 902 | + if (empty($triggerType)) { | |
| 903 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 904 | + $this->listing(); | |
| 905 | + | |
| 906 | + return; | |
| 907 | + } | |
| 908 | + | |
| 909 | + $needConfirmToSend = acym_getVar('int', 'need_confirm', 0); | |
| 910 | + | |
| 911 | + $specificSendingParams = [ | |
| 912 | + $triggerType => acym_getVar('array', $triggerType, ''), | |
| 913 | + 'need_confirm_to_send' => $needConfirmToSend, | |
| 914 | + 'trigger_type' => $triggerType, | |
| 915 | + ]; | |
| 916 | + | |
| 917 | + $nextDate = acym_getVar('string', 'next_date', 0); | |
| 918 | + if (!empty($nextDate)) { | |
| 919 | + $currentCampaign->next_trigger = acym_getTime($nextDate); | |
| 920 | + } | |
| 921 | + | |
| 922 | + if (!empty($currentCampaign->sending_params['number_generated'])) { | |
| 923 | + $specificSendingParams['number_generated'] = $currentCampaign->sending_params['number_generated']; | |
| 924 | + } | |
| 925 | + | |
| 926 | + // Clean old settings saved in the campaign | |
| 927 | + $triggers = []; | |
| 928 | + $fakeSettings = []; | |
| 929 | + acym_trigger('onAcymDeclareTriggers', [&$triggers, &$fakeSettings], 'plgAcymTime'); | |
| 930 | + $settings = array_merge(array_keys($triggers['classic']), ['trigger_type', 'need_confirm_to_send']); | |
| 931 | + foreach ($settings as $oneSetting) { | |
| 932 | + unset($currentCampaign->sending_params[$oneSetting]); | |
| 933 | + } | |
| 934 | + } | |
| 935 | + | |
| 936 | + if (!empty($currentCampaign->sending_params['abtest']) && !empty($sendingParams['abtest'])) { | |
| 937 | + $sendingParams['abtest'] = array_merge($currentCampaign->sending_params['abtest'], $sendingParams['abtest']); | |
| 938 | + } | |
| 939 | + | |
| 940 | + // Handle special emails | |
| 941 | + if (!in_array($sendingType, CampaignClass::SENDING_TYPES)) { | |
| 942 | + $specialSendings = []; | |
| 943 | + acym_trigger('saveCampaignSpecificSendSettings', [$currentCampaign->sending_type, &$specialSendings]); | |
| 944 | + if (!empty($specialSendings)) { | |
| 945 | + $specificSendingParams = $specialSendings[0]; | |
| 946 | + } | |
| 947 | + } | |
| 948 | + | |
| 949 | + if (!empty($currentCampaign->mail_id)) { | |
| 950 | + $currentMail = $mailClass->getOneById($currentCampaign->mail_id); | |
| 951 | + } | |
| 952 | + | |
| 953 | + $currentCampaign->sending_type = $sendingType; | |
| 954 | + if (empty($currentCampaign->sending_params)) { | |
| 955 | + $currentCampaign->sending_params = []; | |
| 956 | + } | |
| 957 | + | |
| 958 | + $currentCampaign->sending_params = array_merge($currentCampaign->sending_params, $sendingParams, $specificSendingParams); | |
| 959 | + if (!empty($currentCampaign->sending_params['admin_notification_emails']) && empty($sendingParams['admin_notification_emails'])) { | |
| 960 | + $currentCampaign->sending_params['admin_notification_emails'] = []; | |
| 961 | + } | |
| 962 | + | |
| 963 | + if (empty($currentMail) || empty($senderInformation)) { | |
| 964 | + $this->listing(); | |
| 965 | + | |
| 966 | + return; | |
| 967 | + } | |
| 968 | + | |
| 969 | + $currentMail->from_name = $senderInformation['from_name']; | |
| 970 | + $currentMail->from_email = $senderInformation['from_email']; | |
| 971 | + $currentMail->reply_to_name = $senderInformation['reply_to_name']; | |
| 972 | + $currentMail->reply_to_email = $senderInformation['reply_to_email']; | |
| 973 | + $currentMail->bounce_email = $senderInformation['bounce_email']; | |
| 974 | + $currentMail->bcc = $senderInformation['bcc']; | |
| 975 | + $currentMail->tracking = $senderInformation['tracking']; | |
| 976 | + $currentMail->translation = empty($senderInformation['translation']) ? '' : $senderInformation['translation']; | |
| 977 | + if (isset($senderInformation['links_language'])) $currentMail->links_language = $senderInformation['links_language']; | |
| 978 | + | |
| 979 | + $mailClass->save($currentMail); | |
| 980 | + | |
| 981 | + if ($isScheduled && !empty($sendingDate)) { | |
| 982 | + $currentCampaign->sending_date = acym_date(acym_getTime($sendingDate), 'Y-m-d H:i:s', false); | |
| 983 | + if ($currentCampaign->sending_date < acym_date('now', 'Y-m-d H:i:s', false)) acym_enqueueMessage(acym_translation('ACYM_BE_CAREFUL_SENDING_DATE_IN_PAST'), 'warning'); | |
| 984 | + } | |
| 985 | + | |
| 986 | + $savedCampaignId = $campaignClass->save($currentCampaign); | |
| 987 | + if (!empty($savedCampaignId)) { | |
| 988 | + if (acym_getVar('string', 'nextstep', '') === 'listing') { | |
| 989 | + acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); | |
| 990 | + } | |
| 991 | + } else { | |
| 992 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 993 | + if (!empty($campaignClass->errors)) { | |
| 994 | + acym_enqueueMessage($campaignClass->errors, 'error'); | |
| 995 | + } | |
| 996 | + | |
| 997 | + $this->listing(); | |
| 998 | + | |
| 999 | + return; | |
| 1000 | + } | |
| 1001 | + | |
| 1002 | + $this->edit(); | |
| 1003 | + } | |
| 1004 | + | |
| 1005 | + /** | |
| 1006 | + * Needed for the steps system | |
| 1007 | + */ | |
| 1008 | + public function saveSummary(): void | |
| 1009 | + { | |
| 1010 | + $this->edit(); | |
| 1011 | + } | |
| 1012 | + | |
| 1013 | + public function summary(): void | |
| 1014 | + { | |
| 1015 | + acym_setVar('step', 'summary'); | |
| 1016 | + acym_setVar('layout', 'summary'); | |
| 1017 | + | |
| 1018 | + $data = [ | |
| 1019 | + 'mailClass' => new MailClass(), | |
| 1020 | + 'campaignClass' => new CampaignClass(), | |
| 1021 | + 'containerClass' => $this->stepContainerClass, | |
| 1022 | + ]; | |
| 1023 | + | |
| 1024 | + $this->prepareCurrentUserSummary($data); | |
| 1025 | + if (!$this->prepareCampaignSummary($data)) { | |
| 1026 | + acym_enqueueMessage(acym_translation('ACYM_CANT_GET_CAMPAIGN_INFORMATION'), 'error'); | |
| 1027 | + $this->listing(); | |
| 1028 | + | |
| 1029 | + return; | |
| 1030 | + } | |
| 1031 | + | |
| 1032 | + $data['mailInformation'] = $this->prepareMailDataSummary($data, (int)$data['campaignInformation']->mail_id); | |
| 1033 | + $this->prepareReceiversSummary($data); | |
| 1034 | + $this->prepareAbTest($data, false); | |
| 1035 | + $this->prepareAllMailsForAbtest($data); | |
| 1036 | + $this->prepareMultilingual($data, false); | |
| 1037 | + $this->prepareAllMailsForMultilingual($data); | |
| 1038 | + $this->prepareListingClasses($data); | |
| 1039 | + $this->prepareSegmentData($data); | |
| 1040 | + $this->prepareSegmentDisplay($data, $data['campaignInformation']->sending_params); | |
| 1041 | + | |
| 1042 | + $this->breadcrumb[$data['campaignInformation']->name] = acym_completeLink('campaigns&task=edit&step=summary&campaignId='.$data['campaignInformation']->id); | |
| 1043 | + parent::display($data); | |
| 1044 | + } | |
| 1045 | + | |
| 1046 | + private function prepareSegmentData(array &$data): void | |
| 1047 | + { | |
| 1048 | + if (empty($data['campaignInformation']->sending_params['segment'])) { | |
| 1049 | + return; | |
| 1050 | + } | |
| 1051 | + | |
| 1052 | + $segmentParams = $data['campaignInformation']->sending_params['segment']; | |
| 1053 | + | |
| 1054 | + $isExcluded = !empty($segmentParams['invert']) && $segmentParams['invert'] === 'exclude'; | |
| 1055 | + | |
| 1056 | + $segmentController = new SegmentsController(); | |
| 1057 | + | |
| 1058 | + if (!empty($segmentParams['segment_id'])) { | |
| 1059 | + $segmentClass = new SegmentClass(); | |
| 1060 | + $segment = $segmentClass->getOneById($segmentParams['segment_id']); | |
| 1061 | + | |
| 1062 | + $data['segment'] = [ | |
| 1063 | + 'name' => $segment->name, | |
| 1064 | + 'count' => $segmentController->countSegmentById((int)$segment->id, $data['listsIds'], false, $isExcluded), | |
| 1065 | + ]; | |
| 1066 | + } else { | |
| 1067 | + $data['segment'] = [ | |
| 1068 | + 'name' => acym_translation('ACYM_YOUR_CUSTOM_SEGMENT'), | |
| 1069 | + 'count' => $segmentController->countSegmentByParams($segmentParams, $data['listsIds'], $isExcluded), | |
| 1070 | + ]; | |
| 1071 | + } | |
| 1072 | + } | |
| 1073 | + | |
| 1074 | + private function prepareCurrentUserSummary(array &$data): void | |
| 1075 | + { | |
| 1076 | + $userClass = new UserClass(); | |
| 1077 | + $currentUserEmail = acym_currentUserEmail(); | |
| 1078 | + $data['receiver'] = $userClass->getOneByEmail($currentUserEmail); | |
| 1079 | + if (empty($data['receiver'])) { | |
| 1080 | + $receiver = new stdClass(); | |
| 1081 | + $receiver->email = $currentUserEmail; | |
| 1082 | + $newID = $userClass->save($receiver); | |
| 1083 | + if (!empty($newID)) { | |
| 1084 | + $data['receiver'] = $userClass->getOneById($newID); | |
| 1085 | + } | |
| 1086 | + } | |
| 1087 | + } | |
| 1088 | + | |
| 1089 | + private function prepareCampaignSummary(array &$data): bool | |
| 1090 | + { | |
| 1091 | + $campaignId = acym_getVar('int', 'campaignId'); | |
| 1092 | + $campaignClass = new CampaignClass(); | |
| 1093 | + $campaign = empty($campaignId) ? null : $campaignClass->getOneByIdWithMail($campaignId); | |
| 1094 | + if (is_null($campaign)) { | |
| 1095 | + return false; | |
| 1096 | + } | |
| 1097 | + | |
| 1098 | + if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 1099 | + die('Access denied for this campaign'); | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + $campaign->isAuto = $campaign->sending_type === CampaignClass::SENDING_TYPE_AUTO; | |
| 1103 | + | |
| 1104 | + $nextDate = ''; | |
| 1105 | + if ($campaign->isAuto) { | |
| 1106 | + $textToDisplay = new stdClass(); | |
| 1107 | + $textToDisplay->triggers = $campaign->sending_params; | |
| 1108 | + acym_trigger('onAcymDeclareSummary_triggers', [&$textToDisplay], 'plgAcymTime'); | |
| 1109 | + $textToDisplay = $textToDisplay->triggers; | |
| 1110 | + if (!empty($campaign->next_trigger)) { | |
| 1111 | + $nextDate = $campaign->next_trigger; | |
| 1112 | + } elseif (!empty($campaign->sending_params['start_date'])) { | |
| 1113 | + $nextDate = $campaign->sending_params['start_date']; | |
| 1114 | + } | |
| 1115 | + } | |
| 1116 | + | |
| 1117 | + $data['automatic'] = [ | |
| 1118 | + 'isAuto' => $campaign->isAuto, | |
| 1119 | + 'text' => empty($textToDisplay) | |
| 1120 | + ? '' | |
| 1121 | + : acym_translation('ACYM_THIS_WILL_GENERATE_CAMPAIGN_AUTOMATICALLY').' '.acym_strtolower( | |
| 1122 | + $textToDisplay[$textToDisplay['trigger_type']] | |
| 1123 | + ), | |
| 1124 | + 'nextDate' => $nextDate, | |
| 1125 | + ]; | |
| 1126 | + $data['campaignInformation'] = $campaign; | |
| 1127 | + $data['mailId'] = $campaign->mail_id; | |
| 1128 | + | |
| 1129 | + acym_trigger('onAcymCampaignSummary', [&$data]); | |
| 1130 | + | |
| 1131 | + return true; | |
| 1132 | + } | |
| 1133 | + | |
| 1134 | + private function prepareMailDataSummary(array &$data, int $mailId): object | |
| 1135 | + { | |
| 1136 | + $mailArchiveClass = new MailArchiveClass(); | |
| 1137 | + $data['isArchiveCached'] = !empty($mailArchiveClass->getOneByMailId($mailId)); | |
| 1138 | + | |
| 1139 | + $mailData = $data['mailClass']->getOneById($mailId); | |
| 1140 | + $mailData->from_name = empty($mailData->from_name) ? $this->config->get('from_name') : $mailData->from_name; | |
| 1141 | + $mailData->from_email = empty($mailData->from_email) ? $this->config->get('from_email') : $mailData->from_email; | |
| 1142 | + | |
| 1143 | + $useFromInReply = $this->config->get('from_as_replyto'); | |
| 1144 | + $replytoName = $this->config->get('replyto_name'); | |
| 1145 | + $replytoEmail = $this->config->get('replyto_email'); | |
| 1146 | + | |
| 1147 | + if (!empty($mailData->reply_to_name)) { | |
| 1148 | + $replytoName = $mailData->reply_to_name; | |
| 1149 | + } elseif ($useFromInReply != 0 || empty($replytoName)) { | |
| 1150 | + $replytoName = $this->config->get('from_name'); | |
| 1151 | + } | |
| 1152 | + | |
| 1153 | + if (!empty($mailData->reply_to_email)) { | |
| 1154 | + $replytoEmail = $mailData->reply_to_email; | |
| 1155 | + } elseif ($useFromInReply != 0 || empty($replytoEmail)) { | |
| 1156 | + $replytoEmail = $this->config->get('from_email'); | |
| 1157 | + } | |
| 1158 | + | |
| 1159 | + $mailData->reply_to_name = $replytoName; | |
| 1160 | + $mailData->reply_to_email = $replytoEmail; | |
| 1161 | + | |
| 1162 | + acym_trigger('replaceContent', [&$mailData, false]); | |
| 1163 | + acym_trigger('replaceUserInformation', [&$mailData, &$data['receiver'], false]); | |
| 1164 | + | |
| 1165 | + $editorHelper = new EditorHelper(); | |
| 1166 | + $mailData->settings = empty($mailData->settings) ? [] : json_decode($mailData->settings, true); | |
| 1167 | + $mailData->stylesheet .= $editorHelper->getSettingsStyle($mailData->settings); | |
| 1168 | + | |
| 1169 | + return $mailData; | |
| 1170 | + } | |
| 1171 | + | |
| 1172 | + protected function prepareReceiversSummary(array &$data): void | |
| 1173 | + { | |
| 1174 | + $nbSubscribers = 0; | |
| 1175 | + $campaignLists = $data['mailClass']->getAllListsWithCountSubscribersByMailIds([$data['campaignInformation']->mail_id]); | |
| 1176 | + $listsIds = []; | |
| 1177 | + | |
| 1178 | + if (!empty($campaignLists)) { | |
| 1179 | + foreach ($campaignLists as $oneList) { | |
| 1180 | + $listsIds[] = $oneList->list_id; | |
| 1181 | + } | |
| 1182 | + | |
| 1183 | + if (empty($data['campaignInformation']->sending_params)) { | |
| 1184 | + // No segment saved, get subscribers count the easy way | |
| 1185 | + $listClass = new ListClass(); | |
| 1186 | + $nbSubscribers = $listClass->getSubscribersCount($listsIds); | |
| 1187 | + } else { | |
| 1188 | + // There may be segments | |
| 1189 | + $campaignClass = new CampaignClass(); | |
| 1190 | + $nbSubscribers = $campaignClass->countUsersCampaign($data['campaignInformation']->id, true); | |
| 1191 | + } | |
| 1192 | + } | |
| 1193 | + | |
| 1194 | + $data['listsReceiver'] = $campaignLists; | |
| 1195 | + $data['listsIds'] = $listsIds; | |
| 1196 | + $data['nbSubscribers'] = $nbSubscribers; | |
| 1197 | + | |
| 1198 | + // Campaign already sent, calculate the number of new receivers | |
| 1199 | + if (!empty($data['campaignInformation']->sent) && !empty($data['campaignInformation']->active)) { | |
| 1200 | + $queueClass = new QueueClass(); | |
| 1201 | + $data['mailInformation']->sending_params = $data['campaignInformation']->sending_params; | |
| 1202 | + | |
| 1203 | + $automationHelper = $queueClass->getMailReceivers($data['mailInformation'], true); | |
| 1204 | + $data['receiversNew'] = acym_loadResult($automationHelper->getQuery(['COUNT(DISTINCT `user`.id)'])); | |
| 1205 | + $automationHelper->removeFlag(SegmentsController::FLAG_COUNT); | |
| 1206 | + } | |
| 1207 | + } | |
| 1208 | + | |
| 1209 | + public function tests(): void | |
| 1210 | + { | |
| 1211 | + $campaignClass = new CampaignClass(); | |
| 1212 | + acym_setVar('step', 'tests'); | |
| 1213 | + acym_setVar('layout', 'tests'); | |
| 1214 | + $campaignId = acym_getVar('int', 'campaignId', 0); | |
| 1215 | + | |
| 1216 | + $campaign = $campaignClass->getOneByIdWithMail($campaignId); | |
| 1217 | + | |
| 1218 | + if (empty($campaign->id)) { | |
| 1219 | + acym_enqueueMessage(acym_translation('ACYM_CANT_GET_CAMPAIGN_INFORMATION'), 'error'); | |
| 1220 | + $this->listing(); | |
| 1221 | + | |
| 1222 | + return; | |
| 1223 | + } | |
| 1224 | + | |
| 1225 | + $testEmails = acym_getVar('array', 'test_emails', [acym_currentUserEmail()]); | |
| 1226 | + foreach ($testEmails as $oneEmail) { | |
| 1227 | + $defaultEmails[$oneEmail] = $oneEmail; | |
| 1228 | + } | |
| 1229 | + | |
| 1230 | + $mailClass = new MailClass(); | |
| 1231 | + $emailsToTest = $mailClass->getParentAndChildMails($campaign->mail_id); | |
| 1232 | + | |
| 1233 | + $isDemo = false; | |
| 1234 | + | |
| 1235 | + $data = [ | |
| 1236 | + 'id' => $campaign->id, | |
| 1237 | + 'currentCampaign' => $campaign, | |
| 1238 | + 'test_emails' => $defaultEmails, | |
| 1239 | + 'upgrade' => !acym_level(ACYM_ESSENTIAL), | |
| 1240 | + 'version' => 'enterprise', | |
| 1241 | + 'emails_to_test' => $emailsToTest, | |
| 1242 | + 'isDemo' => $isDemo, | |
| 1243 | + ]; | |
| 1244 | + if (!acym_isAcyCheckerInstalled()) { | |
| 1245 | + $lists = $campaignClass->getListsByMailId($campaign->mail_id); | |
| 1246 | + $listClass = new ListClass(); | |
| 1247 | + $data['recipients'] = $listClass->getTotalSubCount($lists); | |
| 1248 | + } | |
| 1249 | + | |
| 1250 | + $this->prepareListingClasses($data); | |
| 1251 | + $this->prepareSegmentDisplay($data, $campaign->sending_params); | |
| 1252 | + | |
| 1253 | + $this->breadcrumb[$campaign->name] = acym_completeLink('campaigns&task=edit&step=tests&campaignId='.$campaign->id); | |
| 1254 | + parent::display($data); | |
| 1255 | + } | |
| 1256 | + | |
| 1257 | + public function saveTests(): void | |
| 1258 | + { | |
| 1259 | + if (!acym_isAdmin() || !acym_isAllowed('campaigns')) { | |
| 1260 | + die('Access denied for tests step'); | |
| 1261 | + } | |
| 1262 | + | |
| 1263 | + $this->edit(); | |
| 1264 | + } | |
| 1265 | +} | |