FeaturesIntegrations
6 months ago
GetSettingsCommand.php
1 year ago
GetSettingsCommandHandler.php
6 months ago
UpdateSettingsCategoriesCommand.php
6 months ago
UpdateSettingsCategoriesCommandHandler.php
6 months ago
UpdateSettingsCommand.php
1 year ago
UpdateSettingsCommandHandler.php
6 months ago
UpdateSettingsCommandHandler.php
324 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Settings; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\Location\AbstractCurrentLocation; |
| 9 | use AmeliaBooking\Application\Services\Stash\StashApplicationService; |
| 10 | use AmeliaBooking\Domain\Collection\Collection; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\ForbiddenFileUploadException; |
| 12 | use AmeliaBooking\Domain\Entity\Entities; |
| 13 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 14 | use AmeliaBooking\Domain\Services\Api\BasicApiService; |
| 15 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 16 | use AmeliaBooking\Infrastructure\Services\Apple\AbstractAppleCalendarService; |
| 17 | use AmeliaBooking\Infrastructure\Services\LessonSpace\AbstractLessonSpaceService; |
| 18 | use AmeliaBooking\Infrastructure\WP\Integrations\WooCommerce\StarterWooCommerceService; |
| 19 | use Exception; |
| 20 | use Interop\Container\Exception\ContainerException; |
| 21 | use Slim\Exception\ContainerValueNotFoundException; |
| 22 | |
| 23 | /** |
| 24 | * Class UpdateSettingsCommandHandler |
| 25 | * |
| 26 | * @package AmeliaBooking\Application\Commands\Settings |
| 27 | */ |
| 28 | class UpdateSettingsCommandHandler extends CommandHandler |
| 29 | { |
| 30 | /** |
| 31 | * @param UpdateSettingsCommand $command |
| 32 | * |
| 33 | * @return CommandResult |
| 34 | * @throws AccessDeniedException |
| 35 | * @throws ContainerValueNotFoundException |
| 36 | * @throws ContainerException |
| 37 | * @throws Exception |
| 38 | */ |
| 39 | public function handle(UpdateSettingsCommand $command) |
| 40 | { |
| 41 | $result = new CommandResult(); |
| 42 | |
| 43 | if (!$this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::SETTINGS)) { |
| 44 | /** @var AbstractUser $loggedInUser */ |
| 45 | $loggedInUser = $this->container->get('logged.in.user'); |
| 46 | |
| 47 | if ( |
| 48 | !$loggedInUser || !( |
| 49 | $loggedInUser->getType() === AbstractUser::USER_ROLE_ADMIN || |
| 50 | $loggedInUser->getType() === AbstractUser::USER_ROLE_MANAGER |
| 51 | ) |
| 52 | ) { |
| 53 | throw new AccessDeniedException('You are not allowed to write settings.'); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** @var SettingsService $settingsService */ |
| 58 | $settingsService = $this->getContainer()->get('domain.settings.service'); |
| 59 | |
| 60 | /** @var AbstractCurrentLocation $locationService */ |
| 61 | $locationService = $this->getContainer()->get('application.currentLocation.service'); |
| 62 | |
| 63 | $settingsFields = $command->getFields(); |
| 64 | |
| 65 | if ( |
| 66 | StarterWooCommerceService::isEnabled() && |
| 67 | $command->getField('payments') && |
| 68 | $command->getField('payments')['wc']['enabled'] |
| 69 | ) { |
| 70 | $settingsFields['payments']['wc']['productId'] = StarterWooCommerceService::getIdForExistingOrNewProduct( |
| 71 | $settingsService->getCategorySettings('payments')['wc']['productId'] |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | if ($command->getField('general') && $command->getField('general')['customFieldsUploadsPath']) { |
| 77 | $uploadPath = $command->getField('general')['customFieldsUploadsPath']; |
| 78 | if ($uploadPath[0] !== '/') { |
| 79 | throw new ForbiddenFileUploadException('Attachment upload path must be an absolute path, starting with a slash (/).'); |
| 80 | } |
| 81 | |
| 82 | !is_dir($uploadPath) && !mkdir($uploadPath, 0755, true); |
| 83 | |
| 84 | if (!is_writable($uploadPath) || !is_dir($uploadPath)) { |
| 85 | throw new ForbiddenFileUploadException('Attachment upload path is not writable or does not exist.'); |
| 86 | } |
| 87 | |
| 88 | if (!file_exists("$uploadPath/index.html")) { |
| 89 | file_put_contents("$uploadPath/index.html", ''); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if ($command->getField('sendAllCF') !== null) { |
| 94 | $notificationsSettings = $settingsService->getCategorySettings('notifications'); |
| 95 | |
| 96 | $settingsFields['notifications'] = $notificationsSettings; |
| 97 | |
| 98 | $settingsFields['notifications']['sendAllCF'] = $command->getField('sendAllCF'); |
| 99 | |
| 100 | unset($settingsFields['sendAllCF']); |
| 101 | } |
| 102 | |
| 103 | if ($command->getField('googleCalendar') !== null) { |
| 104 | $googleSettings = $command->getField('googleCalendar'); |
| 105 | |
| 106 | $settingsFields['googleCalendar'] = array_merge( |
| 107 | $settingsService->getCategorySettings('googleCalendar'), |
| 108 | $googleSettings |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | if ($command->getField('outlookCalendar') !== null) { |
| 113 | $outlookSettings = $command->getField('outlookCalendar'); |
| 114 | |
| 115 | unset($outlookSettings['token']); |
| 116 | |
| 117 | $settingsFields['outlookCalendar'] = array_merge( |
| 118 | $settingsService->getCategorySettings('outlookCalendar'), |
| 119 | $outlookSettings |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | if ($command->getField('providerBadges') !== null) { |
| 124 | $rolesSettings = $settingsService->getCategorySettings('roles'); |
| 125 | |
| 126 | $settingsFields['roles'] = $rolesSettings; |
| 127 | |
| 128 | $settingsFields['roles']['providerBadges'] = $command->getField('providerBadges'); |
| 129 | |
| 130 | unset($settingsFields['providerBadges']); |
| 131 | } |
| 132 | |
| 133 | if ( |
| 134 | !$settingsService->getCategorySettings('activation')['stash'] && |
| 135 | !empty($settingsFields['activation']['stash']) |
| 136 | ) { |
| 137 | /** @var StashApplicationService $stashApplicationService */ |
| 138 | $stashApplicationService = $this->container->get('application.stash.service'); |
| 139 | |
| 140 | $stashApplicationService->setStash(); |
| 141 | } |
| 142 | |
| 143 | if ( |
| 144 | isset($settingsFields['daysOff']) && |
| 145 | $settingsService->getCategorySettings('activation')['stash'] && |
| 146 | $settingsService->getCategorySettings('daysOff') !== $settingsFields['daysOff'] && |
| 147 | $command->getField('daysOff') !== null |
| 148 | ) { |
| 149 | /** @var StashApplicationService $stashApplicationService */ |
| 150 | $stashApplicationService = $this->container->get('application.stash.service'); |
| 151 | |
| 152 | $stashApplicationService->setStash($settingsFields['daysOff']); |
| 153 | } |
| 154 | |
| 155 | $settingsFields['activation'] = array_merge( |
| 156 | $settingsService->getCategorySettings('activation'), |
| 157 | isset($settingsFields['activation']['deleteTables']) ? [ |
| 158 | 'deleteTables' => $settingsFields['activation']['deleteTables'] ? true : false |
| 159 | ] : [], |
| 160 | isset($settingsFields['activation']['envatoTokenEmail']) ? [ |
| 161 | 'envatoTokenEmail' => $settingsFields['activation']['envatoTokenEmail'] |
| 162 | ] : [], |
| 163 | isset($settingsFields['activation']['active']) ? [ |
| 164 | 'active' => $settingsFields['activation']['active'] |
| 165 | ] : [], |
| 166 | isset($settingsFields['activation']['stash']) ? [ |
| 167 | 'stash' => $settingsFields['activation']['stash'] |
| 168 | ] : [], |
| 169 | isset($settingsFields['activation']['showAmeliaPromoCustomizePopup']) ? [ |
| 170 | 'showAmeliaPromoCustomizePopup' => $settingsFields['activation']['showAmeliaPromoCustomizePopup'] |
| 171 | ] : [], |
| 172 | isset($settingsFields['activation']['showAmeliaSurvey']) ? [ |
| 173 | 'showAmeliaSurvey' => $settingsFields['activation']['showAmeliaSurvey'] |
| 174 | ] : [], |
| 175 | isset($settingsFields['activation']['customUrl']) ? [ |
| 176 | 'customUrl' => $settingsFields['activation']['customUrl'] |
| 177 | ] : [], |
| 178 | isset($settingsFields['activation']['v3AsyncLoading']) ? [ |
| 179 | 'v3AsyncLoading' => $settingsFields['activation']['v3AsyncLoading'] |
| 180 | ] : [], |
| 181 | isset($settingsFields['activation']['v3RelativePath']) ? [ |
| 182 | 'v3RelativePath' => $settingsFields['activation']['v3RelativePath'] |
| 183 | ] : [], |
| 184 | isset($settingsFields['activation']['enableThriveItems']) ? [ |
| 185 | 'enableThriveItems' => $settingsFields['activation']['enableThriveItems'] |
| 186 | ] : [], |
| 187 | isset($settingsFields['activation']['responseErrorAsConflict']) ? [ |
| 188 | 'responseErrorAsConflict' => $settingsFields['activation']['responseErrorAsConflict'] |
| 189 | ] : [], |
| 190 | isset($settingsFields['activation']['disableUrlParams']) ? [ |
| 191 | 'disableUrlParams' => $settingsFields['activation']['disableUrlParams'] |
| 192 | ] : [], |
| 193 | isset($settingsFields['activation']['hideUnavailableFeatures']) ? [ |
| 194 | 'hideUnavailableFeatures' => $settingsFields['activation']['hideUnavailableFeatures'] |
| 195 | ] : [], |
| 196 | isset($settingsFields['activation']['hideTipsAndSuggestions']) ? [ |
| 197 | 'hideTipsAndSuggestions' => $settingsFields['activation']['hideTipsAndSuggestions'] |
| 198 | ] : [], |
| 199 | isset($settingsFields['activation']['licence']) ? [ |
| 200 | 'licence' => $settingsFields['activation']['licence'] |
| 201 | ] : [], |
| 202 | isset($settingsFields['activation']['premiumBannerVisibility']) ? [ |
| 203 | 'premiumBannerVisibility' => $settingsFields['activation']['premiumBannerVisibility'] |
| 204 | ] : [], |
| 205 | isset($settingsFields['activation']['dismissibleBannerVisibility']) ? [ |
| 206 | 'dismissibleBannerVisibility' => $settingsFields['activation']['dismissibleBannerVisibility'] |
| 207 | ] : [] |
| 208 | ); |
| 209 | |
| 210 | if ($command->getField('usedLanguages') !== null) { |
| 211 | $generalSettings = $settingsService->getCategorySettings('general'); |
| 212 | |
| 213 | $settingsFields['general'] = $generalSettings; |
| 214 | |
| 215 | $settingsFields['general']['usedLanguages'] = $command->getField('usedLanguages'); |
| 216 | |
| 217 | unset($settingsFields['usedLanguages']); |
| 218 | } |
| 219 | |
| 220 | if ($command->getField('lessonSpace') !== null && $settingsFields['lessonSpace']['apiKey']) { |
| 221 | if (!$settingsService->getCategorySettings('lessonSpace')['companyId']) { |
| 222 | /** @var AbstractLessonSpaceService $lessonSpaceService */ |
| 223 | $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service'); |
| 224 | |
| 225 | $companyDetails = $lessonSpaceService->getCompanyId($settingsFields['lessonSpace']['apiKey']); |
| 226 | |
| 227 | $settingsFields['lessonSpace']['companyId'] = $companyDetails['id']; |
| 228 | } else { |
| 229 | $settingsFields['lessonSpace']['companyId'] = $settingsService->getCategorySettings('lessonSpace')['companyId']; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if ($command->getField('payments') && !empty($command->getField('payments')['square'])) { |
| 234 | $settingsFields['payments']['square']['accessToken'] = $settingsService->getCategorySettings('payments')['square']['accessToken']; |
| 235 | } |
| 236 | |
| 237 | if (isset($settingsFields['apiKeys']) && isset($settingsFields['apiKeys']['apiKeys'])) { |
| 238 | /** @var BasicApiService $apiService */ |
| 239 | $apiService = $this->getContainer()->get('domain.api.service'); |
| 240 | foreach ($settingsFields['apiKeys']['apiKeys'] as $index => $apiKey) { |
| 241 | if (!empty($apiKey['isNew'])) { |
| 242 | $settingsFields['apiKeys']['apiKeys'][$index]['key'] = $apiService->createHash($settingsFields['apiKeys']['apiKeys'][$index]['key']); |
| 243 | } |
| 244 | unset($settingsFields['apiKeys']['apiKeys'][$index]['isNew']); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (isset($settingsFields['pageColumnSettings'])) { |
| 249 | $currentPageColumnSettings = $settingsService->getCategorySettings('pageColumnSettings'); |
| 250 | |
| 251 | foreach ($settingsFields['pageColumnSettings'] as $page => $columns) { |
| 252 | $currentPageColumnSettings[$page] = $columns; |
| 253 | } |
| 254 | |
| 255 | $settingsFields['pageColumnSettings'] = $currentPageColumnSettings; |
| 256 | } |
| 257 | |
| 258 | if (!empty($command->getField('appleCalendar')['clientID']) && !empty($command->getField('appleCalendar')['clientSecret'])) { |
| 259 | /** @var AbstractAppleCalendarService $appleCalendarService */ |
| 260 | $appleCalendarService = $this->container->get('infrastructure.apple.calendar.service'); |
| 261 | |
| 262 | $appleId = $command->getField('appleCalendar')['clientID']; |
| 263 | $applePassword = $command->getField('appleCalendar')['clientSecret']; |
| 264 | |
| 265 | $credentials = $appleCalendarService->handleAppleCredentials($appleId, $applePassword); |
| 266 | |
| 267 | if (!$credentials) { |
| 268 | $result->setDataInResponse(true); |
| 269 | $result->setResult(CommandResult::RESULT_ERROR); |
| 270 | $result->setMessage('Make sure you are using the correct iCloud email address and app-specific password.'); |
| 271 | |
| 272 | return $result; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if ( |
| 277 | $command->getField('appleCalendar') && |
| 278 | empty($command->getField('appleCalendar')['clientID']) && |
| 279 | empty($command->getField('appleCalendar')['clientSecret']) |
| 280 | ) { |
| 281 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 282 | /** @var Collection $providers */ |
| 283 | $providers = $providerRepository->getAll(); |
| 284 | foreach ($providers->getItems() as $provider) { |
| 285 | $providerRepository->updateFieldById($provider->getId()->getValue(), null, 'employeeAppleCalendar'); |
| 286 | $providerRepository->updateFieldById($provider->getId()->getValue(), null, 'appleCalendarId'); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (!empty($command->getField('customizedData'))) { |
| 291 | $passedCustomizedData = $command->getField('customizedData'); |
| 292 | $customizedData = $settingsService->getCategorySettings('customizedData'); |
| 293 | |
| 294 | foreach ($passedCustomizedData as $key => $value) { |
| 295 | $customizedData[$key] = $value; |
| 296 | } |
| 297 | |
| 298 | $settingsFields['customizedData'] = $customizedData; |
| 299 | } |
| 300 | |
| 301 | $settingsFields = apply_filters('amelia_before_settings_updated_filter', $settingsFields); |
| 302 | |
| 303 | do_action('amelia_before_settings_updated', $settingsFields); |
| 304 | |
| 305 | $settingsService->setAllSettings($settingsFields); |
| 306 | |
| 307 | $settings = $settingsService->getAllSettingsCategorized(); |
| 308 | $settings['general']['phoneDefaultCountryCode'] = $settings['general']['phoneDefaultCountryCode'] === 'auto' ? |
| 309 | $locationService->getCurrentLocationCountryIso($settings['general']['ipLocateApiKey']) : $settings['general']['phoneDefaultCountryCode']; |
| 310 | |
| 311 | do_action('amelia_after_settings_updated', $settingsFields); |
| 312 | |
| 313 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 314 | $result->setMessage('Successfully updated settings.'); |
| 315 | $result->setData( |
| 316 | [ |
| 317 | 'settings' => $settings |
| 318 | ] |
| 319 | ); |
| 320 | |
| 321 | return $result; |
| 322 | } |
| 323 | } |
| 324 |