PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.12
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.12
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Models / Comment.php

Comment.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.12, at app/Models/Comment.php

152 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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