public function save($publicId, $data) { if ($publicId) { $task = Task::scope($publicId)->firstOrFail(); } else { $task = Task::createNew(); } if (isset($data['client']) && $data['client']) { $task->client_id = Client::getPrivateId($data['client']); } if (isset($data['description'])) { $task->description = trim($data['description']); } //$timeLog = $task->time_log ? json_decode($task->time_log, true) : []; $timeLog = isset($data['time_log']) ? json_decode($data['time_log']) : []; 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; }
public function save($publicId, $data) { if ($publicId) { $task = Task::scope($publicId)->firstOrFail(); } else { $task = Task::createNew(); } if (isset($data['client']) && $data['client']) { $task->client_id = Client::getPrivateId($data['client']); } if (isset($data['description'])) { $task->description = trim($data['description']); } $timeLog = $task->time_log ? json_decode($task->time_log, true) : []; if ($data['action'] == 'start') { $task->start_time = Carbon::now()->toDateTimeString(); $task->is_running = true; $timeLog[] = [strtotime('now'), false]; } else { if ($data['action'] == 'resume') { $task->break_duration = strtotime('now') - strtotime($task->start_time) + $task->duration; $task->resume_time = Carbon::now()->toDateTimeString(); $task->is_running = true; $timeLog[] = [strtotime('now'), false]; } else { if ($data['action'] == 'stop' && $task->is_running) { if ($task->resume_time) { $task->duration = $task->duration + strtotime('now') - strtotime($task->resume_time); $task->resume_time = null; } else { $task->duration = strtotime('now') - strtotime($task->start_time); } $timeLog[count($timeLog) - 1][1] = strtotime('now'); $task->is_running = false; } else { if ($data['action'] == 'save' && !$task->is_running) { $task->start_time = $data['start_time']; $task->duration = $data['duration']; $task->break_duration = $data['break_duration']; } } } } $task->duration = max($task->duration, 0); $task->break_duration = max($task->break_duration, 0); $task->time_log = json_encode($timeLog); $task->save(); return $task; }
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; }