-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDepartmentController.php
More file actions
145 lines (112 loc) · 3.91 KB
/
DepartmentController.php
File metadata and controls
145 lines (112 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace App\Http\Controllers;
use App\Http\Repository\DepartmentRepository;
use App\Http\Requests\CreateDepartmentRequest;
use App\Http\Requests\UpdateDepartmentRequest;
use App\Http\Transformers\DepartmentTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;
class DepartmentController extends AppBaseController
{
/** @var DepartmentRepository */
private $departmentRepository;
protected $permission = 'organisasi-departemen';
public function __construct(DepartmentRepository $departmentRepo)
{
$this->departmentRepository = $departmentRepo;
}
/**
* Display a listing of the Department.
*/
public function index(Request $request)
{
if ($request->ajax()) {
return $this->fractal($this->departmentRepository->listDepartment(), new DepartmentTransformer, 'departments')->respond();
}
$listPermission = $this->generateListPermission();
return view('departments.index')->with($listPermission);
}
/**
* Show the form for creating a new Department.
*/
public function create()
{
return view('departments.create', $this->getOptionItems());
}
/**
* Store a newly created Department in storage.
*/
public function store(CreateDepartmentRequest $request)
{
$input = $request->all();
$department = $this->departmentRepository->create($input);
Session::flash('success', 'Departemen berhasil disimpan.');
return redirect(route('departments.index'));
}
/**
* Display the specified Department.
*/
public function show($id)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Session::flash('error', 'Departemen tidak ditemukan');
return redirect(route('departments.index'));
}
return view('departments.show')->with('department', $department);
}
/**
* Show the form for editing the specified Department.
*/
public function edit($id)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Session::flash('error', 'Departemen tidak ditemukan');
return redirect(route('departments.index'));
}
return view('departments.edit', $this->getOptionItems($id))->with('department', $department);
}
/**
* Update the specified Department in storage.
*/
public function update($id, UpdateDepartmentRequest $request)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Session::flash('error', 'Departemen tidak ditemukan');
return redirect(route('departments.index'));
}
$department = $this->departmentRepository->update($request->all(), $id);
Session::flash('success', 'Departemen berhasil diupdate.');
return redirect(route('departments.index'));
}
/**
* Remove the specified Department from storage.
*
* @throws \Exception
*/
public function destroy($id)
{
$department = $this->departmentRepository->find($id);
if (empty($department)) {
Session::flash('error', 'Departemen tidak ditemukan');
return redirect(route('departments.index'));
}
$this->departmentRepository->delete($id);
if (request()->ajax()) {
return $this->sendSuccess('Departemen berhasil dihapus.');
}
Session::flash('success', 'Departemen berhasil dihapus.');
return redirect(route('departments.index'));
}
protected function getOptionItems($id = null)
{
$parents = (new Collection(['' => 'Pilih departement']))->union((new DepartmentRepository)->pluck());
if ($id) {
$parents->forget($id);
}
return compact('parents');
}
}