aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/ClipboardWidget.class.php
blob: 9de627df0e11620a2b198f1672a01f70b03cacb0 (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
<?php

/**
 * This class is responsible for displaying new clipboards
 * (the ones that use the Clipboard SORM class) in the sidebar.
 *
 * @author  Moritz Strohm <strohm@data-quest.de>
 * @license GNU General Public License v2 or later.
 * @since   4.5
 */
class ClipboardWidget extends SidebarWidget
{
    protected $draggable_items;
    protected $current_selected_items = [];

    /**
     * clipboard_widget_id is required in the case that multiple
     * clipboard widgets exist on one page. The JavaScript code
     * can then distinguish each clipboard widget by its unique ID.
     */
    protected $clipboard_widget_id;


    /**
     * This attribute holds the ID of the clipboard which is stored in the
     * session as currently selected clipboard.
     */
    protected $current_clipboard_id;


    /**
     * This attribute contains a string that shall be the title of the button
     * for applying the selected clipbard to the main area
     * the widget is showing when in read only mode.
     */
    protected $apply_button_title;

    /**
     * This widget can be initialised with the class names of allowed classes
     * to limit the displayed items in a clipboard to items of specific
     * classes.
     */
    public function __construct($allowed_item_classes = [])
    {
        parent::__construct();

        if ($allowed_item_classes) {
            //Check if all allowed item classes are SimpleORMap objects
            //and if the classes implement the StudipItem interface:
            foreach ($allowed_item_classes as $class) {
                if (!is_subclass_of($class, 'StudipItem')) {
                    throw new InvalidArgumentException(
                        sprintf(
                            'The class %s does not implement the StudipItem interface which is required for clipboard items!',
                            htmlReady($class)
                        )
                    );
                }
            }
        } else {
            //Allow all StudipItem implementations:
            $allowed_item_classes = ['StudipItem'];
        }

        $this->allowed_item_classes = $allowed_item_classes;
        $this->template = 'sidebar/clipboard-widget';
        $this->title = _('Eigene Merkzettel');
        $this->apply_button_title = _('Hauptbereich aktualisieren');

        $this->clipboard_widget_id = md5(uniqid('clipboard_widget_id'));

        $this->updateSessionVariables();
        $this->current_clipboard_id = $_SESSION['selected_clipboard_id'];
        if (is_array($_SESSION['selected_clipboard_items'])) {
            $this->current_selected_items = $_SESSION['selected_clipboard_items'];
        }

        $this->setId("ClipboardWidget_{$this->clipboard_widget_id}");
        $this->setAdditionalAttribute('data-widget_id', $this->clipboard_widget_id);
        $this->addLayoutCSSClass('clipboard-widget');
    }

    /**
     * Updates session variables if a special POST request is made.
     */
    public function updateSessionVariables()
    {
        if (Request::submitted('clipboard_update_session_special_action')) {
            CSRFProtection::verifyUnsafeRequest();

            $_SESSION['selected_clipboard_id'] = Request::get(
                'selected_clipboard_id'
            );
            $_SESSION['selected_clipboard_items'] = Request::getArray(
                'selected_clipboard_items'
            );
        }
    }

    public function setApplyButtonTitle($title = '')
    {
        if ($title) {
            $this->apply_button_title = $title;
        }
    }

    public function getClipboardWidgetId()
    {
        return $this->clipboard_widget_id;
    }

    public function render($variables = [])
    {
        $clipboards = Clipboard::getClipboardsForUser(
            $GLOBALS['user']->id
        );

        if (!$this->current_clipboard_id && $clipboards) {
            $_SESSION['selected_clipboard_id'] = $clipboards[0]->id;
            $_SESSION['selected_clipboard_items'] = [];
            $this->current_clipboard_id = $clipboards[0]->id;
        }

        return parent::render($variables + [
            'clipboards'               => $clipboards,
            'allowed_item_classes'     => $this->allowed_item_classes,
            'clipboard_widget_id'      => $this->clipboard_widget_id,
            'draggable_items'          => $this->draggable_items,
            'apply_button_title'       => $this->apply_button_title,
            'elements'                 => $this->elements,
            'selected_clipboard_id'    => $this->current_clipboard_id,
            'selected_clipboard_items' => $this->current_selected_items,
        ]);
    }

    /**
     * Adds a link to the widget
     *
     * @param String $label  Label/content of the link
     * @param String $url    URL/Location of the link
     * @param Icon   $icon   instance of class Icon for the link
     * @param bool   $active Pass true if the link is currently active,
     *                       defaults to false
     */
    public function &addLink($label, $url, $icon = null, $attributes = array(), $index = null)
    {
        if ($index === null) {
            $index = 'link-' . md5($url);
        }
        $element = new LinkElement($label, $url, $icon, $attributes);
        $this->addElement($element, $index);
        return $element;
    }
}