| @@ -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 | } |
| @@ -170,13 +189,24 @@ | ||
| 170 | 189 | } |
| 171 | 190 | |
| 172 | 191 | public function getMeta($key, $default = null) |
| 173 | 192 | { |
| 174 | - $meta = Meta::where('object_type', 'Calendar') | |
| 175 | - ->where('object_id', $this->id) | |
| 176 | - ->where('key', $key) | |
| 177 | - ->first(); | |
| 193 | + if ($this->relationLoaded('metas')) { | |
| 194 | + $meta = $this->metas->firstWhere('key', $key); | |
| 195 | + return $meta ? $meta->value : $default; | |
| 196 | + } | |
| 178 | 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 | + | |
| 179 | 209 | if (!$meta) { |
| 180 | 210 | return $default; |
| 181 | 211 | } |
| 182 | 212 | |
| @@ -201,8 +231,10 @@ | ||
| 201 | 231 | 'value' => $value |
| 202 | 232 | ]); |
| 203 | 233 | } |
| 204 | 234 | |
| 235 | + $this->metaCache[$key] = $exist; | |
| 236 | + | |
| 205 | 237 | return $exist; |
| 206 | 238 | } |
| 207 | 239 | |
| 208 | 240 | public function getLandingPageUrl($isForce = false) |
| @@ -244,20 +276,29 @@ | ||
| 244 | 276 | } |
| 245 | 277 | |
| 246 | 278 | public static function getAllHosts() |
| 247 | 279 | { |
| 248 | - $calendars = self::with(['user']) | |
| 280 | + $calendars = self::with(['user', 'metas']) | |
| 249 | 281 | ->where('type', 'simple') |
| 250 | 282 | ->get(); |
| 251 | 283 | |
| 252 | 284 | $deletedUser = __('Deleted User', 'fluent-booking'); |
| 253 | 285 | |
| 254 | - 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) { | |
| 255 | 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 | + | |
| 256 | 297 | return [ |
| 257 | 298 | 'id' => $user ? $user->ID : (int)$calendar->user_id, |
| 258 | 299 | 'name' => $user ? $user->full_name : $deletedUser, |
| 259 | - 'label' => $user ? $user->display_name . ' (' . $user->user_email . ')' : $deletedUser, | |
| 300 | + 'label' => $label, | |
| 260 | 301 | 'avatar' => $calendar->getAuthorPhoto(), |
| 261 | 302 | 'calendar_id' => $calendar->id, |
| 262 | 303 | 'deleted_user' => $user ? false : true |
| 263 | 304 | ]; |