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
|
<?php
namespace JsonApi\Routes\Forum;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Models\ForumEntry;
use JsonApi\Models\ForumCat;
abstract class AbstractEntriesCreate extends JsonApiController
{
use ValidationTrait;
protected function validateResourceDocument($json, $data)
{
$content = self::arrayHas($json, 'data.attributes.title');
if (empty($content)) {
return 'Entries should not be empty.';
}
}
protected function createEntryFromJSON($user, $parentId, $json)
{
//Check whether the parent is category or entry of first or seccond depth
$title = self::arrayGet($json, 'data.attributes.title');
$content = self::arrayGet($json, 'data.attributes.content');
$content = \Studip\Markup::purifyHtml($content);
$parent = $this->getParentObject($parentId);
return $this->createEntry($title, $content, $parent, $user);
}
protected function getParentObject($parentId)
{
if ($parent = ForumCat::find($parentId)) {
return $parent;
}
return ForumEntry::find($parentId);
}
protected function createEntry($title, $content, $parent, $user)
{
//Do we create id's like this?
$topicId = md5(uniqid(rand()));
if (empty($title)) {
$title = $parent->name;
}
$data = [
'topic_id' => $topicId,
'seminar_id' => $parent->seminar_id,
'user_id' => $user->id,
'name' => $title,
'content' => $content,
'author' => $user->getFullName(),
'anonymous' => 0,
];
$entry = new ForumEntry();
$entry->setData($data);
$entry->storeWith($parent, $entry);
return $entry;
}
}
|