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