PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
← All changes | app/Models/Calendar.php +91 -4 1.5.02 → 2.5.0 View file →
@@ -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)
@@ -215,6 +272,36 @@
215 272 }
216 273 }
217 274
218 275 return $dirty;
276 + }
277 +
278 + public static function getAllHosts()
279 + {
280 + $calendars = self::with(['user', 'metas'])
281 + ->where('type', 'simple')
282 + ->get();
283 +
284 + $deletedUser = __('Deleted User', 'fluent-booking');
285 +
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) {
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 +
297 + return [
298 + 'id' => $user ? $user->ID : (int)$calendar->user_id,
299 + 'name' => $user ? $user->full_name : $deletedUser,
300 + 'label' => $label,
301 + 'avatar' => $calendar->getAuthorPhoto(),
302 + 'calendar_id' => $calendar->id,
303 + 'deleted_user' => $user ? false : true
304 + ];
305 + });
219 306 }
220 307 }