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\Form; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Issue is a class to defines fields & rules for add/edit issue form. |
18
|
|
|
* |
19
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Issue extends FormAbstract |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* An instance of project model. |
25
|
|
|
* |
26
|
|
|
* @var Model\Project |
27
|
|
|
*/ |
28
|
|
|
protected $project; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $params |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
6 |
|
public function setup(array $params) |
36
|
|
|
{ |
37
|
6 |
|
$this->project = $params['project']; |
38
|
6 |
|
if (!empty($params['issue'])) { |
39
|
2 |
|
$this->editingModel($params['issue']); |
40
|
|
|
} |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
6 |
|
public function actions() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
6 |
|
'submit' => $this->isEditing() ? 'update_issue' : 'create_issue', |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
6 |
|
public function fields() |
57
|
|
|
{ |
58
|
6 |
|
$issueModify = \Auth::user()->permission('issue-modify'); |
59
|
|
|
|
60
|
6 |
|
$fields = $this->fieldTitle(); |
61
|
6 |
|
$fields += $this->fieldBody(); |
62
|
6 |
|
$fields += $this->fieldTags(); |
63
|
|
|
|
64
|
|
|
// User with modify issue permission can assign users |
65
|
6 |
|
if ($issueModify) { |
66
|
6 |
|
$fields += $this->fieldAssignedTo(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Only on creating new issue |
70
|
6 |
|
if (!$this->isEditing()) { |
71
|
5 |
|
$fields += $this->fieldUpload(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// User with modify issue permission can add quote |
75
|
6 |
|
if ($issueModify) { |
76
|
6 |
|
$fields += $this->fieldTimeQuote(); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
return $fields; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns title field. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
7 |
|
protected function fieldTitle() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
'title' => [ |
91
|
|
|
'type' => 'text', |
92
|
|
|
'label' => 'title', |
93
|
7 |
|
], |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns body field. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
7 |
|
protected function fieldBody() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'body' => [ |
106
|
|
|
'type' => 'textarea', |
107
|
|
|
'label' => 'issue', |
108
|
7 |
|
], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns tags field. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
7 |
|
protected function fieldTags() |
118
|
|
|
{ |
119
|
|
|
// Populate tag fields with the submitted tags |
120
|
7 |
View Code Duplication |
if ($this->isEditing()) { |
|
|
|
|
121
|
|
|
$selectTags = $this->getModel()->tags()->with('parent')->get()->filter(function (Model\Tag $tag) { |
122
|
2 |
|
return !($tag->name == Model\Tag::STATUS_OPEN || $tag->name == Model\Tag::STATUS_CLOSED); |
123
|
2 |
|
})->map(function (Model\Tag $tag) { |
124
|
|
|
return [ |
125
|
|
|
'value' => $tag->id, |
126
|
|
|
'label' => ($tag->fullname), |
127
|
|
|
'bgcolor' => $tag->bgcolor, |
128
|
|
|
]; |
129
|
2 |
|
})->toJson(); |
130
|
|
|
} else { |
131
|
6 |
|
$selectTags = ''; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return [ |
135
|
|
|
'tag' => [ |
136
|
7 |
|
'type' => 'text', |
137
|
7 |
|
'label' => 'tags', |
138
|
|
|
'multiple' => true, |
139
|
7 |
|
'class' => 'tagit', |
140
|
7 |
|
'data_tokens' => htmlentities($selectTags, ENT_QUOTES), |
141
|
7 |
|
], |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns assigned to field. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
6 |
|
protected function fieldAssignedTo() |
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
|
|
'assigned_to' => [ |
154
|
6 |
|
'type' => 'select', |
155
|
6 |
|
'label' => 'assigned_to', |
156
|
6 |
|
'options' => [0 => ''] + $this->project->users()->get()->lists('fullname', 'id')->all(), |
157
|
6 |
|
'value' => (int) $this->project->default_assignee, |
158
|
|
|
], |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns upload field. |
164
|
|
|
* |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
6 |
|
protected function fieldUpload() |
168
|
|
|
{ |
169
|
6 |
|
$user = \Auth::guest() ? new Model\User() : \Auth::user(); |
170
|
6 |
|
$fields = $this->projectUploadFields('upload', $this->project, $user); |
171
|
6 |
|
$fields['upload']['label'] = 'attachments'; |
172
|
|
|
|
173
|
6 |
|
return $fields; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns time quote field. |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
6 |
|
protected function fieldTimeQuote() |
182
|
|
|
{ |
183
|
|
|
return [ |
184
|
|
|
'time_quote' => [ |
185
|
6 |
|
'type' => 'groupField', |
186
|
6 |
|
'label' => 'quote', |
187
|
|
|
'fields' => [ |
188
|
|
|
'h' => [ |
189
|
6 |
|
'type' => 'number', |
190
|
6 |
|
'append' => trans('tinyissue.hours'), |
191
|
6 |
|
'value' => $this->extractQuoteValue('h'), |
192
|
6 |
|
'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4', |
193
|
|
|
], |
194
|
|
|
'm' => [ |
195
|
6 |
|
'type' => 'number', |
196
|
6 |
|
'append' => trans('tinyissue.minutes'), |
197
|
6 |
|
'value' => $this->extractQuoteValue('m'), |
198
|
6 |
|
'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4', |
199
|
|
|
], |
200
|
|
|
], |
201
|
6 |
|
'addClass' => 'row issue-quote', |
202
|
|
|
], |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
7 |
|
public function rules() |
210
|
|
|
{ |
211
|
|
|
$rules = [ |
212
|
7 |
|
'title' => 'required|max:200', |
213
|
|
|
'body' => 'required', |
214
|
|
|
]; |
215
|
|
|
|
216
|
7 |
|
return $rules; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function getRedirectUrl() |
223
|
|
|
{ |
224
|
|
|
if ($this->isEditing()) { |
225
|
|
|
return $this->getModel()->to('edit'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return 'project/' . $this->project->id . '/issue/new'; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Extract number of hours, or minutes, or seconds from a quote. |
233
|
|
|
* |
234
|
|
|
* @param string $part |
235
|
|
|
* |
236
|
|
|
* @return float|int |
237
|
|
|
*/ |
238
|
6 |
|
protected function extractQuoteValue($part) |
239
|
|
|
{ |
240
|
6 |
|
if ($this->getModel() instanceof Model\Project\Issue) { |
241
|
2 |
|
$seconds = $this->getModel()->time_quote; |
242
|
2 |
|
if ($part === 'h') { |
243
|
2 |
|
return floor($seconds / 3600); |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
if ($part === 'm') { |
247
|
2 |
|
return ($seconds / 60) % 60; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
5 |
|
return 0; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.