Cetaklabel/Http/Controllers/SubSubJobController.php

199 lines
6.6 KiB
PHP

<?php
namespace Modules\Cetaklabel\Http\Controllers;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Cetaklabel\Entities\Approval;
use Modules\Cetaklabel\DataTables\SubSubJobDataTable;
use Modules\Cetaklabel\Entities\Directorat;
use Modules\Cetaklabel\Entities\SubSubJob;
use Modules\Cetaklabel\Http\Requests\SubSubJob\StoreSubSubJobRequest;
use Modules\Cetaklabel\Http\Requests\SubSubJob\UpdateSubSubJobRequest;
class SubSubJobController 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 Sub Sub Jobs.
*
* @param \Modules\Cetaklabel\DataTables\SubSubJobDataTable $dataTable
* @param \Illuminate\Http\Request $request
*
* @return mixed|void
*/
public function index(SubSubJobDataTable $dataTable, Request $request)
{
if (isset($request->sub_job_id) && !empty($request->sub_job_id)) {
$this->show($request);
return;
}
if (is_null($this->user) || !$this->user->can('master.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
addVendor('chained-select');
$directorat = Directorat::all();
return $dataTable->render('cetaklabel::masters.sub-sub-job.index', compact('directorat'));
}
/**
* Lists the specified Sub Sub Job by Sub Job ID.
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
public function show(Request $request)
{
$subSubJob = SubSubJob::where('sub_job_id', $request->sub_job_id)->get();
$data = [];
foreach ($subSubJob as $row) {
$result = [
$row->id => $row->name,
];
$data[] = $result;
}
echo json_encode($data);
}
/**
* Store a newly created Sub Sub Job in storage.
*
* @param \Modules\Cetaklabel\Http\Requests\SubSubJob\StoreSubSubJobRequest $request
*
* @return false
*/
public function store(StoreSubSubJobRequest $request)
{
if (is_null($this->user) || !$this->user->can('master.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
// Validate the request...
$validated = $request->validated();
// Store the SubSubJob...
if ($validated) {
try {
$approval = [
'method' => 'create',
'menu' => 'SubSubJob',
'new_request' => json_encode($validated),
'description' => 'Create new Sub Sub Job',
'status' => '0',
];
Approval::create($approval);
echo json_encode(['status' => 'success', 'message' => 'Sub Sub Job created successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Sub Sub Job created failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Sub Sub Job created failed.']);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('master.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
abort(404);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('master.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
$subJob = SubSubJob::find($id);
echo json_encode($subJob);
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateSubSubJobRequest $request, SubSubJob $subSubJob)
{
if (is_null($this->user) || !$this->user->can('master.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
// Validate the request...
$validated = $request->validated();
// Update the SubSubJob...
if ($validated) {
try {
$approval = [
'method' => 'update',
'menu' => 'SubSubJob',
'new_request' => json_encode($validated),
'old_request' => json_encode($subSubJob),
'description' => 'Update Sub Sub Job',
'status' => '0',
];
Approval::create($approval);
echo json_encode(['status' => 'success', 'message' => 'Sub Sub Job updated successfully.']);
} catch (Exception $e) {
//return redirect()->route('job.index')->with('error', 'SubSubJob updated failed.');
echo json_encode(['status' => 'error', 'message' => 'Sub Sub Job updated failed.']);
}
}
return false;
}
/**
* Remove the specified resource from storage.
*/
public function destroy(SubSubJob $subSubJob)
{
if (is_null($this->user) || !$this->user->can('master.delete')) {
abort(403, 'Sorry !! You are Unauthorized to delete any master data !');
}
$approval = [
'method' => 'delete',
'menu' => 'SubSubJob',
'old_request' => json_encode($subSubJob),
'description' => 'Delete Sub Sub Job',
'status' => '0',
];
Approval::create($approval);
echo json_encode(['status' => 'success', 'message' => 'Sub Sub Job deleted successfully.']);
}
}