|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Project; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Relations; |
|
16
|
|
|
use Tinyissue\Model; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* IssueRelations is trait class containing the relationship methods for the Project\Issue model. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @property static $this |
|
24
|
|
|
*/ |
|
25
|
|
|
trait IssueRelations |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* An issue has one user assigned to (inverse relationship of User::issues). |
|
29
|
|
|
* |
|
30
|
|
|
* @return Model\User |
|
31
|
|
|
*/ |
|
32
|
|
|
public function assigned() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->belongsTo(Model\User::class, 'assigned_to'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* An issue has one user updated by (inverse relationship of User::issuesUpdatedBy). |
|
39
|
|
|
* |
|
40
|
|
|
* @return Model\User |
|
41
|
|
|
*/ |
|
42
|
|
|
public function updatedBy() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->belongsTo(Model\User::class, 'updated_by'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* An issue has one user closed it (inverse relationship of User::issuesClosedBy). |
|
49
|
|
|
* |
|
50
|
|
|
* @return Model\User |
|
51
|
|
|
*/ |
|
52
|
|
|
public function closer() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->belongsTo(Model\User::class, 'closed_by'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* An issue has one user created it (inverse relationship of User::issuesCreatedBy). |
|
59
|
|
|
* |
|
60
|
|
|
* @return Model\User |
|
61
|
|
|
*/ |
|
62
|
|
|
public function user() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->belongsTo(Model\User::class, 'created_by'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Issue belong to a project. |
|
69
|
|
|
* |
|
70
|
|
|
* @return Model\Project |
|
71
|
|
|
*/ |
|
72
|
|
|
public function project() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->belongsTo(Model\Project::class); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Issue can have many attachments. |
|
79
|
|
|
* |
|
80
|
|
|
* @return Model\Project\Issue\Attachment |
|
81
|
|
|
*/ |
|
82
|
|
|
public function attachments() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this |
|
85
|
|
|
->hasMany(Model\Project\Issue\Attachment::class, 'issue_id') |
|
86
|
|
|
->where(function (Builder $query) { |
|
87
|
|
|
$query->where('comment_id', '=', 0); |
|
88
|
|
|
$query->orWhere('comment_id', '=', null); |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Issue have many users activities. |
|
94
|
|
|
* |
|
95
|
|
|
* @return Model\User\Activity |
|
96
|
|
|
*/ |
|
97
|
|
|
public function activities() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this |
|
100
|
|
|
->hasMany(Model\User\Activity::class, 'item_id') |
|
101
|
|
|
->orderBy('created_at', 'ASC'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Issue have many users activities (all except comments). |
|
106
|
|
|
* |
|
107
|
|
|
* @return Model\User\Activity |
|
108
|
|
|
*/ |
|
109
|
|
|
public function generalActivities() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this |
|
112
|
|
|
->hasMany(Model\User\Activity::class, 'item_id') |
|
113
|
|
|
->whereNotIn('type_id', [Model\Activity::TYPE_COMMENT]) |
|
114
|
|
|
->orderBy('created_at', 'ASC') |
|
115
|
|
|
->with('activity', 'user', 'assignTo'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Issue have many users activities (comments). |
|
120
|
|
|
* |
|
121
|
|
|
* @return Model\User\Activity |
|
122
|
|
|
*/ |
|
123
|
|
|
public function commentActivities() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this |
|
126
|
|
|
->hasMany(Model\User\Activity::class, 'item_id') |
|
127
|
|
|
->whereIn('type_id', [Model\Activity::TYPE_COMMENT]) |
|
128
|
|
|
->orderBy('created_at', 'ASC') |
|
129
|
|
|
->with('activity', 'user', 'comment', 'assignTo', 'comment.attachments'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Issue have many tags. |
|
134
|
|
|
* |
|
135
|
|
|
* @return Model\Tag |
|
136
|
|
|
*/ |
|
137
|
|
|
public function tags() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->belongsToMany(Model\Tag::class, 'projects_issues_tags', 'issue_id', 'tag_id') |
|
140
|
|
|
->join('tags as p', 'p.id', '=', 'tags.parent_id') |
|
141
|
|
|
->orderBy('parent_id'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Issue have many comments. |
|
146
|
|
|
* |
|
147
|
|
|
* @return Model\Project\Issue\Comment |
|
148
|
|
|
*/ |
|
149
|
|
|
public function comments() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this |
|
152
|
|
|
->hasMany(Model\Project\Issue\Comment::class, 'issue_id') |
|
153
|
|
|
->orderBy('created_at', 'ASC'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Issue can have many messages queue. |
|
158
|
|
|
* |
|
159
|
|
|
* @return Model\Message\Queue |
|
160
|
|
|
*/ |
|
161
|
|
|
public function messagesQueue() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->morphMany(Model\Message\Queue::class, 'model'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Count number of comments in an issue. |
|
168
|
|
|
* |
|
169
|
|
|
* @return Model\Project\Issue\Comment |
|
170
|
|
|
*/ |
|
171
|
|
|
public function countComments() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->hasOne(Model\Project\Issue\Comment::class, 'issue_id') |
|
174
|
|
|
->selectRaw('issue_id, count(*) as aggregate') |
|
175
|
|
|
->groupBy('issue_id'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
abstract public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null); |
|
179
|
|
|
abstract public function hasMany($related, $foreignKey = null, $localKey = null); |
|
180
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
|
181
|
|
|
abstract public function hasOne($related, $foreignKey = null, $localKey = null); |
|
182
|
|
|
abstract public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null); |
|
183
|
|
|
} |
|
184
|
|
|
|