183 lines
5.5 KiB
PHP
183 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
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;
|
|
|
|
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 \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'));
|
|
}
|
|
|
|
/**
|
|
* 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 \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 !');
|
|
}
|
|
|
|
// 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.']);
|
|
}
|
|
|
|
/**
|
|
* 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 !');
|
|
}
|
|
|
|
show_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 \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.']);
|
|
}
|
|
}
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Job updated failed.']);
|
|
}
|
|
|
|
/**
|
|
* 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 !');
|
|
}
|
|
|
|
$job->delete();
|
|
echo json_encode(['status' => 'success', 'message' => 'Job deleted successfully.']);
|
|
}
|
|
}
|