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
fluent-booking / app / Models / Calendar.php

Calendar.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.5.0, at app/Models/Calendar.php

308 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Models;
4
5 use FluentBooking\App\Models\Model;
6 use FluentBooking\App\Services\Helper;
7 use FluentBooking\App\Services\LandingPage\LandingPageHelper;
8 use FluentBooking\App\Services\PermissionManager;
9 use FluentBooking\Framework\Support\Arr;
10
11 class Calendar extends Model
12 {
13 protected $table = 'fcal_calendars';
14
15 protected $guarded = ['id'];
16
17 protected $metaCache = [];
18
19 protected $fillable = [
20 'hash',
21 'user_id',
22 'account_id',
23 'parent_id',
24 'title',
25 'slug',
26 'media_id',
27 'description',
28 'settings',
29 'status',
30 'type',
31 'event_type',
32 'account_type',
33 'visibility',
34 'author_timezone',
35 'max_book_per_slot',
36 'created_at',
37 'updated_at'
38 ];
39
40 public static function boot()
41 {
42 parent::boot();
43
44 static::creating(function ($model) {
45 if (empty($model->user_id)) {
46 $model->user_id = get_current_user_id();
47 }
48 $model->hash = md5(wp_generate_uuid4() . time());
49 });
50 }
51
52 public function setSettingsAttribute($settings)
53 {
54 $this->attributes['settings'] = \maybe_serialize($settings);
55 }
56
57 public function getSettingsAttribute($settings)
58 {
59 return \maybe_unserialize($settings);
60 }
61
62 public function slots()
63 {
64 return $this->hasMany(CalendarSlot::class, 'calendar_id');
65 }
66
67 public function events()
68 {
69 return $this->hasMany(CalendarSlot::class, 'calendar_id');
70 }
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
88 public function user()
89 {
90 return $this->belongsTo(User::class, 'user_id');
91 }
92
93 public function bookings()
94 {
95 return $this->hasMany(Booking::class, 'calendar_id');
96 }
97
98 public function availabilities()
99 {
100 return $this->hasMany(Availability::class, 'object_id', 'user_id');
101 }
102
103 public function metas()
104 {
105 return $this->hasMany(Meta::class, 'object_id', 'id')
106 ->where('object_type', 'Calendar');
107 }
108
109 public function isTeamCalendar()
110 {
111 return $this->type == 'team';
112 }
113
114 public function isEventCalendar()
115 {
116 return $this->type == 'event';
117 }
118
119 public function isHostCalendar()
120 {
121 return $this->type == 'simple';
122 }
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
143 public function getAuthorPhoto()
144 {
145 $photo = $this->getMeta('profile_photo_url');
146
147 if (!$photo) {
148 $photo = Helper::fluentBookingUserAvatar($this->user_id, $this->user_id);
149 }
150
151 return $photo;
152 }
153
154 public function getAuthorProfile($public = true)
155 {
156 $user = get_user_by('id', $this->user_id);
157 if (!$user) {
158 return [
159 'avatar' => $this->getMeta('profile_photo_url'),
160 'name' => 'Unknown',
161 'email' => '',
162 'phone' => '',
163 'featured_image' => $this->getMeta('featured_image_url')
164 ];
165 }
166
167 $name = trim($user->first_name . ' ' . $user->last_name);
168
169 if (!$name) {
170 $name = $user->display_name;
171 }
172
173 $data = [
174 'ID' => $user->ID,
175 'name' => $name,
176 'author_slug' => $user->user_nicename,
177 'first_name' => $user->first_name,
178 'last_name' => $user->last_name,
179 'avatar' => $this->getAuthorPhoto(),
180 'phone' => $this->user->getMeta('host_phone'),
181 'featured_image' => $this->getMeta('featured_image_url')
182 ];
183
184 if (!$public) {
185 $data['email'] = $user->user_email;
186 }
187
188 return $data;
189 }
190
191 public function getMeta($key, $default = null)
192 {
193 if ($this->relationLoaded('metas')) {
194 $meta = $this->metas->firstWhere('key', $key);
195 return $meta ? $meta->value : $default;
196 }
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
209 if (!$meta) {
210 return $default;
211 }
212
213 return $meta->value;
214 }
215
216 public function updateMeta($key, $value)
217 {
218 $exist = Meta::where('object_type', 'Calendar')
219 ->where('object_id', $this->id)
220 ->where('key', $key)
221 ->first();
222
223 if ($exist) {
224 $exist->value = $value;
225 $exist->save();
226 } else {
227 $exist = Meta::create([
228 'object_type' => 'Calendar',
229 'object_id' => $this->id,
230 'key' => $key,
231 'value' => $value
232 ]);
233 }
234
235 $this->metaCache[$key] = $exist;
236
237 return $exist;
238 }
239
240 public function getLandingPageUrl($isForce = false)
241 {
242 $settings = LandingPageHelper::getSettings($this);
243 if (Arr::get($settings, 'enabled') != 'yes' && !$isForce) {
244 return '';
245 }
246
247 if (defined('FLUENT_BOOKING_LANDING_SLUG')) {
248 return LandingPageHelper::getLandingBaseUrl() . $this->slug;
249 }
250
251 return LandingPageHelper::getLandingBaseUrl() . '&host=' . $this->slug;
252 }
253
254 /**
255 * Get the attributes that have been changed since last sync.
256 *
257 * @return array
258 */
259 public function getDirty()
260 {
261 $dirty = [];
262 foreach ($this->attributes as $key => $value) {
263 if (!in_array($key, $this->fillable)) {
264 continue;
265 }
266
267 if (!array_key_exists($key, $this->original)) {
268 $dirty[$key] = $value;
269 } elseif ($value !== $this->original[$key] &&
270 !$this->originalIsNumericallyEquivalent($key)) {
271 $dirty[$key] = $value;
272 }
273 }
274
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 });
306 }
307 }
308