aboutsummaryrefslogtreecommitdiff
path: root/app/routes/Clipboard.php
blob: 2f630dc78cc48cab53ef4da895db073b0546c525 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace RESTAPI\Routes;


/**
 * This file contains the REST class for the clipboard system.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2017-2019
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @since       4.5
 * @deprecated  Since Stud.IP 5.0. Will be removed in Stud.IP 5.2.
 */
class Clipboard extends \RESTAPI\RouteMap
{
    /**
     * Adds a new clipboard.
     *
     * @post /clipboard/add
     */
    public function addClipboard()
    {
        $name = \Request::get('name');

        if (!$name) {
            $this->halt(400, _('Es wurde kein Name angegeben!'));
        }

        $clipboard = new \Clipboard();
        $clipboard->user_id = $GLOBALS['user']->id;
        $clipboard->name = $name;
        if (!$clipboard->store()) {
            $this->halt(500, _('Fehler beim Speichern des Merkzettels!'));
        }

        $result = $clipboard->toRawArray();
        //A special treatment for the widget_id parameter:
        //It is passed through:
        $widget_id = \Request::get('widget_id');
        if ($widget_id) {
            $result['widget_id'] = $widget_id;
        }

        return $result;
    }


    /**
     * Edits a clipboard.
     *
     * @put /clipboard/:clipboard_id
     */
    public function editCliboard($clipboard_id = null)
    {
        $clipboard = \Clipboard::find($clipboard_id);
        if (!$clipboard) {
            $this->notFound(_('Ungültige Merkzettel-ID!'));
        }

        if ($clipboard->user_id != $GLOBALS['user']->id) {
            //Thou shalt not delete clipboards
            //which don't belong to you!
            throw new \AccessDeniedException();
        }

        $name = $this->data['name'];
        if (!$name) {
            $this->halt(400, _('Es wurde kein Name angegeben!'));
        }

        $clipboard->name = $name;

        if ($clipboard->isDirty()) {
            $success = $clipboard->store();
        } else {
            $success = true;
        }

        if (!$success) {
            $this->halt(500, _('Fehler beim Bearbeiten des Merkzettels!'));
        }

        $result = $clipboard->toRawArray();

        //A special treatment for the widget_id parameter:
        //It is passed through:
        $widget_id = \Request::get('widget_id');
        if ($widget_id) {
            $result['widget_id'] = $widget_id;
        }

        return $result;
    }


    /**
     * Deletes a clipboard.
     *
     * @delete /clipboard/:clipboard_id
     */
    public function deleteClipboard($clipboard_id = null)
    {
        $clipboard = \Clipboard::find($clipboard_id);
        if (!$clipboard) {
            $this->notFound(_('Ungültige Merkzettel-ID!'));
        }

        if ($clipboard->user_id !== $GLOBALS['user']->id) {
            //Thou shalt not delete items of clipboards
            //which don't belong to you!
            throw new \AccessDeniedException();
        }

        if (!$clipboard->delete()) {
            $this->halt(500, _('Fehler beim Löschen des Merkzettels!'));
        }

        return "";
    }


    /**
     * Adds an item to a clipboard.
     *
     * @post /clipboard/:clipboard_id/item
     */
    public function addClipboardItem($clipboard_id = null)
    {
        $clipboard = \Clipboard::find($clipboard_id);
        if (!$clipboard) {
            $this->notFound(_('Ungültige Merkzettel-ID!'));
        }

        if ($clipboard->user_id != $GLOBALS['user']->id) {
            //Thou shalt not add items to clipboards
            //which don't belong to you!
            throw new \AccessDeniedException();
        }

        $range_id = \Request::get('range_id');
        $range_type = \Request::get('range_type');
        $widget_id = \Request::get('widget_id');

        if (!is_a($range_type, $clipboard->allowed_item_class, true)) {
            $this->halt(
                400,
                sprintf(
                    _('Die Klasse %s ist in dieser Merkzettel-Klasse nicht erlaubt!'),
                    $range_type
                )
            );
        }

        try {
            $item = $clipboard->addItem($range_id, $range_type);

            $result = $item->toRawArray();
            $result['name'] = $item->__toString();
            if ($widget_id) {
                $result['widget_id'] = $widget_id;
            }
            return $result;
        } catch (\Exception $e) {
            $this->halt(500, $e->getMessage());
        }
    }


    /**
     * Removes an item (selected by its range-ID) from a clipboard.
     *
     * @delete /clipboard/:clipboard_id/item/:range_id
     */
    public function removeClipboardItem($clipboard_id = null, $range_id = null)
    {
        $clipboard = \Clipboard::find($clipboard_id);
        if (!$clipboard) {
            $this->notFound(_('Ungültige Merkzettel-ID!'));
        }

        if ($clipboard->user_id != $GLOBALS['user']->id) {
            //Thou shalt not delete items of clipboards
            //which don't belong to you!
            throw new \AccessDeniedException();
        }

        if ($clipboard->removeItem($range_id)) {
            return ['range_id' => $range_id];
        } else {
            $this->halt(500, _('Fehler beim Löschen des Eintrags!'));
        }
    }
}