|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bantenprov\VueWorkflow\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Bantenprov\VueWorkflow\Models\Transition; |
|
8
|
|
|
use Bantenprov\VueWorkflow\Models\Workflow; |
|
9
|
|
|
use Bantenprov\VueWorkflow\Models\State; |
|
10
|
|
|
use Validator; |
|
11
|
|
|
|
|
12
|
|
|
class TransitionController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected $transitionModel; |
|
16
|
|
|
protected $workflowModel; |
|
17
|
|
|
protected $stateModel; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* [Function] __construct |
|
21
|
|
|
* @param |
|
22
|
|
|
* |
|
23
|
|
|
* @return |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Transition $transition, Workflow $workflow, State $state) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->transitionModel = $transition; |
|
28
|
|
|
$this->workflowModel = $workflow; |
|
29
|
|
|
$this->stateModel = $state; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* [Function] index |
|
34
|
|
|
* @param Request $req |
|
|
|
|
|
|
35
|
|
|
* |
|
36
|
|
|
* @return json |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function index(Request $request) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
// $response; |
|
41
|
|
|
|
|
42
|
|
|
// $param = explode('|',$req->get('sort')); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
// if($req->get('filter') != ''){ |
|
|
|
|
|
|
45
|
|
|
// $search = "%{$req->get('filter')}%"; |
|
|
|
|
|
|
46
|
|
|
// $response = $this->transitionModel->where('name','like',$search)->orderBy($param[0], $param[1])->paginate(10); |
|
|
|
|
|
|
47
|
|
|
// }else{ |
|
48
|
|
|
// if($req->get('sort') == ''){ |
|
|
|
|
|
|
49
|
|
|
// $response = $this->transitionModel->paginate(10); |
|
|
|
|
|
|
50
|
|
|
// }else{ |
|
51
|
|
|
// $response = $this->transitionModel->orderBy($param[0], $param[1])->paginate(10); |
|
|
|
|
|
|
52
|
|
|
// } |
|
53
|
|
|
// } |
|
54
|
|
|
|
|
55
|
|
|
// foreach($response as $kegiatan){ |
|
|
|
|
|
|
56
|
|
|
// array_set($response->data, 'workflow_id', $kegiatan->kegiatan->label); |
|
|
|
|
|
|
57
|
|
|
// } |
|
58
|
|
|
|
|
59
|
|
|
// =================================================== |
|
60
|
|
|
|
|
61
|
|
|
if (request()->has('sort')) { |
|
62
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
|
63
|
|
|
|
|
64
|
|
|
$query = $this->transitionModel->orderBy($sortCol, $sortDir); |
|
65
|
|
|
} else { |
|
66
|
|
|
$query = $this->transitionModel->orderBy('id', 'asc'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($request->exists('filter')) { |
|
70
|
|
|
$query->where(function($q) use($request) { |
|
71
|
|
|
$value = "%{$request->filter}%"; |
|
72
|
|
|
$q->where('label', 'like', $value) |
|
73
|
|
|
->orWhere('description', 'like', $value); |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
|
78
|
|
|
$response = $query->paginate($perPage); |
|
79
|
|
|
|
|
80
|
|
|
foreach($response as $workflow){ |
|
81
|
|
|
array_set($response->data, 'workflow_id', $workflow->getWorkflow->label); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return response()->json($response); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* [Function] create |
|
91
|
|
|
* @param |
|
92
|
|
|
* |
|
93
|
|
|
* @return json |
|
94
|
|
|
*/ |
|
95
|
|
|
public function create() |
|
96
|
|
|
{ |
|
97
|
|
|
$response = $this->workflowModel->all(); |
|
98
|
|
|
|
|
99
|
|
|
return response()->json($response); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* [Function] getWorkflowState |
|
104
|
|
|
* @param |
|
105
|
|
|
* |
|
106
|
|
|
* @return json |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getWorkflowState($id) |
|
109
|
|
|
{ |
|
110
|
|
|
if($id == 'undefined'){ |
|
111
|
|
|
$response = []; |
|
112
|
|
|
}else{ |
|
113
|
|
|
$response = $this->stateModel->where('workflow_id',$id)->get(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return response()->json($response); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* [Function] store |
|
121
|
|
|
* @param Request $req |
|
122
|
|
|
* |
|
123
|
|
|
* @return json |
|
124
|
|
|
*/ |
|
125
|
|
|
public function store(Request $req) |
|
126
|
|
|
{ |
|
127
|
|
|
$request['label'] = $req->label; |
|
|
|
|
|
|
128
|
|
|
$request['name'] = strtolower($req->name); |
|
129
|
|
|
$request['workflow_id'] = $req->workflow_id; |
|
130
|
|
|
$request['from'] = $req->from; |
|
131
|
|
|
$request['to'] = $req->to; |
|
132
|
|
|
$request['message'] = $req->message; |
|
133
|
|
|
|
|
134
|
|
|
// $check = $this->transitionModel->where('name',$req->name)->whereNull('deleted_at')->count(); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$validator = Validator::make($req->all(),[ |
|
137
|
|
|
'name' => 'required', |
|
138
|
|
|
'label' => 'required', |
|
139
|
|
|
'message' => 'required', |
|
140
|
|
|
'from' => 'required', |
|
141
|
|
|
'to' => 'required', |
|
142
|
|
|
'workflow_id' => 'required', |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
View Code Duplication |
if($validator->fails()){ |
|
|
|
|
|
|
146
|
|
|
//if($check > 0){ |
|
|
|
|
|
|
147
|
|
|
$response['message'] = 'failed transition allready exist !'; |
|
|
|
|
|
|
148
|
|
|
$response['status'] = false; |
|
149
|
|
|
// }else{ |
|
150
|
|
|
// $response['message'] = 'success add new transition'; |
|
|
|
|
|
|
151
|
|
|
// $response['status'] = true; |
|
|
|
|
|
|
152
|
|
|
// $this->transitionModel->create($request); |
|
|
|
|
|
|
153
|
|
|
// } |
|
154
|
|
|
}else{ |
|
155
|
|
|
$response['message'] = 'success add new transition'; |
|
|
|
|
|
|
156
|
|
|
$response['status'] = true; |
|
157
|
|
|
$this->transitionModel->create($request); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return response()->json($response); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* [Function] edit |
|
165
|
|
|
* @param $id |
|
166
|
|
|
* |
|
167
|
|
|
* @return json |
|
168
|
|
|
*/ |
|
169
|
|
|
public function edit($id) |
|
170
|
|
|
{ |
|
171
|
|
|
$response = $this->transitionModel |
|
172
|
|
|
->with('stateTo') |
|
173
|
|
|
->with('stateFrom') |
|
174
|
|
|
->with('getWorkflow') |
|
175
|
|
|
->findOrFail($id); |
|
176
|
|
|
$response['status'] = true; |
|
177
|
|
|
return response()->json($response); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* [Function] update |
|
182
|
|
|
* @param Request $req |
|
183
|
|
|
* @param $id |
|
184
|
|
|
* |
|
185
|
|
|
* @return json |
|
186
|
|
|
*/ |
|
187
|
|
|
public function update(Request $req,$id) |
|
188
|
|
|
{ |
|
189
|
|
|
$request['label'] = $req->label; |
|
|
|
|
|
|
190
|
|
|
$request['name'] = strtolower($req->name); |
|
191
|
|
|
$request['workflow_id'] = $req->workflow_id; |
|
192
|
|
|
$request['from'] = $req->from; |
|
193
|
|
|
$request['to'] = $req->to; |
|
194
|
|
|
$request['message'] = $req->message; |
|
195
|
|
|
|
|
196
|
|
|
$check = $this->transitionModel->where('name',$req->name)->whereNull('deleted_at')->count(); |
|
197
|
|
|
|
|
198
|
|
|
if($req->old_name == $req->name){ |
|
199
|
|
|
$validator = Validator::make($req->all(),[ |
|
200
|
|
|
'label' => 'required', |
|
201
|
|
|
'name' => 'required', |
|
202
|
|
|
'message' => 'required', |
|
203
|
|
|
'from' => 'required', |
|
204
|
|
|
'to' => 'required', |
|
205
|
|
|
'workflow_id' => 'required', |
|
206
|
|
|
]); |
|
207
|
|
|
}else{ |
|
208
|
|
|
$validator = Validator::make($req->all(),[ |
|
209
|
|
|
'label' => 'required', |
|
210
|
|
|
'name' => 'required|unique:workflow_transition,name', |
|
211
|
|
|
'message' => 'required', |
|
212
|
|
|
'from' => 'required', |
|
213
|
|
|
'to' => 'required', |
|
214
|
|
|
'workflow_id' => 'required', |
|
215
|
|
|
]); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
View Code Duplication |
if($validator->fails()){ |
|
|
|
|
|
|
220
|
|
|
if($check > 0){ |
|
221
|
|
|
$response['message'] = 'failed state allready exist !'; |
|
|
|
|
|
|
222
|
|
|
$response['status'] = false; |
|
223
|
|
|
}else{ |
|
224
|
|
|
$response['message'] = 'success add new state'; |
|
|
|
|
|
|
225
|
|
|
$response['status'] = true; |
|
226
|
|
|
$this->transitionModel->findOrFail($id)->update($request); |
|
227
|
|
|
} |
|
228
|
|
|
}else{ |
|
229
|
|
|
$response['message'] = 'success add new state'; |
|
|
|
|
|
|
230
|
|
|
$response['status'] = true; |
|
231
|
|
|
$this->transitionModel->findOrFail($id)->update($request); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
return response()->json($response); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* [Function] destroy |
|
241
|
|
|
* @param Request $req |
|
|
|
|
|
|
242
|
|
|
* |
|
243
|
|
|
* @return json |
|
244
|
|
|
*/ |
|
245
|
|
|
public function destroy($id) |
|
246
|
|
|
{ |
|
247
|
|
|
$state = $this->transitionModel->findOrFail($id)->delete(); |
|
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
return response()->json(['status' => true]); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.