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
|
|
|
{
|
2021-07-06 18:40:55 +02:00
|
|
|
$users = $this->getUsersService()->GetUsersAsDto();
|
2023-05-13 14:24:52 +02:00
|
|
|
$categories = $this->getDatabase()->task_categories()->where('active = 1');
|
2021-07-06 18:40:55 +02:00
|
|
|
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2019-07-06 20:34:01 +02:00
|
|
|
public function UndoTask($taskId)
|
|
|
|
{
|
|
|
|
if (!$this->TaskExists($taskId))
|
|
|
|
{
|
|
|
|
throw new \Exception('Task does not exist');
|
|
|
|
}
|
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
$taskRow = $this->getDatabase()->tasks()->where('id = :1', $taskId)->fetch();
|
2020-08-31 20:40:31 +02:00
|
|
|
$taskRow->update([
|
2019-07-06 20:34:01 +02:00
|
|
|
'done' => 0,
|
|
|
|
'done_timestamp' => null
|
2020-08-31 20:40:31 +02:00
|
|
|
]);
|
2019-07-06 20:34:01 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-22 22:01:32 +02:00
|
|
|
private function TaskExists($taskId)
|
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
$taskRow = $this->getDatabase()->tasks()->where('id = :1', $taskId)->fetch();
|
2018-09-22 22:01:32 +02:00
|
|
|
return $taskRow !== null;
|
|
|
|
}
|
|
|
|
}
|