PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.6.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.6.0
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.6.0, at Modules/Auth/Classes/Invitation.php

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