| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Auth\Classes; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Model; |
| 6 |
use FluentCommunity\App\Models\BaseSpace; |
| 7 |
use FluentCommunity\App\Models\User; |
| 8 |
use FluentCommunity\App\Models\XProfile; |
| 9 |
use FluentCommunity\App\Services\Helper; |
| 10 |
use FluentCommunity\Framework\Support\Arr; |
| 11 |
|
| 12 |
class Invitation extends Model |
| 13 |
{ |
| 14 |
protected $table = 'fcom_post_comments'; |
| 15 |
|
| 16 |
protected $guarded = ['id']; |
| 17 |
|
| 18 |
protected $fillable = [ |
| 19 |
'user_id', |
| 20 |
'post_id', |
| 21 |
'message', |
| 22 |
'message_rendered', |
| 23 |
'meta', |
| 24 |
'type', |
| 25 |
'reactions_count', |
| 26 |
'status', |
| 27 |
'created_at', |
| 28 |
'updated_at' |
| 29 |
]; |
| 30 |
|
| 31 |
protected $searchable = [ |
| 32 |
'message' |
| 33 |
]; |
| 34 |
|
| 35 |
protected $hidden = [ |
| 36 |
'parent_id', |
| 37 |
'content_type', |
| 38 |
'is_sticky' |
| 39 |
]; |
| 40 |
|
| 41 |
public static function boot() |
| 42 |
{ |
| 43 |
parent::boot(); |
| 44 |
|
| 45 |
static::addGlobalScope('type', function ($builder) { |
| 46 |
$builder->where('type', 'invitation'); |
| 47 |
}); |
| 48 |
|
| 49 |
static::creating(function ($model) { |
| 50 |
if (empty($model->user_id)) { |
| 51 |
$model->user_id = get_current_user_id(); |
| 52 |
} |
| 53 |
$model->type = 'invitation'; |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
public function user() |
| 58 |
{ |
| 59 |
return $this->belongsTo(User::class, 'user_id', 'ID'); |
| 60 |
} |
| 61 |
|
| 62 |
public function xprofile() |
| 63 |
{ |
| 64 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 65 |
} |
| 66 |
|
| 67 |
public function space() |
| 68 |
{ |
| 69 |
return $this->belongsTo(BaseSpace::class, 'post_id', 'id'); |
| 70 |
} |
| 71 |
|
| 72 |
public function setMetaAttribute($value) |
| 73 |
{ |
| 74 |
$this->attributes['meta'] = maybe_serialize($value); |
| 75 |
} |
| 76 |
|
| 77 |
public function getMetaAttribute($value) |
| 78 |
{ |
| 79 |
$meta = maybe_unserialize($value); |
| 80 |
|
| 81 |
if (!$meta) { |
| 82 |
$meta = [ |
| 83 |
'invitee_name' => '', |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
return $meta; |
| 88 |
} |
| 89 |
|
| 90 |
public static function getPendingInvitations($contentType, $spaceId, $user) |
| 91 |
{ |
| 92 |
$query = self::where('status', 'pending') |
| 93 |
->where('content_type', $contentType); |
| 94 |
|
| 95 |
if ($spaceId) { |
| 96 |
$spaceRole = $user->getSpaceRole(BaseSpace::findOrFail($spaceId)); |
| 97 |
$query->where('post_id', $spaceId); |
| 98 |
|
| 99 |
if (!in_array($spaceRole, ['admin', 'moderator'])) { |
| 100 |
$query->where('user_id', $user->ID); |
| 101 |
} |
| 102 |
} elseif (!$user->isCommunityModerator()) { |
| 103 |
$query->where('user_id', $user->ID); |
| 104 |
} |
| 105 |
|
| 106 |
return $query->orderBy('updated_at', 'desc')->groupBy('message'); |
| 107 |
} |
| 108 |
|
| 109 |
public static function findExistingInvitation($email, $contentType, $spaceId = null) |
| 110 |
{ |
| 111 |
$query = self::where('message', $email) |
| 112 |
->where('content_type', $contentType) |
| 113 |
->where('status', 'pending'); |
| 114 |
|
| 115 |
if ($spaceId) { |
| 116 |
$query->where('post_id', $spaceId); |
| 117 |
} |
| 118 |
|
| 119 |
return $query->first(); |
| 120 |
} |
| 121 |
|
| 122 |
public static function createNewInvitation($data) |
| 123 |
{ |
| 124 |
$inviteeId = get_current_user_id(); |
| 125 |
|
| 126 |
return self::create([ |
| 127 |
'user_id' => $inviteeId, |
| 128 |
'post_id' => Arr::get($data, 'space_id'), |
| 129 |
'message' => $data['email'], |
| 130 |
'message_rendered' => $data['token'], |
| 131 |
'meta' => ['role' => $data['role']], |
| 132 |
'type' => 'invitation', |
| 133 |
'content_type' => $data['content_type'], |
| 134 |
'status' => 'pending', |
| 135 |
]); |
| 136 |
} |
| 137 |
|
| 138 |
public static function getTodayInvitationCount($userId) |
| 139 |
{ |
| 140 |
return self::where('user_id', $userId) |
| 141 |
->whereDate('created_at', (new \DateTime())->format('Y-m-d')) |
| 142 |
->count(); |
| 143 |
} |
| 144 |
|
| 145 |
public function getAccessUrl() |
| 146 |
{ |
| 147 |
return add_query_arg([ |
| 148 |
'fcom_action' => 'auth', |
| 149 |
'invitation_token' => $this->message_rendered |
| 150 |
], Helper::baseUrl('/')); |
| 151 |
} |
| 152 |
} |
| 153 |
|