1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @Author: ahmadnorin |
5
|
|
|
* @Date: 2017-11-28 00:12:29 |
6
|
|
|
* @Last Modified by: ahmadnorin |
7
|
|
|
* @Last Modified time: 2017-11-28 09:47:54 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\Http\Controllers; |
11
|
|
|
|
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use App\ApiKeys; |
14
|
|
|
use Bantenprov\Workflow\Models\WorkflowModel; |
15
|
|
|
use Bantenprov\Workflow\Models\WorkflowState; |
16
|
|
|
use Bantenprov\Workflow\Models\WorkflowTransition; |
17
|
|
|
use Bantenprov\Workflow\Models\History; |
18
|
|
|
use That0n3guy\Transliteration; |
19
|
|
|
use Validator, Session, Redirect; |
20
|
|
|
|
21
|
|
|
class ApiManagerController extends Controller |
22
|
|
|
{ |
23
|
|
|
public function index(Request $request) |
24
|
|
|
{ |
25
|
|
|
if($request->get('search') != '') |
26
|
|
|
{ |
27
|
|
|
$data['data'] = ApiKeys::where('client', 'like', '%'.$request->get('search').'%') |
|
|
|
|
28
|
|
|
->orderBy('id', 'desc') |
29
|
|
|
->paginate(env('PAGINATE', 10)); |
30
|
|
|
} |
31
|
|
|
else |
32
|
|
|
{ |
33
|
|
|
$data['data'] = ApiKeys::orderBy('id', 'desc')->paginate(env('PAGINATE', 10)); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
return view('api_manager.index', $data); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create() |
39
|
|
|
{ |
40
|
|
|
return view('api_manager.create'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function store(Request $request) |
44
|
|
|
{ |
45
|
|
|
$validator = Validator::make($request->all(), [ |
46
|
|
|
'client' => 'required|unique:api_keys,client', |
47
|
|
|
'description' => 'required', |
48
|
|
|
]); |
49
|
|
|
if($validator->fails()) |
50
|
|
|
{ |
51
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
52
|
|
|
return redirect()->back() |
53
|
|
|
->withErrors($validator) |
54
|
|
|
->withInput(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$token = $this->token(); |
58
|
|
|
$api = New ApiKeys; |
59
|
|
|
$api->client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
60
|
|
|
$api->api_key = $token; |
61
|
|
|
$api->description = $request->input('description'); |
62
|
|
|
$api->user_id = 1; |
63
|
|
|
$api->save(); |
64
|
|
|
|
65
|
|
|
//create history default |
66
|
|
|
$model = "ApiKeys"; |
67
|
|
|
$fromState = "propose"; |
68
|
|
|
$toState = "propose"; |
69
|
|
|
$workflow = $this->getWorkflow($model); |
70
|
|
|
$statesFrom = $this->getState($fromState); |
71
|
|
|
$statesTo = $this->getState($toState); |
72
|
|
|
$this->saveHistory($api, $workflow, $statesFrom, $statesTo); |
73
|
|
|
|
74
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly'); |
75
|
|
|
return Redirect::to('api-manager'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function edit(Request $request, $id) |
79
|
|
|
{ |
80
|
|
|
$data['data'] = ApiKeys::findOrFail($id); |
|
|
|
|
81
|
|
|
return view('api_manager.edit', $data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function update(Request $request, $id) |
85
|
|
|
{ |
86
|
|
|
$validator = Validator::make($request->all(), [ |
87
|
|
|
'client' => 'required|unique:api_keys,client,'.$id, |
88
|
|
|
'description' => 'required', |
89
|
|
|
]); |
90
|
|
|
if($validator->fails()) |
91
|
|
|
{ |
92
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
93
|
|
|
return redirect()->back() |
94
|
|
|
->withErrors($validator) |
95
|
|
|
->withInput(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$api = ApiKeys::findOrFail($id); |
99
|
|
|
$api->client = str_replace(array('https://', 'http://'),array('',''),$request->input('client')); |
100
|
|
|
$api->description = $request->input('description'); |
101
|
|
|
$api->save(); |
102
|
|
|
Session::flash('message', 'Api Keys Data Update Successfuly'); |
103
|
|
|
return Redirect::to('api-manager'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function token() |
107
|
|
|
{ |
108
|
|
|
$length = 70; |
109
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
110
|
|
|
$charactersLength = strlen($characters); |
111
|
|
|
$randomString = ''; |
112
|
|
|
for ($i = 0; $i < $length; $i++) { |
113
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
114
|
|
|
} |
115
|
|
|
return $randomString; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function getWorkflow($model){ |
119
|
|
|
$data = WorkflowModel::where('content_type', 'like', '%' . $model . '%')->get(); |
120
|
|
|
return $data; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function getState($state){ |
124
|
|
|
$name = \Transliteration::clean_filename(strtolower($state)); |
125
|
|
|
$data = WorkflowState::where('status', 1)->where('name', 'like', '%' . $name . '%')->get(); |
126
|
|
|
return $data; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function saveHistory($api, $workflow, $statesFrom, $statesTo){ |
130
|
|
|
$history = New History; |
131
|
|
|
$history->content_id = $api->id; |
132
|
|
|
$history->Workflow_id = $workflow[0]->id; |
133
|
|
|
$history->from_state = $statesFrom[0]->id; |
134
|
|
|
$history->to_state = $statesTo[0]->id; |
135
|
|
|
$history->user_id = 1; |
136
|
|
|
$history->save(); |
137
|
|
|
return $history; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.