| @@ -1,0 +1,92 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Classes; | |
| 4 | + | |
| 5 | +use AcyMailing\Helpers\PaginationHelper; | |
| 6 | +use AcyMailing\Core\AcymClass; | |
| 7 | + | |
| 8 | +class ScenarioProcessClass extends AcymClass | |
| 9 | +{ | |
| 10 | + public function __construct() | |
| 11 | + { | |
| 12 | + parent::__construct(); | |
| 13 | + | |
| 14 | + $this->table = 'scenario_process'; | |
| 15 | + $this->pkey = 'id'; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public function hasUserProcess(int $scenarioId, int $userId): bool | |
| 19 | + { | |
| 20 | + $process = acym_loadObject('SELECT * FROM #__acym_scenario_process WHERE scenario_id = '.$scenarioId.' AND user_id = '.$userId); | |
| 21 | + | |
| 22 | + return !empty($process); | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function endProcess(int $scenarioProcessId): void | |
| 26 | + { | |
| 27 | + $scenarioProcess = new \stdClass(); | |
| 28 | + $scenarioProcess->id = $scenarioProcessId; | |
| 29 | + $scenarioProcess->end_at = acym_date('now', 'Y-m-d H:i:s', false); | |
| 30 | + | |
| 31 | + $this->save($scenarioProcess); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public function getNumberOfTriggerByScenarioId(int $scenarioId): int | |
| 35 | + { | |
| 36 | + $numberOfTrigger = acym_loadResult('SELECT COUNT(*) FROM #__acym_scenario_process WHERE scenario_id = '.$scenarioId); | |
| 37 | + | |
| 38 | + if (empty($numberOfTrigger)) { | |
| 39 | + return 0; | |
| 40 | + } | |
| 41 | + | |
| 42 | + return (int)$numberOfTrigger; | |
| 43 | + } | |
| 44 | + | |
| 45 | + public function getMatchingElements(array $settings = []): array | |
| 46 | + { | |
| 47 | + $results = []; | |
| 48 | + | |
| 49 | + $query = 'SELECT DISTINCT scenario_process.*, user.email FROM #__acym_scenario_process AS `scenario_process`'; | |
| 50 | + $queryCount = 'SELECT COUNT(DISTINCT scenario_process.id) AS total FROM #__acym_scenario_process AS scenario_process'; | |
| 51 | + $filters = []; | |
| 52 | + | |
| 53 | + $joins = [ | |
| 54 | + 'JOIN #__acym_user AS user ON user.id = scenario_process.user_id', | |
| 55 | + ]; | |
| 56 | + | |
| 57 | + if (!empty($joins)) { | |
| 58 | + $query .= ' '.implode(' ', $joins); | |
| 59 | + $queryCount .= ' '.implode(' ', $joins); | |
| 60 | + } | |
| 61 | + | |
| 62 | + if (!empty($settings['scenarioId'])) { | |
| 63 | + $filters[] = 'scenario_process.scenario_id = '.intval($settings['scenarioId']); | |
| 64 | + | |
| 65 | + $results['totalOverall'] = acym_loadResult('SELECT COUNT(*) FROM #__acym_scenario_process WHERE scenario_id = '.intval($settings['scenarioId'])); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if (!empty($settings['search'])) { | |
| 69 | + $searchValue = acym_escapeDB('%'.$settings['search'].'%'); | |
| 70 | + $filters[] = 'user.name LIKE '.$searchValue.' OR user.email LIKE '.$searchValue; | |
| 71 | + } | |
| 72 | + | |
| 73 | + if (!empty($filters)) { | |
| 74 | + $query .= ' WHERE ('.implode(') AND (', $filters).')'; | |
| 75 | + $queryCount .= ' WHERE ('.implode(') AND (', $filters).')'; | |
| 76 | + } | |
| 77 | + | |
| 78 | + if (empty($settings['offset']) || $settings['offset'] < 0) { | |
| 79 | + $settings['offset'] = 0; | |
| 80 | + } | |
| 81 | + | |
| 82 | + if (empty($settings['elementsPerPage']) || $settings['elementsPerPage'] < 1) { | |
| 83 | + $pagination = new PaginationHelper(); | |
| 84 | + $settings['elementsPerPage'] = $pagination->getListLimit(); | |
| 85 | + } | |
| 86 | + | |
| 87 | + $results['elements'] = acym_loadObjectList($query, 'id', $settings['offset'], $settings['elementsPerPage']); | |
| 88 | + $results['total'] = acym_loadObject($queryCount); | |
| 89 | + | |
| 90 | + return $results; | |
| 91 | + } | |
| 92 | +} | |