Cetaklabel/Http/Controllers/JobController.php

187 lines
5.9 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\DataTables\JobDataTable;
use Modules\Cetaklabel\Entities\Directorat;
use Modules\Cetaklabel\Entities\Job;
use Modules\Cetaklabel\Http\Requests\Job\StoreJobRequest;
use Modules\Cetaklabel\Http\Requests\Job\UpdateJobRequest;
class JobController 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 Jobs.
*
* @param \Modules\Cetaklabel\DataTables\JobDataTable $dataTable
* @param \Illuminate\Http\Request $request
*
* @return mixed|void
*/
public function index(JobDataTable $dataTable, Request $request)
{
if (is_null($this->user) || !$this->user->can('masters.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
// Add Vendor
addVendor('chained-select');
if (isset($request->sub_directorat_id) && !empty($request->sub_directorat_id)) {
$this->show($request);
return;
}
$directorat = Directorat::all();
return $dataTable->render('cetaklabel::masters.job.index', compact('directorat'));
}
/**
* Lists the specified Job by Sub Directorat ID.
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
public function show(Request $request)
{
if (is_null($this->user) || !$this->user->can('masters.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
$jobs = Job::where('sub_directorat_id', $request->sub_directorat_id)->get();
$data = [];
foreach ($jobs as $row) {
$result = [
$row->id => $row->name,
];
$data[] = $result;
}
echo json_encode($data);
}
/**
* Store a newly created Job in storage.
*
* @param \Modules\Cetaklabel\Http\Requests\Job\StoreJobRequest $request
*
* @return void
*/
public function store(StoreJobRequest $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 Job...
if ($validated) {
try {
Job::create($validated);
echo json_encode(['status' => 'success', 'message' => 'Job created successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Job created failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Job created failed.']);
}
/**
* Show the form for creating a new Job.
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('masters.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
abort(404);
}
/**
* Show the form for editing the specified Job.
*
* @param $id
*
* @return void
*/
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 !');
}
$job = Job::find($id);
echo json_encode($job);
}
/**
* Update the specified Job in storage.
*
* @param \Modules\Cetaklabel\Http\Requests\Job\UpdateJobRequest $request
* @param \Modules\Cetaklabel\Entities\Job $job
*
* @return void
*/
public function update(UpdateJobRequest $request, Job $job)
{
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 Job...
if ($validated) {
try {
$job->update($validated);
echo json_encode(['status' => 'success', 'message' => 'Job updated successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Job updated failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Job updated failed.']);
}
/**
* Remove the specified Job from storage.
*
* @param \Modules\Cetaklabel\Entities\Job $job
*
* @return void
*/
public function destroy(Job $job)
{
if (is_null($this->user) || !$this->user->can('masters.delete')) {
abort(403, 'Sorry !! You are Unauthorized to delete any master data !');
}
$job->delete();
echo json_encode(['status' => 'success', 'message' => 'Job deleted successfully.']);
}
}