-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdkan_dash.module
167 lines (155 loc) · 4.71 KB
/
dkan_dash.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
<?php
include_once 'dkan_dash.features.inc';
/*
* Implement hook_theme().
*/
function dkan_dash_theme($existing, $type, $theme, $path){
$theme = array();
$path = drupal_get_path('module', 'dkan_dash') . '/templates';
$theme['node__react_dashboard'] = array(
'render element' => 'content',
'base hook' => 'node',
'template' => 'node--react_dashboard',
'path' => $path,
);
$theme['react_dashboard-embed'] = array(
'variables' => array(),
'template' => 'react_dashboard-embed',
'path' => $path,
);
return $theme;
}
/**
* Implements hook_preprocess_node().
*/
function dkan_dash_preprocess_node(&$variables) {
if($variables['type'] == 'react_dashboard') {
drupal_add_js(array(
'dkanDash' => array(
'currentNid' => $variables['nid'],
'dashboard' => json_decode($variables['field_dash_settings'][0]['value']),
'dataHandlers' => (object)array(),
'stateHandlers' => (object)array(),
'devSettings' => (object)array(),
)
), 'setting');
drupal_add_css(drupal_get_path('module', 'dkan_dash') . '/dist/dkan_dash.min.css');
drupal_add_js(drupal_get_path('module', 'dkan_dash') . '/dist/dkan_dash.min.js', array('scope' => 'footer', 'weight' => 10));
drupal_add_js(drupal_get_path('module', 'dkan_dash') . '/js/embed.js', array('scope' => 'footer', 'weight' => 11));
}
}
/**
* Implements hook_menu().
*/
function dkan_dash_menu() {
$items['dashboard/%node/autocomplete'] = array(
'title' => 'Dashboard Autocomplete',
'page arguments' => array(1),
'page callback' => 'dkan_dash_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
$items['dashboard/%dashboard/iframe'] = array(
'page callback' => 'dkan_dash_iframe',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
return $items;
}
function dashboard_load($name) {
$alias = 'dashboard/' . $name;
$path = drupal_lookup_path('source', $alias);
$node = menu_get_object('node', 1, $path);
if(!empty($node)) {
return $node;
}
return FALSE;
}
/**
* Create and endpoint ready to be consumed by the react autocomplete
* component included inside the react dashboard library.
*
* e.g. [ {label: 'One', value: 'one'}, {label: 'Two', value: 'two'}]
*/
function dkan_dash_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);
}
/**
* Implements hook_form_alter().
*/
function dkan_dash_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'react_dashboard_node_form':
drupal_add_css(drupal_get_path('module', 'dkan_dash') . '/css/dkan_dash.css');
$form['#attached']['libraries_load'][] = array('jsoneditor');
$form['#attached']['js'][] = drupal_get_path('module', 'dkan_dash') . '/js/editor.js';
break;
}
}
/**
* Implements hook_libraries_info().
*/
function dkan_dash_libraries_info() {
$libraries = array();
$libraries['jsoneditor'] = array(
'name' => 'JSONEditor',
'vendor url' => 'https://github.com/josdejong/jsoneditor',
'download url' => 'https://github.com/josdejong/jsoneditor/archive/master.zip',
'path' => '',
'version arguments' => array(
'file' => 'package.json',
'pattern' => '/"version": "(\d+\.\d+\.\d+)"/',
),
'dependencies' => array('d3'),
'files' => array(
'js' => array(
'dist/jsoneditor.min.js',
),
'css' => array(
'dist/jsoneditor.min.css',
),
),
);
return $libraries;
}
/**
* Implements hook_preprocess_page().
*/
function dkan_dash_preprocess_page(&$vars) {
if (in_array_match('/page__dashboard__.+__iframe/', $vars['theme_hook_suggestions'])) {
header_remove('X-Frame-Options');
// Need to also remove from Drupal's cache of headers.
drupal_add_http_header('X-Frame-Options', FALSE);
$vars['theme_hook_suggestions'][] = 'react_dashboard-embed';
}
}
function dkan_dash_iframe($node) {
if (!empty($node)) {
$node_view = node_view($node);
return drupal_render($node_view);
}
else {
return MENU_NOT_FOUND;
}
}