| @@ -1,155 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace AcyMailing\Classes; | |
| 4 | - | |
| 5 | -use AcyMailing\Core\AcymClass; | |
| 6 | - | |
| 7 | -class ScenarioStepClass extends AcymClass | |
| 8 | -{ | |
| 9 | - public function __construct() | |
| 10 | - { | |
| 11 | - parent::__construct(); | |
| 12 | - | |
| 13 | - $this->table = 'scenario_step'; | |
| 14 | - $this->pkey = 'id'; | |
| 15 | - } | |
| 16 | - | |
| 17 | - public function save($element) | |
| 18 | - { | |
| 19 | - if (!empty($element->params) && !is_string($element->params)) { | |
| 20 | - $element->params = json_encode($element->params); | |
| 21 | - } | |
| 22 | - | |
| 23 | - $test = $this->getOneById($element->id); | |
| 24 | - $this->forceInsert = empty($test); | |
| 25 | - $id = parent::save($element); | |
| 26 | - $this->forceInsert = false; | |
| 27 | - | |
| 28 | - return $id; | |
| 29 | - } | |
| 30 | - | |
| 31 | - public function getOneById($id) | |
| 32 | - { | |
| 33 | - $scenarioStep = acym_loadObject('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE `'.acym_secureDBColumn($this->pkey).'` = '.acym_escapeDB($id)); | |
| 34 | - | |
| 35 | - if (!empty($scenarioStep->params)) { | |
| 36 | - $scenarioStep->params = json_decode($scenarioStep->params); | |
| 37 | - } | |
| 38 | - | |
| 39 | - return $scenarioStep; | |
| 40 | - } | |
| 41 | - | |
| 42 | - public function getAllByScenarioId(int $scenarioId): array | |
| 43 | - { | |
| 44 | - $steps = array_map([$this, 'formatStep'], acym_loadObjectList('SELECT * FROM #__acym_scenario_step WHERE scenario_id = '.$scenarioId, 'id')); | |
| 45 | - | |
| 46 | - usort($steps, function ($a, $b) { | |
| 47 | - if ($a->previous_id === $b->id) { | |
| 48 | - return 1; | |
| 49 | - } | |
| 50 | - | |
| 51 | - if ($b->previous_id === $a->id) { | |
| 52 | - return -1; | |
| 53 | - } | |
| 54 | - | |
| 55 | - return 0; | |
| 56 | - }); | |
| 57 | - | |
| 58 | - | |
| 59 | - return $steps; | |
| 60 | - } | |
| 61 | - | |
| 62 | - public function getFirstStepByScenarioId(int $scenarioId) | |
| 63 | - { | |
| 64 | - return $this->formatStep(acym_loadObject('SELECT * FROM #__acym_scenario_step WHERE previous_id IS NULL AND scenario_id = '.$scenarioId)); | |
| 65 | - } | |
| 66 | - | |
| 67 | - public function getStepByPreviousStepId(string $parentId) | |
| 68 | - { | |
| 69 | - return $this->formatStep(acym_loadObject('SELECT * FROM #__acym_scenario_step WHERE previous_id = '.acym_escapeDB($parentId))); | |
| 70 | - } | |
| 71 | - | |
| 72 | - public function getAllStepsToDelete(int $scenarioId, array $stepIdsToKeep): array | |
| 73 | - { | |
| 74 | - $stepIdsToKeepEscaped = []; | |
| 75 | - foreach ($stepIdsToKeep as $stepIdToKeep) { | |
| 76 | - $stepIdsToKeepEscaped[] = acym_escapeDB($stepIdToKeep); | |
| 77 | - } | |
| 78 | - | |
| 79 | - $query = 'SELECT id FROM #__acym_scenario_step WHERE scenario_id = '.$scenarioId; | |
| 80 | - | |
| 81 | - if (!empty($stepIdsToKeepEscaped)) { | |
| 82 | - $query .= ' AND id NOT IN ('.implode(',', $stepIdsToKeepEscaped).')'; | |
| 83 | - } | |
| 84 | - | |
| 85 | - return acym_loadResultArray($query); | |
| 86 | - } | |
| 87 | - | |
| 88 | - private function formatStep($step) | |
| 89 | - { | |
| 90 | - if (empty($step)) { | |
| 91 | - return null; | |
| 92 | - } | |
| 93 | - | |
| 94 | - $step->params = json_decode($step->params, true); | |
| 95 | - | |
| 96 | - return $step; | |
| 97 | - } | |
| 98 | - | |
| 99 | - public function getStepByPreviousConditionId(string $parentId, bool $conditionValid) | |
| 100 | - { | |
| 101 | - $conditionValidValue = $conditionValid ? 1 : 0; | |
| 102 | - | |
| 103 | - $query = 'SELECT * FROM #__acym_scenario_step WHERE condition_valid = '.$conditionValidValue.' AND previous_id = '.acym_escapeDB($parentId); | |
| 104 | - | |
| 105 | - return $this->formatStep(acym_loadObject($query)); | |
| 106 | - } | |
| 107 | - | |
| 108 | - public function getAvailableStepsByDate(string $date): array | |
| 109 | - { | |
| 110 | - $query = 'SELECT scenario_step.*, scenario_process.user_id, scenario_process.id AS scenario_process_id | |
| 111 | - FROM #__acym_scenario_step AS scenario_step | |
| 112 | - JOIN #__acym_scenario_queue AS scenario_queue ON scenario_step.id = scenario_queue.step_id AND scenario_queue.execution_date <= '.acym_escapeDB($date).' | |
| 113 | - JOIN #__acym_scenario_process AS scenario_process ON scenario_queue.scenario_process_id = scenario_process.id'; | |
| 114 | - | |
| 115 | - return array_map([$this, 'formatStep'], acym_loadObjectList($query)); | |
| 116 | - } | |
| 117 | - | |
| 118 | - public function getNumberOfProcessByStepByScenarioId(int $scenarioId) | |
| 119 | - { | |
| 120 | - $query = 'SELECT scenario_history.scenario_step_id , COUNT(scenario_history.id) AS count | |
| 121 | - FROM #__acym_scenario_step AS scenario_step | |
| 122 | - JOIN #__acym_scenario_history_line AS scenario_history ON scenario_step.id = scenario_history.scenario_step_id AND scenario_history.scenario_step_id IS NOT NULL | |
| 123 | - WHERE scenario_step.scenario_id = '.$scenarioId.' | |
| 124 | - GROUP BY scenario_step_id'; | |
| 125 | - | |
| 126 | - return acym_loadObjectList($query, 'scenario_step_id'); | |
| 127 | - } | |
| 128 | - | |
| 129 | - public function getAllStepIds(): array | |
| 130 | - { | |
| 131 | - return acym_loadResultArray('SELECT id FROM #__acym_scenario_step'); | |
| 132 | - } | |
| 133 | - | |
| 134 | - public function generateNewStepId(array $allStepIds = []): string | |
| 135 | - { | |
| 136 | - if (empty($allStepIds)) { | |
| 137 | - $allStepIds = $this->getAllStepIds(); | |
| 138 | - } | |
| 139 | - | |
| 140 | - $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | |
| 141 | - $randomString = ''; | |
| 142 | - $length = 24; | |
| 143 | - | |
| 144 | - for ($i = 0 ; $i < $length ; $i++) { | |
| 145 | - $randomIndex = rand(0, strlen($letters) - 1); | |
| 146 | - $randomString .= $letters[$randomIndex]; | |
| 147 | - } | |
| 148 | - | |
| 149 | - if (in_array($randomString, $allStepIds)) { | |
| 150 | - return $this->generateNewStepId($allStepIds); | |
| 151 | - } | |
| 152 | - | |
| 153 | - return $randomString; | |
| 154 | - } | |
| 155 | -} | |