188 lines
5.5 KiB
PHP
188 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\PMS\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Modules\PMS\Models\KanbanColumn;
|
|
use Modules\PMS\Repositories\KanbanColumnInterface;
|
|
|
|
class KanbanColumnController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
private $kanbanColumn;
|
|
|
|
public function __construct(KanbanColumnInterface $kanbanColumn)
|
|
{
|
|
$this->kanbanColumn = $kanbanColumn;
|
|
}
|
|
public function index()
|
|
{
|
|
$data['editable'] = false;
|
|
$data['title'] = 'Kanban Column Lists';
|
|
$data['kanbanColumnLists'] = $this->kanbanColumn->findAll();
|
|
return view('pms::kanbanColumn.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required|unique:kanban_columns,name',
|
|
'category' => 'required|in:pending,backlog,in_progress,completed,on_hold,cancelled'
|
|
]);
|
|
|
|
$request->merge([
|
|
'position' => KanbanColumn::max('position') + 1 ?? 1,
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $validator->errors(),
|
|
], 422);
|
|
}
|
|
$errors = implode(', ', $validator->errors()->all());
|
|
flash()->addError($errors);
|
|
|
|
return redirect()->back();
|
|
}
|
|
try {
|
|
|
|
$taskModel = $this->kanbanColumn->create($request->all());
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => $taskModel,
|
|
'msg' => 'Task Created Created',
|
|
]);
|
|
}
|
|
flash()->addSuccess('Kanban Column Created');
|
|
return redirect()->back();
|
|
|
|
} catch (\Throwable $th) {
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $th->getMessage(),
|
|
]);
|
|
}
|
|
flash()->addError($th->getMessage());
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$data['title'] = 'View Kanban Column';
|
|
$data['kanbanColumn'] = $this->kanbanColumn->getKanbanColumnById($id);
|
|
return view('pms::kanbanColumn.show', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
if (request()->ajax()) {
|
|
$data['editable'] = true;
|
|
$data['kanbanColumn'] = $this->kanbanColumn->getKanbanColumnById($id);
|
|
return view('pms::kanbanColumn.partials.action', $data);
|
|
}
|
|
$data['editable'] = true;
|
|
$data['title'] = 'Kanban Column Lists';
|
|
$data['kanbanColumn'] = $this->kanbanColumn->getKanbanColumnById($id);
|
|
$data['kanbanColumnLists'] = $this->kanbanColumn->findAll();
|
|
return view('pms::kanbanColumn.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'position' => 'required|integer',
|
|
'name' => 'required|unique:kanban_columns,name,' . $id,
|
|
'category' => 'required|in:pending,backlog,in_progress,completed,on_hold,cancelled'
|
|
]);
|
|
|
|
try {
|
|
$kanbanColumn = $this->kanbanColumn->getKanbanColumnById($id);
|
|
|
|
$items = KanbanColumn::whereNot('id', $kanbanColumn->id)->orderBy('position')->get();
|
|
|
|
DB::transaction(function () use ($kanbanColumn, $items, $request) {
|
|
|
|
$items->splice($request->position - 1, 0, [$kanbanColumn]);
|
|
|
|
$items = $items->values();
|
|
|
|
foreach ($items as $index => $item) {
|
|
$item->position = $index + 1;
|
|
$item->save();
|
|
}
|
|
|
|
$kanbanColumn->update($request->only(['name', 'color', 'category']));
|
|
});
|
|
|
|
flash()->success('Kanban Column Updated Successfully');
|
|
return redirect()->back();
|
|
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
return redirect()->back()->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
|
|
DB::transaction(function () use ($id) {
|
|
$column = KanbanColumn::findOrFail($id);
|
|
$column->delete();
|
|
|
|
$items = KanbanColumn::where('position', '>', $column->position)
|
|
->orderBy('position')
|
|
->get();
|
|
|
|
foreach ($items as $item) {
|
|
$item->position = $item->position - 1;
|
|
$item->save();
|
|
}
|
|
});
|
|
|
|
flash()->success('Column has been deleted');
|
|
return redirect()->back();
|
|
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
}
|
|
}
|