1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Http/Controllers/TaskKanbanController.php

199 lines
6.0 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
*/
2017-12-24 20:17:21 +01:00
public function index($clientPublicId = false, $projectPublicId = false)
2017-12-17 18:54:54 +01:00
{
2017-12-19 20:31:23 +01:00
$tasks = Task::scope()
->with(['project', 'client', 'task_status'])
->whereNull('invoice_id')
->orderBy('task_status_sort_order')
2017-12-19 22:02:05 +01:00
->orderBy('id')
->get();
$statuses = TaskStatus::scope()
->orderBy('sort_order')
->orderBy('id')
2017-12-19 20:31:23 +01:00
->get();
2017-12-17 21:10:54 +01:00
$projects = Project::scope()->get();
2017-12-20 09:10:25 +01:00
$clients = Client::scope()->with(['contacts'])->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-19 23:26:56 +01:00
$firstStatus = false;
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-19 23:26:56 +01:00
if (! $firstStatus) {
$firstStatus = $status;
}
}
$i = 0;
foreach ($tasks as $task) {
$task->task_status_id = $firstStatus->id;
$task->task_status_sort_order = $i++;
$task->save();
}
// otherwise, check that the tasks orders are correct
} else {
$firstStatus = $statuses[0];
$adjustment = 0;
$counts = [];
foreach ($tasks as $task) {
2017-12-20 09:10:25 +01:00
if (! $task->task_status || $task->task_status->trashed()) {
2017-12-19 23:26:56 +01:00
$task->task_status_id = $firstStatus->id;
$task->setRelation('task_status', $firstStatus);
}
if (! isset($counts[$task->task_status_id])) {
$counts[$task->task_status_id] = 0;
}
if ($task->task_status_sort_order != $counts[$task->task_status_id]) {
$task->task_status_sort_order = $counts[$task->task_status_id];
}
$counts[$task->task_status_id]++;
if ($task->isDirty()) {
$task->save();
}
2017-12-17 18:54:54 +01:00
}
}
$data = [
2017-12-26 14:59:37 +01:00
'showBreadcrumbs' => false,
2017-12-17 18:54:54 +01:00
'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-24 11:02:49 +01:00
'clientPublicId' => $clientPublicId,
2017-12-26 14:59:37 +01:00
'client' => $clientPublicId ? Client::scope($clientPublicId)->first() : null,
2017-12-24 20:17:21 +01:00
'projectPublicId' => $projectPublicId,
2017-12-26 14:59:37 +01:00
'project' => $projectPublicId ? Project::scope($projectPublicId)->first() : null,
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
2017-12-19 19:41:32 +01:00
$origSortOrder = $status->sort_order;
$newSortOrder = request('sort_order');
2017-12-19 14:52:24 +01:00
2017-12-19 19:41:32 +01:00
if ($newSortOrder && $newSortOrder != $origSortOrder) {
2017-12-19 14:52:24 +01:00
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;
2017-12-19 18:44:24 +01:00
$newStatusId = TaskStatus::getPrivateId(request('task_status_id'));
2017-12-19 13:25:44 +01:00
$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 18:44:24 +01:00
$task->task_status_id = $newStatusId;
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
}