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.api.inc
180 lines (151 loc) · 3.7 KB
/
nice_dash.api.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
<?php
/**
*
* TODO: CLEAN UP THIS API
* - Duplicate functionalities
*
*/
/**
* Dashboard regions
*
* @return: An array of layout regions available for widgets
*/
function nice_dash_regions() {
return array(
'header' => t('Header'),
'left' => t('Left'),
'right' => t('Right'),
'footer' => t('Footer'),
'disabled' => t('Disabled')
);
}
/**
* Fetch widget values
*/
function nice_dash_widget_values($wid, $did = NULL){
// If dashboard ID is supplied only give back dashboard region and weight
if($did){
$sql = 'SELECT region, weight FROM {nice_dash_config} WHERE wid = %d AND did = %d';
}
else {
$sql = 'SELECT * FROM {nice_dash_widgets} WHERE wid = %d';
}
$result = db_query($sql, $wid, $did);
while($row = db_fetch_array($result)){
//if no region set to disabled
if (!$row['region']) {
$row['region'] = 'disabled';
}
return $row;
}
// If empty
return nice_dash_widget_default_values();
}
/**
* Fetch all widgets
*/
function nice_dash_widgets(){
// @TODO add caching !!!!
// Fetch code widgets
$widgets = module_invoke_all('nice_dash_widgets');
// Save them if they don't exist
foreach($widgets as $key => $value){
$wid = nice_dash_widget_wid($key);
if(!$wid){
$widget = new stdClass();
$widget->custom = 0;
$widget->widget_key = $key;
$widget->title = $value['title'];
$widget->description = '';
drupal_write_record('nice_dash_widgets', $widget);
}
}
// Fetch db widgets
$result = db_query("SELECT * FROM {nice_dash_widgets}");
$widgets = array();
while($row = db_fetch_array($result)){
$widgets[] = $row;
}
return $widgets;
}
/**
* Fetch all the custom widgets
*/
function nice_dash_custom_widgets(){
$result = db_query("SELECT * FROM {nice_dash_widgets} WHERE custom = 1");
$widgets = array();
while($row = db_fetch_array($result)){
$widgets[] = $row;
}
return $widgets;
}
/**
* Check if a widget key exists in the db
*/
function nice_dash_widget_wid($key){
return db_result(db_query("SELECT wid FROM {nice_dash_widgets} WHERE widget_key = '%s'", $key));
}
/**
* Default widget values
*/
function nice_dash_widget_default_values(){
return array(
'weight' => 0,
'region' => 'disabled'
);
}
/**
* Delete widget
*/
function nice_dash_remove_widget($wid){
db_query("DELETE FROM {nice_dash_config} WHERE wid = %d", $wid);
db_query("DELETE FROM {nice_dash_widgets} WHERE wid = %d", $wid);
}
/**
* Get the custom widget block
*/
function nice_dash_get_custom_widget($widget){
$bid = $widget['widget_key'];
$split = explode('-', $bid, 2);
if (count($split) === 2) {
list($module, $delta) = $split;
$block = module_invoke($module, 'block', 'view', $delta);
if (!empty($block['content'])) {
if ($module === 'block') {
global $theme;
$block['subject'] = db_result(db_query("SELECT title FROM {blocks} WHERE module = 'block' AND delta = '%s'", $delta));
}
}
}
return theme('nice_dash_component', $widget['title'], $widget['description'], $block['content']);
}
/**
* Fetch dashboards
*/
function nice_dash_dashboard_values($did){
$result = db_query("SELECT * FROM {nice_dash_dashboards} WHERE did = %d", $did);
$rows = array();
while ($row = db_fetch_array($result)){
return $row;
}
}
/**
* Fetch dashboards
*/
function nice_dash_dashboards(){
$result = db_query("SELECT * FROM {nice_dash_dashboards} ORDER BY weight ASC");
$rows = array();
while ($row = db_fetch_array($result)){
$rows[] = $row;
}
return $rows;
}
/**
* Delete dashboard
*/
function nice_dash_remove_dashboard($did){
db_query("DELETE FROM {nice_dash_dashboards} WHERE did = %d", $did);
db_query("DELETE FROM {nice_dash_config} WHERE did = %d", $did);
// Clear them from the menu
module_invoke('menu','rebuild');
}