| @@ -1,0 +1,138 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Automations; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\ActionClass; | |
| 6 | +use AcyMailing\Classes\AutomationClass; | |
| 7 | +use AcyMailing\Classes\ConditionClass; | |
| 8 | +use AcyMailing\Classes\MailClass; | |
| 9 | +use AcyMailing\Classes\StepClass; | |
| 10 | +use AcyMailing\Helpers\PaginationHelper; | |
| 11 | +use AcyMailing\Helpers\ToolbarHelper; | |
| 12 | + | |
| 13 | +trait Listing | |
| 14 | +{ | |
| 15 | + public function listing(): void | |
| 16 | + { | |
| 17 | + if (!acym_level(ACYM_ENTERPRISE)) { | |
| 18 | + acym_redirect(acym_completeLink('dashboard&task=upgrade&version=enterprise', false, true)); | |
| 19 | + } | |
| 20 | + | |
| 21 | + } | |
| 22 | + | |
| 23 | + public function prepareToolbar(array &$data): void | |
| 24 | + { | |
| 25 | + $toolbarHelper = new ToolbarHelper(); | |
| 26 | + $toolbarHelper->addSearchBar($data['search'], 'automation_search', 'ACYM_SEARCH'); | |
| 27 | + $toolbarHelper->addButton(acym_translation('ACYM_NEW_MASS_ACTION'), ['data-task' => 'edit', 'data-step' => 'action'], 'cog'); | |
| 28 | + $toolbarHelper->addButton(acym_translation('ACYM_CREATE'), ['data-task' => 'edit', 'data-step' => 'info'], 'add', true); | |
| 29 | + | |
| 30 | + $data['toolbar'] = $toolbarHelper; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public function duplicate(): void | |
| 34 | + { | |
| 35 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 36 | + | |
| 37 | + $automations = acym_getVar('array', 'elements_checked', []); | |
| 38 | + | |
| 39 | + if (empty($automations)) { | |
| 40 | + $this->listing(); | |
| 41 | + | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + | |
| 45 | + $automationClass = new AutomationClass(); | |
| 46 | + $stepClass = new StepClass(); | |
| 47 | + $conditionClass = new ConditionClass(); | |
| 48 | + $actionClass = new ActionClass(); | |
| 49 | + | |
| 50 | + foreach ($automations as $automationId) { | |
| 51 | + $automation = $automationClass->getOneById($automationId); | |
| 52 | + $step = $stepClass->getOneStepByAutomationId($automation->id); | |
| 53 | + $condition = $conditionClass->getOneByStepId($step->id); | |
| 54 | + | |
| 55 | + unset($automation->id); | |
| 56 | + unset($step->id); | |
| 57 | + | |
| 58 | + $automation->active = 0; | |
| 59 | + $automation->name .= '_copy'; | |
| 60 | + | |
| 61 | + $step->automation_id = $automationClass->save($automation); | |
| 62 | + $step->last_execution = ''; | |
| 63 | + $step->next_execution = ''; | |
| 64 | + | |
| 65 | + $newStepId = $stepClass->save($step); | |
| 66 | + | |
| 67 | + if (!empty($condition)) { | |
| 68 | + $action = $actionClass->getOneByConditionId($condition->id); | |
| 69 | + | |
| 70 | + unset($condition->id); | |
| 71 | + $condition->step_id = $newStepId; | |
| 72 | + $newConditionId = $conditionClass->save($condition); | |
| 73 | + | |
| 74 | + if (!empty($action)) { | |
| 75 | + unset($action->id); | |
| 76 | + $action->condition_id = $newConditionId; | |
| 77 | + if (!empty($action->actions) && strpos($action->actions, 'acy_add_queue') !== false) { | |
| 78 | + $action->actions = json_decode($action->actions, true); | |
| 79 | + $mailClass = new MailClass(); | |
| 80 | + foreach ($action->actions as & $oneAction) { | |
| 81 | + if (!empty($oneAction['acy_add_queue']['mail_id'])) { | |
| 82 | + $newMail = $mailClass->duplicateMail($oneAction['acy_add_queue']['mail_id'], MailClass::TYPE_AUTOMATION); | |
| 83 | + if (!empty($newMail)) { | |
| 84 | + $oneAction['acy_add_queue']['mail_id'] = $newMail->id; | |
| 85 | + } | |
| 86 | + } | |
| 87 | + } | |
| 88 | + $action->actions = json_encode($action->actions); | |
| 89 | + } | |
| 90 | + $actionClass->save($action); | |
| 91 | + } | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + $this->listing(); | |
| 96 | + } | |
| 97 | + | |
| 98 | + public function triggerAutomation(): void | |
| 99 | + { | |
| 100 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 101 | + | |
| 102 | + $automationIds = acym_getVar('array', 'elements_checked', []); | |
| 103 | + | |
| 104 | + if (empty($automationIds)) { | |
| 105 | + $this->listing(); | |
| 106 | + | |
| 107 | + return; | |
| 108 | + } | |
| 109 | + | |
| 110 | + $automationClass = new AutomationClass(); | |
| 111 | + $result = $automationClass->triggerManual($automationIds); | |
| 112 | + | |
| 113 | + if ($result['executed'] > 0) { | |
| 114 | + acym_enqueueMessage(acym_translationSprintf('ACYM_X_AUTOMATIONS_TRIGGERED', $result['executed']), 'info'); | |
| 115 | + } | |
| 116 | + | |
| 117 | + if ($result['skipped'] > 0) { | |
| 118 | + acym_enqueueMessage(acym_translationSprintf('ACYM_X_SKIPPED_USER_AUTOMATIONS', $result['skipped']), 'warning'); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $this->listing(); | |
| 122 | + } | |
| 123 | + | |
| 124 | + public function ajaxSetOrdering(): void | |
| 125 | + { | |
| 126 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 127 | + | |
| 128 | + $order = json_decode(acym_getVar('string', 'order') ?? '[]', true); | |
| 129 | + | |
| 130 | + $error = false; | |
| 131 | + foreach ($order as $index => $automationId) { | |
| 132 | + $query = 'UPDATE #__acym_automation SET `ordering` = '.intval($index + 1).' WHERE `id` = '.intval($automationId); | |
| 133 | + $error = acym_query($query) < 0 || $error; | |
| 134 | + } | |
| 135 | + | |
| 136 | + acym_sendAjaxResponse('', [], !$error); | |
| 137 | + } | |
| 138 | +} | |