This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.tx_mydashboard_widgetmgm.php
339 lines (294 loc) · 9.23 KB
/
class.tx_mydashboard_widgetmgm.php
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/***************************************************************
* Copyright notice
*
* (c) 2008 Tim Lochmueller <webmaster@fruit-lab.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class tx_mydashboard_widgetmgm
{
/* Private Attributes */
private $widgets = [];
private $widgetsKeys = [];
private $baseWidgets = [];
private $dashboardwidgets = [];
private $userConf = [];
private $user = false;
/*
* Construct the Widget Managment
*/
public function __construct()
{
$this->preloadAllWidgets();
}
/*
* Preload the Hook data an collect it in a internal Array
*
* @return boolean
*/
private function preloadAllWidgets()
{
global $TYPO3_CONF_VARS;
if (!is_array($TYPO3_CONF_VARS['SC_OPTIONS']['ext/mydashboard/class.tx_mydashboard_widgetmgm.php']['addWidget'])) {
return false;
}
foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['ext/mydashboard/class.tx_mydashboard_widgetmgm.php']['addWidget'] as $widgetKey => $classRef) {
$this->widgetsKeys[$widgetKey] = $classRef;
}
return true;
}
/*
* Load a Widget with the given WidgetKey
*
* @param String $key Widget key
* @return Object The Widget
*/
public function loadWidget($key)
{
if (!isset($this->widgetsKeys[$key])) {
return false;
}
if (isset($this->baseWidgets[$key])) {
return $this->baseWidgets[$key];
}
$this->baseWidgets[$key] = &\TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($this->widgetsKeys[$key]);
if (is_object($this->baseWidgets[$key]) && !in_array('tx_mydashboard_widgetinterface', class_implements($this->baseWidgets[$key]))) {
echo 'Error: The Widget "<b>' . $key . '</b>" have to implement the interface "tx_mydashboard_widgetinterface"!<br />';
unset($this->baseWidgets[$key]);
return false;
}
$this->baseWidgets[$key]->setWidgetKey($key);
return $this->baseWidgets[$key];
}
/*
* Get Alle Widgets (For Configuration)
*
* @return Array Widget Objects
*/
public function getAllWidgets()
{
$widgets = [];
foreach ($this->widgetsKeys as $key => $value) {
$widget = $this->loadWidget($key);
if (!$widget->init()) {
continue;
}
$widgets[] = $widget;
} // foreach
return $widgets;
}
/*
* Get a widget with the user configuration
*
* @param String $dashboardKey The Dashboard Key
* @return Object/Boolean The Widget
*/
public function getWidget($dashboardKey)
{
if (!is_array($this->userConf['items'][$dashboardKey])) {
return false;
}
$data = $this->userConf['items'][$dashboardKey];
$widget = $this->loadWidget($data['widgetkey']);
if (!$widget) {
return false;
}
if (is_array($data['config'])) {
$widget->setConfigVars($data['config']);
}
$widget->setDashboardKey($dashboardKey);
if (!$widget->init()) {
return false;
}
return $widget;
}
/*
* Helper function to Render a Widget Icon
*
* @param Object Widget Object
* @return String The HTML for the WIdget Icon
*/
public function renderIcon(&$widget)
{
return '<img src="' . $widget->getIcon() . '" width="16" height="16" alt="' . $widget->getTitle() . '" title="' . $widget->getTitle() . '" />';
}
/*
* Return the User data
*
* @param String $field The Fielname (Optional)
* @return Array/String the Data
*/
public function getUserData($field = false)
{
if ($field) {
return $this->userData[$field];
}
return $this->userData;
}
/*
* Set the widget configuration
*
* @param String $dashKey The Dashboard Widget Key
* @param Array the configuration
* @return boolean (Write the User conf only on true)
*/
public function setWidgetConf($dashKey, $conf)
{
foreach ($conf as $key => $val) {
$conf[$key] = htmlspecialchars($val);
}
if (!isset($this->userConf['items'][$dashKey])) {
return false;
}
if (!is_array($conf)) {
return false;
}
if (!is_array($this->userConf['items'][$dashKey]['config'])) {
$this->userConf['items'][$dashKey]['config'] = [];
}
$this->userConf['items'][$dashKey]['config'] = array_merge(
$this->userConf['items'][$dashKey]['config'],
$conf
);
return true;
}
/*
* Return the User Conf
*
* @param String $field The Fielname (Optional)
*/
public function getUserConf($field = false)
{
if ($field) {
return $this->userConf[$field];
}
return $this->userConf;
}
/*
* Set the User Conf
*
* @param String $field The Fielname
* @param String $value The Value
*/
public function setUserConf($field, $value)
{
if (!isset($this->userConf[$field])) {
return false;
}
$this->userConf[$field] = $value;
return true;
}
/*
* Set the new Order of the Dashboard Widgets
*
* @param array $order The multi Array with the new Order
*/
public function setNewOrder($order)
{
//if(count($this->userConf['position'], COUNT_RECURSIVE) != count($order, COUNT_RECURSIVE))
// return false;
ksort($order);
$this->userConf['position'] = $order;
return true;
}
/*
* Add a new Widget to the Dashboard
*
* @param string $key The Widget Key
* @param int $pos The position in the dashboard
*/
public function addWidget($key, $pos = 0)
{
if (!$this->loadWidget($key)) {
return false;
}
$dashboardKey = str_replace('_', '', $key) . substr(md5($key . time()), 0, 15);
$this->userConf['items'][$dashboardKey] = [
'widgetkey' => $key,
'config' => []
];
$this->userConf['position'][$pos][] = $dashboardKey;
return true;
}
/*
* Remove the Widget with the $dashboardKey from the Dashboard
*
* @param string $dashboardKey The DashboardKey
*/
public function removeWidget($dashboardKey)
{
if (isset($this->userConf['items'][$dashboardKey])) {
unset($this->userConf['items'][$dashboardKey]);
}
if (!is_array($this->userConf['position'])) {
return false;
}
foreach ($this->userConf['position'] as $key => $value) {
if ($index = array_search($dashboardKey, $value)) {
unset($value[$index]);
$this->userConf['position'][$key] = $value;
}
} // foreach
return true;
}
/*
* Get the default Conf Object and return the configuration
*/
private function getDefaultConf()
{
$obj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\tx_mydashboard_widgetmgm_defaultconf::class);
return $obj->getConf();
}
/*
* Safe the Widget configuration in the user record
*
* @param int $userID The ID of the user Record
*/
public function safeUserConf($userID = false)
{
if (!is_array($this->userConf)) {
return false;
}
if (!$userID) {
$userID = $this->user;
}
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('be_users', 'uid=' . intval($userID), ['tx_mydashboard_config' => serialize($this->userConf)]);
}
/*
* Load the Widget conf from the user
*
* @param int $userID The User ID of the User
*/
public function loadUserConf($userID)
{
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'be_users', 'uid=' . intval($userID), '', '', 1);
if (!$GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
return false;
}
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
$conf = @unserialize($row['tx_mydashboard_config']);
$this->user = $userID;
$this->userData = $row;
$this->userConf = (is_array($conf))?$conf:$this->getDefaultConf();
return true;
}
}