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
|
<?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 $allowed_item_class;
protected $draggable_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', true)) {
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 = _('Merkzettel');
$this->readonly = false;
$this->apply_button_title = _('Hauptbereich aktualisieren');
$this->clipboard_widget_id = md5(uniqid('clipboard_widget_id'));
$this->draggable_items = false;
$this->updateSessionVariables();
$this->current_clipboard_id = $_SESSION['selected_clipboard_id'];
$this->current_selected_items = $_SESSION['selected_clipboard_items'];
if (!is_array($this->current_selected_items)) {
$this->current_selected_items = [];
}
}
public function clearSessionVariables()
{
$_SESSION['selected_clipboard_id'] = null;
$_SESSION['selected_clipboard_items'] = [];
}
/**
* 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'
);
}
}
/**
* Enables clipboard items to be dragged to the main area of the page.
*/
public function enableDraggableItems()
{
$this->draggable_items = true;
}
/**
* Disables the dragging of clipboard items.
*/
public function disableDraggableItems()
{
$this->draggable_items = false;
}
public function setReadonly($readonly = false)
{
$this->readonly = (bool)$readonly;
}
public function isReadonly()
{
return $this->readonly;
}
public function setApplyButtonTitle($title = '')
{
if ($title) {
$this->apply_button_title = $title;
}
}
public function getApplyButtonTitle()
{
return $this->apply_button_title;
}
public function getClipboardWidgetId()
{
return $this->clipboard_widget_id;
}
public function render($variables = [])
{
$template = $GLOBALS['template_factory']->open(
$this->template
);
$layout = $GLOBALS['template_factory']->open(
'widgets/widget-layout'
);
$template->set_layout('widgets/widget-layout');
$clipboards = Clipboard::getClipboardsForUser(
$GLOBALS['user']->id
);
if (!$this->current_clipboard_id) {
if ($clipboards) {
$_SESSION['selected_clipboard_id'] = $clipboards[0]->id;
$_SESSION['selected_clipboard_items'] = [];
$this->current_clipboard_id = $clipboards[0]->id;
}
}
$template->set_attribute(
'selected_clipboard_id',
$this->current_clipboard_id
);
$template->set_attribute(
'selected_clipboard_items',
$this->current_selected_items
);
$template->set_attribute('clipboards', $clipboards);
$template->set_attribute(
'allowed_item_classes',
$this->allowed_item_classes
);
$template->set_attribute(
'clipboard_widget_id',
$this->clipboard_widget_id
);
$template->set_attribute(
'draggable_items',
$this->draggable_items
);
$template->set_attribute(
'readonly',
$this->readonly
);
$template->set_attribute(
'apply_button_title',
$this->apply_button_title
);
$template->set_attribute(
'elements',
$this->elements
);
return $template->render();
}
/**
* 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;
}
}
|