-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataseed_visualisation.module
More file actions
198 lines (172 loc) · 5.77 KB
/
dataseed_visualisation.module
File metadata and controls
198 lines (172 loc) · 5.77 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
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
<?php
/**
* Implements hook_menu().
*/
function dataseed_visualisation_menu() {
return array(
'visualisation/%/%' => array(
'title' => 'Visualisation',
'page callback' => 'dataseed_visualisation_page',
'page arguments' => array(1, 2),
'access arguments' => array('access dataseed visualisation'),
'type' => MENU_CALLBACK,
),
);
}
/**
* Implements hook_permission().
*/
function dataseed_visualisation_permission() {
return array(
'access dataseed visualisation' => array(
'title' => t('Access Dataseed visualisation'),
'description' => t('View and interact with an embedded Dataseed visualisation.'),
),
);
}
/**
* Implements hook_theme().
*/
function dataseed_visualisation_theme() {
return array(
'dataseed_visualisation_page' => array(
'template' => 'dataseed-visualisation-page',
'variables' => array(
'dataset_id' => NULL,
'visualisation_id' => NULL,
'args' => NULL,
),
),
);
}
/**
* Implements hook_library().
*/
function dataseed_visualisation_library() {
return array(
'visualisation' => array(
'title' => 'Dataseed Visualisation',
'website' => 'https://getdataseed.com',
'version' => '0.2',
'css' => array(
drupal_get_path('module', 'dataseed_visualisation') . '/lib/dist/css/dataseed.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
),
);
}
/**
* Visualisation page callback
*/
function dataseed_visualisation_page($dataset_id, $visualisation_id) {
// Add embed JS and CSS
if (!drupal_add_library('dataseed_visualisation', 'visualisation')) {
watchdog('dataseed_visualisation', 'Unable to load dataseed library', array(), WATCHDOG_ERROR);
return MENU_NOT_FOUND;
}
/*
* @TODO we should add dataseed.js in the HEADER as a library but if we do
* that the visualisation doesn't work on Chrome (BASE_URL is not initialised
* and module.config() of requirejs is undefined)
*
* ref. https://www.assembla.com/spaces/b-l0hszkOr4RsXacwqEsg8/tickets/151#/activity/ticket:
*/
drupal_add_js(
drupal_get_path('module', 'dataseed_visualisation') .
'/lib/dist/js/dataseed.js',
array(
'scope' => 'footer',
'weight' => 49
)
);
// Get API base URL
$options = dataseed_get_settings();
$api_url = $options['scheme'] . '://' . $options['host'] . ':' . $options['port'];
// Get cut
$cut = (empty($_GET['cut'])) ? new StdClass() : $_GET['cut'];
// Get authentication/authorisation parameters
$auth = dataseed_visualisation_auth($dataset_id, $visualisation_id);
// Add embed JS
$js = sprintf('require.config(%s);require(["./views/dataset"],function(V){new V(%s);});',
drupal_json_encode(array(
'config' => array(
'app' => array(
'BASE_URL' => $api_url,
),
'models/authSingleton' => array(
'AUTH' => $auth,
),
),
)),
drupal_json_encode(array(
'el' => '#visualisation-container',
'id' => $dataset_id,
'visualisation_id' => $visualisation_id,
'cut' => $cut,
))
);
drupal_add_js($js, array('type' => 'inline', 'scope' => 'footer', 'weight' => 50));
// Return render array
return array(
'#theme' => 'dataseed_visualisation_page',
'#dataset_id' => 'score',
'#visualisation_id' => 'chart0',
'#args' => array(),
);
}
/**
* Get authentication parameters
*/
function dataseed_visualisation_auth($dataset_id, $visualisation_id) {
// Check user token and key have been set
$user_token = variable_get('dataseed_user_token');
$user_key = variable_get('dataseed_user_key');
if (empty($user_token) || empty($user_key)) {
return new StdClass();
}
// Check hash extension is available
if (!function_exists('hash_algos') || !function_exists('hash_hmac')) {
drupal_set_message(
t(
'Your version of PHP doesn\'t support the !link. You must install this extension before you can access private datasets.',
array('!link' => l('Hash extension', 'http://php.net/manual/en/book.hash.php'))
),
'error'
);
return new StdClass();
}
// Check SHA512 hash function is available
if (!in_array('sha512', hash_algos())) {
drupal_set_message(
t(
'Your version of PHP doesn\'t support the SHA512 hash function. Please ensure that the !link is installed properly.',
array('!link' => l('PHP Hash extension', 'http://php.net/manual/en/book.hash.php'))
),
'error'
);
return new StdClass();
}
// Get mandatory visualisation filters
$filters = new StdClass();
drupal_alter('dataseed_visualisation_filters', $dataset_id, $visualisation_id, $filters);
// Build auth message
$msg = new StdClass();
$msg->user = $user_token;
$msg->dataset = $dataset_id;
$msg->filters = $filters;
// Get message timestamp in UTC
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');
$msg->timestamp = time();
date_default_timezone_set($tz);
// Encode message for transport and hashing
$msg = base64_encode(drupal_json_encode($msg));
// Calculate HMAC and encode for transport
$hmac = base64_encode(hash_hmac('sha512', $msg, $user_key, TRUE));
return array(
'msg' => $msg,
'hmac' => $hmac,
);
}