summaryrefslogtreecommitdiff
path: root/includes/pages/admin_news.php
blob: 19d3c7ed3f3cc3363321a5caa30926064ddc7297 (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
<?php

use Engelsystem\Models\News;

/**
 * @return string
 */
function admin_news()
{
    $request = request();

    if (!$request->has('action')) {
        throw_redirect(page_link_to('news'));
    }

    $html = '<div class="col-md-12"><h1>' . __('Edit news entry') . '</h1>' . msg();
    if ($request->has('id') && preg_match('/^\d{1,11}$/', $request->input('id'))) {
        $news_id = $request->input('id');
    } else {
        return error('Incomplete call, missing News ID.', true);
    }

    $news = News::find($news_id);
    if (empty($news)) {
        return error('No News found.', true);
    }

    switch ($request->input('action')) {
        case 'edit':
            $user_source = $news->user;
            if (
                !auth()->can('admin_news_html')
                && strip_tags($news->text) != $news->text
            ) {
                $html .= warning(
                    __('This message contains HTML. After saving the post some formatting will be lost!'),
                    true
                );
            }

            $html .= form(
                [
                    form_info(__('Date'), $news->created_at->format('Y-m-d H:i')),
                    form_info(__('Author'), User_Nick_render($user_source)),
                    form_text('eBetreff', __('Subject'), $news->title),
                    form_textarea('eText', __('Message'), $news->text),
                    form_checkbox('eTreffen', __('Meeting'), $news->is_meeting, 1),
                    form_submit('submit', __('Save'))
                ],
                page_link_to('admin_news', ['action' => 'save', 'id' => $news_id])
            );

            $html .= '<a class="btn btn-danger" href="'
                . page_link_to('admin_news', ['action' => 'delete', 'id' => $news_id])
                . '">'
                . '<span class="glyphicon glyphicon-trash"></span> ' . __('Delete')
                . '</a>';
            break;

        case 'save':
            $text = $request->postData('eText');
            if (!auth()->can('admin_news_html')) {
                $text = strip_tags($text);
            }

            $news->title = strip_tags($request->postData('eBetreff'));
            $news->text = $text;
            $news->is_meeting = $request->has('eTreffen');
            $news->save();

            engelsystem_log('News updated: ' . $request->postData('eBetreff'));
            success(__('News entry updated.'));
            throw_redirect(page_link_to('news'));
            break;

        case 'delete':
            $news->delete();
            engelsystem_log('News deleted: ' . $news->title);
            success(__('News entry deleted.'));
            throw_redirect(page_link_to('news'));
            break;
        default:
            throw_redirect(page_link_to('news'));
    }
    return $html . '</div>';
}