diff --git a/lib/Controller/AjaxController.php b/lib/Controller/AjaxController.php index b064743..5c97259 100644 --- a/lib/Controller/AjaxController.php +++ b/lib/Controller/AjaxController.php @@ -51,7 +51,7 @@ class AjaxController extends Controller { public function __construct($AppName, IRequest $request, IUserSession $userSession, IL10N $l10n, WorkIntervalMapper $workIntervalMapper, ClientMapper $clientMapper, UserToClientMapper $userToClientMapper, - ProjectMapper $projectMapper, UserToProjectMapper $userToProjectMapper, TagMapper $tagMapper, WorkIntervalToTagMapper $workIntervalToTagMapper, ReportItemMapper $reportItemMapper, + ProjectMapper $projectMapper, UserToProjectMapper $userToProjectMapper, TagMapper $tagMapper, WorkIntervalToTagMapper $workIntervalToTagMapper, ReportItemMapper $reportItemMapper, TimelineMapper $timelineMapper, TimelineEntryMapper $timelineEntryMapper, GoalMapper $goalMapper, $UserId){ parent::__construct($AppName, $request); $this->userId = $UserId; @@ -102,7 +102,7 @@ public function workIntervals() { if ($wi->projectId != null){ $project = $this->projectMapper->find($wi->projectId); } - + $tags = []; $wiToTags = $this->workIntervalToTagMapper->findAllForWorkInterval($wi->id); foreach($wiToTags as $wiToTag){ @@ -110,7 +110,7 @@ public function workIntervals() { if ($t != null) $tags[] = $t; } - + $wa = ['duration' => $wi->duration, 'id' => $wi->id, 'name' => $wi->name, @@ -126,13 +126,13 @@ public function workIntervals() { $days[$dt][$wi->name]['children'][] = $wa; $days[$dt][$wi->name]['totalTime'] += $wa['duration']; } - + $running = $this->workIntervalMapper->findAllRunning($this->userId); return new JSONResponse(["WorkIntervals" => $l, "running" => $running, 'days' => $days, 'now' => time()]); } - + public function isThisAdminUser(){ return \OC_User::isAdminUser(\OC_User::getUser()); } @@ -147,7 +147,7 @@ public function isUserAdmin($user){ * @NoCSRFRequired */ public function index() { - + } /** @@ -176,11 +176,11 @@ public function startTimer($name) { $winterval->setRunning(1); $winterval->setName($name); $winterval->setUserUid($this->userId); - + // first get tags and project ids from the last work item with the same name $lwinterval = $this->workIntervalMapper->findLatestByName($this->userId, $name); if ($projectId == null && $lwinterval != null){ - + $winterval->setProjectId($lwinterval->projectId); } @@ -200,7 +200,7 @@ public function startTimer($name) { } } - + if ($tags != null){ $tagsArray = explode(",", $tags); foreach($tagsArray as $t){ @@ -213,12 +213,12 @@ public function startTimer($name) { } - - - + + + //echo json_encode((array)$winterval); return new JSONResponse(["WorkIntervals" => $winterval, "running" => 1]); - + } @@ -237,7 +237,7 @@ public function stopTimer($name) { } $running = $this->workIntervalMapper->findAllRunning($this->userId); - + $now = time(); foreach($running as $r){ $r->setRunning(0); @@ -260,7 +260,7 @@ public function deleteWorkInterval($id) { $this->workIntervalMapper->delete($wi); $running = $this->workIntervalMapper->findAllRunning($this->userId); - + return new JSONResponse(["WorkIntervals" => json_decode(json_encode($running), true)]); } @@ -270,9 +270,9 @@ public function deleteWorkInterval($id) { */ public function updateWorkInterval($id) { - + $wi = $this->workIntervalMapper->find($id); - + if (isset($this->request->name)) { if (strlen($this->request->name) > 255){ return new JSONResponse(["Error" => "Name too long"]); @@ -306,14 +306,14 @@ public function updateWorkInterval($id) { $newWiToTag->setTagId($tag); $newWiToTag->setCreatedAt(time()); $this->workIntervalToTagMapper->insert($newWiToTag); - + } - + } } } - + if (isset($this->request->tagId)) { if (is_array($this->request->tagId)){ $tags = $this->request->tagId; @@ -324,7 +324,7 @@ public function updateWorkInterval($id) { $this->workIntervalToTagMapper->deleteAllForWorkInterval($id); $project = null; $locked = 0; - + foreach($tags as $tag){ if (empty($tag)) @@ -365,10 +365,10 @@ public function updateWorkInterval($id) { $de->setTimeZone(new \DateTimeZone('UTC')); $wi->setDuration($de->getTimestamp() - $dt->getTimestamp()); } - + $this->workIntervalMapper->update($wi); $running = $this->workIntervalMapper->findAllRunning($this->userId); - + return new JSONResponse(["WorkIntervals" => json_decode(json_encode($running), true)]); } @@ -379,11 +379,11 @@ public function updateWorkInterval($id) { */ public function addWorkInterval() { - + $wi = new WorkInterval(); $wi->setUserUid($this->userId); $wi->setRunning(0); - + if (isset($this->request->name)) { $wi->setName(urldecode($this->request->name)); } @@ -414,9 +414,9 @@ public function addWorkInterval() { $newWiToTag->setTagId($tag); $newWiToTag->setCreatedAt(time()); $this->workIntervalToTagMapper->insert($newWiToTag); - + } - + } } @@ -426,7 +426,7 @@ public function addWorkInterval() { $this->workIntervalToTagMapper->deleteAllForWorkInterval($id); $project = null; $locked = 0; - + foreach($tags as $tag){ if (empty($tag)) @@ -456,7 +456,7 @@ public function addWorkInterval() { } $this->workIntervalMapper->insert($wi); - + return new JSONResponse(["WorkIntervals" => json_decode(json_encode($running), true)]); } @@ -524,7 +524,7 @@ public function deleteClient($id) { $utoc = $this->userToClientMapper->findForUserAndClient($this->userId, $c); if ($utoc != null){ - + $this->userToClientMapper->delete($utoc); } return $this->getClients(); @@ -541,7 +541,7 @@ public function getClients(){ return new JSONResponse(["Clients" => json_decode(json_encode($clients), true)]); } - + /** @@ -639,10 +639,10 @@ public function editProject($id) { $this->userToProjectMapper->insert($up); } } - + if (isset($this->request->archived) && $p->getArchived() != $this->request->archived){ if (($this->isThisAdminUser() || $p->createdByUserUid == $this->userId) ){ - + $archived = $this->request->archived; $p->setArchived($archived); } else { @@ -651,7 +651,7 @@ public function editProject($id) { } $this->projectMapper->update($p); - + return $this->getProjects(); } /** @@ -665,7 +665,7 @@ public function deleteProject($id) { $utop = $this->userToProjectMapper->findForUserAndProject($this->userId, $p); if ($utop != null){ - + $this->userToProjectMapper->delete($utop); } return $this->getProjects(); @@ -686,17 +686,23 @@ public function deleteProjectWithData($id) { $this->tagMapper->allowedTags($id,[]); $this->projectMapper->delete($id); - + return $this->getProjects(); } - + /** * * @NoAdminRequired */ public function getProjects(){ - $projects = $this->projectMapper->findAll($this->userId); + $projectName = $this->request->term ?? null; + + if ($projectName) { + $projects = $this->projectMapper->searchByName($this->userId, $projectName); + } else { + $projects = $this->projectMapper->findAll($this->userId); + } $parray = json_decode(json_encode($projects), true); foreach($parray as $pi => $pv){ if (isset($pv->id)) { @@ -744,7 +750,7 @@ public function getProjectsTable(){ $out['clientId'] = $client->id; } } - + $outProjects[] = $out; } @@ -770,7 +776,7 @@ public function addTag($name) { return new JSONResponse(["Error" => "This tag name already exists"]); } - + return $this->getTags(); } /** @@ -793,7 +799,7 @@ public function editTag($id) { } $c->setName($name); $this->tagMapper->update($c); - + return $this->getTags(); } /** @@ -823,7 +829,7 @@ public function getTags(){ $wi = $this->workIntervalMapper->find($workItem); if ($wi->projectId != null){ $project = $this->projectMapper->find($wi->projectId); - + } } if($project != null && $project->locked){ @@ -869,7 +875,7 @@ public function getReport(){ $name = $this->userId; } - + if(!$this->isThisAdminUser()){ $allowedClients = $this->clientMapper->findAll($this->userId); $allowedClientsId = array_map(function($client){ return $client->id;}, $allowedClients ); @@ -889,7 +895,7 @@ public function getReport(){ } } - + $filterTagId = []; $groupOn1 = $this->request->group1; $groupOn2 = $this->request->group2; @@ -923,7 +929,7 @@ public function postTimeline(){ $name = $this->userId; } - + if(!$this->isThisAdminUser()){ $allowedClients = $this->clientMapper->findAll($this->userId); $allowedClientsId = array_map(function($client){ return $client->id;}, $allowedClients ); @@ -943,12 +949,12 @@ public function postTimeline(){ } } - + $filterTagId = []; $groupOn1 = $this->request->group1; $groupOn2 = $this->request->group2; $items = $this->reportItemMapper->report($name, $from, $to, $filterProjectId, $filterClientId, $filterTagId, $timegroup, $groupOn1, $groupOn2, $this->isThisAdminUser(), 0, 1000); - + $timeline = new Timeline(); $timeline->setUserUid($this->userId); $timeline->setGroup1($this->request->group1); @@ -1015,7 +1021,7 @@ public function secondsToTime($seconds){ $timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs); return $timeFormat; } - + /** * * @NoAdminRequired @@ -1040,7 +1046,7 @@ public function downloadTimeline($id){ fputcsv($output, array('id', 'User Uid', 'Name', 'Project Name', 'Client Name', 'Time Interval', 'Total Duration')); $totalDuration = 0; foreach($te as $t){ - + fputcsv($output, [$t->id, $t->userUid, $t->name, $t->projectName, $t->clientName, $t->timeInterval, $this->secondsToTime($t->totalDuration)]); $totalDuration += $t->totalDuration; } @@ -1092,7 +1098,7 @@ public function emailTimeline($id) { $emails = explode(';',$email); $subject = $this->request->subject; $content = $this->request->content; - + // output headers so that the file is downloaded rather than displayed header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=timeline-'.$user.'-'.$id.'.csv'); @@ -1106,14 +1112,14 @@ public function emailTimeline($id) { fputcsv($output, array('id', 'User Uid', 'Name', 'Project Name', 'Client Name', 'Time Interval', 'Total Duration')); $totalDuration = 0; foreach($te as $t){ - + fputcsv($output, [$t->id, $t->userUid, $t->name, $t->projectName, $t->clientName, $t->timeInterval, $this->secondsToTime($t->totalDuration)]); $totalDuration += $t->totalDuration; } fputcsv($output, ['TOTAL', '', '', '', '', '', $this->secondsToTime($totalDuration)]); - - - + + + $mailer = \OC::$server->getMailer(); $message = $mailer->createMessage(); $attach = $mailer->createAttachmentFromPath($path); @@ -1124,7 +1130,7 @@ public function emailTimeline($id) { //$message->setHtmlBody($content); $message->attach($attach); $mailer->send($message); - + fclose($output); unlink($path); return new JSONResponse([]); @@ -1157,10 +1163,10 @@ public function addGoal() { return new JSONResponse(["Error" => "There can be only one goal per project"]); } - + return $this->getGoals(); } - + /** * * @NoAdminRequired @@ -1229,7 +1235,7 @@ public function getGoals(){ $goals = $this->goalMapper->findAll($this->userId); $weekStart = $this->getStartOfWeek(time())->format('Y-m-d'); $monthStart = $this->getStartOfMonth(time())->format('Y-m'); - + $ret = []; foreach($goals as $goal){ $rgoal = []; @@ -1252,11 +1258,11 @@ public function getGoals(){ if ($goal->interval == 'Weekly'){ if ($interval == $this->getStartOfWeek($repItem->time)->format('Y-m-d')) { $workedInInterval += $repItem->totalDuration; - } + } } elseif ($goal->interval == 'Monthly'){ if ($interval == $this->getStartOfMonth($repItem->time)->format('Y-m')) { $workedInInterval += $repItem->totalDuration; - } + } } } $debtSeconds += ($goal->hours*3600 - $workedInInterval); diff --git a/lib/Db/ProjectMapper.php b/lib/Db/ProjectMapper.php index aa41da4..7176979 100644 --- a/lib/Db/ProjectMapper.php +++ b/lib/Db/ProjectMapper.php @@ -16,14 +16,21 @@ public function __construct(IDBConnection $db) { public function findByName($name) { $sql = 'SELECT * FROM `*PREFIX*timetracker_project` ' . 'WHERE upper(`name`) = ?'; - + try { $e = $this->findEntity($sql, [strtoupper($name)]); return $e; } catch (\OCP\AppFramework\Db\DoesNotExistException $e){ return null; } - + + } + + public function searchByName($user, string $name) { + $name = strtoupper($name); + $sql = 'SELECT tp.* FROM `*PREFIX*timetracker_project` tp LEFT JOIN `*PREFIX*timetracker_user_to_project` up ON up.project_id = tp.id WHERE up.`user_uid` = ? AND upper(tp.`name`) LIKE ? ORDER BY tp.`name`'; + + return $this->findEntities($sql, [$user, $name ."%"]); } /** @@ -59,17 +66,17 @@ public function findAllAdmin($getArchived = 0){ public function delete($project_id) { $sql = 'delete FROM `*PREFIX*timetracker_project` ' . ' where id = ?'; - + try { $this->execute($sql, [$project_id]); return; } catch (\OCP\AppFramework\Db\DoesNotExistException $e){ return; } - + } - -} \ No newline at end of file + +}