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

221 lines 5.4 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 isTeamCalendar()
85 {
86 return $this->type == 'team';
87 }
88
89 public function isEventCalendar()
90 {
91 return $this->type == 'event';
92 }
93
94 public function isHostCalendar()
95 {
96 return $this->type == 'simple';
97 }
98
99 public function getAuthorPhoto()
100 {
101 $photo = $this->getMeta('profile_photo_url');
102
103 if (!$photo) {
104 $photo = Helper::fluentBookingUserAvatar($this->user_id, $this->user_id);
105 }
106
107 return $photo;
108 }
109
110 public function getAuthorProfile($public = true)
111 {
112 $user = get_user_by('id', $this->user_id);
113 if (!$user) {
114 return [
115 'avatar' => $this->getMeta('profile_photo_url'),
116 'name' => 'Unknown',
117 'email' => '',
118 'phone' => '',
119 'featured_image' => $this->getMeta('featured_image_url')
120 ];
121 }
122
123 $name = trim($user->first_name . ' ' . $user->last_name);
124
125 if (!$name) {
126 $name = $user->display_name;
127 }
128
129 $data = [
130 'ID' => $user->ID,
131 'name' => $name,
132 'author_slug' => $user->user_nicename,
133 'first_name' => $user->first_name,
134 'last_name' => $user->last_name,
135 'avatar' => $this->getAuthorPhoto(),
136 'phone' => $this->user->getMeta('host_phone'),
137 'featured_image' => $this->getMeta('featured_image_url')
138 ];
139
140 if (!$public) {
141 $data['email'] = $user->user_email;
142 }
143
144 return $data;
145 }
146
147 public function getMeta($key, $default = null)
148 {
149 $meta = Meta::where('object_type', 'Calendar')
150 ->where('object_id', $this->id)
151 ->where('key', $key)
152 ->first();
153
154 if (!$meta) {
155 return $default;
156 }
157
158 return $meta->value;
159 }
160
161 public function updateMeta($key, $value)
162 {
163 $exist = Meta::where('object_type', 'Calendar')
164 ->where('object_id', $this->id)
165 ->where('key', $key)
166 ->first();
167
168 if ($exist) {
169 $exist->value = $value;
170 $exist->save();
171 } else {
172 $exist = Meta::create([
173 'object_type' => 'Calendar',
174 'object_id' => $this->id,
175 'key' => $key,
176 'value' => $value
177 ]);
178 }
179
180 return $exist;
181 }
182
183 public function getLandingPageUrl($isForce = false)
184 {
185 $settings = LandingPageHelper::getSettings($this);
186 if (Arr::get($settings, 'enabled') != 'yes' && !$isForce) {
187 return '';
188 }
189
190 if (defined('FLUENT_BOOKING_LANDING_SLUG')) {
191 return LandingPageHelper::getLandingBaseUrl() . $this->slug;
192 }
193
194 return LandingPageHelper::getLandingBaseUrl() . '&host=' . $this->slug;
195 }
196
197 /**
198 * Get the attributes that have been changed since last sync.
199 *
200 * @return array
201 */
202 public function getDirty()
203 {
204 $dirty = [];
205 foreach ($this->attributes as $key => $value) {
206 if (!in_array($key, $this->fillable)) {
207 continue;
208 }
209
210 if (!array_key_exists($key, $this->original)) {
211 $dirty[$key] = $value;
212 } elseif ($value !== $this->original[$key] &&
213 !$this->originalIsNumericallyEquivalent($key)) {
214 $dirty[$key] = $value;
215 }
216 }
217
218 return $dirty;
219 }
220 }
221