This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
forked from GetDKAN/dkan_datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdkan_datastore.module
428 lines (401 loc) · 12.1 KB
/
dkan_datastore.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/**
* @file
* Creates DKAN Datastore
*/
// Datastore is created for a given resource.
define('DKAN_DATASTORE_EXISTS', 1);
// File is attached to a resource, but not added to the datastore.
define('DKAN_DATASTORE_FILE_EXISTS', 2);
// File is attached to a resource, but cannot be added to the datastore.
define('DKAN_DATASTORE_WRONG_TYPE', 3);
include_once "dkan_datastore.features.inc";
/**
* Implements hook_menu().
*/
function dkan_datastore_menu() {
$items['node/%node/download'] = array(
'title' => 'Download',
'page callback' => 'dkan_datastore_download',
'page arguments' => array(1),
'access callback' => 'dkan_datastore_download_access',
'access arguments' => array(1),
'file' => 'dkan_datastore.pages.inc',
'weight' => '20',
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/api'] = array(
'title' => 'Data API',
'page callback' => 'dkan_datastore_datastore_api',
'page arguments' => array(1),
'access callback' => 'dkan_datastore_datastore_api_access',
'access arguments' => array(1),
'file' => 'dkan_datastore.pages.inc',
'weight' => '25',
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore'] = array(
'title' => 'Manage Datastore',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_import_tab_form', 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('import', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => '15',
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_import_tab_form', 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('import', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['node/%node/datastore/delete-items'] = array(
'title' => 'Delete items',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_delete_tab_form', NULL, 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('clear', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 11,
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore/unlock'] = array(
'title' => 'Unlock',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_unlock_tab_form', NULL, 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('unlock', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 11,
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore/drop'] = array(
'title' => 'Drop Datastore',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_drop_tab_form', 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('drop', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 12,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function dkan_datastore_menu_alter(&$items) {
// Removing these here, readding in hook_menu().
unset($items['node/%node/delete-items']);
unset($items['node/%node/import']);
unset($items['node/%node/unlock']);
}
/**
* Copy of feeds_access.
*/
function dkan_datastore_feeds_access($action, $node) {
if (!in_array($action, array('import', 'clear', 'unlock', 'drop'))) {
// If $action is not one of the supported actions, we return access denied.
return FALSE;
}
if ($action == 'drop' && user_access('manage datastore')){
return TRUE;
}
if ($action != 'import') {
if ($importer_ids = feeds_get_importer_ids($node->type, $node->nid)) {
foreach ($importer_ids as $importer_id) {
$source_config = feeds_source($importer_id, $node->nid);
$fetcher = get_class($source_config->importer->fetcher);
$source = isset($source_config->config[$fetcher]['source']) ? $source_config->config[$fetcher]['source'] : '';
if ($source) {
return TRUE;
}
}
return FALSE;
}
else {
return FALSE;
}
}
if (is_string($node)) {
$importer_id = $node;
}
elseif ($node->type) {
$importer_id = feeds_get_importer_id($node->type);
}
// Check for permissions if feed id is present, otherwise return FALSE.
if ($importer_id) {
if (user_access('administer feeds') || user_access("{$action} {$importer_id} feeds")) {
return TRUE;
}
}
return FALSE;
}
/**
* Access callback for Data API instructions.
*/
function dkan_datastore_datastore_api_access($resource) {
$node = is_object($resource) ? $resource : node_load($resource);
$upload = '';
$link = '';
if (isset($node->field_upload) && $node->field_upload) {
$upload = isset($node->field_upload[$node->language]) ? $node->field_upload[$node->language] : $node->field_upload['und'];
}
if (isset($node->field_link_remote_file) && $node->field_link_remote_file) {
$link = isset($node->field_link_remote_file[$node->language]) ? $node->field_link_remote_file[$node->language] : $node->field_link_remote_file['und'];
}
if ($node->type == 'resource' && ($upload || $link)) {
return node_access('view', $node);
}
else {
return FALSE;
}
}
/**
* Access callback for download tab.
*/
function dkan_datastore_download_access($node) {
$upload = '';
$link = '';
if (isset($node->field_upload) && $node->field_upload) {
$upload = isset($node->field_upload[$node->language]) ? $node->field_upload[$node->language] : $node->field_upload['und'];
}
if (isset($node->field_link_remote_file) && $node->field_link_remote_file) {
$link = isset($node->field_link_remote_file[$node->language]) ? $node->field_link_remote_file[$node->language] : $node->field_link_remote_file['und'];
}
if ($node->type == 'resource' && ($upload || $link)) {
return node_access('view', $node);
}
else {
return FALSE;
}
}
/**
* Access callback for back link.
*/
function dkan_datastore_back_access($node) {
if ($node->type != 'resource') {
return FALSE;
}
else {
return node_access('view', $node);
}
}
/**
* Access callback for 'Add Resource' tab.
*/
function dkan_add_resource($node) {
if ($node->type != 'dataset') {
return FALSE;
}
else {
return _node_add_access();
}
}
/**
* Implements hook_node_update().
*/
function dkan_datastore_node_insert($node) {
if ($node->type == 'resource') {
if (!isset($node->feeds)) {
// Feeds only saves if there is a form present. We want this to work
// programmatically as well.
feeds_node_prepare($node);
feeds_node_update($node);
}
}
}
/**
* Implements hook_node_update().
*/
function dkan_datastore_node_update($node) {
if ($node->type == 'resource') {
// See if we have a file link or upload.
$wrapper = entity_metadata_wrapper('node', $node);
$file = $wrapper->field_upload->value();
if (!$file && $wrapper->__isset("field_link_remote_file")) {
$link = $wrapper->field_link_remote_file->value();
if (isset($link) && isset($link['fid'])) {
$file = file_load($link['fid']);
}
elseif (isset($link) && isset($link->fid)) {
$file = $link;
}
else {
$file = '';
}
}
if ($file) {
if (!isset($node->feeds)) {
// Feeds only saves if there is a form present. We want this to work
// programmatically as well.
feeds_node_prepare($node);
}
// Remove node from feeds list if not csv.
elseif ($file->filemime != 'text/csv') {
feeds_node_delete($node);
}
}
}
}
/**
* Implements hook_permission().
*/
function dkan_datastore_permission() {
return array(
'manage datastore' => array(
'title' => t('Manage Datastore'),
),
);
}
/**
* Implements hook_node_view().
*/
function dkan_datastore_node_view($node, $view_mode, $langcode) {
if ($node->type == 'resource') {
$status = dkan_datastore_status($node);
if (user_access('manage datastore') && $status = dkan_datastore_status($node)) {
if ($status === DKAN_DATASTORE_FILE_EXISTS) {
drupal_set_message(t('Your file for this resource is not added to the datastore. Click "Manage Datastore" to import file into the datastore.'));
}
elseif ($status === DKAN_DATASTORE_EXISTS) {
drupal_set_message(t('Your file for this resource has been added to the datastore.'));
}
}
}
}
/**
* Determines status of datastore attached to resource node.
*/
function dkan_datastore_status($node) {
if ($file = dkan_datastore_file_field($node)) {
if ((is_object($file) && $file->filemime == 'text/csv') || (is_array($file) && $file['filemime'] == 'text/csv')) {
if (dkan_datastore_records($node->nid)) {
return DKAN_DATASTORE_EXISTS;
}
else {
return DKAN_DATASTORE_FILE_EXISTS;
}
}
else {
return DKAN_DATASTORE_WRONG_TYPE;
}
}
return FALSE;
}
/**
* Determines if records exist in a datastore.
*
* @param string $nid
* Node id for resource node.
*/
function dkan_datastore_records($nid) {
if (function_exists('dkan_datastore_api_get_feeds_source')) {
$source_id = dkan_datastore_api_get_feeds_source($nid);
if ($table = feeds_flatstore_processor_table_name($source_id, $nid)) {
if (db_table_exists($table)) {
$query = db_select($table, 't')
->fields('t', array('timestamp'))
->range(0, 1);
if ($result = $query->execute()) {
return TRUE;
}
}
}
}
return FALSE;
}
/**
* Retrieves loaded file from resource node.
*/
function dkan_datastore_file_field($node) {
if ($node->type == 'resource') {
$wrapper = entity_metadata_wrapper('node', $node);
$file = $wrapper->__isset("field_upload") ? $wrapper->field_upload->value() : NULL;
$link = $wrapper->__isset("field_link_remote_file") ? $wrapper->field_link_remote_file->value() : NULL;
if (isset($file)) {
return $file;
}
elseif ($link) {
return $link;
}
}
return FALSE;
}
/**
* Implements hook_field_formatter_info().
*/
function dkan_datastore_field_formatter_info() {
$formatters = array(
'dkan_datastore_status_formatter' => array(
'label' => t('Datastore Status'),
'field types' => array('recline_field'),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_view().
*/
function dkan_datastore_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$item['entity'] = $entity;
$element[$delta] = array(
'#theme' => 'dkan_datastore_status_formatter',
'#item' => $item,
);
}
return $element;
}
/**
* Implements hook_theme().
*/
function dkan_datastore_theme() {
return array(
'dkan_datastore_status_formatter' => array(
'variables' => array('item' => NULL),
),
);
}
/**
* Returns HTML for an recline field formatter.
*
* @param array $variables
* An associative array containing:
* - item: Associative array of recline field
*
* @ingroup themeable
*/
function theme_dkan_datastore_status_formatter($variables) {
$status = dkan_datastore_status($variables['item']['entity']);
if ($status === DKAN_DATASTORE_FILE_EXISTS) {
return '<span class="circle false" title="' . t('A file has been uploaded but not added to the datastore') . '">' . t('Data ready to be added') . '</span>';
}
elseif ($status === DKAN_DATASTORE_EXISTS) {
return '<span class="circle true">' . t('Datastore enabled') . '</span>';
}
return '<span class="circle na">' . t('Correct file type not attached to resource') . '</span>';
}
/**
* Gets a datastore instance.
*
* @param $id
* The unique id of the importer object.
*
* @return
* A Datastore object or an object of a class defined by the Drupal
* variable 'dkan_datastore_class'. There is only one importer object
* per $id system-wide.
*/
function dkan_datastore_go($id, $class = NULL) {
if (!$class) {
$class = variable_get('dkan_datastore_class', 'DkanDatastore');
}
return Datastore::instance($class, $id);
}