| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentBooking\App\Models\Model; |
| 6 | 6 | use FluentBooking\App\Services\Helper; |
| 7 | 7 | use FluentBooking\App\Services\LandingPage\LandingPageHelper; |
| 8 | +use FluentBooking\App\Services\PermissionManager; | |
| 8 | 9 | use FluentBooking\Framework\Support\Arr; |
| 9 | 10 | |
| 10 | 11 | class Calendar extends Model |
| 11 | 12 | { |
| @@ -12,8 +13,10 @@ | ||
| 12 | 13 | protected $table = 'fcal_calendars'; |
| 13 | 14 | |
| 14 | 15 | protected $guarded = ['id']; |
| 15 | 16 | |
| 17 | + protected $metaCache = []; | |
| 18 | + | |
| 16 | 19 | protected $fillable = [ |
| 17 | 20 | 'hash', |
| 18 | 21 | 'user_id', |
| 19 | 22 | 'account_id', |
| @@ -65,8 +68,24 @@ | ||
| 65 | 68 | { |
| 66 | 69 | return $this->hasMany(CalendarSlot::class, 'calendar_id'); |
| 67 | 70 | } |
| 68 | 71 | |
| 72 | + /** | |
| 73 | + * The calendar owner plus every host its events already list. | |
| 74 | + * | |
| 75 | + * @return int[] | |
| 76 | + */ | |
| 77 | + public function getMemberIds() | |
| 78 | + { | |
| 79 | + $memberIds = [(int) $this->user_id]; | |
| 80 | + | |
| 81 | + foreach ($this->events()->get(['settings']) as $event) { | |
| 82 | + $memberIds = array_merge($memberIds, array_map('intval', (array) Arr::get($event->settings, 'team_members', []))); | |
| 83 | + } | |
| 84 | + | |
| 85 | + return array_values(array_unique($memberIds)); | |
| 86 | + } | |
| 87 | + | |
| 69 | 88 | public function user() |
| 70 | 89 | { |
| 71 | 90 | return $this->belongsTo(User::class, 'user_id'); |
| 72 | 91 | } |
| @@ -80,8 +99,14 @@ | ||
| 80 | 99 | { |
| 81 | 100 | return $this->hasMany(Availability::class, 'object_id', 'user_id'); |
| 82 | 101 | } |
| 83 | 102 | |
| 103 | + public function metas() | |
| 104 | + { | |
| 105 | + return $this->hasMany(Meta::class, 'object_id', 'id') | |
| 106 | + ->where('object_type', 'Calendar'); | |
| 107 | + } | |
| 108 | + | |
| 84 | 109 | public function isTeamCalendar() |
| 85 | 110 | { |
| 86 | 111 | return $this->type == 'team'; |
| 87 | 112 | } |
| @@ -95,8 +120,27 @@ | ||
| 95 | 120 | { |
| 96 | 121 | return $this->type == 'simple'; |
| 97 | 122 | } |
| 98 | 123 | |
| 124 | + public function updateEventOrder($eventId) | |
| 125 | + { | |
| 126 | + $eventOrder = $this->getMeta('event_order', []); | |
| 127 | + | |
| 128 | + if ($eventOrder) { | |
| 129 | + $updatedOrder = array_merge($eventOrder, [$eventId]); | |
| 130 | + | |
| 131 | + if (in_array($eventId, $eventOrder)) { | |
| 132 | + $updatedOrder = array_filter($eventOrder, function($id) use ($eventId) { | |
| 133 | + return $id !== $eventId; | |
| 134 | + }); | |
| 135 | + } | |
| 136 | + | |
| 137 | + return $this->updateMeta('event_order', array_values($updatedOrder)); | |
| 138 | + } | |
| 139 | + | |
| 140 | + return $eventOrder; | |
| 141 | + } | |
| 142 | + | |
| 99 | 143 | public function getAuthorPhoto() |
| 100 | 144 | { |
| 101 | 145 | $photo = $this->getMeta('profile_photo_url'); |
| 102 | 146 | |
| @@ -145,13 +189,24 @@ | ||
| 145 | 189 | } |
| 146 | 190 | |
| 147 | 191 | public function getMeta($key, $default = null) |
| 148 | 192 | { |
| 149 | - $meta = Meta::where('object_type', 'Calendar') | |
| 150 | - ->where('object_id', $this->id) | |
| 151 | - ->where('key', $key) | |
| 152 | - ->first(); | |
| 193 | + if ($this->relationLoaded('metas')) { | |
| 194 | + $meta = $this->metas->firstWhere('key', $key); | |
| 195 | + return $meta ? $meta->value : $default; | |
| 196 | + } | |
| 153 | 197 | |
| 198 | + if (array_key_exists($key, $this->metaCache)) { | |
| 199 | + $meta = $this->metaCache[$key]; | |
| 200 | + } else { | |
| 201 | + $meta = Meta::where('object_type', 'Calendar') | |
| 202 | + ->where('object_id', $this->id) | |
| 203 | + ->where('key', $key) | |
| 204 | + ->first(); | |
| 205 | + | |
| 206 | + $this->metaCache[$key] = $meta; | |
| 207 | + } | |
| 208 | + | |
| 154 | 209 | if (!$meta) { |
| 155 | 210 | return $default; |
| 156 | 211 | } |
| 157 | 212 | |
| @@ -176,8 +231,10 @@ | ||
| 176 | 231 | 'value' => $value |
| 177 | 232 | ]); |
| 178 | 233 | } |
| 179 | 234 | |
| 235 | + $this->metaCache[$key] = $exist; | |
| 236 | + | |
| 180 | 237 | return $exist; |
| 181 | 238 | } |
| 182 | 239 | |
| 183 | 240 | public function getLandingPageUrl($isForce = false) |
| @@ -219,20 +276,29 @@ | ||
| 219 | 276 | } |
| 220 | 277 | |
| 221 | 278 | public static function getAllHosts() |
| 222 | 279 | { |
| 223 | - $calendars = self::with(['user']) | |
| 280 | + $calendars = self::with(['user', 'metas']) | |
| 224 | 281 | ->where('type', 'simple') |
| 225 | 282 | ->get(); |
| 226 | 283 | |
| 227 | 284 | $deletedUser = __('Deleted User', 'fluent-booking'); |
| 228 | 285 | |
| 229 | - return $calendars->map(function ($calendar) use ($deletedUser) { | |
| 286 | + // Every permitted user gets this list; only those who manage other hosts need their emails. | |
| 287 | + $showEmail = PermissionManager::canManageOtherHosts(); | |
| 288 | + | |
| 289 | + return $calendars->map(function ($calendar) use ($deletedUser, $showEmail) { | |
| 230 | 290 | $user = $calendar->user; |
| 291 | + | |
| 292 | + $label = $deletedUser; | |
| 293 | + if ($user) { | |
| 294 | + $label = $showEmail ? $user->display_name . ' (' . $user->user_email . ')' : $user->display_name; | |
| 295 | + } | |
| 296 | + | |
| 231 | 297 | return [ |
| 232 | 298 | 'id' => $user ? $user->ID : (int)$calendar->user_id, |
| 233 | 299 | 'name' => $user ? $user->full_name : $deletedUser, |
| 234 | - 'label' => $user ? $user->display_name . ' (' . $user->user_email . ')' : $deletedUser, | |
| 300 | + 'label' => $label, | |
| 235 | 301 | 'avatar' => $calendar->getAuthorPhoto(), |
| 236 | 302 | 'calendar_id' => $calendar->id, |
| 237 | 303 | 'deleted_user' => $user ? false : true |
| 238 | 304 | ]; |