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