Completed
Push — develop ( 4ae1ad...049b1a )
by Mohamed
08:18
created

Issue::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
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 6
    protected function fieldTitle()
88
    {
89
        return [
90
            'title' => [
91
                'type' => 'text',
92
                'label' => 'title',
93 6
            ],
94
        ];
95
    }
96
97
    /**
98
     * Returns body field
99
     *
100
     * @return array
101
     */
102 6
    protected function fieldBody()
103
    {
104
        return [
105
            'body' => [
106
                'type' => 'textarea',
107
                'label' => 'issue',
108 6
            ],
109
        ];
110
    }
111
112
    /**
113
     * Returns tags field
114
     *
115
     * @return array
116
     */
117 6
    protected function fieldTags()
118
    {
119
        // Populate tag fields with the submitted tags
120 6
        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 View Code Duplication
            })->map(function (Model\Tag $tag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
124
                return [
125
                    'value' => $tag->id,
126
                    'label' => ($tag->fullname),
127
                    'bgcolor' => $tag->bgcolor,
128
                ];
129 2
            })->toJson();
130
        } else {
131 5
            $selectTags = '';
132
        }
133
134
        return [
135
            'tag' => [
136 6
                'type' => 'text',
137 6
                'label' => 'tags',
138
                'multiple' => true,
139 6
                'class' => 'tagit',
140 6
                'data_tokens' => htmlentities($selectTags, ENT_QUOTES),
141 6
            ],
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 5
    protected function fieldUpload()
168
    {
169 5
        $fields = $this->projectUploadFields('upload', $this->project, \Auth::user());
170 5
        $fields['upload']['label'] = 'attachments';
171
172 5
        return $fields;
173
    }
174
175
    /**
176
     * Returns time quote field
177
     *
178
     * @return array
179
     */
180 6
    protected function fieldTimeQuote()
181
    {
182
        return [
183
            'time_quote' => [
184 6
                'type' => 'groupField',
185 6
                'label' => 'quote',
186
                'fields' => [
187
                    'h' => [
188 6
                        'type' => 'number',
189 6
                        'append' => trans('tinyissue.hours'),
190 6
                        'value' => $this->extractQuoteValue('h'),
191 6
                        'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4',
192
                    ],
193
                    'm' => [
194 6
                        'type' => 'number',
195 6
                        'append' => trans('tinyissue.minutes'),
196 6
                        'value' => $this->extractQuoteValue('m'),
197 6
                        'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4',
198
                    ],
199
                    's' => [
200 6
                        'type' => 'number',
201 6
                        'append' => trans('tinyissue.seconds'),
202 6
                        'value' => $this->extractQuoteValue('s'),
203 6
                        'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4',
204
                    ],
205
                ],
206 6
                'addClass' => 'row issue-quote',
207
            ],
208
        ];
209
    }
210
211
    /**
212
     * @return array
213
     */
214 6
    public function rules()
215
    {
216
        $rules = [
217 6
            'title' => 'required|max:200',
218
            'body' => 'required',
219
        ];
220
221 6
        return $rules;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getRedirectUrl()
228
    {
229
        if ($this->isEditing()) {
230
            return $this->getModel()->to('edit');
231
        }
232
233
        return 'project/' . $this->project->id . '/issue/new';
234
    }
235
236
    /**
237
     * Extract number of hours, or minutes, or seconds from a quote
238
     *
239
     * @param string $part
240
     *
241
     * @return float|int
242
     */
243 6
    protected function extractQuoteValue($part)
244
    {
245 6
        if ($this->getModel() instanceof Model\Project\Issue) {
246 2
            $seconds = $this->getModel()->time_quote;
247 2
            if ($part === 'h') {
248 2
                return floor($seconds / 3600);
249
            }
250
251 2
            if ($part === 'm') {
252 2
                return (($seconds / 60) % 60);
253
            }
254
255 2
            if ($part === 's') {
256 2
                return $seconds % 60;
257
            }
258
        }
259
260 5
        return 0;
261
    }
262
}
263