1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\VueWorkflow\Http\Traits; |
4
|
|
|
use Bantenprov\VueWorkflow\Models\WorkflowType; |
5
|
|
|
use Bantenprov\VueWorkflow\Models\History; |
6
|
|
|
use Bantenprov\VueWorkflow\Models\TransitionState; |
7
|
|
|
use Bantenprov\VueWorkflow\Models\Transition; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Workflow trait |
12
|
|
|
*/ |
13
|
|
|
trait WorkflowTrait |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
public function insertWithWorkflow($class, $datas, $method_type = 'create', $entity_id = null) |
17
|
|
|
{ |
18
|
|
|
$workflow = WorkflowType::where('content_type', class_basename($class)); |
19
|
|
|
|
20
|
|
|
if($workflow->count() > 0){ |
21
|
|
|
|
22
|
|
|
if($method_type == 'update' && !is_null($entity_id)){ |
23
|
|
|
$get = $class->$method_type($datas); |
|
|
|
|
24
|
|
|
$content_id = $entity_id; |
25
|
|
|
}else{ |
26
|
|
|
$get = $class->$method_type($datas); |
27
|
|
|
$content_id = $get->id; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$transition = Transition::where('name','propose-to-propose')->where('workflow_id',$workflow->first()->workflow_id)->first(); |
31
|
|
|
|
32
|
|
|
$this->storeHistory( |
33
|
|
|
class_basename($class), |
34
|
|
|
$content_id, |
35
|
|
|
$workflow->first()->workflow_id, |
36
|
|
|
$transition->id, |
37
|
|
|
$transition->from, |
38
|
|
|
$transition->to, |
39
|
|
|
\Auth::user()->id |
40
|
|
|
); |
41
|
|
|
}else{ |
42
|
|
|
$class->create($datas); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function storeHistory($content_type, $content_id, $workflow_id, $workflow_transition_id, $from_state, $to_state, $user_id, $message = '') |
48
|
|
|
{ |
49
|
|
|
$save['content_type'] = $content_type; |
|
|
|
|
50
|
|
|
$save['content_id'] = $content_id; |
51
|
|
|
$save['workflow_id'] = $workflow_id; |
52
|
|
|
$save['workflow_transition_id'] = $workflow_transition_id; |
53
|
|
|
$save['from_state'] = $from_state; |
54
|
|
|
$save['to_state'] = $to_state; |
55
|
|
|
$save['user_id'] = $user_id; |
56
|
|
|
$save['message'] = $message; |
57
|
|
|
|
58
|
|
|
$history = History::create($save); |
59
|
|
|
|
60
|
|
|
$this->storeTransitionState($history->to_state ,$history->content_id, $history->id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function storeTransitionState($current_state, $content_id, $history_id) |
64
|
|
|
{ |
65
|
|
|
$save['current_state'] = $current_state; |
|
|
|
|
66
|
|
|
$save['content_id'] = $content_id; |
67
|
|
|
$save['history_id'] = $history_id; |
68
|
|
|
|
69
|
|
|
TransitionState::create($save); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function updateWithWorkflow($class, $content_id, $request) |
73
|
|
|
{ |
74
|
|
|
$check = History::where('content_id', $content_id); |
75
|
|
|
|
76
|
|
|
$response = []; |
77
|
|
|
|
78
|
|
|
if($check->count() > 1){ |
79
|
|
|
$response['message'] = "Cant update, current state must be propose !"; |
80
|
|
|
}else{ |
81
|
|
|
$class->update($request); |
82
|
|
|
$response['message'] = "Update success"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $response; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.