| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
class Comment extends Model |
| 6 |
{ |
| 7 |
protected $table = 'fbs_comments'; |
| 8 |
|
| 9 |
protected $guarded = ['id']; |
| 10 |
|
| 11 |
protected $appends = ['avatar']; |
| 12 |
|
| 13 |
protected $fillable = [ |
| 14 |
'board_id', |
| 15 |
'task_id', |
| 16 |
'parent_id', |
| 17 |
'type', |
| 18 |
'privacy', |
| 19 |
'status', |
| 20 |
'author_name', |
| 21 |
'author_email', |
| 22 |
'author_ip', |
| 23 |
'description', |
| 24 |
'settings', |
| 25 |
'created_by', |
| 26 |
]; |
| 27 |
|
| 28 |
public static function boot() |
| 29 |
{ |
| 30 |
parent::boot(); |
| 31 |
static::creating(function ($model) { |
| 32 |
if (empty($model->created_by) && is_user_logged_in()) { |
| 33 |
$model->created_by = get_current_user_id(); |
| 34 |
} |
| 35 |
|
| 36 |
if (empty($model->type)) { |
| 37 |
$model->type = 'comment'; |
| 38 |
} |
| 39 |
|
| 40 |
if (empty($model->privacy)) { |
| 41 |
$model->privacy = 'private'; |
| 42 |
} |
| 43 |
|
| 44 |
if (!empty($model->created_by) && empty($model->author_email)) { |
| 45 |
$user = get_user_by('ID', $model->created_by); |
| 46 |
if ($user) { |
| 47 |
$model->author_email = $user->user_email; |
| 48 |
$name = trim($user->first_name . ' ' . $user->last_name); |
| 49 |
if (!$name) { |
| 50 |
$name = $user->display_name; |
| 51 |
} |
| 52 |
$model->author_name = $name; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
static::created(function ($model) { |
| 57 |
if ($model->type == 'comment') { |
| 58 |
$task = $model->task; |
| 59 |
if ($task) { |
| 60 |
$task->comments_count = $task->comments_count + 1; |
| 61 |
$task->save(); |
| 62 |
} |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
static::deleted(function ($model) { |
| 67 |
if ($model->type == 'comment') { |
| 68 |
$task = $model->task; |
| 69 |
if ($task) { |
| 70 |
$task->comments_count = $task->comments_count - 1; |
| 71 |
$task->save(); |
| 72 |
} |
| 73 |
} |
| 74 |
}); |
| 75 |
|
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* One2One: Activity belongs to one User |
| 81 |
* @return \FluentCrm\Framework\Database\Orm\Relations\BelongsTo |
| 82 |
*/ |
| 83 |
public function user() |
| 84 |
{ |
| 85 |
return $this->belongsTo(User::class, 'created_by', 'ID'); |
| 86 |
} |
| 87 |
|
| 88 |
public function task() |
| 89 |
{ |
| 90 |
return $this->belongsTo(Task::class, 'task_id', 'id'); |
| 91 |
} |
| 92 |
|
| 93 |
public function scopeByTask($query, $taskId) |
| 94 |
{ |
| 95 |
return $query->where('task_id', $taskId); |
| 96 |
} |
| 97 |
|
| 98 |
public function scopePrivacy($query, $type) |
| 99 |
{ |
| 100 |
return $query->where('privacy', $type); |
| 101 |
} |
| 102 |
|
| 103 |
public function setSettingsAttribute($settings) |
| 104 |
{ |
| 105 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 106 |
} |
| 107 |
|
| 108 |
public function getSettingsAttribute($settings) |
| 109 |
{ |
| 110 |
return \maybe_unserialize($settings); |
| 111 |
} |
| 112 |
|
| 113 |
public function replies() |
| 114 |
{ |
| 115 |
return $this->hasMany(Comment::class, 'parent_id', 'id'); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Accessor to get dynamic photo attribute |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function getAvatarAttribute() |
| 123 |
{ |
| 124 |
$fallBack = ''; |
| 125 |
if (isset($this->attributes['author_name'])) { |
| 126 |
$fallBack = $this->attributes['author_name']; |
| 127 |
} |
| 128 |
|
| 129 |
$email = $this->attributes['author_email']; |
| 130 |
|
| 131 |
if (!$email && $this->created_by) { |
| 132 |
$user = get_user_by('ID', $this->created_by); |
| 133 |
if ($user) { |
| 134 |
$email = $user->user_email; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return fluent_boards_user_avatar($email, $fallBack); |
| 139 |
} |
| 140 |
|
| 141 |
public function scopeType($query, $type) |
| 142 |
{ |
| 143 |
return $query->where('type', $type); |
| 144 |
} |
| 145 |
|
| 146 |
public function parentComment() |
| 147 |
{ |
| 148 |
return $this->hasOne(Comment::class, 'parent_id', 'id'); |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|