|
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\Model as BaseModel; |
|
15
|
|
|
use Tinyissue\Model; |
|
16
|
|
|
use Tinyissue\Model\Traits\CountAttributeTrait; |
|
17
|
|
|
use Tinyissue\Model\Traits\Project\Issue\CountTrait; |
|
18
|
|
|
use Tinyissue\Model\Traits\Project\Issue\CrudTagTrait; |
|
19
|
|
|
use Tinyissue\Model\Traits\Project\Issue\CrudTrait; |
|
20
|
|
|
use Tinyissue\Model\Traits\Project\Issue\QueryTrait; |
|
21
|
|
|
use Tinyissue\Model\Traits\Project\Issue\QueueTrait; |
|
22
|
|
|
use Tinyissue\Model\Traits\Project\Issue\RelationTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Issue is model class for project issues. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
* @property int $id |
|
30
|
|
|
* @property int $created_by |
|
31
|
|
|
* @property int $project_id |
|
32
|
|
|
* @property string $title |
|
33
|
|
|
* @property string $body |
|
34
|
|
|
* @property int $assigned_to |
|
35
|
|
|
* @property int $time_quote |
|
36
|
|
|
* @property bool $lock_quote |
|
37
|
|
|
* @property int $closed_by |
|
38
|
|
|
* @property int $closed_at |
|
39
|
|
|
* @property int status |
|
40
|
|
|
* @property int $updated_at |
|
41
|
|
|
* @property int $updated_by |
|
42
|
|
|
* @property Model\Project $project |
|
43
|
|
|
* @property Model\User $user |
|
44
|
|
|
* @property Model\User $updatedBy |
|
45
|
|
|
*/ |
|
46
|
|
|
class Issue extends BaseModel |
|
47
|
|
|
{ |
|
48
|
|
|
use CountAttributeTrait, |
|
49
|
|
|
CountTrait, |
|
50
|
|
|
CrudTrait, |
|
51
|
|
|
CrudTagTrait, |
|
52
|
|
|
RelationTrait, |
|
53
|
|
|
QueryTrait, |
|
54
|
|
|
QueueTrait; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Issue status: Open. |
|
58
|
|
|
* |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
const STATUS_OPEN = 1; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Issue status: Closed. |
|
65
|
|
|
* |
|
66
|
|
|
* @var int |
|
67
|
|
|
*/ |
|
68
|
|
|
const STATUS_CLOSED = 0; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Timestamp enabled. |
|
72
|
|
|
* |
|
73
|
|
|
* @var bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public $timestamps = true; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Name of database table. |
|
79
|
|
|
* |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $table = 'projects_issues'; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* List of allowed columns to be used in $this->fill(). |
|
86
|
|
|
* |
|
87
|
|
|
* @var array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote', 'lock_quote']; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set attributes default value. |
|
93
|
|
|
* |
|
94
|
|
|
* @var array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $attributes = [ |
|
97
|
|
|
'status' => self::STATUS_OPEN, |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the aggregate value of number of comments in an issue. |
|
102
|
|
|
* |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
6 |
|
public function getCountCommentsAttribute() |
|
106
|
|
|
{ |
|
107
|
6 |
|
return $this->getCountAttribute('countComments'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Generate a URL for the active project. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $url |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
30 |
|
public function to($url = '') |
|
118
|
|
|
{ |
|
119
|
30 |
|
return \URL::to('project/' . $this->project_id . '/issue/' . $this->id . (($url) ? '/' . $url : '')); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Convert time quote from an array into seconds. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $value |
|
126
|
|
|
*/ |
|
127
|
36 |
|
public function setTimeQuoteAttribute($value) |
|
128
|
|
|
{ |
|
129
|
36 |
|
$seconds = $value; |
|
130
|
36 |
|
if (is_array($value)) { |
|
131
|
35 |
|
$seconds = 0; |
|
132
|
35 |
|
$seconds += isset($value['m']) ? ($value['m'] * 60) : 0; |
|
133
|
35 |
|
$seconds += isset($value['h']) ? ($value['h'] * 60 * 60) : 0; |
|
134
|
|
|
} |
|
135
|
36 |
|
$this->attributes['time_quote'] = (int) $seconds; |
|
136
|
36 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns the color of tag status. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getTypeColorAttribute() |
|
144
|
|
|
{ |
|
145
|
1 |
|
$tag = $this->tags->filter(function (Model\Tag $tag) { |
|
|
|
|
|
|
146
|
1 |
|
return $tag->parent->name === 'type'; |
|
147
|
1 |
|
})->first(); |
|
148
|
|
|
|
|
149
|
1 |
|
if ($tag) { |
|
150
|
|
|
return $tag->bgcolor; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
return null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Whether or not the issue is new. |
|
158
|
|
|
* |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
|
|
public function isNew() |
|
162
|
|
|
{ |
|
163
|
|
|
if ($this->status === 0) { |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $this->tags->count() === 0; |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Whether or not the issue is open or closed. |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
27 |
|
public function isOpen() |
|
176
|
|
|
{ |
|
177
|
27 |
|
return (boolean) $this->status; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Check if the issue contains a tag with option to set the issue as readonly to current user. |
|
182
|
|
|
* |
|
183
|
|
|
* @param Model\User $user |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
14 |
|
public function hasReadOnlyTag(Model\User $user) |
|
188
|
|
|
{ |
|
189
|
14 |
|
$hasReadOnly = $this->tags->where('readonly', $user->role_id, false); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
14 |
|
return !$hasReadOnly->isEmpty(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Whether or not the issue quote is locked by manager. |
|
196
|
|
|
* |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
11 |
|
public function isQuoteLocked() |
|
200
|
|
|
{ |
|
201
|
11 |
|
return (boolean) $this->lock_quote; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Check if a user is allowed to see the issue quote. |
|
206
|
|
|
* |
|
207
|
|
|
* @param Model\User $user |
|
208
|
|
|
* |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
23 |
|
public function canUserViewQuote(Model\User $user = null) |
|
212
|
|
|
{ |
|
213
|
23 |
|
if ($user && $this->time_quote > 0 && |
|
214
|
23 |
|
(!$this->isQuoteLocked() || $user->permission(Model\Permission::PERM_ISSUE_VIEW_LOCKED_QUOTE)) |
|
215
|
|
|
) { |
|
216
|
5 |
|
return true; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
23 |
|
return false; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Whether or not a user is the creator of the issue. |
|
224
|
|
|
* |
|
225
|
|
|
* @param Model\User $user |
|
226
|
|
|
* |
|
227
|
|
|
* @return bool |
|
228
|
|
|
*/ |
|
229
|
17 |
|
public function isCreatedBy(Model\User $user) |
|
230
|
|
|
{ |
|
231
|
17 |
|
return $this->created_by === $user->id; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Whether a user can view the issue. |
|
236
|
|
|
* |
|
237
|
|
|
* @param Model\User $user |
|
238
|
|
|
* |
|
239
|
|
|
* @return bool |
|
240
|
|
|
*/ |
|
241
|
9 |
|
public function canView(Model\User $user) |
|
242
|
|
|
{ |
|
243
|
|
|
// not access if issue limited to developers and managers, or user is not member of the project |
|
244
|
|
|
if ( |
|
245
|
9 |
|
($this->project->isPrivateInternal() && $user->isUser() && !$this->isCreatedBy($user)) || |
|
246
|
9 |
|
!$this->project->isMember($user->id) |
|
247
|
|
|
) { |
|
248
|
2 |
|
return false; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
9 |
|
return true; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Whether a user can edit the issue. |
|
256
|
|
|
* |
|
257
|
|
|
* @param Model\User $user |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool |
|
260
|
|
|
*/ |
|
261
|
17 |
|
public function canEdit(Model\User $user) |
|
262
|
|
|
{ |
|
263
|
|
|
// If you have permission to modify issue or a creator and current tag is not read only. |
|
264
|
17 |
|
return ($this->isCreatedBy($user) && !$this->hasReadOnlyTag($user)) || ($this->canView($user) && $user->permission(Model\Permission::PERM_ISSUE_MODIFY)); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.