158 lines
5.1 KiB
PHP
158 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\DataTables\DirectoratDataTable;
|
|
use App\Http\Requests\StoreDirectoratRequest;
|
|
use App\Http\Requests\UpdateDirectoratRequest;
|
|
use App\Models\Directorat;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Spatie\Activitylog\Facades\CauserResolver;
|
|
|
|
class DirectoratController extends Controller
|
|
{
|
|
public $user;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->middleware(function ($request, $next) {
|
|
$this->user = Auth::guard('web')->user();
|
|
return $next($request);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the Directorats.
|
|
*
|
|
* @param \App\DataTables\DirectoratDataTable $dataTable
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function index(DirectoratDataTable $dataTable, Request $request)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.read')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
|
|
}
|
|
|
|
return $dataTable->render('pages.masters.directorat.index');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created Directorat in storage.
|
|
*
|
|
* @param \App\Http\Requests\StoreDirectoratRequest $request
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function store(StoreDirectoratRequest $request)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.create')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
|
|
}
|
|
|
|
// Validate the request...
|
|
$validated = $request->validated();
|
|
|
|
// Store the Directorat...
|
|
if ($validated) {
|
|
try {
|
|
Directorat::create($validated);
|
|
echo json_encode(['status' => 'success', 'message' => 'Directorat created successfully.']);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Directorat created failed.']);
|
|
}
|
|
}
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Directorat created failed.']);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new Directorat.
|
|
*/
|
|
public function create()
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.create')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
|
|
}
|
|
|
|
show_404();
|
|
}
|
|
|
|
/**
|
|
* Display the specified Directorat.
|
|
*
|
|
* @param \App\Models\Directorat $directorat
|
|
*/
|
|
public function show(Directorat $directorat)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.read')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified Directorat.
|
|
*
|
|
* @param $id
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.update')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
|
|
}
|
|
|
|
$directorat = Directorat::find($id);
|
|
echo json_encode($directorat);
|
|
}
|
|
|
|
/**
|
|
* Update the specified Directorat in storage.
|
|
*
|
|
* @param \App\Http\Requests\UpdateDirectoratRequest $request
|
|
* @param \App\Models\Directorat $directorat
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function update(UpdateDirectoratRequest $request, Directorat $directorat)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.update')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
|
|
}
|
|
|
|
// Validate the request...
|
|
$validated = $request->validated();
|
|
|
|
// Update the Directorat...
|
|
if ($validated) {
|
|
try {
|
|
CauserResolver::setCauser($this->user);
|
|
$directorat->update($validated);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'Directorat updated successfully.']);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Directorat updated failed.']);
|
|
}
|
|
}
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Directorat updated failed.']);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified Directorat from storage.
|
|
*
|
|
* @param \App\Models\Directorat $directorat
|
|
*
|
|
* @return void
|
|
*/
|
|
public function destroy(Directorat $directorat)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('masters.delete')) {
|
|
abort(403, 'Sorry !! You are Unauthorized to delete any master data !');
|
|
}
|
|
|
|
$directorat->delete();
|
|
echo json_encode(['status' => 'success', 'message' => 'Directorat deleted successfully.']);
|
|
}
|
|
}
|