| @@ -26,18 +26,27 @@ | ||
| 26 | 26 | |
| 27 | 27 | return Availability::create($defaultSchedule); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - public static function availabilitySchedules() | |
| 30 | + /** | |
| 31 | + * Schedules the current user may read and attach, as a query so callers that only | |
| 32 | + * need ids can avoid formatting every schedule. | |
| 33 | + */ | |
| 34 | + public static function usableAvailabilityQuery() | |
| 31 | 35 | { |
| 32 | - $permissions = ['read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars']; | |
| 36 | + $permissions = ['manage_all_data', 'read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars']; | |
| 33 | 37 | |
| 34 | - $availabilities = Availability::when( | |
| 38 | + return Availability::when( | |
| 35 | 39 | !PermissionManager::userCan($permissions), |
| 36 | 40 | function ($query) { |
| 37 | 41 | return $query->where('object_id', get_current_user_id()); |
| 38 | - })->get()->toArray(); | |
| 42 | + }); | |
| 43 | + } | |
| 39 | 44 | |
| 45 | + public static function availabilitySchedules() | |
| 46 | + { | |
| 47 | + $availabilities = self::usableAvailabilityQuery()->get()->toArray(); | |
| 48 | + | |
| 40 | 49 | $formattedSchedules = []; |
| 41 | 50 | foreach ($availabilities as $availability) { |
| 42 | 51 | $toTimezone = Arr::get($availability, 'value.timezone', 'UTC'); |
| 43 | 52 | $formattedSchedules[] = [ |
| @@ -115,9 +124,9 @@ | ||
| 115 | 124 | } |
| 116 | 125 | |
| 117 | 126 | public static function getScheduleOptions() |
| 118 | 127 | { |
| 119 | - $permissions = ['read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars']; | |
| 128 | + $permissions = ['manage_all_data', 'read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars']; | |
| 120 | 129 | |
| 121 | 130 | $availabilities = Availability::when( |
| 122 | 131 | !PermissionManager::userCan($permissions), |
| 123 | 132 | function ($query) { |
| @@ -130,9 +139,9 @@ | ||
| 130 | 139 | ->where('type', 'simple') |
| 131 | 140 | ->where('user_id', $availability->object_id) |
| 132 | 141 | ->first(); |
| 133 | 142 | |
| 134 | - if ($calendar) { | |
| 143 | + if ($calendar && $calendar->user) { | |
| 135 | 144 | $hostName = $calendar->user->full_name; |
| 136 | 145 | if ($calendar->user_id == get_current_user_id()) { |
| 137 | 146 | $hostName = __('My Schedules', 'fluent-booking'); |
| 138 | 147 | } |
| @@ -140,14 +149,13 @@ | ||
| 140 | 149 | $hostName = __('Deleted User', 'fluent-booking'); |
| 141 | 150 | } |
| 142 | 151 | |
| 143 | 152 | $scheduleOptions[$hostName] = $scheduleOptions[$hostName] ?? []; |
| 144 | - | |
| 145 | - $default = Arr::isTrue($availability, 'value.default') ? ' (Default)' : ''; | |
| 146 | - | |
| 153 | + | |
| 147 | 154 | $scheduleOptions[$hostName][] = [ |
| 148 | - 'label' => Arr::get($availability, 'key') . $default, | |
| 149 | - 'value' => Arr::get($availability, 'id') | |
| 155 | + 'label' => Arr::get($availability, 'key'), | |
| 156 | + 'value' => Arr::get($availability, 'id'), | |
| 157 | + 'default' => Arr::isTrue($availability, 'value.default') | |
| 150 | 158 | ]; |
| 151 | 159 | } |
| 152 | 160 | |
| 153 | 161 | return apply_filters('fluent_booking/availability_schedule_options', $scheduleOptions); |
| @@ -154,16 +162,13 @@ | ||
| 154 | 162 | } |
| 155 | 163 | |
| 156 | 164 | public static function getDefaultSchedule($userId) |
| 157 | 165 | { |
| 158 | - $schedules = Availability::where('object_id', $userId)->get(); | |
| 159 | - | |
| 160 | - foreach ($schedules as $schedule) { | |
| 161 | - if (Arr::isTrue($schedule, 'value.default')) { | |
| 162 | - return $schedule; | |
| 163 | - } | |
| 164 | - } | |
| 165 | - return null; | |
| 166 | + return Availability::where('object_id', $userId) | |
| 167 | + ->get() | |
| 168 | + ->first(function ($schedule) { | |
| 169 | + return Arr::isTrue($schedule, 'value.default'); | |
| 170 | + }); | |
| 166 | 171 | } |
| 167 | 172 | |
| 168 | 173 | public static function createScheduleSchema($userId, $title, $default, $fromTimezone, $toTimezone = 'UTC', $weeklySchedule = [], $dateOverrides = []) |
| 169 | 174 | { |
| @@ -168,9 +173,9 @@ | ||
| 168 | 173 | public static function createScheduleSchema($userId, $title, $default, $fromTimezone, $toTimezone = 'UTC', $weeklySchedule = [], $dateOverrides = []) |
| 169 | 174 | { |
| 170 | 175 | $weeklySchedule = $weeklySchedule ? $weeklySchedule : Helper::getWeeklyScheduleSchema(); |
| 171 | 176 | |
| 172 | - $dateOverrides = $dateOverrides ? SanitizeService::slotDateOverrides($dateOverrides, $fromTimezone, $toTimezone) : []; | |
| 177 | + $dateOverrides = $dateOverrides ? SanitizeService::slotDateOverrides($dateOverrides, $fromTimezone, $toTimezone, null, true) : []; | |
| 173 | 178 | |
| 174 | 179 | $defaultSchedule = [ |
| 175 | 180 | 'object_id' => $userId, |
| 176 | 181 | 'key' => sanitize_text_field($title), |
| @@ -177,9 +182,9 @@ | ||
| 177 | 182 | 'value' => [ |
| 178 | 183 | 'default' => (bool)$default, |
| 179 | 184 | 'timezone' => sanitize_text_field($fromTimezone), |
| 180 | 185 | 'date_overrides' => $dateOverrides, |
| 181 | - 'weekly_schedules' => SanitizeService::weeklySchedules($weeklySchedule, $fromTimezone, $toTimezone), | |
| 186 | + 'weekly_schedules' => SanitizeService::weeklySchedules($weeklySchedule, $fromTimezone, $toTimezone, true), | |
| 182 | 187 | ] |
| 183 | 188 | ]; |
| 184 | 189 | return $defaultSchedule; |
| 185 | 190 | } |
| @@ -215,9 +220,10 @@ | ||
| 215 | 220 | continue; |
| 216 | 221 | } |
| 217 | 222 | |
| 218 | 223 | $schedule['enabled'] = true; |
| 219 | - | |
| 224 | + | |
| 225 | + $nextDay = null; | |
| 220 | 226 | $nextDayIndex = 0; |
| 221 | 227 | $dayIndex = array_search($day, $weekDays); |
| 222 | 228 | foreach ($schedule['slots'] as $index => $slot) { |
| 223 | 229 | if (!$slot['start'] || !$slot['end']) { |