View source
<?php
function rdfx_mapping_overview() {
$render = array();
$entities = entity_get_info();
$fields = field_info_instances();
$render['tabs'] = array(
'#type' => 'vertical_tabs',
);
foreach ($entities as $entity_type => $entity) {
$render['tabs'][$entity_type] = array(
'#type' => 'fieldset',
'#title' => $entity['label'],
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
foreach ($entity['bundles'] as $bundle_name => $bundle) {
$rows = array();
$real_fields = array();
$fake_fields = array();
$bundle_fields = $fields[$entity_type][$bundle_name];
$bundle['edit_path'] = NULL;
$rdf_mapping = $bundle['rdf_mapping'];
if (isset($bundle['admin']['real path'])) {
$bundle['edit_path'] = $bundle['admin']['real path'] . '/rdf';
}
if (isset($rdf_mapping['rdftype'])) {
$rdftype = implode(', ', $rdf_mapping['rdftype']);
unset($rdf_mapping['rdftype']);
}
foreach ($rdf_mapping as $field_name => $mapping_info) {
if (isset($bundle_fields[$field_name])) {
$real_fields[$field_name]['rdf_mapping'] = $mapping_info;
$real_fields[$field_name]['label'] = $fields[$entity_type][$bundle_name][$field_name]['label'];
$real_fields[$field_name]['edit_path'] = $bundle['edit_path'];
unset($bundle_fields[$field_name]);
}
else {
$fake_fields[$field_name]['rdf_mapping'] = $mapping_info;
$fake_fields[$field_name]['label'] = $field_name;
$fake_fields[$field_name]['edit_path'] = $field_name == 'title' ? $bundle['edit_path'] : NULL;
}
}
$variables = array(
'bundle' => $bundle,
'rdftype' => $rdftype,
'real_fields' => $real_fields,
'fake_fields' => $fake_fields,
);
$render['tabs'][$entity_type][$bundle_name] = theme('rdfx_mapping_admin_overview', $variables);
}
}
return $render;
}
function theme_rdfx_mapping_admin_overview($variables) {
$bundle_label = $variables['bundle']['label'];
$bundle = $variables['bundle'];
$real_fields = $variables['real_fields'];
$fake_fields = $variables['fake_fields'];
$rows = array();
$header = array(
t('Fields'),
t('RDF predicates'),
t('Mapping type'),
t('Datatype'),
);
if (module_exists('rdfui')) {
$header[] = t('Operations');
}
$title = "<h3>{$bundle_label}</h3>";
$edit_link = module_exists('rdfui') && !empty($bundle['edit_path']) ? ' (' . l(t('edit'), $bundle['edit_path']) . ')' : '';
$title .= "<p>" . t('RDF Types:') . ' ' . $variables['rdftype'] . $edit_link . '</p>';
foreach ($real_fields as $name => $field) {
$rows[] = array(
'data' => theme('rdfx_mapping_admin_overview_row', array(
'field' => $field,
'field_name' => $name,
'edit_path' => $field['edit_path'],
)),
);
}
foreach ($fake_fields as $name => $field) {
$rows[] = array(
'data' => theme('rdfx_mapping_admin_overview_row', array(
'field' => $field,
'field_name' => $name,
'edit_path' => $field['edit_path'],
)),
);
}
if (!count($rows)) {
$rows[] = array(
'data' => array(
array(
'data' => t('No mappings have been configured for this bundle.'),
'colspan' => 5,
),
),
);
}
return array(
'#prefix' => $title,
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
);
}
function theme_rdfx_mapping_admin_overview_row($variables) {
$field = $variables['field'];
$field_label = '<strong>' . t($field['label']) . '</strong>';
$predicates = isset($field['rdf_mapping']['predicates']) ? t(implode(', ', $field['rdf_mapping']['predicates'])) : '';
$predicate_type = isset($field['rdf_mapping']['type']) ? check_plain($field['rdf_mapping']['type']) : 'property';
$datatype = isset($field['rdf_mapping']['datatype']) ? $field['rdf_mapping']['datatype'] : '';
$row = array(
$field_label,
$predicates,
$predicate_type,
$datatype,
);
if (module_exists('rdfui')) {
$operations = '';
if (isset($variables['edit_path'])) {
$id = $variables['field_name'] == 'title' ? 'rdf-title' : $variables['field_name'];
$id = strtr(drupal_strtolower($id), array(
' ' => '-',
'_' => '-',
'[' => '-',
']' => '',
));
$id = preg_replace('/[^A-Za-z0-9\\-_]/', '', $id);
$id = preg_replace('/\\-+/', '-', $id);
$operations = l(t('edit'), $variables['edit_path'], array(
'fragment' => "edit-{$id}",
));
}
$row[] = $operations;
}
return $row;
}
function rdfx_admin_namespaces() {
$output = '';
$conflicting_namespaces = rdfx_get_conflicting_namespaces();
if ($conflicting_namespaces) {
$table_conflicting_namespaces = array();
$table_conflicting_namespaces['header'] = array(
'Prefix',
'Conflicting Namespaces',
);
foreach ($conflicting_namespaces as $prefix => $uris) {
$table_conflicting_namespaces['rows'][] = array(
$prefix,
implode(", ", $uris),
);
}
$output .= '<div class="messages warning">' . t("Warning: The following namespaces have conflicts") . '</div>';
$output .= theme('table', $table_conflicting_namespaces);
}
$table_namespaces = array();
$table_namespaces['header'] = array(
'Prefix',
'Namespace',
);
foreach (rdf_get_namespaces() as $prefix => $namespace) {
$table_namespaces['rows'][] = array(
$prefix,
$namespace,
);
}
if ($conflicting_namespaces) {
$output .= '<div class="messages status">' . t("The following namespaces do not have conflicts") . '</div>';
}
$output .= theme('table', $table_namespaces);
$form = drupal_get_form('rdfx_admin_namespaces_form');
$output .= drupal_render($form);
return $output;
}
function rdfx_admin_namespaces_form($form, &$form_state) {
$form['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix'),
'#required' => TRUE,
'#description' => t('Choose a prefix for this namespace, e.g. dc, foaf, sioc. This prefix will be used as an abbreviation for the namespace URI.'),
);
$form['ns_uri'] = array(
'#type' => 'textfield',
'#title' => t('Namespace URI'),
'#required' => TRUE,
'#default_value' => isset($form_state['values']['ns_uri']) ? $form_state['values']['ns_uri'] : NULL,
'#description' => t("Enter the URI of the namespace. Make sure it ends with either / or #."),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function rdfx_admin_namespaces_form_validate($form, &$form_state) {
module_load_include('inc', 'rdfx');
$prefix = $form_state['values']['prefix'];
$ns_uri = $form_state['values']['ns_uri'];
if (!valid_url($ns_uri, $absolute = TRUE)) {
form_set_error('ns_uri', t('The namespace URI must be a valid URI.'));
}
if (!preg_match('/(\\/|\\#)$/', $ns_uri)) {
form_set_error('ns_uri', t('The namespace URI must end in either a / or a #.'));
}
if (!preg_match('/^' . PREFIX . '$/', $prefix)) {
form_set_error('prefix', t('The prefix must follow the !link.', array(
'!link' => '<a href="http://www.w3.org/TR/xml-names11/#NT-NCName">XML Namespace Specification</a>',
)));
}
}
function rdfx_admin_namespaces_form_submit($form, &$form_state) {
$prefix = $form_state['values']['prefix'];
$ns_uri = $form_state['values']['ns_uri'];
$vocabulary = array(
'title' => array(),
'description' => array(),
'namespaces' => array(),
);
_rdfx_save_vocabulary($ns_uri, $prefix, $vocabulary);
drupal_set_message(t('The namespace @namespace has been saved with the prefix @prefix.', array(
'@namespace' => $ns_uri,
'@prefix' => $prefix,
)));
}