PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / Modules / Auth / Classes / Invitation.php

Invitation.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.7, at Modules/Auth/Classes/Invitation.php

200 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Modules\Auth\Classes;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Models\Model;
7 use FluentCommunity\App\Models\BaseSpace;
8 use FluentCommunity\App\Models\User;
9 use FluentCommunity\App\Models\XProfile;
10 use FluentCommunity\App\Services\Helper;
11 use FluentCommunity\Framework\Support\Arr;
12
13 /**
14 * @property int $id
15 * @property int|null $user_id
16 * @property int|null $post_id
17 * @property string|null $message
18 * @property string|null $message_rendered
19 * @property mixed $meta
20 * @property string|null $type
21 * @property int $reactions_count
22 * @property string|null $status
23 * @property string|null $created_at
24 * @property string|null $updated_at
25 * @property-read User|null $user
26 * @property-read XProfile|null $xprofile
27 * @property-read BaseSpace|null $space
28 */
29 class Invitation extends Model
30 {
31 protected $table = 'fcom_post_comments';
32
33 protected $guarded = [ 'id' ];
34
35 protected $fillable = [
36 'user_id',
37 'post_id',
38 'message',
39 'message_rendered',
40 'meta',
41 'type',
42 'reactions_count',
43 'status',
44 'created_at',
45 'updated_at',
46 ];
47
48 protected $searchable = [
49 'message',
50 ];
51
52 protected $hidden = [
53 'parent_id',
54 'content_type',
55 'is_sticky',
56 ];
57
58 public static function boot()
59 {
60 parent::boot();
61
62 static::addGlobalScope('type', function ($builder) {
63 $builder->where('type', 'invitation');
64 });
65
66 static::creating(function ($model) {
67 if (empty($model->user_id)) {
68 $model->user_id = get_current_user_id();
69 }
70 $model->type = 'invitation';
71 });
72 }
73
74 public function user()
75 {
76 return $this->belongsTo(User::class, 'user_id', 'ID');
77 }
78
79 public function xprofile()
80 {
81 return $this->belongsTo(XProfile::class, 'user_id', 'user_id');
82 }
83
84 public function space()
85 {
86 return $this->belongsTo(BaseSpace::class, 'post_id', 'id');
87 }
88
89 public function setMetaAttribute($value)
90 {
91 $this->attributes['meta'] = maybe_serialize($value);
92 }
93
94 public function getMetaAttribute($value)
95 {
96 $meta = Utility::safeUnserialize($value);
97
98 if (!$meta) {
99 $meta = [
100 'invitee_name' => '',
101 ];
102 }
103
104 return $meta;
105 }
106
107 public static function getPendingInvitations($contentType, $spaceId, $user)
108 {
109 $query = self::where('status', 'pending')
110 ->where('content_type', $contentType);
111
112 if ($spaceId) {
113 $spaceRole = $user->getSpaceRole(BaseSpace::findOrFail($spaceId));
114 $query->where('post_id', $spaceId);
115
116 if (!in_array($spaceRole, [ 'admin', 'moderator' ])) {
117 $query->where('user_id', $user->ID);
118 }
119 } elseif (!$user->isCommunityModerator()) {
120 $query->where('user_id', $user->ID);
121 }
122
123 return $query->orderBy('updated_at', 'desc')->groupBy('message');
124 }
125
126 public static function findExistingInvitation($email, $contentType, $spaceId = null)
127 {
128 $query = self::where('message', $email)
129 ->where('content_type', $contentType)
130 ->where('status', 'pending');
131
132 if ($spaceId) {
133 $query->where('post_id', $spaceId);
134 }
135
136 return $query->first();
137 }
138
139 public static function createNewInvitation($data)
140 {
141 $inviteeId = get_current_user_id();
142
143 return self::create([
144 'user_id' => $inviteeId,
145 'post_id' => Arr::get($data, 'space_id'),
146 'message' => $data['email'],
147 'message_rendered' => $data['token'],
148 'meta' => [ 'role' => $data['role'] ],
149 'type' => 'invitation',
150 'content_type' => $data['content_type'],
151 'status' => 'pending',
152 ]);
153 }
154
155 public static function getTodayInvitationCount($userId)
156 {
157 return self::where('user_id', $userId)
158 ->whereDate('created_at', (new \DateTime())->format('Y-m-d'))
159 ->count();
160 }
161
162 public function getAccessUrl()
163 {
164 return add_query_arg([
165 'fcom_action' => 'auth',
166 'invitation_token' => $this->message_rendered,
167 ], Helper::baseUrl('/'));
168 }
169
170 public function isValid()
171 {
172 if ($this->message) {
173 return $this->status == 'pending';
174 }
175
176 if ($this->status != 'active') {
177 return false;
178 }
179
180 $meta = $this->meta;
181
182 $expireDate = Arr::get($meta, 'expire_date', '');
183 $limit = Arr::get($meta, 'limit', 0);
184
185 if ($expireDate && strtotime($expireDate) < strtotime(gmdate('Y-m-d', current_time('timestamp')))) {
186 $this->status = 'expired';
187 $this->save();
188 return false;
189 }
190
191 if ($limit && $this->reactions_count >= $limit) {
192 $this->status = 'expired';
193 $this->save();
194 return false;
195 }
196
197 return true;
198 }
199 }
200