forked from klimeto/deims_iso19139_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso19139.module
294 lines (256 loc) · 9.67 KB
/
iso19139.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
<?php
/**
* @file
* ISO 19139 generation.
*
* ISO 19139 view mode and template for nodes
*
*/
require_once dirname(__FILE__) . '/iso19139.field.inc';
/**
* Implements hook_entity_info_alter().
*/
function iso19139_entity_info_alter(array &$info) {
// Add an 'ISO 19139' view mode to all possible entity types.
foreach (array_keys($info) as $entity_type) {
if (!empty($info[$entity_type]['view modes'])) {
$info[$entity_type]['view modes']['iso19139'] = array(
'label' => t('ISO 19139'),
'custom settings' => FALSE,
);
}
}
}
/**
* Implements hook_menu().
*/
// defines the path where the iso 19139 is defined
function iso19139_menu() {
$items = array();
$items['node/%node/iso19139'] = array(
'page callback' => 'iso19139_output_node',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
'file' => 'iso19139.pages.inc',
);
$items['admin/config/services/iso19139'] = array(
'title' => 'ISO 19139',
'description' => 'Configure ISO 19139 settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('iso19139_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'iso19139.admin.inc',
);
return $items;
}
/**
* Implements hook_entity_view().
*
* When an entity is being rendered with the 'iso19139' view mode, convert the theme
* and template being used to use our own ISO theme/template.
*
* @see iso19139_preprocess_iso_1939()
*/
function iso19139_entity_view($entity, $type, $view_mode, $langcode) {
if ($view_mode == 'iso19139') {
list(, , $bundle) = entity_extract_ids($type, $entity);
$entity->content += array(
'#entity' => $entity,
'#bundle' => $bundle,
'#theme' => "iso19139",
);
}
}
/**
* Implements hook_theme().
*/
function iso19139_theme($existing, $type, $theme, $path) {
$info['iso19139'] = array(
'render element' => 'elements',
'template' => 'iso19139',
'path' => $path . '/templates',
);
// Add support for our additional ISO templates.
$info += drupal_find_theme_templates($info, '.tpl.php', drupal_get_path('module', 'iso19139') . '/templates');
$info['iso19139_tag'] = array(
'render element' => 'element',
);
$info['iso19139_tags'] = array(
'render element' => 'element',
);
// A theme wrapper for format_xml_elements().
$info['iso19139_elements'] = array(
'variables' => array('iso19139' => array()),
);
return $info;
}
function theme_iso19139_elements($variables) {
return format_xml_elements($variables['iso19139']);
}
function theme_iso19139_tag($variables) {
$element = &$variables['element'];
if (isset($element['#value'])) {
$element['#value'] = check_plain($element['#value']);
}
elseif ($children = element_children($element)) {
$element['#value'] = '';
foreach ($children as $key) {
$element[$key] += array('#theme' => 'iso19139_tag');
$element['#value'] .= drupal_render($element[$key]);
}
}
return theme_html_tag($variables);
}
function theme_iso19139_tags($variables) {
$output = '';
$element = &$variables['element'];
foreach (element_children($element) as $key) {
$sub_element = array('element' => array());
$sub_element['element']['#tag'] = $element['#tag'];
if (isset($element['#attributes'])) {
$sub_element['element']['#attributes'] = $element['#attributes'];
}
$sub_element['element']['#value'] = drupal_render($element[$key]);
$output .= theme_html_tag($sub_element);
}
return $output;
}
/**
* Implements hook_node_view().
*/
function iso19139_node_view($node, $view_mode, $langcode) {
if ($view_mode != 'iso19139') {
return;
}
// HERE -- mind methods in ISO, use EML guidance rather.
if ($node->type == 'data_set' || $node->type == 'data_source') {
$methods = array();
$methods['gmd:LI_Lineage'] = array();
if ($qa_items = field_get_items('node', $node, 'field_quality_assurance')) {
$qa_instance = field_info_instance('node', 'field_quality_assurance', $node->type);
$methods['gmd:LI_Lineage']['gmd:statement']['gco:CharacterString']= ' Quality Control: '.strip_tags(_text_sanitize($qa_instance, $langcode, $qa_items[0], 'value'));
}
if ($items = field_get_items('node', $node, 'field_methods')) {
$method_instance = field_info_instance('node', 'field_methods', $node->type);
$instrumentation_instance = field_info_instance('node', 'field_instrumentation', $node->type);
$instrumentation_values = field_get_items('node', $node, 'field_instrumentation');
$method = array();
$method['gmd:LI_ProcessStep'] = array();
$method['gmd:LI_ProcessStep']['gmd:description'] = array();
foreach ($items as $delta => $item) {
$method['gmd:LI_ProcessStep']['gmd:description']['gco:CharacterString'] = strip_tags(_text_sanitize($method_instance, $langcode, $item, 'value'));
if (!empty($instrumentation_values[$delta])) {
$method['gmd:LI_ProcessStep']['gmd:description']['gco:CharacterString'] .= ' Instrumentation:'. strip_tags(_text_sanitize($instrumentation_instance, $langcode, $instrumentation_values[$delta], 'value'));
}
$methods['gmd:LI_Lineage'][] = array('key' => 'gmd:processStep', 'value' => $method);
}
}
if (!empty($methods)) {
$node->content['methods'] = array(
'#theme' => 'iso19139_elements',
'#iso19139' => $methods,
);
}
}
switch ($node->type) {
case 'data_set':
// Join all taxonomy fields into one content array to output in the
// template.
$node->content['keywordSets'] = array();
foreach (element_children($node->content) as $key) {
if (isset($node->content[$key]['#field_type'])
&& $node->content[$key]['#field_type'] == 'taxonomy_term_reference'
&& $node->content[$key]['#formatter'] == 'taxonomy_iso19139_keywordset') {
$node->content['keywordSets'][] = $node->content[$key];
}
}
break;
}
}
/**
* Implements hook_date_format_types().
*/
function iso19139_date_format_types() {
return array(
'iso19139_yeardate' => t('ISO 19139 yearDate'),
);
}
/**
* Implements hook_date_formats().
*/
function iso19139_date_formats() {
return array(
array(
'type' => 'iso19139_yeardate',
'format' => 'Y-m-d',
'locales' => array(),
),
);
}
/**
* Implements hook_preprocess_HOOK() for iso19139.tpl.php.
*/
function template_preprocess_iso19139(&$variables, $hook) {
$variables['entity'] = $variables['elements']['#entity'];
$entity = $variables['entity'];
$entity_type = $variables['elements']['#entity_type'];
$bundle = $variables['elements']['#bundle'];
$label = entity_label($entity_type, $entity);
$uri = entity_uri($entity_type, $entity);
$variables['url'] = url($uri['path'], array('absolute' => TRUE) + $uri['options']);
$variables['label'] = check_plain($label);
$variables['language'] = check_plain($GLOBALS[LANGUAGE_TYPE_CONTENT]->name);
$variables['pubPlace'] = check_plain(variable_get('site_name', 'Drupal'));
$variables['station'] = check_plain(variable_get('station_acronym', 'STATION'));
$variables['data_policies'] = check_plain(strip_tags(variable_get('iso19139_data_policies', '')));
$variables['namespaces_array'] = array(
'xmlns:gmd' => "http://www.isotc211.org/2005/gmd",
'xmlns:gco' => "http://www.isotc211.org/2005/gco",
'xmlns:gfc' => "http://www.isotc211.org/2005/gfc",
'xmlns:gmx' => "http://www.isotc211.org/2005/gmx",
'xmlns:gml' => "http://www.opengis.net/gml/3.2",
'xmlns:xlink' => "http://www.w3.org/1999/xlink",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' => "http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20070417/gmd/gmd.xsd",
);
if ($entity_type == 'node' && $variables['elements']['#bundle'] == 'data_set') {
$variables['pubDate'] = format_date($entity->created, 'iso19139_yeardate');
}
if ($entity_type == 'node' && $variables['elements']['#bundle'] == 'data_set') {
$variables['date'] = format_date($entity->field_date, 'iso19139_yeardate');
}
// Helpful $content variable for templates.
$variables += array('content' => array());
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
// Remove the field wrappers and classes so we just get native output.
if (isset($variables['content'][$key]['#theme']) && $variables['content'][$key]['#theme'] == 'field') {
unset($variables['content'][$key]['#theme']);
}
}
// Ensure that all the fields for this entity are available, even if empty.
foreach (field_info_instances($entity_type, $bundle) as $instance) {
if (!isset($variables['content'][$instance['field_name']])) {
$variables['content'][$instance['field_name']] = '';
}
}
// Add template suggestions to use, starting with the least preferred, and
// ending with the one to try first if it exists. The last one should be the
// most specific.
$variables['theme_hook_suggestions'][] = 'iso19139';
$variables['theme_hook_suggestions'][] = "iso19139__{$entity_type}__{$bundle}";
}
/**
* Implements hook_process_HOOK() for iso19139.tpl.php.
*/
function template_process_iso19139(array &$variables) {
$variables['namespaces'] = $variables['namespaces_array'] ? drupal_attributes($variables['namespaces_array']) : '';
}
function iso_debug19139($message, array $variables = array()) {
if (variable_get('iso19139_debugging')) {
drupal_set_message(format_string('ISO 19139 DEBUG: ' . $message, $variables));
watchdog('iso19139', $message, $variables, WATCHDOG_DEBUG);
}
}