1
|
|
|
<?php |
2
|
|
|
// ganti name class dg WorkflowTransitionController |
3
|
|
|
namespace Bantenprov\VueWorkflow\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Bantenprov\VueWorkflow\Facades\VueWorkflow; |
8
|
|
|
use Bantenprov\VueWorkflow\Models\State; |
9
|
|
|
use Bantenprov\VueWorkflow\Models\Workflow; |
10
|
|
|
use Bantenprov\VueWorkflow\Models\WorkflowType; |
11
|
|
|
use Bantenprov\VueWorkflow\Models\TransitionState; |
12
|
|
|
use Bantenprov\VueWorkflow\Models\Transition; |
13
|
|
|
use Bantenprov\VueWorkflow\Models\History; |
14
|
|
|
|
15
|
|
|
use Bantenprov\VueWorkflow\Http\Traits\WorkflowTrait; |
16
|
|
|
|
17
|
|
|
use App\Http\Controllers\Traits\WorkflowConditionTrait; |
18
|
|
|
|
19
|
|
|
use Validator; |
20
|
|
|
|
21
|
|
|
class WorkflowProcessController extends Controller |
22
|
|
|
{ |
23
|
|
|
use WorkflowTrait; |
24
|
|
|
use WorkflowConditionTrait; |
25
|
|
|
|
26
|
|
|
protected $wokflowModel; |
27
|
|
|
protected $stateModel; |
28
|
|
|
protected $historyModel; |
29
|
|
|
protected $transitionStateModel; |
30
|
|
|
protected $transitionModel; |
31
|
|
|
protected $workflowTypeModel; |
32
|
|
|
|
33
|
|
|
public function __construct(State $state, Workflow $workflow, WorkflowType $workflowType, TransitionState $transitionState, Transition $transition, History $history){ |
34
|
|
|
|
35
|
|
|
$this->wokflowModel = $workflow; |
36
|
|
|
$this->stateModel = $state; |
37
|
|
|
// $this->historyModel = config('vue-workflow.WORKFLOW_HISTORY'); |
|
|
|
|
38
|
|
|
$this->historyModel = $history; |
39
|
|
|
$this->transitionStateModel = $transitionState; |
40
|
|
|
$this->transitionModel = $transition; |
41
|
|
|
$this->workflowTypeModel = $workflowType; |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getLastHistory() |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function availableState($content_type,$content_id) |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
$workflow = WorkflowType::where('content_type', $content_type)->first(); |
54
|
|
|
|
55
|
|
|
if(!$workflow){ |
56
|
|
|
return response()->json([ |
57
|
|
|
'status' => false |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$history = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->orderBy('created_at','desc')->first(); |
62
|
|
|
|
63
|
|
|
if(!$history){ |
64
|
|
|
return response()->json([ |
65
|
|
|
'status' => false |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
$check_history = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->count(); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$transition_state = TransitionState::where('history_id',$history->id)->orderBy('created_at','desc')->first(); |
76
|
|
|
|
77
|
|
|
$transitions = Transition::where('from',$transition_state->current_state)->get(); |
78
|
|
|
|
79
|
|
|
$states = State::where('workflow_id',$history->workflow_id)->get(); |
80
|
|
|
|
81
|
|
|
$current_state = State::find($transition_state->current_state); |
82
|
|
|
|
83
|
|
|
$state_response = []; |
84
|
|
|
|
85
|
|
|
foreach($transitions as $transition) |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
foreach($states as $state) |
89
|
|
|
{ |
90
|
|
|
if($state->id == $transition->to && $state->id != $transition_state->current_state){ |
91
|
|
|
if(!empty($transition->vueGuard->permission_id)){ |
92
|
|
|
$permission = \App\Permission::find($transition->vueGuard->permission_id); |
93
|
|
|
if( (\Auth::user()->hasPermission($permission->name)) ){ |
94
|
|
|
if($this->availableStateCondition()){ |
95
|
|
|
array_push($state_response,$state); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
array_set($state,'permission',$transition->vueGuard->permission_id); |
99
|
|
|
|
100
|
|
|
}else{ |
101
|
|
|
array_set($state,'permission',0); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
if($check_history == 1){ |
107
|
|
|
return response()->json([ |
108
|
|
|
'status' => true, |
109
|
|
|
'state' => $state_response, |
110
|
|
|
'transition_state' => $transition_state, |
111
|
|
|
'current_state' => $current_state, |
112
|
|
|
'current_history' => $history, |
113
|
|
|
'user' => '-' |
114
|
|
|
]); |
115
|
|
|
}elseif($check_history > 1){ |
116
|
|
|
return response()->json([ |
117
|
|
|
'status' => true, |
118
|
|
|
'state' => $state_response, |
119
|
|
|
'transition_state' => $transition_state, |
120
|
|
|
'current_state' => $current_state, |
121
|
|
|
'current_history' => $history, |
122
|
|
|
'user' => (empty($history->user)) ? '-' : $history->user->name |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
return response()->json([ |
126
|
|
|
'status' => false, |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getAllHistoryThisContent(Request $req, $content_type, $content_id){ |
132
|
|
|
|
133
|
|
|
$response; |
|
|
|
|
134
|
|
|
|
135
|
|
|
$param = explode('|',$req->get('sort')); |
136
|
|
|
|
137
|
|
|
$workflow = WorkflowType::where('content_type', $content_type)->first(); |
138
|
|
|
|
139
|
|
|
$histories = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->orderBy('created_at','desc')->paginate(10); |
|
|
|
|
140
|
|
|
|
141
|
|
|
if($req->get('filter') != ''){ |
142
|
|
|
$search = "%{$req->get('filter')}%"; |
143
|
|
|
|
144
|
|
|
$response = $this->historyModel |
145
|
|
|
->where('workflow_id',$workflow->workflow_id) |
146
|
|
|
->where('content_id',$content_id) |
147
|
|
|
->with('fromState') |
148
|
|
|
->with('toState') |
149
|
|
|
->with('user') |
150
|
|
|
->where('content_type','like',$search) |
151
|
|
|
->orderBy($param[0], $param[1]) |
152
|
|
|
->paginate(10); |
153
|
|
|
}else{ |
154
|
|
|
if($req->get('sort') == ''){ |
155
|
|
|
$response = $this->historyModel |
156
|
|
|
->where('workflow_id',$workflow->workflow_id) |
157
|
|
|
->where('content_id',$content_id) |
158
|
|
|
->with('fromState') |
159
|
|
|
->with('toState') |
160
|
|
|
->with('user') |
161
|
|
|
->paginate(10); |
162
|
|
|
}else{ |
163
|
|
|
$response = $this->historyModel |
164
|
|
|
->where('workflow_id',$workflow->workflow_id) |
165
|
|
|
->where('content_id',$content_id) |
166
|
|
|
->with('fromState') |
167
|
|
|
->with('toState') |
168
|
|
|
->with('user') |
169
|
|
|
->orderBy($param[0], $param[1]) |
170
|
|
|
->paginate(10); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
foreach($response as $data){ |
175
|
|
|
if(empty($data->message)){ |
176
|
|
|
$data->message = "Start"; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return response()->json($response); |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function changeState(Request $request, $content_id) |
185
|
|
|
{ |
186
|
|
|
$validator = Validator::make($request->all(), [ |
187
|
|
|
'message' => 'required', |
188
|
|
|
]); |
189
|
|
|
|
190
|
|
|
if($validator->fails()){ |
191
|
|
|
$response['status'] = false; |
|
|
|
|
192
|
|
|
$response['message'] = 'reason is required'; |
193
|
|
|
}else{ |
194
|
|
|
$this->storeHistory( |
195
|
|
|
$request->content_type, |
196
|
|
|
$content_id, |
197
|
|
|
$request->workflow_id, |
198
|
|
|
$request->workflow_transition_id, |
199
|
|
|
$request->from_state, |
200
|
|
|
$request->to_state, |
201
|
|
|
\Auth::user()->id, |
202
|
|
|
$request->message |
203
|
|
|
); |
204
|
|
|
$response['status'] = true; |
|
|
|
|
205
|
|
|
$response['message'] = 'success'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return response()->json($response); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
|
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.