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