| @@ -1,0 +1,142 @@ | ||
| 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\StepClass; | |
| 9 | +use AcyMailing\Helpers\WorkflowHelper; | |
| 10 | + | |
| 11 | +trait Info | |
| 12 | +{ | |
| 13 | + public function info(): void | |
| 14 | + { | |
| 15 | + if (!acym_level(ACYM_ENTERPRISE)) { | |
| 16 | + acym_redirect(acym_completeLink('dashboard&task=upgrade&version=enterprise', false, true)); | |
| 17 | + } | |
| 18 | + | |
| 19 | + } | |
| 20 | + | |
| 21 | + private function saveInfos(): array | |
| 22 | + { | |
| 23 | + $automationId = acym_getVar('int', 'id'); | |
| 24 | + $automation = acym_getVar('array', 'automation'); | |
| 25 | + $automationClass = new AutomationClass(); | |
| 26 | + | |
| 27 | + $stepAutomationId = acym_getVar('int', 'stepAutomationId'); | |
| 28 | + $stepAutomation = acym_getVar('array', 'stepAutomation'); | |
| 29 | + $typeTrigger = acym_getVar('string', 'type_trigger'); | |
| 30 | + $stepClass = new StepClass(); | |
| 31 | + | |
| 32 | + if (!empty($automationId)) { | |
| 33 | + $automation['id'] = $automationId; | |
| 34 | + } | |
| 35 | + | |
| 36 | + // if the automation already exists | |
| 37 | + if (!empty($stepAutomationId)) { | |
| 38 | + $stepAutomation['id'] = $stepAutomationId; | |
| 39 | + $conditionClass = new ConditionClass(); | |
| 40 | + $actionClass = new ActionClass(); | |
| 41 | + $condition = $conditionClass->getOneByStepId($stepAutomationId); | |
| 42 | + $step = $stepClass->getOneById($stepAutomationId); | |
| 43 | + | |
| 44 | + // and the type_trigger has changed from "user" to "classic" | |
| 45 | + $triggerChanged = $typeTrigger === 'classic' && json_decode($step->triggers)->type_trigger === 'user'; | |
| 46 | + if ($triggerChanged && !empty($condition)) { | |
| 47 | + $action = $actionClass->getOneByConditionId($condition->id); | |
| 48 | + // if conditions are set on "user", we reset conditions | |
| 49 | + if (json_decode($condition->conditions)->type_condition === 'user') { | |
| 50 | + $condition->conditions = null; | |
| 51 | + $conditionClass->save($condition); | |
| 52 | + } | |
| 53 | + // if actions filters are set on "user", we reset actions filters | |
| 54 | + if (!empty($action->filters) && json_decode($action->filters)->type_filter === 'user') { | |
| 55 | + $action->filters = null; | |
| 56 | + $actionClass->save($action); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + if (empty($automation['admin'])) { | |
| 62 | + if (empty($automation['name'])) { | |
| 63 | + return []; | |
| 64 | + } | |
| 65 | + | |
| 66 | + $automation['admin'] = 0; | |
| 67 | + } | |
| 68 | + | |
| 69 | + if (empty($stepAutomation['triggers'][$typeTrigger])) { | |
| 70 | + acym_enqueueMessage(acym_translation('ACYM_PLEASE_SELECT_ONE_TRIGGER'), 'error'); | |
| 71 | + | |
| 72 | + $this->info(); | |
| 73 | + | |
| 74 | + return []; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $stepAutomation['triggers'][$typeTrigger]['type_trigger'] = $typeTrigger; | |
| 78 | + $stepAutomation['triggers'] = json_encode($stepAutomation['triggers'][$typeTrigger]); | |
| 79 | + | |
| 80 | + $stepAutomation['automation_id'] = $automationId; | |
| 81 | + | |
| 82 | + foreach ($automation as $column => $value) { | |
| 83 | + acym_secureDBColumn($column); | |
| 84 | + } | |
| 85 | + | |
| 86 | + foreach ($stepAutomation as $stepColumn => $stepValue) { | |
| 87 | + acym_secureDBColumn($stepColumn); | |
| 88 | + } | |
| 89 | + | |
| 90 | + //We need objects to save it so we make objects | |
| 91 | + $automation = (object)$automation; | |
| 92 | + $stepAutomation = (object)$stepAutomation; | |
| 93 | + | |
| 94 | + $automation->id = $automationClass->save($automation); | |
| 95 | + $stepAutomation->automation_id = $automation->id; | |
| 96 | + $stepAutomation->id = $stepClass->save($stepAutomation); | |
| 97 | + | |
| 98 | + $returnIds = [ | |
| 99 | + 'automationId' => $automation->id, | |
| 100 | + 'stepId' => $stepAutomation->id, | |
| 101 | + 'typeTrigger' => $typeTrigger, | |
| 102 | + ]; | |
| 103 | + | |
| 104 | + if (!empty($returnIds['automationId']) && !empty($returnIds['stepId'])) { | |
| 105 | + return $returnIds; | |
| 106 | + } else { | |
| 107 | + return []; | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + public function saveExitInfo(): void | |
| 112 | + { | |
| 113 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 114 | + | |
| 115 | + $ids = $this->saveInfos(); | |
| 116 | + | |
| 117 | + if (empty($ids)) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + | |
| 121 | + acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); | |
| 122 | + | |
| 123 | + acym_setVar('id', $ids['automationId']); | |
| 124 | + acym_setVar('stepId', $ids['stepId']); | |
| 125 | + $this->listing(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + public function saveInfo(): void | |
| 129 | + { | |
| 130 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 131 | + | |
| 132 | + $ids = $this->saveInfos(); | |
| 133 | + | |
| 134 | + if (empty($ids)) { | |
| 135 | + return; | |
| 136 | + } | |
| 137 | + | |
| 138 | + acym_setVar('id', $ids['automationId']); | |
| 139 | + acym_setVar('stepId', $ids['stepId']); | |
| 140 | + $this->condition(); | |
| 141 | + } | |
| 142 | +} | |