forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComment.php
More file actions
202 lines (184 loc) · 6.08 KB
/
Comment.php
File metadata and controls
202 lines (184 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use ProcessMaker\Traits\SerializeToIso8601;
use ProcessMaker\Traits\SqlsrvSupportTrait;
/**
* Represents a business process definition.
*
* @property int 'id',
* @property int 'user_id',
* @property int 'commentable_id',
* @property string 'commentable_type',
* @property int 'up',
* @property int 'down',
* @property string 'subject',
* @property string 'body',
* @property bool 'hidden',
* @property string 'type',
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $created_at
*
* @OA\Schema(
* schema="commentsEditable",
* @OA\Property(property="id", type="string", format="id"),
* @OA\Property(property="user_id", type="string", format="id"),
* @OA\Property(property="commentable_id", type="string", format="id"),
* @OA\Property(property="commentable_type", type="string"),
* @OA\Property(property="up", type="integer"),
* @OA\Property(property="down", type="integer"),
* @OA\Property(property="subject", type="string"),
* @OA\Property(property="body", type="string"),
* @OA\Property(property="hidden", type="boolean"),
* @OA\Property(property="type", type="string", enum={"LOG", "MESSAGE"}),
* ),
* @OA\Schema(
* schema="comments",
* allOf={@OA\Schema(ref="#/components/schemas/commentsEditable")},
* @OA\Property(property="created_at", type="string", format="date-time"),
* @OA\Property(property="updated_at", type="string", format="date-time"),
* )
*/
class Comment extends ProcessMakerModel
{
use SerializeToIso8601;
use SqlsrvSupportTrait;
use SoftDeletes;
protected $fillable = [
'user_id', 'parent_id', 'group_id', 'group_name', 'commentable_id', 'commentable_type', 'subject', 'body', 'hidden', 'type',
];
protected $casts = [
'up' => 'array',
'down' => 'array',
];
public static function rules()
{
return [
'user_id' => 'required',
'commentable_id' => 'required',
'commentable_type' => 'required|in:' . ProcessRequestToken::class . ',' . ProcessRequest::class,
'subject' => 'required',
'body' => 'required',
'hidden' => 'required|boolean',
'type' => 'required|in:LOG,MESSAGE',
];
}
/**
* Scope comments hidden
*
* @param $query
* @param $parameter hidden, visible, all
*
* @return mixed
*/
public function scopeHidden($query, $parameter)
{
switch ($parameter) {
case 'visible':
return $query->where('hidden', false);
break;
case 'hidden':
return $query->where('hidden', true);
break;
case 'ALL':
return $query;
break;
}
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function commentable()
{
return $this->morphTo(null, null, 'commentable_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Children comments with user
*/
public function children()
{
return $this->hasMany(self::class, 'commentable_id', 'id')
->where('commentable_type', self::class)
->with('user');
}
/**
* Replied message.
*/
public function repliedMessage()
{
return $this->hasOne(self::class, 'id', 'parent_id')
->with('user');
}
/**
* Get element_name attribute for notifications
*/
public function getElementNameAttribute()
{
if ($this->commentable instanceof ProcessRequest) {
return $this->commentable->name;
} elseif ($this->commentable instanceof ProcessRequestToken) {
return $this->commentable->getDefinition()['name'];
} elseif ($this->commentable instanceof Media) {
return $this->commentable->manager_name;
} elseif ($this->commentable instanceof self) {
return $this->commentable->element_name;
} elseif ($this->commentable instanceof Process) {
return $this->commentable->name;
} else {
return get_class($this->commentable);
}
}
public function setBodyAttribute($value)
{
// Get al mentions and replace with the user id in mustaches
$value = mb_ereg_replace_callback('(^|\s)([@][\p{L}\p{N}\-_]+)', function ($matches) {
$username = str_replace([' @', '@'], '', $matches[0]);
$user = User::where('username', $username)->first();
if ($user) {
return ' {{' . $user->id . '}}';
}
return $matches[0];
}, $value);
$this->attributes['body'] = $value;
}
public function getBodyAttribute($body)
{
// Replace mustache user id with username
$body = preg_replace_callback('/\{\{(\d+)\}\}/', function ($matches) {
$user = User::find($matches[1]);
if ($user) {
return '@' . $user->username;
}
}, $body);
return $body;
}
/**
* Get url attribute for notifications
*/
public function getUrlAttribute($id = null)
{
if (!$id) {
$id = $this->id;
}
if ($this->commentable instanceof ProcessRequest) {
return sprintf('/requests/%s#comment-%s', $this->commentable->id, $id);
} elseif ($this->commentable instanceof ProcessRequestToken) {
return sprintf('/tasks/%s/edit#comment-%s', $this->commentable->id, $id);
} elseif ($this->commentable instanceof Process) {
return sprintf('/modeler/%s#comment-%s', $this->commentable->id, $id);
} elseif ($this->commentable instanceof Media) {
return $this->commentable->manager_url;
} elseif ($this->commentable instanceof self) {
return $this->commentable->getUrlAttribute($this->id);
} else {
return '/';
}
}
}