1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Http/Controllers/TaskController.php

300 lines
11 KiB
PHP
Raw Normal View History

2015-05-27 18:52:10 +02:00
<?php namespace App\Http\Controllers;
2015-06-14 16:49:13 +02:00
use Auth;
2015-05-27 18:52:10 +02:00
use View;
use URL;
use Utils;
use Input;
use Datatable;
use Validator;
use Redirect;
use Session;
2015-06-14 16:49:13 +02:00
use DropdownButton;
use DateTime;
use DateTimeZone;
2015-05-27 18:52:10 +02:00
use App\Models\Client;
use App\Models\Task;
use App\Ninja\Repositories\TaskRepository;
2015-07-12 21:43:45 +02:00
use App\Ninja\Repositories\InvoiceRepository;
2015-05-27 18:52:10 +02:00
class TaskController extends BaseController
{
protected $taskRepo;
2015-07-12 21:43:45 +02:00
public function __construct(TaskRepository $taskRepo, InvoiceRepository $invoiceRepo)
2015-05-27 18:52:10 +02:00
{
parent::__construct();
$this->taskRepo = $taskRepo;
2015-07-12 21:43:45 +02:00
$this->invoiceRepo = $invoiceRepo;
2015-05-27 18:52:10 +02:00
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
2015-06-14 16:49:13 +02:00
{
2015-05-27 18:52:10 +02:00
return View::make('list', array(
'entityType' => ENTITY_TASK,
'title' => trans('texts.tasks'),
'sortCol' => '2',
'columns' => Utils::trans(['checkbox', 'client', 'date', 'duration', 'description', 'status', 'action']),
));
}
public function getDatatable($clientPublicId = null)
{
$tasks = $this->taskRepo->find($clientPublicId, Input::get('sSearch'));
$table = Datatable::query($tasks);
if (!$clientPublicId) {
$table->addColumn('checkbox', function ($model) { return '<input type="checkbox" name="ids[]" value="'.$model->public_id.'" '.Utils::getEntityRowClass($model).'>'; })
->addColumn('client_name', function ($model) { return $model->client_public_id ? link_to('clients/'.$model->client_public_id, Utils::getClientDisplayName($model)) : ''; });
}
2015-09-04 09:20:30 +02:00
return $table->addColumn('created_at', function($model) { return link_to("tasks/{$model->public_id}/edit", Task::calcStartTime($model)); })
2015-10-05 19:48:16 +02:00
->addColumn('time_log', function($model) { return Utils::formatTime(Task::calcDuration($model)); })
2015-05-27 18:52:10 +02:00
->addColumn('description', function($model) { return $model->description; })
->addColumn('invoice_number', function($model) { return self::getStatusLabel($model); })
->addColumn('dropdown', function ($model) {
$str = '<div class="btn-group tr-action" style="visibility:hidden;">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
'.trans('texts.select').' <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">';
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
$str .= '<li><a href="'.URL::to('tasks/'.$model->public_id.'/edit').'">'.trans('texts.edit_task').'</a></li>';
}
if ($model->invoice_number) {
$str .= '<li>' . link_to("/invoices/{$model->invoice_public_id}/edit", trans('texts.view_invoice')) . '</li>';
2015-06-14 14:21:29 +02:00
} elseif ($model->is_running) {
2015-05-27 18:52:10 +02:00
$str .= '<li><a href="javascript:stopTask('.$model->public_id.')">'.trans('texts.stop_task').'</a></li>';
} elseif (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
$str .= '<li><a href="javascript:invoiceTask('.$model->public_id.')">'.trans('texts.invoice_task').'</a></li>';
}
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
$str .= '<li class="divider"></li>
<li><a href="javascript:archiveEntity('.$model->public_id.')">'.trans('texts.archive_task').'</a></li>';
} else {
$str .= '<li><a href="javascript:restoreEntity('.$model->public_id.')">'.trans('texts.restore_task').'</a></li>';
}
if (!$model->is_deleted) {
$str .= '<li><a href="javascript:deleteEntity('.$model->public_id.')">'.trans('texts.delete_task').'</a></li></ul>';
}
return $str . '</div>';
})
->make();
}
private function getStatusLabel($model) {
if ($model->invoice_number) {
$class = 'success';
$label = trans('texts.invoiced');
2015-06-14 14:21:29 +02:00
} elseif ($model->is_running) {
2015-05-27 18:52:10 +02:00
$class = 'primary';
$label = trans('texts.running');
} else {
$class = 'default';
$label = trans('texts.logged');
}
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
return $this->save();
}
2015-09-29 20:19:18 +02:00
public function show($publicId)
{
Session::reflash();
return Redirect::to("tasks/{$publicId}/edit");
}
2015-05-27 18:52:10 +02:00
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create($clientPublicId = 0)
{
$data = [
'task' => null,
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
'method' => 'POST',
'url' => 'tasks',
'title' => trans('texts.new_task'),
2015-09-03 09:32:39 +02:00
'timezone' => Auth::user()->account->timezone ? Auth::user()->account->timezone->name : DEFAULT_TIMEZONE,
'datetimeFormat' => Auth::user()->account->getMomentDateTimeFormat(),
2015-05-27 18:52:10 +02:00
];
$data = array_merge($data, self::getViewModel());
return View::make('tasks.edit', $data);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($publicId)
{
2015-10-28 20:22:07 +01:00
$task = Task::scope($publicId)->with('client', 'invoice')->withTrashed()->firstOrFail();
2015-06-14 16:49:13 +02:00
$actions = [];
if ($task->invoice) {
$actions[] = ['url' => URL::to("inovices/{$task->invoice->public_id}/edit"), 'label' => trans("texts.view_invoice")];
} else {
2015-09-07 11:07:55 +02:00
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans("texts.invoice_task")];
2015-07-12 21:43:45 +02:00
// check for any open invoices
$invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id) : [];
foreach ($invoices as $invoice) {
$actions[] = ['url' => 'javascript:submitAction("add_to_invoice", '.$invoice->public_id.')', 'label' => trans("texts.add_to_invoice", ["invoice" => $invoice->invoice_number])];
}
2015-06-14 16:49:13 +02:00
}
2015-07-12 21:43:45 +02:00
2015-06-14 16:49:13 +02:00
$actions[] = DropdownButton::DIVIDER;
if (!$task->trashed()) {
$actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_task')];
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_task')];
} else {
$actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_task')];
}
2015-05-27 18:52:10 +02:00
$data = [
'task' => $task,
'clientPublicId' => $task->client ? $task->client->public_id : 0,
'method' => 'PUT',
'url' => 'tasks/'.$publicId,
2015-06-14 16:03:37 +02:00
'title' => trans('texts.edit_task'),
2015-07-12 21:43:45 +02:00
'duration' => $task->is_running ? $task->getCurrentDuration() : $task->getDuration(),
'actions' => $actions,
2015-09-03 09:32:39 +02:00
'timezone' => Auth::user()->account->timezone ? Auth::user()->account->timezone->name : DEFAULT_TIMEZONE,
'datetimeFormat' => Auth::user()->account->getMomentDateTimeFormat(),
2015-05-27 18:52:10 +02:00
];
$data = array_merge($data, self::getViewModel());
return View::make('tasks.edit', $data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($publicId)
{
return $this->save($publicId);
}
private static function getViewModel()
{
return [
'clients' => Client::scope()->with('contacts')->orderBy('name')->get()
];
}
private function save($publicId = null)
{
2015-06-14 16:49:13 +02:00
$action = Input::get('action');
2015-05-27 18:52:10 +02:00
2015-07-12 21:43:45 +02:00
if (in_array($action, ['archive', 'delete', 'invoice', 'restore', 'add_to_invoice'])) {
2015-06-14 16:49:13 +02:00
return self::bulk();
}
2015-09-25 11:57:40 +02:00
if ($validator = $this->taskRepo->getErrors(Input::all())) {
$url = $publicId ? 'tasks/'.$publicId.'/edit' : 'tasks/create';
Session::flash('error', trans('texts.task_errors'));
return Redirect::to($url)
->withErrors($validator)
->withInput();
}
2015-06-14 16:49:13 +02:00
$task = $this->taskRepo->save($publicId, Input::all());
2015-05-27 18:52:10 +02:00
Session::flash('message', trans($publicId ? 'texts.updated_task' : 'texts.created_task'));
2015-06-14 14:21:29 +02:00
return Redirect::to("tasks/{$task->public_id}/edit");
2015-05-27 18:52:10 +02:00
}
public function bulk()
{
$action = Input::get('action');
2015-10-28 20:22:07 +01:00
$ids = Input::get('public_id') ?: (Input::get('id') ?: Input::get('ids'));
2015-05-27 18:52:10 +02:00
if ($action == 'stop') {
$this->taskRepo->save($ids, ['action' => $action]);
Session::flash('message', trans('texts.stopped_task'));
return Redirect::to('tasks');
2015-07-12 21:43:45 +02:00
} else if ($action == 'invoice' || $action == 'add_to_invoice') {
2015-05-27 18:52:10 +02:00
$tasks = Task::scope($ids)->with('client')->get();
$clientPublicId = false;
$data = [];
2015-07-12 21:43:45 +02:00
2015-05-27 18:52:10 +02:00
foreach ($tasks as $task) {
if ($task->client) {
if (!$clientPublicId) {
$clientPublicId = $task->client->public_id;
} else if ($clientPublicId != $task->client->public_id) {
Session::flash('error', trans('texts.task_error_multiple_clients'));
return Redirect::to('tasks');
}
}
2015-06-14 14:21:29 +02:00
if ($task->is_running) {
2015-05-27 18:52:10 +02:00
Session::flash('error', trans('texts.task_error_running'));
return Redirect::to('tasks');
} else if ($task->invoice_id) {
Session::flash('error', trans('texts.task_error_invoiced'));
return Redirect::to('tasks');
}
2015-07-12 21:43:45 +02:00
2015-05-27 18:52:10 +02:00
$data[] = [
'publicId' => $task->public_id,
'description' => $task->description,
2015-07-12 21:43:45 +02:00
'startTime' => $task->getStartTime(),
'duration' => $task->getHours(),
2015-05-27 18:52:10 +02:00
];
}
2015-07-12 21:43:45 +02:00
if ($action == 'invoice') {
return Redirect::to("invoices/create/{$clientPublicId}")->with('tasks', $data);
} else {
$invoiceId = Input::get('invoice_id');
return Redirect::to("invoices/{$invoiceId}/edit")->with('tasks', $data);
}
2015-05-27 18:52:10 +02:00
} else {
$count = $this->taskRepo->bulk($ids, $action);
$message = Utils::pluralize($action.'d_task', $count);
Session::flash('message', $message);
if ($action == 'restore' && $count == 1) {
return Redirect::to('tasks/'.$ids[0].'/edit');
} else {
return Redirect::to('tasks');
}
}
}
}