Mercurial > hg > rr-repo
comparison sites/all/modules/rdfx/rdfui/rdfui.module @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:b28be78d8160 | 4:ce11bbd8f642 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * User interface for RDF. | |
6 */ | |
7 | |
8 include_once(drupal_get_path('module', 'rdfx') . '/rdfx.inc'); | |
9 | |
10 /** | |
11 * Implements hook_help(). | |
12 */ | |
13 function rdfui_help($path, $arg) { | |
14 switch ($path) { | |
15 case 'admin/structure/types/manage/%/rdf': | |
16 return '<p>' . t('Manage the way this bundle and its fields are represented in RDF. The mappings defined here will be used to publish RDFa in the site\'s HTML pages.', array()) . '</p>'; | |
17 case 'admin/help#rdf': | |
18 $output = ''; | |
19 $output .= '<h3>' . t('About') . '</h3>'; | |
20 $output .= '<p>' . t('The RDF UI module provides a user interface for altering a bundle\'s RDF mapping.') . '</p>'; | |
21 return $output; | |
22 } | |
23 } | |
24 | |
25 /** | |
26 * Implements hook_theme(). | |
27 */ | |
28 function rdfui_theme($path, $arg) { | |
29 return array( | |
30 'rdfui_term_autocomplete' => array( | |
31 'render element' => 'term', | |
32 ), | |
33 ); | |
34 } | |
35 | |
36 /** | |
37 * Implements hook_permission(). | |
38 */ | |
39 function rdfui_permission() { | |
40 return array( | |
41 'administer RDF field mappings' => array( | |
42 'title' => t('Change the RDF mappings for types and fields'), | |
43 ), | |
44 ); | |
45 } | |
46 | |
47 /** | |
48 * Implements hook_menu(). | |
49 */ | |
50 function rdfui_menu() { | |
51 $items = array(); | |
52 | |
53 // Create tabs for all possible bundles. | |
54 foreach (entity_get_info() as $entity_type => $info) { | |
55 if ($info['fieldable']) { | |
56 foreach ($info['bundles'] as $bundle_name => $bundle_info) { | |
57 if (isset($bundle_info['admin'])) { | |
58 // Extract informations from the bundle description. | |
59 $path = $bundle_info['admin']['path']; | |
60 $bundle_arg = isset($bundle_info['admin']['bundle argument']) ? $bundle_info['admin']['bundle argument'] : $bundle_name; | |
61 $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments'))); | |
62 $instance_position = count(explode('/', $path)) + 1; | |
63 | |
64 if ($entity_type == 'node') { | |
65 $title = t('RDF Mappings'); | |
66 $weight = 2; | |
67 } | |
68 elseif ($entity_type == 'comment') { | |
69 continue; | |
70 $title = t('Comment RDF Mappings'); | |
71 $weight = 60; | |
72 } | |
73 else { | |
74 $title = t('RDF Mappings'); | |
75 $weight = 50; | |
76 } | |
77 | |
78 $items["$path/rdf"] = array( | |
79 'title' => $title, | |
80 // @todo Load arguments are not necessary / are cruft, right? | |
81 //'load arguments' => array($obj_type, $bundle_arg), | |
82 'page callback' => 'drupal_get_form', | |
83 'page arguments' => array('rdfui_admin_rdf_overview_form', $entity_type, $bundle_arg, $instance_position), | |
84 'type' => MENU_LOCAL_TASK, | |
85 'weight' => $weight, | |
86 ) + $access; | |
87 } | |
88 } | |
89 } | |
90 } | |
91 | |
92 $items['rdfui/classes/autocomplete'] = array( | |
93 'title' => t('RDF classes autocomplete'), | |
94 'page callback' => 'rdfui_classes_autocomplete', | |
95 'access arguments' => array('administer RDF field mappings'), | |
96 'type' => MENU_CALLBACK, | |
97 ); | |
98 | |
99 $items['rdfui/datatype/autocomplete'] = array( | |
100 'title' => t('RDF datatype autocomplete'), | |
101 'page callback' => 'rdfui_datatype_autocomplete', | |
102 'access arguments' => array('administer RDF field mappings'), | |
103 'type' => MENU_CALLBACK, | |
104 ); | |
105 | |
106 $items['rdfui/predicates/autocomplete'] = array( | |
107 'title' => t('RDF predicates autocomplete'), | |
108 'page callback' => 'rdfui_predicates_autocomplete', | |
109 'access arguments' => array('administer RDF field mappings'), | |
110 'type' => MENU_CALLBACK, | |
111 ); | |
112 | |
113 return $items; | |
114 } | |
115 | |
116 /** | |
117 * Menu callback; listing of field RDF mappings for a content type. | |
118 * | |
119 * Allows the content type to be mapped to a RDF class and | |
120 * fields to be mapped to RDF properties. | |
121 */ | |
122 function rdfui_admin_rdf_overview_form($form, &$form_state, $entity_type, $bundle, $instance) { | |
123 $bundle = field_extract_bundle($entity_type, $bundle); | |
124 $fields = field_info_instances($entity_type, $bundle); | |
125 $mapping = rdf_mapping_load($entity_type, $bundle); | |
126 // Don't make assumptions about entities and only get the type if dealing with a node entity | |
127 $type = ($entity_type == 'node') ? node_type_get_type($bundle) : ''; | |
128 $form['rdf_mappings'] = array( | |
129 '#type' => 'vertical_tabs', | |
130 '#attached' => array( | |
131 'js' => array(drupal_get_path('module', 'rdfui') . '/rdfui.js'), | |
132 ), | |
133 ); | |
134 | |
135 // Declare the fieldset and field for RDF type. | |
136 $form['type'] = array( | |
137 '#type' => 'fieldset', | |
138 '#title' => t('Type'), | |
139 '#group' => 'rdf_mappings', | |
140 '#attributes' => array('class' => array('rdf-type')), | |
141 ); | |
142 $form['type']['rdf_rdftype'] = array( | |
143 '#type' => 'textarea', | |
144 '#title' => t('RDF Type'), | |
145 '#attached' => array( | |
146 'js' => array(drupal_get_path('module', 'rdfui') . '/rdfui.js'), | |
147 ), | |
148 ); | |
149 | |
150 // Declare the fieldset and field for RDF type. | |
151 $form['type'] = array( | |
152 '#type' => 'fieldset', | |
153 '#title' => t('Type'), | |
154 '#group' => 'rdf_mappings', | |
155 '#attributes' => array('class' => array('rdf-field')), | |
156 ); | |
157 $form['type']['rdf_rdftype'] = array( | |
158 '#type' => 'textarea', | |
159 '#title' => t('RDF Type'), | |
160 '#attributes' => array('class' => array('rdf-field')), | |
161 ); | |
162 | |
163 // If this entity is a node and has a title display a fieldset for mapping. | |
164 if (isset($type->has_title)) { | |
165 $form['rdf_title'] = array( | |
166 '#type' => 'fieldset', | |
167 '#group' => 'rdf_mappings', | |
168 '#title' => $type->title_label, | |
169 '#attributes' => array('class' => array('rdf-field')), | |
170 ); | |
171 // Add the options for this entity's title. | |
172 rdfui_predicate_fieldset($form['rdf_title'], $mapping, 'title', 'title'); | |
173 } | |
174 | |
175 // Add the field for RDF type. | |
176 rdfui_rdftype_fieldset($form['type']['rdf_rdftype'], $mapping); | |
177 // Add the options for all other fields. | |
178 foreach ($fields as $field) { | |
179 $label = $field['label']; | |
180 $field_name = $field['field_name']; | |
181 $form[$field_name] = array( | |
182 '#type' => 'fieldset', | |
183 '#group' => 'rdf_mappings', | |
184 '#title' => t('@label', array('@label' => $label)), | |
185 '#attributes' => array('class' => array('rdf-field')), | |
186 ); | |
187 rdfui_predicate_fieldset($form[$field_name], $mapping, $field_name, $label); | |
188 } | |
189 | |
190 $form['field_names'] = array('#type' => 'value', '#value' => array_keys($fields)); | |
191 $form['submit'] = array('#type' => 'submit', '#value' => t('Save mappings')); | |
192 | |
193 // @todo where is RDFUI_CURIE_REGEX declared? | |
194 // drupal_add_js(array('rdfui' => array('termRegex' => RDFUI_CURIE_REGEX)), 'setting'); | |
195 return $form; | |
196 } | |
197 | |
198 /** | |
199 * Implements hook_form_FORM_ID_alter(). | |
200 */ | |
201 function rdfui_form_field_ui_field_edit_form_alter(&$form, &$form_state) { | |
202 | |
203 $field_name = $form['#field']['field_name']; | |
204 $instance = $form['instance']; | |
205 $label = isset($instance['label']) ? $instance['label']['#default_value'] : $instance['field_name']; | |
206 $entity_type = $instance['entity_type']['#value']; | |
207 $mapping = rdf_mapping_load($entity_type, $instance['bundle']['#value']); | |
208 | |
209 $form['rdf'] = array( | |
210 '#type' => 'fieldset', | |
211 '#title' => t('@label RDF Mapping', array('@label' => $label)), | |
212 ); | |
213 | |
214 // add the predicate, datatype, etc fields | |
215 rdfui_predicate_fieldset($form['rdf'], $mapping, $field_name, $label); | |
216 | |
217 $form['submit']['#weight'] = 1; | |
218 | |
219 // add submit and validate handlers | |
220 $form['#validate'] = array_merge($form['#validate'], array('rdfui_form_field_ui_field_edit_form_validate')); | |
221 $form['#submit'] = array_merge($form['#submit'], array('rdfui_form_field_ui_field_edit_form_submit')); | |
222 | |
223 // drupal_add_js(array('rdfui'=>array('termRegex'=>RDFUI_CURIE_REGEX)), 'setting'); | |
224 } | |
225 | |
226 /** | |
227 * Implements hook_form_FORM_ID_alter(). | |
228 */ | |
229 function rdfui_form_node_type_form_alter(&$form, &$form_state) { | |
230 $mapping = array(); | |
231 // If we are editing an existing content type, the mapping will be available | |
232 // via the bundles in entity info. | |
233 if ($bundle = $form['type']['#default_value']) { | |
234 $entity = entity_get_info('node'); | |
235 $mapping = $entity['bundles'][$bundle]['rdf_mapping']; | |
236 } | |
237 | |
238 $form['rdf_settings'] = array( | |
239 '#type' => 'fieldset', | |
240 '#title' => 'RDF Settings', | |
241 '#collapsible' => TRUE, | |
242 '#collapsed' => TRUE, | |
243 '#group' => 'additional_settings' | |
244 ); | |
245 $form['rdf_settings']['rdf_rdftype'] = array( | |
246 '#type' => 'fieldset', | |
247 '#title' => 'RDF Type', | |
248 ); | |
249 $form['rdf_settings']['rdf_title'] = array( | |
250 '#type' => 'fieldset', | |
251 '#title' => 'RDF Title Predicate', | |
252 ); | |
253 | |
254 // Add the RDF type field for the bundle. | |
255 rdfui_rdftype_fieldset($form['rdf_settings']['rdf_rdftype'], $mapping); | |
256 //Add the predicate, datatype, property fields for the bundle title. | |
257 rdfui_predicate_fieldset($form['rdf_settings']['rdf_title'], $mapping, 'title', 'title'); | |
258 | |
259 // add submit and validate handlers | |
260 $form['#validate'] = array_merge($form['#validate'], array('rdfui_form_node_type_form_validate')); | |
261 $form['#submit'] = array_merge($form['#submit'], array('rdfui_form_node_type_form_submit')); | |
262 | |
263 // drupal_add_js(array('rdfui'=>array('termRegex'=>RDFUI_CURIE_REGEX)), 'setting'); | |
264 } | |
265 | |
266 | |
267 function rdfui_form_field_ui_field_edit_form_validate($form, &$form_state) { | |
268 $field_name = $form['#field']['field_name']; | |
269 | |
270 _rdfui_validate_terms($form_state, $field_name); | |
271 _rdfui_validate_datatype($form_state, $field_name); | |
272 } | |
273 | |
274 function rdfui_admin_rdf_overview_form_validate($form, &$form_state) { | |
275 // Validate bundle's RDF type(s). | |
276 _rdfui_validate_terms($form_state); | |
277 | |
278 // Validate title predicate(s). | |
279 if ((isset($form_state['input']['rdf_title_type']))) { | |
280 $field_name = 'title'; | |
281 _rdfui_validate_terms($form_state, $field_name); | |
282 _rdfui_validate_datatype($form_state, $field_name); | |
283 } | |
284 | |
285 // Validate predicates for all fields. | |
286 foreach ($form_state['values']['field_names'] as $field_name) { | |
287 _rdfui_validate_terms($form_state, $field_name); | |
288 _rdfui_validate_datatype($form_state, $field_name); | |
289 } | |
290 } | |
291 | |
292 function rdfui_form_node_type_form_validate($form, &$form_state) { | |
293 // Validate bundle's RDF type(s). | |
294 _rdfui_validate_terms($form_state); | |
295 | |
296 // Validate title predicate(s). | |
297 if ((isset($form_state['input']['rdf_title_type']))) { | |
298 $field_name = 'title'; | |
299 _rdfui_validate_terms($form_state, $field_name); | |
300 _rdfui_validate_datatype($form_state, $field_name); | |
301 } | |
302 } | |
303 | |
304 /** | |
305 * Saves RDF mapping for all fields and node type. | |
306 */ | |
307 function rdfui_admin_rdf_overview_form_submit($form, &$form_state) { | |
308 $entity_type = $form_state['build_info']['args'][0]; | |
309 // @todo somehow $form_state['build_info']['args'][1] is an object for the | |
310 // node RDF mapping but a string 'user' for the user RDF mapping. | |
311 if (isset($form_state['build_info']['args'][1]->type)) { | |
312 $bundle = $form_state['build_info']['args'][1]->type; | |
313 } | |
314 elseif ($form_state['build_info']['args'][0] == 'taxonomy_term') { | |
315 $bundle = $form_state['build_info']['args'][1]->machine_name; | |
316 } | |
317 else { | |
318 $bundle = $form_state['build_info']['args'][1]; | |
319 } | |
320 _rdfui_mapping_save($form_state, $entity_type, $bundle, 'title'); | |
321 | |
322 foreach ($form_state['values']['field_names'] as $field_name) { | |
323 _rdfui_mapping_save($form_state, $entity_type, $bundle, $field_name); | |
324 } | |
325 drupal_set_message(t('RDF mappings have been saved.'), 'status'); | |
326 } | |
327 | |
328 /** | |
329 * Saves RDF mapping for individual field. | |
330 */ | |
331 function rdfui_form_field_ui_field_edit_form_submit($form, &$form_state) { | |
332 $entity_type = $form['instance']['entity_type']['#value']; | |
333 $bundle = $form['instance']['bundle']['#value']; | |
334 $field_name = $form['#field']['field_name']; | |
335 | |
336 _rdfui_mapping_save($form_state, $entity_type, $bundle, $field_name); | |
337 } | |
338 | |
339 /** | |
340 * Saves RDF mapping for Title field. | |
341 */ | |
342 function rdfui_form_node_type_form_submit($form, &$form_state) { | |
343 $entity_type = 'node'; | |
344 $bundle = $form_state['input']['type']; | |
345 // We only need to call _rdfui_mapping_save once because it checks whether | |
346 // rdf_type is set in the form before saving the field value. | |
347 _rdfui_mapping_save($form_state, $entity_type, $bundle, 'title'); | |
348 } | |
349 | |
350 /** | |
351 * Menu callback for classes autocomplete | |
352 */ | |
353 function rdfui_classes_autocomplete($string) { | |
354 // The user enters a comma-separated list of classes. We only autocomplete | |
355 // the last class. | |
356 $classes_typed = drupal_explode_tags($string); | |
357 $class_last = drupal_strtolower(array_pop($classes_typed)); | |
358 | |
359 $matches = array(); | |
360 if ($class_last != '') { | |
361 $classes = array(); | |
362 $classes_entered = count($classes_typed) ? implode(', ', $classes_typed) . ', ' : ''; | |
363 $class_tids = rdfx_get_classes(); | |
364 foreach ($class_tids as $class_tid) { | |
365 $class = rdfx_curie($class_tid) . ', '; | |
366 if (preg_match("/$class_last/", $class)) { | |
367 $details = _rdfx_get_term_details($class_tid); | |
368 $details->curie = $class; | |
369 $matches[$classes_entered . $class] = theme('rdfui_term_autocomplete', array('term' => $details,)); | |
370 } | |
371 } | |
372 } | |
373 drupal_json_output($matches); | |
374 } | |
375 | |
376 /** | |
377 * Menu callback for datatype autocomplete | |
378 */ | |
379 function rdfui_datatype_autocomplete($string) { | |
380 | |
381 //@todo Make sure datatype is only active if property is the relationship. | |
382 | |
383 $datatypes = 'xsd:string, xsd:boolean, xsd:decimal, xsd:float, xsd:double, xsd:dateTime, xsd:time, xsd:date, xsd:gYearMonth, xsd:gYear, xsd:gMonthDay, xsd:gDay, xsd:gMonth, xsd:hexBinary, xsd:base64Binary, xsd:anyURI, xsd:normalizedString, xsd:token, xsd:language, xsd:NMTOKEN, xsd:Name, xsd:NCName, xsd:integer, xsd:nonPositiveInteger, xsd:negativeInteger, xsd:long, xsd:int, xsd:short, xsd:byte, xsd:nonNegativeInteger, xsd:unsignedLong, xsd:unsignedInt, xsd:unsignedShort, xsd:unsignedByte, xsd:positiveInteger'; | |
384 | |
385 $datatypes = explode(', ', $datatypes); | |
386 $matches = array(); | |
387 | |
388 foreach($datatypes as $datatype) { | |
389 if (preg_match("/^$string/", $datatype)) | |
390 $matches[$datatype] = $datatype; | |
391 } | |
392 | |
393 drupal_json_output($matches); | |
394 } | |
395 | |
396 /** | |
397 * Menu callback for predicates autocomplete | |
398 */ | |
399 function rdfui_predicates_autocomplete($string) { | |
400 // The user enters a comma-separated list of predicates. We only autocomplete | |
401 // the last predicate. | |
402 $predicates_typed = drupal_explode_tags($string); | |
403 $predicate_last = drupal_strtolower(array_pop($predicates_typed)); | |
404 | |
405 $matches = array(); | |
406 if ($predicate_last != '') { | |
407 $predicates = array(); | |
408 $predicates_entered = count($predicates_typed) ? implode(', ', $predicates_typed) . ', ' : ''; | |
409 $predicate_tids = rdfx_get_properties(); | |
410 foreach ($predicate_tids as $predicate_tid) { | |
411 $predicate = rdfx_curie($predicate_tid) . ', '; | |
412 if (preg_match("/$predicate_last/", $predicate)) { | |
413 $details = _rdfx_get_term_details($predicate_tid); | |
414 $details->curie = $predicate; | |
415 $matches[$predicates_entered . $predicate] = theme('rdfui_term_autocomplete', array('term' => $details,)); | |
416 } | |
417 } | |
418 } | |
419 drupal_json_output($matches); | |
420 } | |
421 | |
422 | |
423 /** | |
424 * appends the rdf form fields to a fieldset dedicated to a fields.module field | |
425 */ | |
426 function rdfui_predicate_fieldset(&$fieldset, $mapping, $field_name, $label) { | |
427 $fieldset['rdf_'. $field_name .'_predicates'] = array( | |
428 '#type' => 'textfield', | |
429 '#autocomplete_path' => 'rdfui/predicates/autocomplete', | |
430 '#title' => t('RDF Predicates'), | |
431 '#default_value' => empty($mapping[$field_name]['predicates']) ? '' : implode(", ",$mapping[$field_name]['predicates']), | |
432 '#description' => t('Enter a comma-separated list of predicates for %label using CURIE syntax. For example: %predicates', | |
433 array( | |
434 '%label' => $label, | |
435 '%predicates' => 'foaf:familyName, foaf:lastName' | |
436 ) | |
437 ), | |
438 '#resizable' => FALSE, | |
439 ); | |
440 | |
441 $fieldset['rdf_'. $field_name .'_type'] = array( | |
442 '#type' => 'select', | |
443 '#title' => t('Attribute Type'), | |
444 '#default_value' => empty($mapping[$field_name]['type']) ? 'property' : $mapping[$field_name]['type'], | |
445 '#description' => t('For fields containing literals—things such as plain text, html, numbers—use the !property attribute. For fields containing references to other things—urls and node references, for example—use the !rel or !rev attribute.', | |
446 array( | |
447 '%label' => $label, | |
448 '!property' => l('property', 'http://www.w3.org/2006/07/SWD/RDFa/syntax/#id103203'), | |
449 '!rel' => l('rel','http://www.w3.org/2006/07/SWD/RDFa/syntax/#id103235'), | |
450 '!rev' => l('rev','http://www.w3.org/2006/07/SWD/RDFa/syntax/#id103282') | |
451 ) | |
452 ), | |
453 '#options' => array('property'=>'property', 'rel'=>'rel', 'rev'=>'rev'), | |
454 '#attributes' => array('class' => array('rdf-attribute-type')), | |
455 ); | |
456 | |
457 // @todo Possibly autocomplete the builtin xsd datatypes, but do not restrict to xsd datatypes. | |
458 $fieldset['rdf_'. $field_name .'_datatype'] = array( | |
459 '#type' => 'textfield', | |
460 '#title' => t('Datatype'), | |
461 '#autocomplete_path' => 'rdfui/datatype/autocomplete', | |
462 '#default_value' => empty($mapping[$field_name]['datatype']) ? '' : $mapping[$field_name]['datatype'], | |
463 '#description' => t('Enter the datatype for %label using CURIE syntax. For a list of common datatypes, see !link.', | |
464 array( | |
465 '%label' => $label, | |
466 '!link' => l('XML Schema datatypes', 'http://www.w3.org/TR/2004/REC-rdf-mt-20040210/#dtype_interp', array('attributes' => array('target' => '_blank'))), | |
467 ) | |
468 ), | |
469 '#states' => array( | |
470 'visible' => array( | |
471 ':input[name="rdf_' . $field_name . '_type"]' => array('value' => 'property'), | |
472 ), | |
473 ), | |
474 ); | |
475 drupal_add_css(drupal_get_path('module', 'rdfui') .'/css/rdfui.css', 'file'); | |
476 } | |
477 | |
478 /** | |
479 * Appends the RDF type fieldset. Because this fieldset is always on the same | |
480 * page as fields created by rdfui_predicate_fieldset(), which add the css and | |
481 * js files, we do not add the files again. | |
482 * | |
483 */ | |
484 function rdfui_rdftype_fieldset(&$fieldset, $mapping) { | |
485 $fieldset = array( | |
486 '#type' => 'textfield', | |
487 '#autocomplete_path' => 'rdfui/classes/autocomplete', | |
488 '#title' => t('RDF Type'), | |
489 '#default_value' => empty($mapping['rdftype']) ? '' : implode(", ",$mapping['rdftype']), | |
490 '#description' => t('Enter a comma-separated list of classes for this bundle using CURIE syntax. For example: %classes', array('%classes' => 'sioc:Item, foaf:Document')), | |
491 ); | |
492 } | |
493 | |
494 /** | |
495 * Sets a form error if predicates don't validate. | |
496 */ | |
497 function _rdfui_validate_terms($form_state, $field_name = '') { | |
498 // If the field_name is NOT NULL, then this is a predicate. If it is NULL, | |
499 // this is a type. | |
500 $field_id = $field_name != NULL ? 'rdf_'. $field_name .'_predicates' : 'rdf_rdftype'; | |
501 | |
502 // Validate terms. | |
503 if ($terms = trim($form_state['values'][$field_id])) { | |
504 $terms = rdfui_extract_curies($terms); | |
505 $rdf_namespaces = module_invoke_all('rdf_namespaces'); | |
506 $errors = array(); | |
507 $warnings = array(); | |
508 | |
509 foreach($terms as $term) { | |
510 // Ensure this is a CURIE. | |
511 if (!preg_match(QNAME, $term)){ | |
512 // @todo Add a page to the drupal docs about CURIE format and adding | |
513 // namespaces. Then add a link to this error that says 'For more info'. | |
514 $errors[] = t('Term %term must be in CURIE format.', array('%term'=>$term)); | |
515 } | |
516 else { | |
517 // Check the prefixes to ensure they are bound. If not, issue a warning. | |
518 $term_parts = explode(':', $term); | |
519 $prefix = $term_parts[0]; | |
520 if (!isset($rdf_namespaces[$prefix])) { | |
521 $warnings[] = t('Term %term uses unmapped prefix %prefix. Please map the prefix in !rdf_publishing.', | |
522 array( | |
523 '%term' => $term, | |
524 '%prefix' => $prefix, | |
525 '!rdf_publishing' => l(t('RDF publishing settings'), 'admin/config/services/rdf/namespaces'), | |
526 ) | |
527 ); | |
528 } | |
529 } | |
530 } | |
531 | |
532 if (!empty($errors)) { | |
533 foreach ($errors as $error) { | |
534 form_set_error($field_id, $error); | |
535 } | |
536 } | |
537 if (!empty($warnings)) { | |
538 foreach ($warnings as $warning) { | |
539 drupal_set_message($warning, $type='warning'); | |
540 } | |
541 } | |
542 } | |
543 } | |
544 | |
545 /** | |
546 * Sets a form error if datatype don't validate. | |
547 */ | |
548 function _rdfui_validate_datatype($form_state, $field_name) { | |
549 | |
550 // validate datatype | |
551 if ($datatype = trim($form_state['values']['rdf_'. $field_name .'_datatype'])) { | |
552 if(!preg_match(QNAME, $datatype)) | |
553 form_set_error('rdf_'. $field_name .'_datatype', t('Datatype %datatype is an invalid format.', array('%datatype'=>$datatype))); | |
554 } | |
555 | |
556 } | |
557 | |
558 /** | |
559 * Saves the mapping | |
560 */ | |
561 | |
562 function _rdfui_mapping_save($form_state, $entity_type, $bundle, $field_name) { | |
563 $mapping = rdf_mapping_load($entity_type, $bundle); | |
564 // Set the RDF type if it is in this form. | |
565 if (!empty($form_state['values']['rdf_rdftype'])) { | |
566 $mapping['rdftype'] = rdfui_extract_curies($form_state['values']['rdf_rdftype']); | |
567 } | |
568 // Turn the predicates string into an array. | |
569 if (isset($form_state['input']['rdf_title_type']) || $field_name != 'title') { | |
570 $form_state['values']['rdf_'. $field_name .'_predicates'] = rdfui_extract_curies($form_state['values']['rdf_'. $field_name .'_predicates']); | |
571 } | |
572 | |
573 foreach (array('type', 'datatype', 'predicates') as $key) { | |
574 // If there is form input for this key, set it in the mapping. | |
575 if (!empty($form_state['values']['rdf_'. $field_name .'_' . $key])) { | |
576 $mapping[$field_name][$key] = $form_state['values']['rdf_'. $field_name .'_' . $key]; | |
577 } | |
578 // If there is no form input for this key, we need to remove it from the | |
579 // mapping. | |
580 else { | |
581 // If there are no predicates, there should be no mapping at all for the | |
582 // field. | |
583 if ($key == 'predicates') { | |
584 unset($mapping[$field_name]); | |
585 } | |
586 else { | |
587 unset($mapping[$field_name][$key]); | |
588 } | |
589 } | |
590 } | |
591 | |
592 $mapping_info = array( | |
593 'mapping' => $mapping, | |
594 'type' => $entity_type, | |
595 'bundle' => $bundle | |
596 ); | |
597 rdf_mapping_save($mapping_info); | |
598 } | |
599 | |
600 /** | |
601 * Returns HTML for a term. | |
602 * | |
603 * @param $term | |
604 * An associative array containing: | |
605 * - term: A render element representing the term. | |
606 * - curie: The term's compact URI. | |
607 * - label: The term's human readable label. | |
608 * - comment: A description of the term. | |
609 * | |
610 * @ingroup themeable | |
611 */ | |
612 function theme_rdfui_term_autocomplete($variables) { | |
613 $term = $variables['term']; | |
614 | |
615 $output = '<div class="form-item rdf-term-autocomplete">'; | |
616 $output .= $term->curie . ' '; | |
617 $output .= '<p class="rdf-comment-autocomplete">' . $term->comment . '</p>'; | |
618 $output .= '</div>'; | |
619 | |
620 return $output; | |
621 } | |
622 | |
623 /** | |
624 * Generates an array of CURIE values from a string. | |
625 * | |
626 * Explodes a string with CURIES separated with , and with each a | |
627 * value on its own line. | |
628 */ | |
629 function rdfui_extract_curies($string_values) { | |
630 $list = explode(", ", $string_values); | |
631 $last = array_pop($list); | |
632 $last = str_replace(',', '', $last); | |
633 array_push($list, $last); | |
634 $list = array_map('trim', $list); | |
635 $list = array_filter($list, 'strlen'); | |
636 $list = array_unique($list); | |
637 return $list; | |
638 } |