1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Http/Controllers/TaskKanbanController.php

153 lines
4.3 KiB
PHP
Raw Normal View History

2017-12-17 18:54:54 +01:00
<?php
namespace App\Http\Controllers;
2017-12-17 19:59:10 +01:00
use App\Models\Task;
2017-12-17 18:54:54 +01:00
use App\Models\TaskStatus;
2017-12-17 21:10:54 +01:00
use App\Models\Project;
use App\Models\Client;
2017-12-17 18:54:54 +01:00
class TaskKanbanController extends BaseController
{
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
2017-12-18 12:05:34 +01:00
$tasks = Task::scope()->with(['project', 'client', 'task_status'])->orderBy('task_status_sort_order')->get();
2017-12-19 14:52:24 +01:00
$statuses = TaskStatus::scope()->orderBy('sort_order')->get();
2017-12-17 21:10:54 +01:00
$projects = Project::scope()->get();
$clients = Client::scope()->get();
2017-12-17 18:54:54 +01:00
2017-12-17 19:59:10 +01:00
// check initial statuses exist
2017-12-19 14:52:24 +01:00
if (! $statuses->count()) {
$statuses = collect([]);
2017-12-17 18:54:54 +01:00
$defaults = [
'backlog',
'ready_to_do',
'in_progress',
'done',
];
for ($i=0; $i<count($defaults); $i++) {
$status = TaskStatus::createNew();
$status->name = trans('texts.' . $defaults[$i]);
$status->sort_order = $i;
$status->save();
2017-12-19 14:52:24 +01:00
$statuses[] = $status;
2017-12-17 18:54:54 +01:00
}
}
$data = [
'title' => trans('texts.kanban'),
2017-12-19 14:52:24 +01:00
'statuses' => $statuses,
2017-12-17 19:59:10 +01:00
'tasks' => $tasks,
2017-12-17 21:10:54 +01:00
'clients' => $clients,
'projects' => $projects,
2017-12-17 18:54:54 +01:00
];
return view('tasks.kanban', $data);
}
2017-12-19 09:14:26 +01:00
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function storeStatus()
{
2017-12-19 14:44:51 +01:00
$status = TaskStatus::createNew();
$status->fill(request()->all());
$status->save();
return response()->json($status);
2017-12-19 09:14:26 +01:00
}
/**
* @param $publicId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function updateStatus($publicId)
{
2017-12-19 14:44:51 +01:00
$status = TaskStatus::scope($publicId)->firstOrFail();
2017-12-19 14:52:24 +01:00
if ($status->sort_order != request('sort_order')) {
$origSortOrder = $status->sort_order;
$newSortOrder = request('sort_order');
TaskStatus::scope()
->where('sort_order', '>', $origSortOrder)
->decrement('sort_order');
TaskStatus::scope()
->where('sort_order', '>=', $newSortOrder)
->increment('sort_order');
}
2017-12-19 12:25:13 +01:00
$status->fill(request()->all());
2017-12-19 09:14:26 +01:00
$status->save();
return response()->json($status);
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteStatus($publicId)
{
2017-12-19 13:25:44 +01:00
$status = TaskStatus::scope($publicId)->firstOrFail();
$status->delete();
2017-12-19 09:14:26 +01:00
2017-12-19 15:00:25 +01:00
TaskStatus::scope()
->where('sort_order', '>', $status->sort_order)
->decrement('sort_order');
2017-12-19 17:33:41 +01:00
$firstStatus = TaskStatus::scope()
->orderBy('sort_order')
->first();
// Move linked tasks to the end of the first status
if ($firstStatus) {
$firstCount = $firstStatus->tasks->count();
Task::scope()
->where('task_status_id', '=', $status->id)
->increment('task_status_sort_order', $firstCount, [
'task_status_id' => $firstStatus->id
]);
}
2017-12-19 09:14:26 +01:00
return response()->json(['message' => RESULT_SUCCESS]);
}
2017-12-19 13:25:44 +01:00
/**
* @param $publicId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function updateTask($publicId)
{
$task = Task::scope($publicId)->firstOrFail();
$origStatusId = $task->task_status_id;
$origSortOrder = $task->task_status_sort_order;
$newStatusId = request('task_status_id');
$newSortOrder = request('task_status_sort_order');
Task::scope()
->where('task_status_id', '=', $origStatusId)
->where('task_status_sort_order', '>', $origSortOrder)
->decrement('task_status_sort_order');
Task::scope()
->where('task_status_id', '=', $newStatusId)
->where('task_status_sort_order', '>=', $newSortOrder)
->increment('task_status_sort_order');
2017-12-19 15:00:25 +01:00
$task->task_status_id = TaskStatus::getPrivateId(request('task_status_id'));
2017-12-19 13:25:44 +01:00
$task->task_status_sort_order = $newSortOrder;
$task->save();
return response()->json($task);
}
2017-12-17 18:54:54 +01:00
}