This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
react_dashboard.module
213 lines (192 loc) · 6.15 KB
/
react_dashboard.module
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
<?php
/*
* Implement hook_theme().
*/
function react_dashboard_theme(){
return array(
'react_dashboard' => array(
'template' => 'react-dashboard',
'path' => drupal_get_path('module', 'react_dashboard') . '/templates',
),
);
}
/**
* Implements hook_menu().
*/
function react_dashboard_menu() {
$items['dashboard/%node'] = array(
'title' => '',
'page arguments' => array(1),
'page callback' => '_react_dashboard_page',
'access arguments' => array('access content'),
);
$items['dashboard_data/%node'] = array(
'title' => 'Dashboard Data',
'page arguments' => array(1),
'page callback' => '_react_dashboard_data',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
$items['dashboard_autocomplete/%node'] = array(
'title' => 'Dashboard Autocomplete',
'page arguments' => array(1),
'page callback' => '_react_dashboard_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Returns the dashboard page
*/
function _react_dashboard_page($node) {
drupal_add_js(array('react_dashboard' => array('currentNid' => $node->nid)), 'setting');
drupal_add_css( drupal_get_path('module','react_dashboard') . '/app/dist/bundle.min.css' );
drupal_add_js( drupal_get_path('module','react_dashboard') . '/app/dist/bundle.min.js' );
return theme('react_dashboard');
}
/**
* Just a placeholder function showing how to create and enpoind
* ready to be consumed by the react autocomplete component
* included inside the react dashboard library.
* In short you need to retrieve an array with the selection
* where each selection is an object with the label and value
* property inside.
*
* e.g. [ {label: 'One', value: 'one'}, {label: 'Two', value: 'two'}]
*/
function _react_dashboard_autocomplete($node, $field, $value) {
$options = array();
if(empty($node) || empty($field)) return drupal_json_output($options);
$datastore = dkan_datastore_go($node->uuid);
$table = $datastore->tableName;
$limit = array(0, 100);
try {
$query = db_select($table , 'r')
->fields('r')
->groupBy($field);
if(!empty($value)) {
$query->condition($field, '%'. $value . '%', 'LIKE');
}
$result = $query
->range($limit[0], $limit[1])
->execute();
while ($row = $result->fetchAssoc()) {
$options[] = array('label' => $row[$field], 'value' => $row[$field]);
}
} catch(Exception $e) {
return drupal_json_output(array('error' => $e->getMessage()));
}
return drupal_json_output($options);
}
/**
* Convert an array of nodes to an array with items
* for the autocomplete
* @param array $items An array with nodes
* @param array $mappings An array with the mappings. e.g. array('label'=>'title', 'value' => 'uid')
* @return array The converted array
*/
function _to_autocomplete($items, $mappings) {
return array_map(
_map_autocomplete_fields($mappings),
array_values($items));
}
/**
* Returns a function to map fields to autocomplete fields.
* @param array $mappings An array with the mappings. e.g. array('label'=>'title', 'value' => 'uid')
* @return function
*/
function _map_autocomplete_fields($mappings){
return function($item) use ($mappings) {
return array(
'label' => $item->{$mappings['label']},
'value' => $item->{$mappings['value']}
);
};
}
/**
* This is a placeholder function to retrieve the data for
* your dashboard.
* Sometimes you need to retrieve an array with the
* needed data for the whole dashboard. In that cases you just
* need to create a query retrieving all the required data.
*
* In some cases you need to query server data by passing
* parameters. In that case you need to modify the menu item
* to get parameters and add those to this function. Then
* you can retrieve parametrized data by creating the appropiate
* query statement.
* @return array An array with the data for your dashboard
*/
function _react_dashboard_data($node) {
if($_SERVER['REQUEST_METHOD'] === 'GET') {
$params = drupal_get_query_parameters();
$result = _react_dashboard_query($node, $params);
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result = array();
$request_body = json_decode(file_get_contents('php://input'));
$queries = $request_body;
foreach ((array)$queries as $name => $query) {
$result[$name] = _react_dashboard_query($node, (array)$query);
}
}
return drupal_json_output($result);
}
function _react_dashboard_query($node, $params) {
$omit = array('limit', 'groupBy');
$aggregations = array('sum', 'avg', 'count', 'max', 'min', 'std', 'variance');
$datastore = dkan_datastore_go($node->uuid);
$table = $datastore->tableName;
$limit = array(0, 10);
$fields = array();
$query = db_select($table);
foreach ($params as $key => $value) {
switch ($key) {
case 'fields':
$fields = (array)_param($params['fields']);
break;
case 'limit':
$limit = _param($params['limit']);
break;
case 'orderBy':
$orderBy = (array)_param($value);
foreach ($orderBy as $st) {
$st = (is_string($st) && strpos($st, ':') !== FALSE) ? explode(':', $st) : $st;
$query->orderBy($st[0], $st[1]);
}
break;
case 'groupBy':
$groupBy = (array)_param($value);
foreach ($groupBy as $column) {
$query->groupBy($column);
}
break;
case in_array($key, $aggregations):
$query->addExpression($key . '(' .$value. ')', $value . '_' . $key);
break;
default:
$values = _param($value);
if(is_array($values)) {
$db_or = db_or();
foreach ($values as $val) {
$db_or->condition($key, $val, '=');
}
$query->condition($db_or);
} else {
$query->condition($key, $values, '=');
}
break;
}
}
$query->fields($table);
$query->range($limit[0],$limit[1]);
return $query
->execute()
->fetchAll();
}
function _param($value) {
if ($_SERVER['REQUEST_METHOD'] == 'GET' && strpos($value, ',') !== FALSE) {
return explode(',', $value);
}
return $value;
}