SettingsService.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Services\Settings; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Settings\Settings; |
| 6 | use AmeliaBooking\Domain\Factory\Settings\SettingsFactory; |
| 7 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 8 | |
| 9 | /** |
| 10 | * Class SettingsService |
| 11 | * |
| 12 | * @package AmeliaBooking\Domain\Services\Settings |
| 13 | */ |
| 14 | class SettingsService |
| 15 | { |
| 16 | public const NUMBER_OF_DAYS_AVAILABLE_FOR_BOOKING = 365; |
| 17 | |
| 18 | /** @var SettingsStorageInterface */ |
| 19 | private $settingsStorage; |
| 20 | |
| 21 | /** |
| 22 | * SettingsService constructor. |
| 23 | * |
| 24 | * @param SettingsStorageInterface $settingsStorage |
| 25 | */ |
| 26 | public function __construct(SettingsStorageInterface $settingsStorage) |
| 27 | { |
| 28 | $this->settingsStorage = $settingsStorage; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param $settingCategoryKey |
| 33 | * @param $settingKey |
| 34 | * @param null $defaultValue |
| 35 | * |
| 36 | * @return mixed|null |
| 37 | */ |
| 38 | public function getSetting($settingCategoryKey, $settingKey, $defaultValue = null) |
| 39 | { |
| 40 | if (null !== $this->settingsStorage->getSetting($settingCategoryKey, $settingKey)) { |
| 41 | return $this->settingsStorage->getSetting($settingCategoryKey, $settingKey); |
| 42 | } |
| 43 | |
| 44 | return $defaultValue; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param $settingCategoryKey |
| 49 | * |
| 50 | * @return mixed|array |
| 51 | */ |
| 52 | public function getCategorySettings($settingCategoryKey) |
| 53 | { |
| 54 | return $this->settingsStorage->getCategorySettings($settingCategoryKey); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return array of all settings where keys are settings names and values are settings values |
| 59 | * |
| 60 | * @return mixed |
| 61 | */ |
| 62 | public function getAllSettings() |
| 63 | { |
| 64 | return $this->settingsStorage->getAllSettings(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return array of arrays where keys are settings categories names and values are categories settings |
| 69 | * |
| 70 | * @return mixed |
| 71 | */ |
| 72 | public function getAllSettingsCategorized() |
| 73 | { |
| 74 | return $this->settingsStorage->getAllSettingsCategorized(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return mixed |
| 79 | */ |
| 80 | public function getFrontendSettings() |
| 81 | { |
| 82 | return $this->settingsStorage->getFrontendSettings(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @return mixed |
| 87 | */ |
| 88 | public function getBackendSettings() |
| 89 | { |
| 90 | return $this->settingsStorage->getBackendSettings(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param $settingCategoryKey |
| 95 | * @param $settingKey |
| 96 | * @param $settingValue |
| 97 | * |
| 98 | * @return mixed |
| 99 | */ |
| 100 | public function setSetting($settingCategoryKey, $settingKey, $settingValue) |
| 101 | { |
| 102 | return $this->settingsStorage->setSetting($settingCategoryKey, $settingKey, $settingValue); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param $settingCategoryKey |
| 107 | * @param $settingValues |
| 108 | * |
| 109 | * @return mixed |
| 110 | */ |
| 111 | public function setCategorySettings($settingCategoryKey, $settingValues) |
| 112 | { |
| 113 | return $this->settingsStorage->setCategorySettings($settingCategoryKey, $settingValues); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param $settings |
| 118 | * |
| 119 | * @return mixed |
| 120 | */ |
| 121 | public function setAllSettings($settings) |
| 122 | { |
| 123 | return $this->settingsStorage->setAllSettings($settings); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @param Json $entitySettingsJson |
| 128 | * |
| 129 | * @return Settings |
| 130 | */ |
| 131 | public function getEntitySettings($entitySettingsJson) |
| 132 | { |
| 133 | return SettingsFactory::create($entitySettingsJson, $this->getAllSettingsCategorized()); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param Json $entitySettingsJson |
| 138 | * |
| 139 | * @return Settings |
| 140 | */ |
| 141 | public function getSavedSettings($entitySettingsJson) |
| 142 | { |
| 143 | $data = $entitySettingsJson ? json_decode($entitySettingsJson->getValue(), true) : []; |
| 144 | |
| 145 | $isOldEntitySettings = ! isset($data['activation']['version']); |
| 146 | |
| 147 | if ($isOldEntitySettings && isset($data['general']['minimumTimeRequirementPriorToCanceling'])) { |
| 148 | $data['general']['minimumTimeRequirementPriorToRescheduling'] = |
| 149 | $data['general']['minimumTimeRequirementPriorToCanceling']; |
| 150 | } |
| 151 | |
| 152 | return $data; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param array $entities |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function setStashEntities($entities) |
| 161 | { |
| 162 | update_option('amelia_stash', json_encode($entities)); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return array |
| 167 | */ |
| 168 | public function getStashEntities() |
| 169 | { |
| 170 | $entitiesStash = get_option('amelia_stash'); |
| 171 | |
| 172 | return $entitiesStash ? json_decode($entitiesStash, true) : []; |
| 173 | } |
| 174 | |
| 175 | public function isFeatureEnabled($feature) |
| 176 | { |
| 177 | // First check if the feature is enabled in settings |
| 178 | $featureSettings = $this->getSetting('featuresIntegrations', $feature); |
| 179 | |
| 180 | if (!is_array($featureSettings) || !isset($featureSettings['enabled'])) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // If the feature is not enabled in settings, return false |
| 185 | if (!$featureSettings['enabled']) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Check if the current license has access to this feature |
| 190 | // If not, the feature is considered disabled even if enabled in settings |
| 191 | return \AmeliaBooking\Infrastructure\Licence\Licence::hasFeatureAccess($feature); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param array $settings |
| 196 | * |
| 197 | * @return void |
| 198 | */ |
| 199 | public function fixCustomization(&$settings) |
| 200 | { |
| 201 | if (isset($settings['forms']) && $settings['forms']) { |
| 202 | foreach ($settings['forms'] as $formName => &$form) { |
| 203 | if (isset($form['confirmBookingForm'])) { |
| 204 | foreach ($form['confirmBookingForm'] as $entityName => &$entity) { |
| 205 | if (isset($entity['itemsStatic']['paymentMethodFormField']['switchPaymentMethodViewOptions'])) { |
| 206 | array_splice( |
| 207 | $settings['forms'][$formName]['confirmBookingForm'][$entityName]['itemsStatic'] |
| 208 | ['paymentMethodFormField']['switchPaymentMethodViewOptions'], |
| 209 | 2 |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 |