Files
grocy/services/TasksService.php
T

75 lines
1.5 KiB
PHP
Raw Normal View History

2018-09-22 22:01:32 +02:00
<?php
namespace Grocy\Services;
class TasksService extends BaseService
{
2020-09-01 19:59:40 +02:00
public function GetCurrent(): \LessQL\Result
2018-09-22 22:01:32 +02:00
{
$users = $this->getUsersService()->GetUsersAsDto();
$categories = $this->getDatabase()->task_categories();
$tasks = $this->getDatabase()->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->getDatabase()->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->getDatabase()->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->getDatabase()->tasks()->where('id = :1', $taskId)->fetch();
2018-09-22 22:01:32 +02:00
return $taskRow !== null;
}
}