-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemf.module
313 lines (276 loc) · 10.3 KB
/
emf.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
<?php
/**
* @file
* EMF generation.
*
* EMF view mode and template for nodes
*
*/
require_once dirname(__FILE__) . '/emf.field.inc';
/**
* Implements hook_entity_info_alter().
*/
function emf_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']['emf'] = array(
'label' => t('EMF'),
'custom settings' => FALSE,
);
}
}
}
/**
* Implements hook_menu().
*/
// defines the path where the iso 19139 is defined
function emf_menu() {
$items = array();
$items['node/%node/emf'] = array(
'page callback' => 'emf_output_node',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
'file' => 'emf.pages.inc',
);
$items['admin/config/services/emf'] = array(
'title' => 'EMF',
'description' => 'Configure EMF settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('emf_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'emf.admin.inc',
);
return $items;
}
/**
* Implements hook_entity_view().
*
* When an entity is being rendered with the 'emf' view mode, convert the theme
* and template being used to use our own EMF theme/template.
*
* @see emf_preprocess_emf()
*/
function emf_entity_view($entity, $type, $view_mode, $langcode) {
if ($view_mode == 'emf') {
list(, , $bundle) = entity_extract_ids($type, $entity);
$entity->content += array(
'#entity' => $entity,
'#bundle' => $bundle,
'#theme' => "emf",
);
}
}
/**
* Implements hook_theme().
*/
function emf_theme($existing, $type, $theme, $path) {
$info['emf'] = array(
'render element' => 'elements',
'template' => 'emf',
'path' => $path . '/templates',
);
// Add support for our additional ISO templates.
$info += drupal_find_theme_templates($info, '.tpl.php', drupal_get_path('module', 'emf') . '/templates');
$info['emf_tag'] = array(
'render element' => 'element',
);
$info['emf_tags'] = array(
'render element' => 'element',
);
// A theme wrapper for format_xml_elements().
$info['emf_elements'] = array(
'variables' => array('emf' => array()),
);
return $info;
}
function theme_emf_elements($variables) {
return format_xml_elements($variables['emf']);
}
function theme_emf_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' => 'emf_tag');
$element['#value'] .= drupal_render($element[$key]);
}
}
return theme_html_tag($variables);
}
function theme_emf_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 emf_node_view($node, $view_mode, $langcode) {
if ($view_mode != 'emf') {
return;
}
// HERE -- mind methods in ISO, use EML guidance rather.
if ($node->type == 'emf' || $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' => 'emf_elements',
'#emf' => $methods,
);
}
}
switch ($node->type) {
case 'emf':
// 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_emf_keywordset') {
$node->content['keywordSets'][] = $node->content[$key];
}
}
break;
}
}
/**
* Implements hook_date_format_types().
*/
function emf_date_format_types() {
return array(
'emf_yeardate' => t('EMF yearDate'),
);
}
/**
* Implements hook_date_formats().
*/
function emf_date_formats() {
return array(
array(
'type' => 'emf_yeardate',
'format' => 'Y-m-d',
'locales' => array(),
),
);
}
/**
* Implements hook_preprocess_HOOK() for emf.tpl.php.
*/
function template_preprocess_emf(&$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('emf_data_policies', '')));
$variables['namespaces_array'] = array(
'xmlns:gml' => "http://www.opengis.net/gml/3.2",
'xmlns:xlink' => "http://www.w3.org/1999/xlink",
'xmlns:gsr' => "http://www.isotc211.org/2005/gsr",
'xmlns:gco' => "http://www.isotc211.org/2005/gco",
'xmlns:gts' => "http://www.isotc211.org/2005/gts",
'xmlns:gss' => "http://www.isotc211.org/2005/gss",
'xmlns:gmd' => "http://www.isotc211.org/2005/gmd",
'xmlns:base' => "http://inspire.ec.europa.eu/schemas/base/3.3",
'xmlns:om' => "http://www.opengis.net/om/2.0",
'xmlns:gn' => "http://inspire.ec.europa.eu/schemas/gn/4.0",
'xmlns:net' => "http://inspire.ec.europa.eu/schemas/net/4.0",
'xmlns:sc' => "http://www.interactive-instruments.de/ShapeChange/AppInfo",
'xmlns:ad' => "http://inspire.ec.europa.eu/schemas/ad/4.0",
'xmlns:base2' => "http://inspire.ec.europa.eu/schemas/base2/2.0",
'xmlns:tn' => "http://inspire.ec.europa.eu/schemas/tn/4.0",
'xmlns:au' => "http://inspire.ec.europa.eu/schemas/au/4.0",
'xmlns:cp' => "http://inspire.ec.europa.eu/schemas/cp/4.0",
'xmlns:bu-base' => "http://inspire.ec.europa.eu/schemas/bu-base/4.0",
'xmlns:ef' => "http://inspire.ec.europa.eu/schemas/ef/4.0",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' => "http://inspire.ec.europa.eu/schemas/ef/4.0 http://inspire.ec.europa.eu/schemas/ef/4.0/EnvironmentalMonitoringFacilities.xsd",
);
if ($entity_type == 'node' && $variables['elements']['#bundle'] == 'emf') {
$variables['pubDate'] = format_date($entity->created, 'emf_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'][] = 'emf';
$variables['theme_hook_suggestions'][] = "emf__{$entity_type}__{$bundle}";
}
/**
* Implements hook_process_HOOK() for emf.tpl.php.
*/
function template_process_emf(array &$variables) {
$variables['namespaces'] = $variables['namespaces_array'] ? drupal_attributes($variables['namespaces_array']) : '';
}
function emf_debug($message, array $variables = array()) {
if (variable_get('emf_debugging')) {
drupal_set_message(format_string('EMF DEBUG: ' . $message, $variables));
watchdog('emf', $message, $variables, WATCHDOG_DEBUG);
}
}
function emf_extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}