ApiConnectionsController.php
1 month ago
EmailsController.php
1 month ago
MembershipsController.php
1 month ago
PagesController.php
1 month ago
SectionsController.php
1 month ago
StatisticsController.php
1 month ago
UsersController.php
1 month ago
SectionsController.php
206 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Api\V2\Endpoints; |
| 4 | |
| 5 | use FapiMember\Api\V2\ApiController; |
| 6 | use FapiMember\Container\Container; |
| 7 | use FapiMember\Library\SmartEmailing\Types\IntType; |
| 8 | use FapiMember\Library\SmartEmailing\Types\StringType; |
| 9 | use FapiMember\Model\Enums\Alert; |
| 10 | use FapiMember\Model\Enums\Keys\MetaKey; |
| 11 | use FapiMember\Model\Enums\Types\RequestMethodType; |
| 12 | use FapiMember\Repository\LevelRepository; |
| 13 | use FapiMember\Service\ApiService; |
| 14 | use FapiMember\Service\LevelOrderService; |
| 15 | use FapiMember\Service\LevelService; |
| 16 | use Throwable; |
| 17 | use WP_REST_Request; |
| 18 | |
| 19 | class SectionsController |
| 20 | { |
| 21 | private LevelRepository $levelRepository; |
| 22 | private LevelService $levelService; |
| 23 | private ApiService $apiService; |
| 24 | private ApiController $apiController; |
| 25 | private LevelOrderService $levelOrderService; |
| 26 | |
| 27 | public function __construct() |
| 28 | { |
| 29 | $this->levelRepository = Container::get(LevelRepository::class); |
| 30 | $this->levelService = Container::get(LevelService::class); |
| 31 | $this->apiService = Container::get(ApiService::class); |
| 32 | $this->apiController = Container::get(ApiController::class); |
| 33 | $this->levelOrderService = Container::get(LevelOrderService::class); |
| 34 | } |
| 35 | |
| 36 | public function list(): array |
| 37 | { |
| 38 | $sections = $this->levelService->getAllSectionsInOrder(); |
| 39 | $array = []; |
| 40 | |
| 41 | if ($sections === null) { |
| 42 | $this->apiController->callbackError([ |
| 43 | 'class'=> self::class, |
| 44 | 'description' => "Ordering of Levels is broken.", |
| 45 | ]); |
| 46 | } |
| 47 | |
| 48 | foreach ($sections as $section) { |
| 49 | $array[] = $section->toArray(); |
| 50 | } |
| 51 | |
| 52 | return $array; |
| 53 | } |
| 54 | |
| 55 | public function getAllAsLevels(WP_REST_Request $request): array |
| 56 | { |
| 57 | $this->apiController->checkRequestMethod($request, RequestMethodType::GET); |
| 58 | $array = []; |
| 59 | |
| 60 | $levels = $this->levelRepository->getAllAsLevels(); |
| 61 | |
| 62 | foreach ($levels as $level) { |
| 63 | $array[] = $level->toArray(); |
| 64 | } |
| 65 | |
| 66 | return $array; |
| 67 | } |
| 68 | |
| 69 | public function get(int $id): array |
| 70 | { |
| 71 | $section = $this->levelRepository->getSectionById($id); |
| 72 | |
| 73 | if ($section === null) { |
| 74 | $this->apiController->callbackError([ |
| 75 | 'class'=> self::class, |
| 76 | 'description' => "section with id: " . $id . " doesn't exist.", |
| 77 | ]); |
| 78 | } |
| 79 | |
| 80 | return $section->toArray(); |
| 81 | } |
| 82 | |
| 83 | public function delete(int $id): void |
| 84 | { |
| 85 | try { |
| 86 | $this->levelRepository->remove($id); |
| 87 | } catch (Throwable) { |
| 88 | $this->apiController->callbackError([ |
| 89 | 'class'=> self::class, |
| 90 | 'description' => "section/level with id: " . $id . " doesn't exist.", |
| 91 | ]); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | $this->apiController->callbackResponse([], Alert::REMOVE_LEVEL_SUCCESSFUL); |
| 96 | } |
| 97 | |
| 98 | public function create(array $data): void |
| 99 | { |
| 100 | $name = $this->apiController->extractParamOrNull($data, 'name', StringType::class); |
| 101 | $parentId = StringType::extractOrNull($data, 'parent_id'); |
| 102 | |
| 103 | if (trim($name) === '' || $name === null) { |
| 104 | $this->apiController->callbackError([], Alert::SECTION_NAME_EMPTY); |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | $levelId = $this->levelService->create(trim($name), $parentId); |
| 109 | } catch (Throwable) { |
| 110 | $this->apiController->callbackError([ |
| 111 | 'class'=> self::class, |
| 112 | 'description' => "error while creating section/level", |
| 113 | ]); |
| 114 | } |
| 115 | |
| 116 | if ($levelId !== null) { |
| 117 | $this->apiController->callbackSuccess( |
| 118 | ['id' => $levelId, 'name' => trim($name)] |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | $this->apiController->callbackError([], Alert::LEVEL_ALREADY_EXISTS); |
| 123 | } |
| 124 | |
| 125 | public function update(int $id, array $data): void |
| 126 | { |
| 127 | |
| 128 | try { |
| 129 | $this->levelRepository->update($id, $data); |
| 130 | } catch (Throwable) { |
| 131 | $this->apiController->callbackError([ |
| 132 | 'class'=> self::class, |
| 133 | 'description' => "error while updating section/level", |
| 134 | ]); |
| 135 | } |
| 136 | |
| 137 | $this->apiController->callbackSuccess(); |
| 138 | } |
| 139 | |
| 140 | public function reorder(WP_REST_Request $request): void |
| 141 | { |
| 142 | $this->apiController->checkRequestMethod($request, RequestMethodType::POST); |
| 143 | $body = json_decode($request->get_body(), true); |
| 144 | |
| 145 | $levelId = $this->apiController->extractParam($body, 'id', IntType::class); |
| 146 | $direction = $this->apiController->extractParam($body, 'direction', IntType::class); |
| 147 | |
| 148 | if ($direction !== 1 && $direction !== -1) { |
| 149 | $this->apiController->callbackError([ |
| 150 | 'class'=> self::class, |
| 151 | 'description' => "Direction value has to be 1 or -1.", |
| 152 | ]); |
| 153 | } |
| 154 | |
| 155 | $success = $this->levelOrderService->reorder($levelId, $direction); |
| 156 | |
| 157 | if ($success) { |
| 158 | $this->apiController->callbackSuccess(); |
| 159 | } |
| 160 | |
| 161 | $this->apiController->callbackError([], Alert::REORDER_FAILED); |
| 162 | } |
| 163 | |
| 164 | public function getUnlocking(WP_REST_Request $request): array |
| 165 | { |
| 166 | $this->apiController->checkRequestMethod($request, RequestMethodType::POST); |
| 167 | $body = json_decode($request->get_body(), true); |
| 168 | |
| 169 | $levelId = $this->apiController->extractParam($body, 'id', IntType::class); |
| 170 | |
| 171 | return [ |
| 172 | MetaKey::BUTTON_UNLOCK => $this->levelRepository->isButtonUnlock($levelId), |
| 173 | MetaKey::TIME_UNLOCK => $this->levelRepository->getTimeUnlock($levelId), |
| 174 | MetaKey::DATE_UNLOCK => $this->levelRepository->getDateUnlock($levelId), |
| 175 | MetaKey::DAYS_TO_UNLOCK => $this->levelRepository->getDaysUnlock($levelId), |
| 176 | MetaKey::HOUR_UNLOCK => $this->levelRepository->getHourUnlock($levelId), |
| 177 | MetaKey::AFTER_DATE_UNLOCK => $this->levelRepository->getAfterDateUnlock($levelId), |
| 178 | ]; |
| 179 | } |
| 180 | |
| 181 | public function updateUnlocking(WP_REST_Request $request): void |
| 182 | { |
| 183 | $this->apiController->checkRequestMethod($request, RequestMethodType::POST); |
| 184 | $body = json_decode($request->get_body(), true); |
| 185 | |
| 186 | $levelId = $this->apiController->extractParam($body, 'id', IntType::class); |
| 187 | |
| 188 | if (isset($body['unlocking']) && is_array($body['unlocking'])) { |
| 189 | $this->levelRepository->updateSetUnlocking( |
| 190 | $levelId, |
| 191 | $body['unlocking'][MetaKey::BUTTON_UNLOCK] ?? null, |
| 192 | $body['unlocking'][MetaKey::TIME_UNLOCK] ?? null, |
| 193 | $body['unlocking'][MetaKey::DAYS_TO_UNLOCK] ?? null, |
| 194 | $body['unlocking'][MetaKey::DATE_UNLOCK] ?? null, |
| 195 | $body['unlocking'][MetaKey::HOUR_UNLOCK] ?? 0, |
| 196 | $body['unlocking'][MetaKey::AFTER_DATE_UNLOCK] ?? false, |
| 197 | ); |
| 198 | } else { |
| 199 | $this->apiController->invalidParameterError('unlocking'); |
| 200 | } |
| 201 | |
| 202 | $this->apiController->callbackSettingsSaved(); |
| 203 | } |
| 204 | |
| 205 | } |
| 206 |