clara/app/Http/Controllers/JobController.php

183 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2023-05-05 00:54:31 +00:00
namespace App\Http\Controllers;
2023-05-05 00:54:31 +00:00
use App\DataTables\JobDataTable;
use App\Http\Requests\StoreJobRequest;
use App\Http\Requests\UpdateJobRequest;
use App\Models\Directorat;
use App\Models\Job;
use Exception;
use Illuminate\Http\Request;
2023-05-05 00:54:31 +00:00
class JobController extends Controller
{
2023-05-05 00:54:31 +00:00
public $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::guard('web')->user();
return $next($request);
});
}
2023-05-05 00:54:31 +00:00
/**
* Display a listing of the Jobs.
*
* @param \App\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('pages.masters.job.index', compact('directorat'));
}
2023-05-05 00:54:31 +00:00
/**
* 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 !');
}
2023-05-05 00:54:31 +00:00
$jobs = Job::where('sub_directorat_id', $request->sub_directorat_id)->get();
2023-05-05 00:54:31 +00:00
$data = [];
foreach ($jobs as $row) {
$result = [
$row->id => $row->name,
];
$data[] = $result;
}
2023-05-05 00:54:31 +00:00
echo json_encode($data);
}
2023-05-05 00:54:31 +00:00
/**
* Store a newly created Job in storage.
*
* @param \App\Http\Requests\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 !');
}
2023-05-05 00:54:31 +00:00
// 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.']);
}
}
echo json_encode(['status' => 'error', 'message' => 'Job created failed.']);
}
2023-05-05 00:54:31 +00:00
/**
* 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 !');
}
2023-05-05 00:54:31 +00:00
show_404();
}
2023-05-05 00:54:31 +00:00
/**
* 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 !');
}
2023-05-05 00:54:31 +00:00
$job = Job::find($id);
echo json_encode($job);
}
2023-05-05 00:54:31 +00:00
/**
* Update the specified Job in storage.
*
* @param \App\Http\Requests\UpdateJobRequest $request
* @param \App\Models\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.']);
}
}
2023-05-05 00:54:31 +00:00
echo json_encode(['status' => 'error', 'message' => 'Job updated failed.']);
}
2023-05-05 00:54:31 +00:00
/**
* Remove the specified Job from storage.
*
* @param \App\Models\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 !');
}
2023-05-05 00:54:31 +00:00
$job->delete();
echo json_encode(['status' => 'success', 'message' => 'Job deleted successfully.']);
}
}