Files
grocy/services/TasksService.php

77 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2018-09-22 22:01:32 +02:00
<?php
namespace Grocy\Services;
2023-05-17 23:04:39 +04:00
use LessQL\Result;
2023-05-13 14:43:51 +02:00
2018-09-22 22:01:32 +02:00
class TasksService extends BaseService
{
2023-05-13 14:43:51 +02:00
public function GetCurrent(): Result
2018-09-22 22:01:32 +02:00
{
$users = UsersService::GetInstance()->GetUsersAsDto();
$categories = $this->DB->task_categories()->where('active = 1');
$tasks = $this->DB->tasks_current();
foreach ($tasks as $task)
{
if (!empty($task->assigned_to_user_id))
{
$task->assigned_to_user = FindObjectInArrayByPropertyValue($users, 'id', $task->assigned_to_user_id);
}
else
{
$task->assigned_to_user = null;
}
if (!empty($task->category_id))
{
$task->category = FindObjectInArrayByPropertyValue($categories, 'id', $task->category_id);
}
else
{
$task->category = null;
}
}
return $tasks;
2018-09-22 22:01:32 +02:00
}
2018-09-23 19:26:13 +02:00
public function MarkTaskAsCompleted($taskId, $doneTime)
{
if (!$this->TaskExists($taskId))
{
throw new \Exception('Task does not exist');
}
$taskRow = $this->DB->tasks()->where('id = :1', $taskId)->fetch();
2020-08-31 20:40:31 +02:00
$taskRow->update([
2018-09-23 19:26:13 +02:00
'done' => 1,
'done_timestamp' => $doneTime
2020-08-31 20:40:31 +02:00
]);
2018-09-23 19:26:13 +02:00
return true;
}
public function UndoTask($taskId)
{
if (!$this->TaskExists($taskId))
{
throw new \Exception('Task does not exist');
}
$taskRow = $this->DB->tasks()->where('id = :1', $taskId)->fetch();
2020-08-31 20:40:31 +02:00
$taskRow->update([
'done' => 0,
'done_timestamp' => null
2020-08-31 20:40:31 +02:00
]);
return true;
}
2018-09-22 22:01:32 +02:00
private function TaskExists($taskId)
{
$taskRow = $this->DB->tasks()->where('id = :1', $taskId)->fetch();
2018-09-22 22:01:32 +02:00
return $taskRow !== null;
}
}