| @@ -1,0 +1,107 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers; | |
| 4 | + | |
| 5 | +use AcyMailing\Core\AcymController; | |
| 6 | + | |
| 7 | +class EntitySelectController extends AcymController | |
| 8 | +{ | |
| 9 | + public function __construct() | |
| 10 | + { | |
| 11 | + parent::__construct(); | |
| 12 | + | |
| 13 | + $this->loadScripts = [ | |
| 14 | + 'all' => ['vue-applications' => ['entity_select']], | |
| 15 | + ]; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public function loadEntityFront(): void | |
| 19 | + { | |
| 20 | + $entity = acym_getVar('string', 'entity'); | |
| 21 | + $offset = acym_getVar('int', 'offset'); | |
| 22 | + $perCalls = acym_getVar('int', 'perCalls'); | |
| 23 | + $join = acym_getVar('string', 'join'); | |
| 24 | + $joinColumnGet = acym_getVar('string', 'join_table', ''); | |
| 25 | + $columnsToDisplay = explode(',', acym_getVar('string', 'columns', '')); | |
| 26 | + if (!empty($joinColumnGet)) { | |
| 27 | + $columnsToDisplay['join'] = $joinColumnGet; | |
| 28 | + } | |
| 29 | + | |
| 30 | + $entityBack = $this->loadEntityBack($entity, $offset, $perCalls, $join, $columnsToDisplay); | |
| 31 | + if (!empty($entityBack['error'])) { | |
| 32 | + acym_sendAjaxResponse($entityBack['error'], [], false); | |
| 33 | + } else { | |
| 34 | + acym_sendAjaxResponse('', ['results' => $entityBack]); | |
| 35 | + } | |
| 36 | + } | |
| 37 | + | |
| 38 | + private function loadEntityBack(string $entity, int $offset, int $perCalls, string $join, array $columnsToDisplay): array | |
| 39 | + { | |
| 40 | + if (empty($entity) || (empty($offset) && 0 !== $offset) || empty($perCalls)) { | |
| 41 | + return ['error' => acym_translation('ACYM_MISSING_PARAMETERS')]; | |
| 42 | + } | |
| 43 | + | |
| 44 | + if (!in_array($entity, ['list', 'user'])) { | |
| 45 | + return ['error' => acym_translation('ACYM_UNAUTHORIZED_ACCESS')]; | |
| 46 | + } | |
| 47 | + | |
| 48 | + $entityParams = [ | |
| 49 | + 'offset' => $offset, | |
| 50 | + 'elementsPerPage' => $perCalls, | |
| 51 | + 'entitySelect' => true, | |
| 52 | + ]; | |
| 53 | + if ($entity === 'list') { | |
| 54 | + $entityParams['status'] = 'active'; | |
| 55 | + } | |
| 56 | + if (!empty($join)) { | |
| 57 | + $entityParams['join'] = $join; | |
| 58 | + } | |
| 59 | + if (!empty($columnsToDisplay)) { | |
| 60 | + foreach ($columnsToDisplay as $column) { | |
| 61 | + acym_secureDBColumn($column); | |
| 62 | + } | |
| 63 | + $entityParams['columns'] = $columnsToDisplay; | |
| 64 | + } | |
| 65 | + | |
| 66 | + if ('list' === $entity) { | |
| 67 | + $entityParams['columns'][] = 'description'; | |
| 68 | + } | |
| 69 | + | |
| 70 | + if (!acym_isAdmin()) { | |
| 71 | + $entityParams['creator_id'] = acym_currentUserId(); | |
| 72 | + } | |
| 73 | + | |
| 74 | + $namespaceClass = 'AcyMailing\\Classes\\'.ucfirst($entity).'Class'; | |
| 75 | + $entityClass = new $namespaceClass(); | |
| 76 | + | |
| 77 | + $availableEntity = $entityClass->getMatchingElements($entityParams); | |
| 78 | + | |
| 79 | + $this->formatEntites($availableEntity, $entity); | |
| 80 | + | |
| 81 | + return ['data' => empty($availableEntity) ? 'end' : $availableEntity]; | |
| 82 | + } | |
| 83 | + | |
| 84 | + private function formatEntites(array &$availableEntity, string $entity): void | |
| 85 | + { | |
| 86 | + if ($entity === 'list') { | |
| 87 | + foreach ($availableEntity['elements'] as $key => $element) { | |
| 88 | + $availableEntity['elements'][$key]->color = '<i style="color: '.$element->color.'" class="acym_subscription acymicon-circle">'; | |
| 89 | + if (!empty($element->description)) { | |
| 90 | + ob_start(); | |
| 91 | + acym_info(['textShownInTooltip' => $element->description]); | |
| 92 | + $tooltip = ob_get_clean(); | |
| 93 | + $availableEntity['elements'][$key]->name = $element->name.$tooltip; | |
| 94 | + } | |
| 95 | + } | |
| 96 | + } elseif ($entity === 'user') { | |
| 97 | + foreach ($availableEntity['elements'] as $key => $element) { | |
| 98 | + ob_start(); | |
| 99 | + acym_info(['textShownInTooltip' => '<i class="acymicon-circle-o-notch acymicon-spin"></i>']); | |
| 100 | + $tooltip = ob_get_clean(); | |
| 101 | + $availableEntity['elements'][$key]->email = $element->email.'<span class="acym__hover__user_info" data-id="'.esc_attr($availableEntity['elements'][$key]->id).'"> | |
| 102 | + '.$tooltip.' | |
| 103 | + </span>'; | |
| 104 | + } | |
| 105 | + } | |
| 106 | + } | |
| 107 | +} | |