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\Issue; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Model\User; |
15
|
|
|
use Tinyissue\Model\Permission; |
16
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
17
|
|
|
use Tinyissue\Model\Project; |
18
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Attachment\CrudTrait; |
19
|
|
|
use Tinyissue\Model\Traits\Project\Issue\Attachment\RelationTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Attachment is model class for project attachments. |
23
|
|
|
* |
24
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
25
|
|
|
* |
26
|
|
|
* @property int $id |
27
|
|
|
* @property int $uploaded_by |
28
|
|
|
* @property int $issue_id |
29
|
|
|
* @property int $comment_id |
30
|
|
|
* @property string $filename |
31
|
|
|
* @property string $fileextension |
32
|
|
|
* @property int $filesize |
33
|
|
|
* @property string $upload_token |
34
|
|
|
* @property Project\Issue $issue |
35
|
|
|
*/ |
36
|
|
|
class Attachment extends BaseModel |
37
|
|
|
{ |
38
|
|
|
use CrudTrait, |
39
|
|
|
RelationTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Timestamp enabled. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
public $timestamps = true; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Name of database table. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $table = 'projects_issues_attachments'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* List of allowed columns to be used in $this->fill(). |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $fillable = [ |
61
|
|
|
'uploaded_by', |
62
|
|
|
'filename', |
63
|
|
|
'fileextension', |
64
|
|
|
'filesize', |
65
|
|
|
'upload_token', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Whether or not the file extension is supported image type. |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isImage() |
74
|
|
|
{ |
75
|
|
|
return in_array($this->fileextension, [ |
76
|
|
|
'jpg', |
77
|
|
|
'jpeg', |
78
|
|
|
'JPG', |
79
|
|
|
'JPEG', |
80
|
|
|
'png', |
81
|
|
|
'PNG', |
82
|
|
|
'gif', |
83
|
|
|
'GIF', |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Url to attachment download. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function download() |
93
|
|
|
{ |
94
|
|
|
return \URL::to('project/' . $this->issue->project_id . '/issue/' . $this->issue_id . '/download/' . $this->id); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Url to display attachment. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function display() |
103
|
|
|
{ |
104
|
|
|
return \URL::to('project/' . $this->issue->project_id . '/issue/' . $this->issue_id . '/display/' . $this->id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generate a URL to delete attachment. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function toDelete() |
113
|
|
|
{ |
114
|
|
|
return \URL::to('project/' . $this->issue->project_id . '/issue/' . $this->issue_id . '/delete/' . $this->id); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Whether a user can edit the attachment. |
119
|
|
|
* |
120
|
|
|
* @param User $user |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function canEdit(User $user) |
125
|
|
|
{ |
126
|
|
|
return $user->id === $this->uploaded_by || $user->permission(Permission::PERM_PROJECT_MODIFY); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|