aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/course/forum/categories.php
blob: ff1ddfaee64361e208901e42aa17bdcdc417ce37 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

use Forum\Category;

class Course_Forum_CategoriesController extends Forum\BaseController
{
    public function before_filter(&$action, &$args): void
    {
        parent::before_filter($action, $args);

        if (!RangeConfig::get($this->range_id)->FORUM_HIDE_CATEGORIES_NAVIGATION) {
            Navigation::activateItem('course/forum/categories');
        } else {
            Navigation::activateItem('course/forum/topics');
        }
    }

    public function index_action(): void
    {
        $this->render_vue_app(
            Studip\VueApp::create('forum/categories/Index')
        );
    }

    public function show_action(Category $category): void
    {
        if (!$category) {
            throw new NotFoundException();
        }

        PageLayout::setTitle($category->name);

        $this->render_vue_app(
            Studip\VueApp::create('forum/categories/Show')
                ->withProps([
                    'category' => $category->transformData(),
                    'metadata' => [
                        'postings_count' => (int) $category->metadata['postings_count'],
                        'users_count' => (int) $category->metadata['users_count'],
                        'recent_activity' => $category->metadata['recent_activity'] ? date('c', $category->metadata['recent_activity']) : null
                    ]
            ])
        );
    }

    public function edit_action($category_id = null): void
    {
        if (!$this->is_moderator) {
            throw new AccessDeniedException();
        }

        if ($category_id) {
            PageLayout::setTitle(_('Kategorie bearbeiten'));
            $category = Category::findOneBySQL("range_id = ? AND category_id = ?", [$this->range_id, $category_id]);

            if (!$category) {
                throw new AccessDeniedException();
            }
        } else {
            PageLayout::setTitle(_('Neue Kategorie anlegen'));
            $category = new Category();
        }

        $this->render_vue_app(
            Studip\VueApp::create('forum/categories/Edit')
                ->withProps([
                    'category' => $category->transformData()
                ])
        );
    }

    public function save_action($category_id = null): void
    {
        if (!$this->is_moderator) {
            throw new AccessDeniedException();
        }

        CSRFProtection::verifyUnsafeRequest();

        if ($category_id) {
            $category = Category::findOneBySQL("range_id = ? AND category_id = ?", [$this->range_id, $category_id]);
            if (!$category) {
                throw new AccessDeniedException();
            }
        } else {
            $category = new Category();
            $category->range_id = $this->range_id;
        }

        $category->name = Request::get('name');
        $category->description = Request::get('description');
        $category->color = Request::get('color');

        $category->store();

        PageLayout::postSuccess(sprintf(_('Die Kategorie „%s“ wurde gespeichert.'), $category->name));

        $this->relocate('course/forum/categories');
    }

    public function delete_action(Category $category): void
    {
        CSRFProtection::verifyUnsafeRequest();

        if (!$this->is_moderator) {
            throw new AccessDeniedException();
        }

        if (!$category) {
            throw new NotFoundException();
        }

        $category->delete();

        PageLayout::postSuccess(_('Die Kategorie wurde gelöscht.'));

        $this->relocate('course/forum/categories');
    }
}