-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdkan_dataset.theme.inc
376 lines (353 loc) · 11.5 KB
/
dkan_dataset.theme.inc
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
<?php
/**
* @file
* Theme functions for datasets.
*/
/**
* Implements hook_theme().
*/
function dkan_dataset_theme() {
$theme = array(
'dkan_dataset_resource_view' => array(
'variables' => array('node' => NULL),
),
'dkan_dataset_release_date_view' => array(
'variables' => array('node' => NULL),
),
'filefield_source_dkan_remotefile_element' => array(
'render element' => 'element',
),
'dkan_dataset_modified_date_view' => array(
'variables' => array('node' => NULL),
),
'dkan_dataset_identifier_view' => array(
'variables' => array('node' => NULL),
),
'dkan_dataset_resource_teaser_view' => array(
'variables' => array('node' => NULL),
),
'dkan_dataset_teaser_link' => array(
'variables' => array(
'title' => NULL,
'nid' => NULL,
'desc' => NULL,
'format' => NULL,
'type' => NULL,
),
),
'dkan_dataset_teaser_preview_link' => array(
'variables' => array('node' => NULL),
),
'dkan_dataset_preview_settings_external_table_form' => array(
'render element' => 'form',
'file' => 'dkan_dataset.admin.inc',
),
);
return $theme;
}
/**
* Outputs the identifier content.
*/
function theme_dkan_dataset_identifier_view($vars) {
return '<div property="dcterms:identifier" class="field-name-field-identifier">' . $vars['node']->uuid . '</div>';
}
/**
* Outputs the modified date content.
*/
function theme_dkan_dataset_modified_date_view($vars) {
$date = dkan_dataset_modified_date($vars['node']);
return '<div property="dcterms:modified" class="field-name-field-modified-date">' . $date . '</div>';
}
/**
* Outputs the release date content.
*/
function theme_dkan_dataset_release_date_view($vars) {
return '<div property="dcterms:issued" class="field-name-field-release-date">' . date('Y-m-d', $vars['node']->created) . '</div>';
}
/**
* Output the resource into the node content.
*/
function theme_dkan_dataset_resource_view($vars) {
$output = '<h4>' . t('Data and Resources') . '</h4>';
$node = entity_metadata_wrapper('node', $vars['node']);
$nodes = $node->field_resources->value();
$links = array();
$output .= '<div property="dcat:distribution">';
if (isset($nodes)) {
$file_count = 0;
foreach ($nodes as $node) {
// Node could be empty if it has been deleted.
if (!$node || !node_access('view', $node)) {
continue;
}
$node_wrapper = entity_metadata_wrapper('node', $node);
$body = $node_wrapper->body->value();
$term = '';
if (isset($body['safe_value'])) {
$desc = '<p class="description">' . dkan_dataset_text_trim($body['safe_value'], 80) . '</p>';
}
else {
$desc = '<p class="description"></p>';
}
$format = t("data");
if (isset($node->field_format) && $node->field_format && $node_wrapper->field_format->value()) {
$format = $node_wrapper->field_format->value()->name;
}
if (isset($node->field_upload[LANGUAGE_NONE])) {
$type = 'file_upload';
$file_count++;
}
elseif (isset($node->field_link_remote_file[LANGUAGE_NONE])) {
$type = 'link_remote_file';
}
else {
$type = 'link_api';
}
$teaser_link = theme('dkan_dataset_teaser_preview_link', array('node' => $node, 'type' => $type));
$links[] = theme('dkan_dataset_teaser_link', array(
'link' => $teaser_link, 'title' => $node->title, 'nid' => $node->nid, 'desc' => $desc,
'format' => $format, 'type' => $type, 'node' => $node,
));
}
if ($file_count > 1) {
$download_all = '<div property="dcat:Distribution"><span class="links">';
$download_all .= l('Download All <i class="fa fa-download"></i>', 'node/' . $vars['node']->nid . '/dataset/download', array(
'html' => TRUE,
'attributes' => array(
'class' => array('btn', 'btn-primary')
)
));
$download_all .= '</span></div>';
$links[] = $download_all;
}
$output .= theme('item_list', array('items' => $links, 'attributes' => array('class' => array('resource-list'))));
// Close first dcat declaration.
$output .= '</div>';
}
return $output;
}
/**
* Provides direct link url for resource.
*/
function dkan_dataset_resource_direct_link($node) {
if (isset($node->field_upload[LANGUAGE_NONE])) {
$url = file_create_url($node->field_upload[LANGUAGE_NONE][0]['uri']);
}
elseif (isset($node->field_link_remote_file[LANGUAGE_NONE])) {
$url = $node->field_link_remote_file[LANGUAGE_NONE][0]['uri'];
}
elseif (isset($node->field_link_api[LANGUAGE_NONE])) {
$url = $node->field_link_api[LANGUAGE_NONE][0]['url'];
}
else {
$url = dkan_dataset_preview_url_local($node);
}
return $url;
}
/**
* Theme function for DKAN Dataset teaser link.
*/
function theme_dkan_dataset_teaser_link($vars) {
$desc = $vars['desc'];
$output = '<div property="dcat:Distribution">';
$format = '<span data-toggle="tooltip" data-placement="top" data-original-title="' . $vars['format'] . '" class="format-label" property="dc:format" data-format="' . $vars['format'] . '">' . $vars['format'] . '</span>';
$title = l($vars['title'] . $format, 'node/' . $vars['nid'], array(
'html' => TRUE,
'attributes' => array(
'class' => array('heading'),
'title' => $vars['title'],
'property' => 'dcat:accessURL',
))
);
$output .= $title . $desc . '<span class="links">' . $vars['link'] . '</span></div>';
return $output;
}
/**
* Theme function to create "open with" version of teaser links on Datasets.
*/
function theme_dkan_dataset_teaser_preview_link($vars) {
$preview_link = $direct_link = '';
$resource = $vars['node'];
$url = dkan_dataset_resource_direct_link($resource);
if ($vars['type'] == 'file_upload' || $vars['type'] == 'link_remote_file') {
$direct_link = l(
'<i class="fa fa-download"></i> '. t('Download'),
$url,
array(
'html' => TRUE,
'attributes' => array(
'class' => array('btn', 'btn-primary', 'data-link'),
),
)
);
}
else {
$direct_link = l(
'<i class="fa fa-external-link"></i> ' . t('Go to resource'),
$url,
array(
'html' => TRUE,
'attributes' => array(
'class' => array('btn', 'btn-primary', 'data-link'),
),
)
);
}
$node = $vars['node'];
$previews = dkan_dataset_teaser_external_previews_for_resource($node);
if (count($previews)) {
// If multiple previews, use the "open with" format
if (count($previews) > 1) {
$preview_link =
'<div class="btn-group">
<button class="btn btn-primary" data-toggle="dropdown">' . t('Open With') . '</button>
<button class="btn btn-primary" data-toggle="dropdown">
<span class="caret"></span><span class="sr-only">Toggle dropdown</span>
</button>
<ul class="dropdown-menu">
';
foreach ($previews as $provider => $preview) {
$preview_link .= '<li class="' . $provider . ' ">';
$preview_link .= l('<span> ' . $preview['name'] . '</span>', $preview['url'], array('html' => TRUE));
$preview_link .= '</li>';
}
$preview_link .= '</ul></div>';
}
// Otherwise use single button
else {
$machine_name = current(array_keys($previews));
$preview = $previews[$machine_name];
if ($machine_name == 'dkan') {
$button_text = t('Preview');
}
else {
$button_text = $preview['name'];
}
$preview_link = l(
'<i class="fa fa-bar-chart"></i> ' . $button_text,
$preview['url'],
array(
'html' => TRUE,
'attributes' => array(
'class' => array('btn', 'btn-primary'),
),
)
);
}
}
$link = $preview_link . $direct_link;
return $link;
}
/**
* Output the resource into the node content.
*/
function theme_dkan_dataset_resource_teaser_view($vars) {
if ($vars['node']->type == 'dataset') {
$node = entity_metadata_wrapper('node', $vars['node']);
$nodes = $node->field_resources->value();
}
else {
$nodes = array($vars['node']);
}
$links = array();
if (isset($nodes)) {
// Data links for generate grouping resources link.
$links_data = array();
foreach ($nodes as $node) {
$node_wrapper = entity_metadata_wrapper('node', $node);
$name = dkan_dataset_get_resource_format($node);
$term = '';
// Check node is published.
if (isset($node->status) && $node->status) {
if (!isset($links_data[$name])) {
$links_data[$name] = array(
'term_name' => $name,
'count' => 1,
'url' => 'node/' . $vars['node']->nid,
'link' => array(
'attributes' => array(
'class' => array('label'),
'title' => t('Resources') . ': ' . $vars['node']->title,
'data-format' => $name,
),
),
);
}
else {
$links_data[$name]['count'] = $links_data[$name]['count'] + 1;
}
}
}
foreach ($links_data as $term_name => $link) {
if($link['count'] > 1) {
$links[] = '<span class="count-resource">' . $link['count'] . 'x </span>' . l($term_name, $link['url'], $link['link']);
}
else {
$links[] = l($term_name, $link['url'], $link['link']);
}
}
$output = theme('item_list', array('items' => $links, 'attributes' => array('class' => array('resource-list', 'clearfix'))));
}
return $output;
}
/**
* Theme function for previews form table
*/
function theme_dkan_dataset_preview_settings_external_table_form(&$vars) {
$form = $vars['form'];
$rows = $form['rows'];
$header =
$content = array(
'#theme' => 'table',
'#header' => $form['#header'],
'#rows' => array(),
);
// Traverse each row. @see element_chidren().
foreach (element_children($rows) as $row_index) {
$row = array();
// Traverse each column in the row. @see element_children().
foreach (element_children($rows[$row_index]) as $col_index) {
// Render the column form element.
$row[] = array('data' => drupal_render($rows[$row_index][$col_index]));
}
// Add the row to the table.
$content['#rows'][] = $row;
}
// Redner the table and return.
return drupal_render($content);
}
/**
* Theme the output of the autocomplete field.
*/
function theme_filefield_source_dkan_remotefile_element($variables) {
$element = $variables['element'];
$element['url']['#field_suffix'] = drupal_render($element['select']);
return '<div class="filefield-source filefield-source-remotefile clear-block">' . drupal_render($element['url']) . '</div>';
}
/*
* Hides the link to API (URL) iframe when either the field
* upload or the remote field are also present.
*/
function dkan_dataset_preprocess_field(&$variables) {
$field_name = $variables['element']['#field_name'];
if ($field_name == 'field_link_api') {
$node = $variables['element']['#object'];
$resource_fields = array(
$node->field_link_api,
$node->field_link_remote_file,
$node->field_upload
);
$count = array_reduce($resource_fields, function($result, $field){
if(!empty($field)) {
$result += count($field);
}
return $result;
}, 0);
// If there is more than type of resource linked we have to be sure
// link to URL resource is not being displayed.
if($count > 1 && $node->field_link_api) {
unset($variables['element'][0]['iframe']);
unset($variables['items'][0]['iframe']);
}
}
}