first change
This commit is contained in:
163
Modules/Employee/app/Http/Controllers/DepartmentController.php
Normal file
163
Modules/Employee/app/Http/Controllers/DepartmentController.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Employee\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Employee\Models\Department;
|
||||
use Modules\Employee\Services\DepartmentService;
|
||||
use Modules\User\Services\UserService;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class DepartmentController extends Controller
|
||||
{
|
||||
protected $departmentService;
|
||||
protected $userService;
|
||||
|
||||
public function __construct(DepartmentService $departmentService, UserService $userService)
|
||||
{
|
||||
$this->departmentService = $departmentService;
|
||||
$this->userService = $userService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(?int $id = null)
|
||||
{
|
||||
$isEditing = !is_null($id);
|
||||
$department = $isEditing ? $this->departmentService->getDepartmentById($id) : null;
|
||||
|
||||
if (request()->ajax()) {
|
||||
$model = Department::query()->orderBy('order');
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('status', function (Department $department) {
|
||||
$status = $department->status ? 'Published' : 'Draft';
|
||||
$color = $department->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('head', function (Department $department) {
|
||||
return $department->departmentHead?->name ?? '-';
|
||||
})
|
||||
->addColumn('action', 'employee::department.datatable.action')
|
||||
->rawColumns(['action', 'status', 'head'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
$userOptions = $this->userService->pluck();
|
||||
|
||||
return view('employee::department.index', [
|
||||
'department' => $department,
|
||||
'userOptions' => $userOptions,
|
||||
'title' => $isEditing ? 'Edit Department' : 'Add Department',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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:departments,title,' . $request->id],
|
||||
'slug' => ['required', 'string'],
|
||||
'user_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$department = $this->departmentService->updateDepartment($request->id, $validated);
|
||||
flash()->success("Department for {$department->title} has been updated.");
|
||||
return to_route('department.index');
|
||||
}
|
||||
|
||||
$maxOrder = Department::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'order' => $order
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'unique:departments,title'],
|
||||
'slug' => ['required', 'string'],
|
||||
'order' => ['integer'],
|
||||
'user_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$department = $this->departmentService->storeDepartment($validated);
|
||||
flash()->success("Department for {$department->title} has been created.");
|
||||
return to_route('department.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)
|
||||
{
|
||||
$department = $this->departmentService->deleteDepartment($id);
|
||||
return response()->json(['status' => 200, 'message' => "Department has been deleted."], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
$departments = $this->departmentService->getAllDepartments();
|
||||
|
||||
foreach ($departments as $department) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $department->id) {
|
||||
$department->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$department = Department::findOrFail($id);
|
||||
$department->update(['status' => !$department->status]);
|
||||
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user