This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
forked from kristofvanroy/nice-dash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nice_dash.pages.inc
379 lines (305 loc) · 10.3 KB
/
nice_dash.pages.inc
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Menu callback for the dashboard page.
*/
function nice_dash_dashboard_page($did = NULL) {
$widgets = module_invoke_all('nice_dash_widgets');
if($widgets){
// Fetch the default dashboard
if(!$did){
$did = db_result(db_query("SELECT did FROM {nice_dash_dashboards} ORDER BY weight ASC LIMIT 1"));
}
$result = db_query("SELECT * FROM {nice_dash_config} c
LEFT JOIN {nice_dash_widgets} w ON c.wid = w.wid
WHERE did = %d
ORDER BY weight", $did);
while($row = db_fetch_array($result)){
if($row['region'] != 'disabled'){
if($row['custom']){
$widget = nice_dash_get_custom_widget($row);
}
else {
if (function_exists($widgets[$row['widget_key']]['callback'])) {
$widget = call_user_func($widgets[$row['widget_key']]['callback']);
}
}
$regions[$row['region']] .= $widget;
}
}
}
if(!count($regions)){
drupal_set_message(t('You have not configured any widgets yet. Goto the <a href="@settings-page">dashboard settings</a> and configure some widgets first.',array('@settings-page' => url("admin/dashboard/settings"))),'warning');
return '';
}
return theme('nice_dash_page', $regions);
}
/**
* Overview page with the dashboards and custom widgets.
*/
function nice_dash_dashboard_overview_page(){
// Dashboards
$dashboards = nice_dash_dashboards();
$output .= '<h2>'.t('Dashboards').'</h2>';
if(count($dashboards) > 0){
$headers = array(t('Name'), t('Actions'));
$rows = array();
foreach($dashboards as $dashboard){
$rows[] = array(
l($dashboard['name'], 'admin/dashboard'),
l(t('View'), 'admin/dashboard/'.$dashboard['did']).' | '.l(t('Edit'), 'admin/dashboard/configure/'.$dashboard['did'].'/edit').' | '.l(t('Delete'), 'admin/dashboard/configure/'.$dashboard['did'].'/delete'));
}
$output .= theme('table', $headers, $rows);
}
else {
drupal_set_message(t('You have to create a dashboard first. You can create your first dashboard on the <a href="@create-page">dashboard create page</a>.',array('@create-page' => url("admin/dashboard/configure/create-dashboard"))), 'warning');
$output .= '<p>'.t('No dashboards configured.').'</p>';
}
$regions['left'] = $output;
// Widgets
$output = '';
$widgets = nice_dash_custom_widgets();
$output .= '<h2>'.t('Widgets').'</h2>';
if(count($widgets) > 0){
$headers = array(t('Title'), t('Actions'));
$rows = array();
foreach($widgets as $widget){
$rows[] = array(
$widget['title'],
l(t('Edit'), 'admin/dashboard/widget/'.$widget['wid'].'/edit').' | '.l(t('Delete'), 'admin/dashboard/widget/'.$widget['wid'].'/delete')
);
}
$output .= theme('table', $headers, $rows);
}
else {
$output .= '<p>'.t('No widgets configured.').'</p>';
}
$regions['right'] = $output;
return theme('nice_dash_page', $regions);
}
/**
* Menu callback for the dashboard settings page.
*/
function nice_dash_dashboard_settings_form(&$form_state, $did = NULL) {
$form = array();
if(isset($did)){
$dashboard = nice_dash_dashboard_values($did);
}
$widgets = nice_dash_widgets();
if($widgets){
$form['#tree'] = TRUE;
$form['#theme'] = 'nice_dash_settings_form';
foreach($widgets as $widget){
//Fetch values
$default_values = nice_dash_widget_values($widget['wid'], $did);
$form['#widgets'][] = $widget['wid'];
$form[$widget['wid']]['title'] = array('#value' => $widget['title']);
$form[$widget['wid']]['region'] = array(
'#type' => 'select',
'#options' => nice_dash_regions(),
'#default_value' => $default_values['region'],
'#attributes' => array(
'class' => 'field-region-select field-region-disabled ',
),
);
$form[$widget['wid']]['weight'] = array('#type' => 'weight', '#delta' => 1, '#default_value' => $default_values['weight']);
}
$form['widgets'] = array('#type' => 'value', '#value' => $form['#widgets']);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
}
else {
$form = array();
drupal_set_message(t('You have to enable at least one of the Nice Dashboard plugin modules. Goto the <a href="@modules-page">modules page</a> and enable one or more of the Nice Dashboard plugins.',array('@modules-page' => url("admin/build/modules"))),'warning');
}
$form['dashboard_name'] = array(
'#type' => 'textfield',
'#default_value' => $dashboard['name'],
'#title' => t('Dashboard name'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#weight' => -100,
);
// Current dashboard id
$form['dashboard_id'] = array('#type' => 'value', '#value' => $did);
return $form;
}
function nice_dash_dashboard_settings_form_validate(&$form, &$form_state){
// CHECK IF THE NAME OF THE DASHBOARD DOES NOT YET EXIST
}
/**
* Settings form submit handler
*/
function nice_dash_dashboard_settings_form_submit(&$form, &$form_state) {
$values = $form_state['values'];
// Dashboard
$update_keys = array();
$dashboard = new stdClass();
$dashboard->name = $values['dashboard_name'];
if(isset($values['dashboard_id'])){
$update_keys = array('did');
}
drupal_write_record('nice_dash_dashboards', $dashboard, $update_keys);
if(!isset($values['dashboard_id'])){
$values['dashboard_id'] = db_last_insert_id('nice_dash_dasboard','did');
}
// Widgets
if (sizeof($values['widgets']) > 0) {
foreach($values['widgets'] as $wid){
$widget = new stdClass();
$update_keys = array();
// Check if row exists
$db_wid = db_result(db_query("SELECT wid FROM {nice_dash_config} WHERE wid = %d AND did = %d", $wid, $values['dashboard_id']));
$widget->wid = $wid;
$widget->did = $values['dashboard_id'];
$widget->region = $values[$wid]['region'];
$widget->weight = $values[$wid]['weight'];
if(is_numeric($db_wid)){
$widget->wid = $db_wid;
$update_keys = array('wid','did');
}
drupal_write_record('nice_dash_config', $widget, $update_keys);
}
}
module_invoke('menu','rebuild');
// Success
drupal_set_message(t('Saved dashboard.'));
}
/**
* Confirm delete form for dashboards
*/
function nice_dash_dashboard_delete_form($form_state, $did) {
$form = array();
$form['did'] = array('#value' => $did, '#type' => 'value');
$dashboard = nice_dash_dashboard_values($did);
if(empty($dashboard)){
drupal_set_message(t('The dashboard you want to delete does not exist. Go back to the <a href="@overview-page">dashboard overview page</a>.', array('@overview-page' => url("admin/dashboard/configure"))),'warning');
return $form;
}
return confirm_form(
$form,
t('Are your sure you want to delete the dashboard %name', array('%name' => $dashboard['name'])),
'admin/dashboard/configure',
t('This action cannot be undone.'),
t('Delete')
);
}
/**
* Submit for confirm delete form for dashboards
*/
function nice_dash_dashboard_delete_form_submit($form, &$form_state) {
nice_dash_remove_dashboard($form_state['values']['did']);
drupal_set_message(t('The dashboard has been removed'));
drupal_goto('admin/dashboard/configure');
}
function nice_dash_widget_settings_form(&$form_state, $wid = NULL){
if(isset($wid)){
$widget = nice_dash_widget_values($wid);
}
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#size' => 60,
'#required' => TRUE,
'#default_value' => $widget['title'],
);
$form['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#size' => 60,
'#default_value' => $widget['description'],
);
$block_info = array();
foreach (module_implements('block') as $module) {
$module_blocks = module_invoke($module, 'block', 'list');
if ($module_blocks) {
foreach ($module_blocks as $delta => $info) {
$block_info["{$module}-{$delta}"] = $info;
}
}
}
// Get default block options.
$options = array();
foreach ($block_info as $bid => $info) {
if (!empty($enabled[$bid])) {
$options[$bid] = $info['info'];
}
}
asort($options);
$result = db_query("SELECT name,type,info FROM {system} WHERE type = 'module' AND status = 1");
$modules = array();
while ($row = db_fetch_object($result)) {
$info = unserialize($row->info);
$modules[$row->name] = isset($info['name']) ? $info['name'] : $row->name;
}
foreach (array_diff_key($block_info, $options) as $bid => $info) {
$module = array_shift(explode('-', $bid));
$module = isset($modules[$module]) ? $modules[$module] : $module;
$custom_options[$module][$bid] = $info['info'];
}
$custom_options = $custom_options + array(-1 => '<'. t('Choose a block') .'>');
ksort($custom_options);
// Default value from block select
if($widget['widget_key']){
$block_default = $widget['widget_key'];
}
else {
$block_default = -1;
}
$form['block'] = array(
'#title' => t('Block'),
'#type' => 'select',
'#options' => $custom_options,
'#default_value' => $block_default,
);
$form['submit']['#type'] = 'submit';
if ($wid) {
$form['submit']['#value'] = t('Save changes');
}
else {
$form['submit']['#value'] = t('Add widget');
}
$form['wid'] = array('#type' => 'value', '#value' => $wid);
$form['#redirect'] = 'admin/dashboard/configure';
return $form;
}
function nice_dash_widget_settings_form_submit(&$form, &$form_state){
$values = $form_state['values'];
$update_keys = array();
$widget = new stdClass();
$widget->widget_key = $values['block'];
$widget->title = $values['title'];
$widget->description = $values['description'];
$widget->custom = 1;
if($values['wid']){
$update_keys = array('wid');
$widget->wid = $values['wid'];
}
drupal_write_record('nice_dash_widgets', $widget, $update_keys);
//Succes
drupal_set_message(t('The widget has been saved'));
}
/**
* Widget delete form
*/
function nice_dash_widget_delete_form($form_state, $wid){
$form = array();
$form['wid'] = array('#value' => $wid, '#type' => 'value');
return confirm_form(
$form,
t('Are your sure you want to delete the widget'),
'admin/dashboard/configure',
t('This action cannot be undone.'),
t('Delete')
);
}
/**
* Submit for confirm delete form for widgets
*/
function nice_dash_widget_delete_form_submit($form, &$form_state) {
nice_dash_remove_widget($form_state['values']['wid']);
drupal_set_message(t('The widget has been removed'));
drupal_goto('admin/dashboard/configure');
}