PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / trunk
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution vtrunk
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 1.7.2 All 33 releases
fluent-booking / app / Models / Calendar.php

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

282 lines 7.3 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\Framework\Support\Arr;
9
10 class Calendar extends Model
11 {
12 protected $table = 'fcal_calendars';
13
14 protected $guarded = ['id'];
15
16 protected $metaCache = [];
17
18 protected $fillable = [
19 'hash',
20 'user_id',
21 'account_id',
22 'parent_id',
23 'title',
24 'slug',
25 'media_id',
26 'description',
27 'settings',
28 'status',
29 'type',
30 'event_type',
31 'account_type',
32 'visibility',
33 'author_timezone',
34 'max_book_per_slot',
35 'created_at',
36 'updated_at'
37 ];
38
39 public static function boot()
40 {
41 parent::boot();
42
43 static::creating(function ($model) {
44 if (empty($model->user_id)) {
45 $model->user_id = get_current_user_id();
46 }
47 $model->hash = md5(wp_generate_uuid4() . time());
48 });
49 }
50
51 public function setSettingsAttribute($settings)
52 {
53 $this->attributes['settings'] = \maybe_serialize($settings);
54 }
55
56 public function getSettingsAttribute($settings)
57 {
58 return \maybe_unserialize($settings);
59 }
60
61 public function slots()
62 {
63 return $this->hasMany(CalendarSlot::class, 'calendar_id');
64 }
65
66 public function events()
67 {
68 return $this->hasMany(CalendarSlot::class, 'calendar_id');
69 }
70
71 public function user()
72 {
73 return $this->belongsTo(User::class, 'user_id');
74 }
75
76 public function bookings()
77 {
78 return $this->hasMany(Booking::class, 'calendar_id');
79 }
80
81 public function availabilities()
82 {
83 return $this->hasMany(Availability::class, 'object_id', 'user_id');
84 }
85
86 public function metas()
87 {
88 return $this->hasMany(Meta::class, 'object_id', 'id')
89 ->where('object_type', 'Calendar');
90 }
91
92 public function isTeamCalendar()
93 {
94 return $this->type == 'team';
95 }
96
97 public function isEventCalendar()
98 {
99 return $this->type == 'event';
100 }
101
102 public function isHostCalendar()
103 {
104 return $this->type == 'simple';
105 }
106
107 public function updateEventOrder($eventId)
108 {
109 $eventOrder = $this->getMeta('event_order', []);
110
111 if ($eventOrder) {
112 $updatedOrder = array_merge($eventOrder, [$eventId]);
113
114 if (in_array($eventId, $eventOrder)) {
115 $updatedOrder = array_filter($eventOrder, function($id) use ($eventId) {
116 return $id !== $eventId;
117 });
118 }
119
120 return $this->updateMeta('event_order', array_values($updatedOrder));
121 }
122
123 return $eventOrder;
124 }
125
126 public function getAuthorPhoto()
127 {
128 $photo = $this->getMeta('profile_photo_url');
129
130 if (!$photo) {
131 $photo = Helper::fluentBookingUserAvatar($this->user_id, $this->user_id);
132 }
133
134 return $photo;
135 }
136
137 public function getAuthorProfile($public = true)
138 {
139 $user = get_user_by('id', $this->user_id);
140 if (!$user) {
141 return [
142 'avatar' => $this->getMeta('profile_photo_url'),
143 'name' => 'Unknown',
144 'email' => '',
145 'phone' => '',
146 'featured_image' => $this->getMeta('featured_image_url')
147 ];
148 }
149
150 $name = trim($user->first_name . ' ' . $user->last_name);
151
152 if (!$name) {
153 $name = $user->display_name;
154 }
155
156 $data = [
157 'ID' => $user->ID,
158 'name' => $name,
159 'author_slug' => $user->user_nicename,
160 'first_name' => $user->first_name,
161 'last_name' => $user->last_name,
162 'avatar' => $this->getAuthorPhoto(),
163 'phone' => $this->user->getMeta('host_phone'),
164 'featured_image' => $this->getMeta('featured_image_url')
165 ];
166
167 if (!$public) {
168 $data['email'] = $user->user_email;
169 }
170
171 return $data;
172 }
173
174 public function getMeta($key, $default = null)
175 {
176 if ($this->relationLoaded('metas')) {
177 $meta = $this->metas->firstWhere('key', $key);
178 return $meta ? $meta->value : $default;
179 }
180
181 if (array_key_exists($key, $this->metaCache)) {
182 $meta = $this->metaCache[$key];
183 } else {
184 $meta = Meta::where('object_type', 'Calendar')
185 ->where('object_id', $this->id)
186 ->where('key', $key)
187 ->first();
188
189 $this->metaCache[$key] = $meta;
190 }
191
192 if (!$meta) {
193 return $default;
194 }
195
196 return $meta->value;
197 }
198
199 public function updateMeta($key, $value)
200 {
201 $exist = Meta::where('object_type', 'Calendar')
202 ->where('object_id', $this->id)
203 ->where('key', $key)
204 ->first();
205
206 if ($exist) {
207 $exist->value = $value;
208 $exist->save();
209 } else {
210 $exist = Meta::create([
211 'object_type' => 'Calendar',
212 'object_id' => $this->id,
213 'key' => $key,
214 'value' => $value
215 ]);
216 }
217
218 $this->metaCache[$key] = $exist;
219
220 return $exist;
221 }
222
223 public function getLandingPageUrl($isForce = false)
224 {
225 $settings = LandingPageHelper::getSettings($this);
226 if (Arr::get($settings, 'enabled') != 'yes' && !$isForce) {
227 return '';
228 }
229
230 if (defined('FLUENT_BOOKING_LANDING_SLUG')) {
231 return LandingPageHelper::getLandingBaseUrl() . $this->slug;
232 }
233
234 return LandingPageHelper::getLandingBaseUrl() . '&host=' . $this->slug;
235 }
236
237 /**
238 * Get the attributes that have been changed since last sync.
239 *
240 * @return array
241 */
242 public function getDirty()
243 {
244 $dirty = [];
245 foreach ($this->attributes as $key => $value) {
246 if (!in_array($key, $this->fillable)) {
247 continue;
248 }
249
250 if (!array_key_exists($key, $this->original)) {
251 $dirty[$key] = $value;
252 } elseif ($value !== $this->original[$key] &&
253 !$this->originalIsNumericallyEquivalent($key)) {
254 $dirty[$key] = $value;
255 }
256 }
257
258 return $dirty;
259 }
260
261 public static function getAllHosts()
262 {
263 $calendars = self::with(['user', 'metas'])
264 ->where('type', 'simple')
265 ->get();
266
267 $deletedUser = __('Deleted User', 'fluent-booking');
268
269 return $calendars->map(function ($calendar) use ($deletedUser) {
270 $user = $calendar->user;
271 return [
272 'id' => $user ? $user->ID : (int)$calendar->user_id,
273 'name' => $user ? $user->full_name : $deletedUser,
274 'label' => $user ? $user->display_name . ' (' . $user->user_email . ')' : $deletedUser,
275 'avatar' => $calendar->getAuthorPhoto(),
276 'calendar_id' => $calendar->id,
277 'deleted_user' => $user ? false : true
278 ];
279 });
280 }
281 }
282