App\Ninja\Repositories\TaskRepository::save PHP Method

save() public method

public save ( $publicId, $data, $task = null )
    public function save($publicId, $data, $task = null)
    {
        if ($task) {
            // do nothing
        } elseif ($publicId) {
            $task = Task::scope($publicId)->withTrashed()->firstOrFail();
        } else {
            $task = Task::createNew();
        }
        if ($task->is_deleted) {
            return $task;
        }
        if (isset($data['client']) && $data['client']) {
            $task->client_id = Client::getPrivateId($data['client']);
        }
        if (isset($data['description'])) {
            $task->description = trim($data['description']);
        }
        if (isset($data['time_log'])) {
            $timeLog = json_decode($data['time_log']);
        } elseif ($task->time_log) {
            $timeLog = json_decode($task->time_log);
        } else {
            $timeLog = [];
        }
        array_multisort($timeLog);
        if (isset($data['action'])) {
            if ($data['action'] == 'start') {
                $task->is_running = true;
                $timeLog[] = [strtotime('now'), false];
            } else {
                if ($data['action'] == 'resume') {
                    $task->is_running = true;
                    $timeLog[] = [strtotime('now'), false];
                } else {
                    if ($data['action'] == 'stop' && $task->is_running) {
                        $timeLog[count($timeLog) - 1][1] = time();
                        $task->is_running = false;
                    }
                }
            }
        }
        $task->time_log = json_encode($timeLog);
        $task->save();
        return $task;
    }

Usage Example

 /**
  * @return \Illuminate\Http\RedirectResponse
  */
 public function bulk()
 {
     $action = Input::get('action');
     $ids = Input::get('public_id') ?: (Input::get('id') ?: Input::get('ids'));
     if ($action == 'stop') {
         $this->taskRepo->save($ids, ['action' => $action]);
         Session::flash('message', trans('texts.stopped_task'));
         return Redirect::to('tasks');
     } else {
         if ($action == 'invoice' || $action == 'add_to_invoice') {
             $tasks = Task::scope($ids)->with('client')->get();
             $clientPublicId = false;
             $data = [];
             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');
                         }
                     }
                 }
                 if ($task->is_running) {
                     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');
                     }
                 }
                 $account = Auth::user()->account;
                 $data[] = ['publicId' => $task->public_id, 'description' => $task->description . "\n\n" . $task->present()->times($account), 'duration' => $task->getHours()];
             }
             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);
             }
         } 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');
             }
         }
     }
 }