Newer
Older
<?php
namespace JsonApi\Routes\Messages;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
/**
* Liefert den Posteingang eines Nutzers zurück.
*/
abstract class BoxController extends JsonApiController
{
protected $allowedIncludePaths = ['sender', 'recipients', 'attachments'];
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
protected $allowedPagingParameters = ['offset', 'limit'];
abstract public function __invoke(Request $request, Response $response, $args);
protected function getBoxResponse($request, $args, $sndrec, $onlyUnread = false)
{
if (!$otherUser = \User::find($args['id'])) {
throw new RecordNotFoundException();
}
if (!MessageAuthority::canShowMessagesOfUser($this->getUser($request), $otherUser)) {
throw new AuthorizationFailedException();
}
$ids = self::folder($sndrec, $otherUser, $onlyUnread);
list($offset, $limit) = $this->getOffsetAndLimit();
return $this->getPaginatedContentResponse(
array_slice(self::load($ids, $otherUser), $offset, $limit),
count($ids)
);
}
private static function folder($sndrec, \User $user, $onlyUnread)
{
if ($onlyUnread) {
$query = 'SELECT message_id
FROM message_user
WHERE snd_rec = ? AND user_id = ? AND deleted = 0 AND readed = 0
ORDER BY mkdate DESC';
} else {
$query = 'SELECT message_id
FROM message_user
WHERE snd_rec = ? AND user_id = ? AND deleted = 0
ORDER BY mkdate DESC';
}
$statement = \DBManager::get()->prepare($query);
$statement->execute([$sndrec, $user->id]);
return $statement->fetchAll(\PDO::FETCH_COLUMN);
}
private static function load(array $ids, \User $user)
{
if (empty($ids)) {
return [];
}
$query = 'SELECT DISTINCT m.*
FROM message AS m
WHERE m.message_id IN (:ids)
ORDER BY m.mkdate DESC';
$statement = \DBManager::get()->prepare($query);
$statement->execute(
[
':ids' => $ids,
':user_id' => $user->id,
]
);
return array_map('Message::buildExisting', $statement->fetchAll(\PDO::FETCH_ASSOC));
}
}