164 lines
5.0 KiB
PHP
164 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Employee\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Modules\Employee\Models\Designation;
|
|
use Modules\Employee\Services\DepartmentService;
|
|
use Modules\Employee\Services\DesignationService;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class DesignationController extends Controller
|
|
{
|
|
protected $designationService;
|
|
protected $departmentService;
|
|
|
|
public function __construct(DesignationService $designationService, DepartmentService $departmentService)
|
|
{
|
|
$this->designationService = $designationService;
|
|
$this->departmentService = $departmentService;
|
|
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(?int $id = null)
|
|
{
|
|
$isEditing = !is_null($id);
|
|
$designation = $isEditing ? $this->designationService->getDesignationById($id) : null;
|
|
|
|
if (request()->ajax()) {
|
|
$model = Designation::query()->orderBy('order');
|
|
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->setRowClass('tableRow')
|
|
->editColumn('status', function (Designation $designation) {
|
|
$status = $designation->status ? 'Published' : 'Draft';
|
|
$color = $designation->status ? 'text-success' : 'text-danger';
|
|
return "<p class='{$color}'>{$status}</p>";
|
|
})
|
|
->editColumn('department_id', function (Designation $designation) {
|
|
return $designation->department?->title ?? "-";
|
|
})
|
|
->addColumn('action', 'employee::designation.datatable.action')
|
|
->rawColumns(['action', 'status'])
|
|
->toJson();
|
|
}
|
|
|
|
$departmentOptions = $this->departmentService->pluck();
|
|
return view('employee::designation.index', [
|
|
'designation' => $designation,
|
|
'departmentOptions'=> $departmentOptions,
|
|
'title' => $isEditing ? 'Edit Designation' : 'Add Designation',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$isEditing = $request->has('id');
|
|
|
|
$request->merge([
|
|
'slug' => Str::slug($request->title),
|
|
]);
|
|
|
|
if ($isEditing) {
|
|
$validated = $request->validate([
|
|
'title' => ['required', 'string', 'max:255', 'unique:designations,title,' . $request->id],
|
|
'slug' => ['required', 'string'],
|
|
'department_id' => ['nullable', 'integer'],
|
|
]);
|
|
|
|
$designation = $this->designationService->updateDesignation($request->id, $validated);
|
|
flash()->success("Designation for {$designation->title} has been updated.");
|
|
return to_route('designation.index');
|
|
}
|
|
|
|
$maxOrder = Designation::max('order');
|
|
$order = $maxOrder ? ++$maxOrder : 1;
|
|
|
|
$request->mergeIfMissing([
|
|
'order' => $order
|
|
]);
|
|
|
|
$validated = $request->validate([
|
|
'title' => ['required', 'string', 'unique:designations,title'],
|
|
'slug' => ['required', 'string'],
|
|
'order' => ['integer'],
|
|
'department_id' => ['nullable', 'integer'],
|
|
]);
|
|
|
|
$designation = $this->designationService->storeDesignation($validated);
|
|
flash()->success("Designation for {$designation->title} has been created.");
|
|
return to_route('designation.index');
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$designation = $this->designationService->deleteDesignation($id);
|
|
return response()->json(['status' => 200, 'message' => "Designation has been deleted."], 200);
|
|
}
|
|
|
|
public function reorder(Request $request)
|
|
{
|
|
$designations = $this->designationService->getAllDesignations();
|
|
|
|
foreach ($designations as $designation) {
|
|
foreach ($request->order as $order) {
|
|
if ($order['id'] == $designation->id) {
|
|
$designation->update(['order' => $order['position']]);
|
|
}
|
|
}
|
|
}
|
|
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
|
}
|
|
|
|
public function toggle($id)
|
|
{
|
|
$designation = Designation::findOrFail($id);
|
|
$designation->update(['status' => !$designation->status]);
|
|
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
|
}
|
|
}
|