Skip to content

Commit

Permalink
code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Eichhorn committed Mar 15, 2024
1 parent f6a19ba commit 7808452
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 41 deletions.
54 changes: 34 additions & 20 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
use Modules\Media\Models\ReferenceMapper;
use Modules\Notification\Models\Notification;
use Modules\Notification\Models\NotificationMapper;
use Modules\Notification\Models\NotificationStatus;
use Modules\Notification\Models\NotificationType;
use Modules\Profile\Models\ProfileMapper;
use Modules\Tasks\Models\DutyType;
use Modules\Tasks\Models\PermissionCategory;
use Modules\Tasks\Models\Task;
use Modules\Tasks\Models\TaskElement;
Expand Down Expand Up @@ -133,7 +131,7 @@ private function validateTaskCreate(RequestAbstract $request) : array
*/
public function createTaskReminderFromRequest(RequestAbstract $request) : array
{
/** @var AccountRelation[] $responsible */
/** @var \Modules\Tasks\Models\AccountRelation[] $responsible */
$responsible = TaskMapper::getResponsible((int) $request->getData('id'));

$reminder = [];
Expand Down Expand Up @@ -176,7 +174,10 @@ public function apiTaskCreate(RequestAbstract $request, ResponseAbstract $respon
$task = $this->createTaskFromRequest($request);
$this->createModel($request->header->account, $task, TaskMapper::class, 'task', $request->getOrigin());

$this->createNotifications(\reset($task->taskElements), NotificationType::CREATE, $request);
$first = \reset($task->taskElements);
if ($first !== false) {
$this->createNotifications($first, NotificationType::CREATE, $request);
}

if (!empty($request->files)
|| !empty($request->getDataJson('media'))
Expand All @@ -187,6 +188,19 @@ public function apiTaskCreate(RequestAbstract $request, ResponseAbstract $respon
$this->createStandardCreateResponse($request, $response, $task);
}

/**
* Create notifications for users.
*
* This is usually called when creating a task or adding a new child element.
*
* @param TaskElement $ele Task element
* @param int $type Notification type (e.g. new, new child element, ...)
* @param RequestAbstract $request Request that caused this notification to get created
*
* @return void
*
* @since 1.0.0
*/
private function createNotifications(TaskElement $ele, int $type, RequestAbstract $request) : void
{
$accChecked = [];
Expand Down Expand Up @@ -221,15 +235,15 @@ private function createNotifications(TaskElement $ele, int $type, RequestAbstrac
continue;
}

$notification = new Notification();
$notification->module = self::NAME;
$notification->title = $task->title;
$notification->createdBy = $element->createdBy;
$notification = new Notification();
$notification->module = self::NAME;
$notification->title = $task->title;
$notification->createdBy = $element->createdBy;
$notification->createdFor = $rel->relation;
$notification->type = $type;
$notification->category = PermissionCategory::TASK;
$notification->element = $task->id;
$notification->redirect = '{/base}/task/view?{?}&id=' . $element->task;
$notification->type = $type;
$notification->category = PermissionCategory::TASK;
$notification->element = $task->id;
$notification->redirect = '{/base}/task/view?{?}&id=' . $element->task;

$this->createModel($request->header->account, $notification, NotificationMapper::class, 'notification', $request->getOrigin());
$accChecked[] = $rel->relation->id;
Expand Down Expand Up @@ -259,15 +273,15 @@ private function createNotifications(TaskElement $ele, int $type, RequestAbstrac
continue;
}

$notification = new Notification();
$notification->module = self::NAME;
$notification->title = $task->title;
$notification->createdBy = $element->createdBy;
$notification = new Notification();
$notification->module = self::NAME;
$notification->title = $task->title;
$notification->createdBy = $element->createdBy;
$notification->createdFor = $account;
$notification->type = $type;
$notification->category = PermissionCategory::TASK;
$notification->element = $task->id;
$notification->redirect = '{/base}/task/view?{?}&id=' . $element->task;
$notification->type = $type;
$notification->category = PermissionCategory::TASK;
$notification->element = $task->id;
$notification->redirect = '{/base}/task/view?{?}&id=' . $element->task;

$this->createModel($request->header->account, $notification, NotificationMapper::class, 'notification', $request->getOrigin());
$accChecked[] = $account->id;
Expand Down
6 changes: 3 additions & 3 deletions Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function viewTaskDashboard(RequestAbstract $request, ResponseAbstract $re
->execute();
}

/** @var \Modules\Tasks\Models\TaskSeen[] $unread */
$view->data['unread'] = TaskMapper::getUnread($request->header->account);

return $view;
Expand Down Expand Up @@ -195,6 +194,8 @@ public function viewTaskList(RequestAbstract $request, ResponseAbstract $respons
}

$view->data['task_media'] = [];

/** @var \Modules\Tasks\Models\Task $task */
foreach ($view->data['tasks'] as $task) {

Check failure on line 199 in Controller/BackendController.php

View workflow job for this annotation

GitHub Actions / general_module_workflow / static-tests (8.1)

Argument of an invalid type mixed supplied for foreach, only iterables are supported.
$view->data['task_media'][$task->id] = TaskMapper::has()
->with('files')
Expand All @@ -203,7 +204,6 @@ public function viewTaskList(RequestAbstract $request, ResponseAbstract $respons
->execute();
}

/** @var \Modules\Tasks\Models\TaskSeen[] $unread */
$view->data['unread'] = TaskMapper::getUnread($request->header->account);

return $view;
Expand Down Expand Up @@ -321,7 +321,7 @@ public function viewTaskView(RequestAbstract $request, ResponseAbstract $respons
// Shows all reminders
if ($unseen->reminderBy !== null
&& ($unseen->reminderAt?->getTimestamp() ?? 0) < $request->header->getRequestTime()
&& ($unseen->reminderAt?->getTimestamp() ?? 0) > ($unseen->seenAt?->getTimestamp() ?? 0) - 300
&& ($unseen->reminderAt?->getTimestamp() ?? 0) > $unseen->seenAt->getTimestamp() - 300
) {
$reminderStatus[] = $unseen;

Expand Down
20 changes: 10 additions & 10 deletions Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function searchTag(RequestAbstract $request, ResponseAbstract $response,
->execute();

$results = [];
$count = 0;
$count = 0;

foreach ($tasks as $task) {
if ($count >= 8) {
Expand All @@ -89,10 +89,10 @@ public function searchTag(RequestAbstract $request, ResponseAbstract $response,
'link' => '{/base}/task/view?id=' . $task->id,
'account' => '',
'createdAt' => $task->createdAt,
'image' => '',
'tags' => $task->tags,
'type' => 'list_links',
'module' => 'Tasks',
'image' => '',
'tags' => $task->tags,
'type' => 'list_links',
'module' => 'Tasks',
];
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public function searchGeneral(RequestAbstract $request, ResponseAbstract $respon
->execute();

$results = [];
$count = 0;
$count = 0;

foreach ($tasks as $task) {
if ($count >= 8) {
Expand All @@ -156,10 +156,10 @@ public function searchGeneral(RequestAbstract $request, ResponseAbstract $respon
'link' => '{/base}/task/view?id=' . $task->id,
'account' => '',
'createdAt' => $task->createdAt,
'image' => '',
'tags' => $task->tags,
'type' => 'list_links',
'module' => 'Tasks',
'image' => '',
'tags' => $task->tags,
'type' => 'list_links',
'module' => 'Tasks',
];
}

Expand Down
28 changes: 23 additions & 5 deletions Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@ public function getTaskElement(int $id) : TaskElement
return $this->taskElements[$id] ?? new NullTaskElement();
}

/**
* Get accounts that are responsible for this task
*
* @return Account[]
*
* @since 1.0.0
*/
public function getResponsible() : array
{
$responsible = [];
Expand All @@ -445,7 +452,7 @@ public function getResponsible() : array
}

$responsible[] = $accRel->relation;
$first = false;
$first = false;
}
}
}
Expand Down Expand Up @@ -482,6 +489,17 @@ public function jsonSerialize() : mixed
return $this->toArray();
}

/**
* Create a new task from a task template.
*
* A task template is a normal task with the type of TEMPLATE.
*
* @param self $task Task to "clone"
*
* @return self
*
* @since 1.0.0
*/
public static function fromTemplate(self $task) : self
{
$now = new \DateTimeImmutable('now');
Expand All @@ -500,12 +518,12 @@ public static function fromTemplate(self $task) : self

// We need to create a new relation since the old one references the template
foreach ($task->attributes as $attribute) {
$attribute->id = 0;
$attribute->id = 0;
$attribute->ref = 0;
}

foreach ($task->taskElements as $element) {
$element->id = 0;
$element->id = 0;
$element->task = 0;

if ($element->due !== null) {
Expand All @@ -519,12 +537,12 @@ public static function fromTemplate(self $task) : self

// We need to create a new relation since the old one references the template
foreach ($element->accRelation as $relation) {
$relation->id = 0;
$relation->id = 0;
$relation->element = 0;
}

foreach ($element->grpRelation as $relation) {
$relation->id = 0;
$relation->id = 0;
$relation->element = 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Models/TaskMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public static function getUnread(int $user) : array
->innerJoin(AccountRelationMapper::TABLE)
->on(TaskElementMapper::TABLE . '.' . TaskElementMapper::PRIMARYFIELD, '=', AccountRelationMapper::TABLE . '.task_account_task_element')
->leftJoin(TaskSeenMapper::TABLE)
->on(TaskMapper::TABLE . '.' . TaskMapper::PRIMARYFIELD, '=', TaskSeenMapper::TABLE . '.task_seen_task')
->on(self::TABLE . '.' . self::PRIMARYFIELD, '=', TaskSeenMapper::TABLE . '.task_seen_task')
->where(self::TABLE . '.task_status', '=', TaskStatus::OPEN)
->andWhere(AccountRelationMapper::TABLE . '.task_account_account', '=', $user)
->andWhere(TaskSeenMapper::TABLE . '.task_seen_task', '=', null);
Expand Down
2 changes: 1 addition & 1 deletion Theme/Backend/Lang/de.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'Completion' => 'Abgeschlossen',
'Created' => 'Erstellt',
'Creator' => 'Ersteller',
'Advanced' => 'Fortgeschritten',
'Advanced' => 'Fortgeschritten',
'Due' => 'Fällig',
'Due/Priority' => 'Fällig / Priorität',
'For' => 'Für',
Expand Down
2 changes: 1 addition & 1 deletion Theme/Backend/Lang/en.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'Completion' => 'Completion',
'Created' => 'Created',
'Creator' => 'Creator',
'Advanced' => 'Advanced',
'Advanced' => 'Advanced',
'Due' => 'Due',
'Due/Priority' => 'Due/Priority',
'For' => 'For',
Expand Down

0 comments on commit 7808452

Please sign in to comment.