blob: 4ef1ee10d2a2769d27c35841439807dd4a1153a0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
|
<?php
namespace JsonApi\Routes\Forum;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Errors\InternalServerError;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use JsonApi\Models\ForumEntry;
/**
* Edits content of a news.
*/
class ForumEntriesUpdate extends JsonApiController
{
use ValidationTrait;
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(Request $request, Response $response, $args)
{
$json = $this->validate($request);
if (!$entry = ForumEntry::find($args['id'])) {
throw new RecordNotFoundException('Entry has not been found.');
}
if (!$course = \Course::find($entry->seminar_id)) {
throw new RecordNotFoundException('Course does not exist.');
}
if (!ForumAuthority::has($this->getUser($request), 'view', $course)) {
throw new AuthorizationFailedException();
}
if (!$entry = $this->updateEntryFromJSON($entry, $json)) {
throw new InternalServerError('Could not update the entry.');
}
return $this->getContentResponse($entry);
}
protected function updateEntryFromJSON($entry, $json)
{
$title = self::arrayGet($json, 'data.attributes.title');
$content = self::arrayGet($json, 'data.attributes.content');
if (!empty($title)) {
$entry->name = $title;
}
if (!empty($content)) {
if (method_exists(\Studip\Markup::class, 'purifyHtml')) {
$content = transformBeforeSave(\Studip\Markup::purifyHtml($content));
}
$entry->content = $content;
}
if ($entry->isDirty()) {
$entry->store();
}
return $entry;
}
protected function validateResourceDocument($json)
{
$title = self::arrayGet($json, 'data.attributes.title');
$content = self::arrayGet($json, 'data.attributes.content');
if (empty($title) && empty($content)) {
return 'You must change entry-data to update.';
}
}
}
|