forked from NuCivic/react_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact_dashboard.module
More file actions
165 lines (149 loc) · 4.66 KB
/
Copy pathreact_dashboard.module
File metadata and controls
165 lines (149 loc) · 4.66 KB
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
<?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'] = array(
'title' => 'Dashboard',
'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'),
);
$items['dashboard_autocomplete/%node/%field/%value'] = array(
'title' => 'Dashboard Autocomplete',
'page arguments' => array(2),
'page callback' => '_react_dashboard_autocomplete',
'access arguments' => array('access content'),
);
return $items;
}
/**
* Returns the dashboard page
*/
function _react_dashboard_page() {
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, 10);
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
*/
// year=123,month=Dec,adult=true,limit=0,10,county='',agency=''
function _react_dashboard_data($node) {
$omit = array('limit', 'groupBy');
$aggregations = array('sum', 'avg', 'count', 'max', 'min', 'std', 'variance');
$datastore = dkan_datastore_go($node->uuid);
$table = $datastore->tableName;
$params = drupal_get_query_parameters();
$limit = array(0, 10);
$query = db_select($table , 'r')->fields('r');
foreach ($params as $key => $value) {
switch ($key) {
case 'limit':
$limit = $params['limit'];
$limit = explode(',', $limit);
break;
case 'groupBy':
$groupBy = explode(',', $value);
foreach ($groupBy as $column) {
$query->groupBy($column);
}
break;
case in_array($key, $aggregations):
$query->addExpression($key . '(' .$value. ')', $value . '_' . $key);
break;
default:
$query->condition($key, $value, '=');
break;
}
}
$result = $query
->range($limit[0],$limit[1])
->execute()
->fetchAll();
drupal_json_output($result);
}