aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/help_content.php
blob: 3f767663295bcba5f20aa6e4582a4a0ac723876a (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO

/*
 * help_content.php - Stud.IP-Help Content controller
 *
 * Copyright (C) 2014 - Arne Schröder <schroeder@data-quest.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Arne Schröder <schroeder@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @package     help
*/

class HelpContentController extends AuthenticatedController
{
    /**
     * Callback function being called before an action is executed.
     */
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        if (
            !$GLOBALS['perm']->have_perm('root')
            && !User::findCurrent()->hasRole('Hilfe-Administrator(in)')
        ) {
            throw new AccessDeniedException();
        }

        $this->buildSidebar($action);
    }

    /**
     * Administration page for help content
     */
    public function admin_overview_action()
    {
        $this->help_content_searchterm = '';
        // initialize
        PageLayout::setTitle(_('Verwalten von Hilfe-Texten'));
        PageLayout::setHelpKeyword('Basis.HelpContentAdmin');
        // set navigation
        if ($GLOBALS['perm']->have_perm('root')) {
            Navigation::activateItem('/admin/config/help_content');
        } else {
            Navigation::activateItem('/contents/help_admin/help_content');
        }

        if (Request::get('help_content_filter') == 'set') {
            $this->help_content_searchterm = Request::option('help_content_filter_term');
        }
        if (Request::submitted('reset_filter')) {
            $this->help_content_searchterm = '';
        }
        if (Request::submitted('apply_help_content_filter')) {
            if (Request::get('help_content_searchterm') AND (mb_strlen(trim(Request::get('help_content_searchterm'))) < 3))
                PageLayout::postError(_('Der Suchbegriff muss mindestens 3 Zeichen lang sein.'));
            if (mb_strlen(trim(Request::get('help_content_searchterm'))) >= 3) {
                $this->help_content_searchterm = htmlReady(Request::get('help_content_searchterm'));
                $this->filter_text             = sprintf(_('Angezeigt werden Hilfe-Texte zum Suchbegriff "%s".'), $this->help_content_searchterm);
            }
        }

        // load help content
        $this->help_contents = HelpContent::GetContentByFilter($this->help_content_searchterm);
    }

    /**
     * Administration page for help content conflicts
     */
    public function admin_conflicts_action()
    {
        // initialize
        PageLayout::setTitle(_('Versions-Konflikte der Hilfe-Texte'));
        PageLayout::setHelpKeyword('Basis.HelpContentAdmin');
        // set navigation
        if ($GLOBALS['perm']->have_perm('root')) {
            Navigation::activateItem('/admin/config/help_content');
        } else {
            Navigation::activateItem('/contents/help_admin/help_content');
        }

        // load help content
        $this->conflicts = HelpContent::GetConflicts();
    }

    /**
     * resolves help content conflict
     *
     * @param String $id id of help content
     */
    public function resolve_conflict_action($id, $mode)
    {
        $GLOBALS['perm']->check('root');

        $this->help_content = HelpContent::GetContentByID($id);
        if ($mode == 'accept') {
            $this->help_content->studip_version = $GLOBALS['SOFTWARE_VERSION'];
            $this->help_content->store();
        } elseif ($mode == 'delete') {
            $this->help_content->delete();
        }

        $this->redirect('help_content/admin_conflicts');
    }

    /**
     * add new help content
     */
    public function add_action()
    {
        PageLayout::setTitle(_('Hilfe-Text erstellen'));

        $parameters = [];
        if (Request::get('from')) {
            $parameters['from'] = Request::get('from');
        }
        $this->parameters         = $parameters;
        $this->help_content_route = Request::get('help_content_route');
        $this->help_content_id    = null;
        $this->render_template('help_content/edit');
    }

    /**
     * edit help content
     *
     * @param String $id id of help content
     */
    public function edit_action($id)
    {
        PageLayout::setTitle(_('Hilfe-Text bearbeiten'));

        $parameters = [];
        if (Request::get('from')) {
            $parameters['from'] = Request::get('from');
        }
        $this->parameters = $parameters;

        $this->help_content       = HelpContent::GetContentByID($id);
        $this->help_content_route = $this->help_content->route;
        $this->help_content_id    = $id;
    }

    /**
     * Store changes
     *
     * @param string $id
     *
     * @throws InvalidSecurityTokenException
     */
    public function store_action($id = '')
    {
        CSRFProtection::verifyUnsafeRequest();

        $content_id         = md5(uniqid('help_content', 1));
        $create_new_content = false;
        if ($id == '') {
            $create_new_content = true;
        } else {
            // load content by id
            $help_content = HelpContent::GetContentByID($id);
            // check if the language has been changed
            if (Request::option('help_content_language') && $help_content != Request::option('help_content_language') && Request::get('from')) {
                // check if a content exists for the given route and the language
                $help_content = HelpContent::findOneBySQL('`language` = ? AND `route` = ?', [Request::option('help_content_language'), Request::get('from')]);
                if (is_null($help_content)) {
                    $create_new_content = true;
                }
            }
        }

        // create new content
        if ($create_new_content) {
            $help_content = new HelpContent();
            $help_content->setNew(true);
        }

        if ($help_content->isNew()) {
            $help_content->global_content_id = $content_id;
            $help_content->content_id        = $content_id;
            $help_content->studip_version    = $GLOBALS['SOFTWARE_VERSION'];
            $help_content->position          = 1;
            $help_content->custom            = 1;
            $help_content->language          = Request::get('help_content_language') ?: mb_substr($GLOBALS['user']->preferred_language, 0, 2);
        }

        $help_content->content         = Request::get('help_content_content');
        $help_content->route           = Request::get('help_content_route');
        $help_content->comment         = Request::get('help_content_comment');
        $help_content->author_email    = $GLOBALS['user']->Email;
        $help_content->installation_id = Config::get()->STUDIP_INSTALLATION_ID;

        if ($help_content->store()) {
            PageLayout::postSuccess(_('Der Hilfe-Text wurde erfolgreich gespeichert!'));
        }

        if (Request::isXhr() && Request::get('from')) {
            $this->response->add_header('X-Location', URLHelper::getURL(Request::get('from')));
            $this->render_nothing();
        } else {
            $this->relocate('help_content/admin_overview');
        }
    }

    /**
     * save settings
     */
    public function store_settings_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $this->help_contents = HelpContent::GetContentByFilter(Request::get('help_content_searchterm'));
        $count               = 0;
        foreach ($this->help_contents as $help_content_id => $help_content_id) {
            // set status as chosen
            if ((Request::get('help_content_status_' . $help_content_id) == '1') && (!$this->help_contents[$help_content_id]->visible)) {
                $this->help_contents[$help_content_id]->visible = 1;
            } elseif ((Request::get('help_content_status_' . $help_content_id) != '1') && ($this->help_contents[$help_content_id]->visible)) {
                $this->help_contents[$help_content_id]->visible = 0;
            }
            if ($this->help_contents[$help_content_id]->store()) {
                $count++;
            }
        }

        if($count) {
            PageLayout::postSuccess(sprintf(ngettext('%u Änderung wurde durchgeführt', '%u Änderungen wurden durchgeführt', $count), $count));
        }
        $this->redirect('help_content/admin_overview');
    }

    /**
     * delete help content
     *
     * @param String $id id of help content
     */
    public function delete_action($id)
    {
        PageLayout::setTitle(_('Hilfe-Text löschen'));

        $this->help_content = HelpContent::GetContentByID($id);
        if (is_object($this->help_content)) {
            if (Request::submitted('delete_help_content')) {
                CSRFProtection::verifyUnsafeRequest();

                $this->help_content->delete();
                PageLayout::postSuccess(sprintf(_('Der Hilfe-Text zur Route "%s" wurde gelöscht.'), htmlReady($this->help_content->route)));

                $this->response->add_header('X-Dialog-Close', 1);
                $this->render_nothing();
                return;
            }
        }

        // prepare delete dialog
        $this->help_content_id = $id;
    }

    /**
     * Build local sidebar
     */
    private function buildSidebar($action)
    {
        $sidebar = Sidebar::get();
        $widget  = new ViewsWidget();

        $widget->addLink(
            _('Übersicht'),
            $this->url_for('help_content/admin_overview')
        )->setActive($action == 'admin_overview');

        $widget->addLink(
            _('Konflikte'),
            $this->url_for('help_content/admin_conflicts')
        )->setActive($action == 'admin_conflicts');

        $sidebar->addWidget($widget);

        if ($action == 'admin_overview') {
            $widget = new ActionsWidget();
            $widget->addLink(
                _('Hilfe-Text erstellen'),
                $this->url_for('help_content/add'),
                Icon::create('add'),
                ['data-dialog' => 'size=auto;reload-on-close', 'target' => '_blank']
            );
            $sidebar->addWidget($widget);
            $search = new SearchWidget('?apply_help_content_filter=1');
            $search->addNeedle(_('Suchbegriff'), 'help_content_searchterm', true,);
            $sidebar->addWidget($search);
        }
    }
}