PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.7.2
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.7.2
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 1.7.2, at app/Models/Calendar.php

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