|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Http\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Facades\Event; |
|
7
|
|
|
|
|
8
|
|
|
use Fabrica\Events\IssueEvent; |
|
9
|
|
|
use Fabrica\Http\Requests; |
|
10
|
|
|
use Fabrica\Http\Api\Controller; |
|
11
|
|
|
use Fabrica\Customization\Eloquent\State; |
|
12
|
|
|
use Fabrica\Project\Eloquent\Labels; |
|
13
|
|
|
use Fabrica\Project\Eloquent\Board; |
|
14
|
|
|
use Fabrica\Project\Provider; |
|
15
|
|
|
use DB; |
|
16
|
|
|
|
|
17
|
|
|
class LabelsController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->middleware('privilege:manage_project'); |
|
22
|
|
|
parent::__construct(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Display a listing of the resource. |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Illuminate\Http\Response |
|
29
|
|
|
*/ |
|
30
|
|
|
public function index(Request $request, $project_key) |
|
31
|
|
|
{ |
|
32
|
|
|
$labels = Labels::where([ 'project_key' => $project_key ])->orderBy('_id', 'asc')->get(); |
|
33
|
|
|
foreach ($labels as $key => $label) |
|
34
|
|
|
{ |
|
35
|
|
|
$label->is_used = $this->isFieldUsedByIssue($project_key, 'labels', $label->toArray()); |
|
36
|
|
|
|
|
37
|
|
|
$completed_issue_cnt = $incompleted_issue_cnt = 0; |
|
|
|
|
|
|
38
|
|
|
$unresolved_cnt = DB::collection('issue_' . $project_key) |
|
39
|
|
|
->where('resolution', 'Unresolved') |
|
40
|
|
|
->where('labels', $label['name']) |
|
41
|
|
|
->where('del_flg', '<>', 1) |
|
42
|
|
|
->count(); |
|
43
|
|
|
$label->unresolved_cnt = $unresolved_cnt; |
|
44
|
|
|
|
|
45
|
|
|
$all_cnt = DB::collection('issue_' . $project_key) |
|
46
|
|
|
->where('labels', $label['name']) |
|
47
|
|
|
->where('del_flg', '<>', 1) |
|
48
|
|
|
->count(); |
|
49
|
|
|
$label->all_cnt = $all_cnt; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return response()->json([ 'ecode' => 0, 'data' => $labels ]); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Store a newly created resource in storage. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \Illuminate\Http\Request $request |
|
59
|
|
|
* @return \Illuminate\Http\Response |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function store(Request $request, $project_key) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$name = $request->input('name'); |
|
64
|
|
|
if (!$name) { |
|
65
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -16100); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (Provider::isLabelExisted($project_key, $name)) { |
|
69
|
|
|
throw new \UnexpectedValueException('label name cannot be repeated', -16102); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$label = Labels::create([ 'project_key' => $project_key ] + $request->all()); |
|
73
|
|
|
return response()->json(['ecode' => 0, 'data' => $label]); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Update the specified resource in storage. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Illuminate\Http\Request $request |
|
80
|
|
|
* @param int $id |
|
81
|
|
|
* @return \Illuminate\Http\Response |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
public function update(Request $request, $project_key, $id) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$name = $request->input('name'); |
|
86
|
|
|
if (isset($name)) { |
|
87
|
|
|
if (!$name) { |
|
88
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -16100); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$label = Labels::find($id); |
|
93
|
|
|
if (!$label || $project_key != $label->project_key) { |
|
94
|
|
|
throw new \UnexpectedValueException('the label does not exist or is not in the project.', -16103); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($label->name !== $name && Provider::isLabelExisted($project_key, $name)) { |
|
98
|
|
|
throw new \UnexpectedValueException('label name cannot be repeated', -16102); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($label->name !== $name) { |
|
102
|
|
|
$this->updIssueLabels($project_key, $label->name, $name); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$label->fill($request->except(['project_key']))->save(); |
|
106
|
|
|
|
|
107
|
|
|
return response()->json(['ecode' => 0, 'data' => Labels::find($id)]); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Remove the specified resource from storage. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Illuminate\Http\Request $request |
|
114
|
|
|
* @param int $id |
|
115
|
|
|
* @return \Illuminate\Http\Response |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
public function delete(Request $request, $project_key, $id) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$label = Labels::find($id); |
|
120
|
|
|
if (!$label || $project_key != $label->project_key) { |
|
121
|
|
|
throw new \UnexpectedValueException('the label does not exist or is not in the project.', -16103); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$operate_flg = $request->input('operate_flg'); |
|
125
|
|
|
if (!isset($operate_flg) || $operate_flg === '0') { |
|
126
|
|
|
$is_used = $this->isFieldUsedByIssue($project_key, 'labels', $label->toArray()); |
|
127
|
|
|
if ($is_used) { |
|
128
|
|
|
throw new \UnexpectedValueException('the label has been used by some issues.', -16104); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
else if ($operate_flg === '1') { |
|
132
|
|
|
$swap_label = $request->input('swap_label'); |
|
133
|
|
|
if (!isset($swap_label) || !$swap_label) { |
|
134
|
|
|
throw new \UnexpectedValueException('the swap label cannot be empty.', -16106); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$slabel = Labels::find($swap_label); |
|
138
|
|
|
if (!$slabel || $project_key != $slabel->project_key) { |
|
139
|
|
|
throw new \UnexpectedValueException('the swap label does not exist or is not in the project.', -16107); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->updIssueLabels($project_key, $label->name, $slabel->name); |
|
143
|
|
|
} |
|
144
|
|
|
else if ($operate_flg === '2') { |
|
145
|
|
|
$this->updIssueLabels($project_key, $label->name, ''); |
|
146
|
|
|
} |
|
147
|
|
|
else |
|
148
|
|
|
{ |
|
149
|
|
|
throw new \UnexpectedValueException('the operation has error.', -16105); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
Labels::destroy($id); |
|
153
|
|
|
|
|
154
|
|
|
return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
//if ($operate_flg === '1') |
|
157
|
|
|
//{ |
|
158
|
|
|
// return $this->show($project_key, $request->input('swap_label')); |
|
159
|
|
|
//} |
|
160
|
|
|
//else |
|
161
|
|
|
//{ |
|
162
|
|
|
// return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]); |
|
163
|
|
|
//} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* update the issues label |
|
168
|
|
|
* |
|
169
|
|
|
* @param array $issues |
|
|
|
|
|
|
170
|
|
|
* @param string $source |
|
171
|
|
|
* @param string $dest |
|
172
|
|
|
* @return \Illuminate\Http\Response |
|
173
|
|
|
*/ |
|
174
|
|
|
public function updIssueLabels($project_key, $source, $dest) |
|
175
|
|
|
{ |
|
176
|
|
|
$issues = DB::collection('issue_' . $project_key) |
|
177
|
|
|
->where('labels', $source) |
|
178
|
|
|
->where('del_flg', '<>', 1) |
|
179
|
|
|
->get(); |
|
180
|
|
|
|
|
181
|
|
|
foreach ($issues as $issue) |
|
182
|
|
|
{ |
|
183
|
|
|
$updValues = []; |
|
184
|
|
|
|
|
185
|
|
|
$newLabels = []; |
|
186
|
|
|
foreach ($issue['labels'] as $label) |
|
187
|
|
|
{ |
|
188
|
|
|
if ($source == $label) { |
|
189
|
|
|
if ($dest) { |
|
190
|
|
|
$newLabels[] = $dest; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
else |
|
194
|
|
|
{ |
|
195
|
|
|
$newLabels[] = $label; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
$updValues['labels'] = array_values(array_unique(array_filter($newLabels))); |
|
199
|
|
|
|
|
200
|
|
|
$updValues['modifier'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ]; |
|
201
|
|
|
$updValues['updated_at'] = time(); |
|
202
|
|
|
|
|
203
|
|
|
$issue_id = $issue['_id']->__toString(); |
|
204
|
|
|
|
|
205
|
|
|
DB::collection('issue_' . $project_key)->where('_id', $issue_id)->update($updValues); |
|
206
|
|
|
// add to histroy table |
|
207
|
|
|
$snap_id = Provider::snap2His($project_key, $issue_id, [], [ 'labels' ]); |
|
208
|
|
|
// trigger event of issue edited |
|
209
|
|
|
Event::fire(new IssueEvent($project_key, $issue_id, $updValues['modifier'], [ 'event_key' => 'edit_issue', 'snap_id' => $snap_id ])); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.