Mercurial > hg > rr-repo
comparison sites/all/modules/rdfx/rdfx.admin.inc @ 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 * Callback function for viewing all bundles' RDF mappings. | |
5 */ | |
6 function rdfx_mapping_overview() { | |
7 $render = array(); | |
8 $entities = entity_get_info(); | |
9 $fields = field_info_instances(); | |
10 $render['tabs'] = array( | |
11 '#type' => 'vertical_tabs', | |
12 ); | |
13 | |
14 // Create a tab for each entity. | |
15 foreach ($entities as $entity_type => $entity) { | |
16 $render['tabs'][$entity_type] = array( | |
17 '#type' => 'fieldset', | |
18 '#title' => $entity['label'], | |
19 '#collapsible' => TRUE, | |
20 '#collapsed' => TRUE, | |
21 ); | |
22 // The bundle's RDF mapping array may contain mappings for entity attributes | |
23 // that are not fields. The bundle's field array may contain fields that are | |
24 // not in the RDF mapping array. In order to ensure we get all the available | |
25 // fields and all the mapped entity attributes, we compare the arrays. | |
26 foreach ($entity['bundles'] as $bundle_name => $bundle) { | |
27 $rows = array(); | |
28 $real_fields = array(); | |
29 $fake_fields = array(); | |
30 $bundle_fields = $fields[$entity_type][$bundle_name]; | |
31 $bundle['edit_path'] = NULL; | |
32 $rdf_mapping = $bundle['rdf_mapping']; | |
33 if (isset($bundle['admin']['real path'])) { | |
34 $bundle['edit_path'] = $bundle['admin']['real path'] . '/rdf'; | |
35 } | |
36 | |
37 // Set RDF type. | |
38 if (isset($rdf_mapping['rdftype'])) { | |
39 $rdftype = implode(', ', $rdf_mapping['rdftype']); | |
40 unset($rdf_mapping['rdftype']); | |
41 } | |
42 foreach ($rdf_mapping as $field_name => $mapping_info) { | |
43 // Gather Field API fields. | |
44 if (isset($bundle_fields[$field_name])) { | |
45 $real_fields[$field_name]['rdf_mapping'] = $mapping_info; | |
46 $real_fields[$field_name]['label'] = $fields[$entity_type][$bundle_name][$field_name]['label']; | |
47 $real_fields[$field_name]['edit_path'] = $bundle['edit_path']; | |
48 unset($bundle_fields[$field_name]); | |
49 } | |
50 // Gather non-field content variables. | |
51 else { | |
52 $fake_fields[$field_name]['rdf_mapping'] = $mapping_info; | |
53 $fake_fields[$field_name]['label'] = $field_name; | |
54 $fake_fields[$field_name]['edit_path'] = ($field_name == 'title') ? $bundle['edit_path'] : NULL; | |
55 } | |
56 } | |
57 // Theme this bundle's table and add it to it's parent entity's tab. | |
58 $variables = array( | |
59 'bundle' => $bundle, | |
60 'rdftype' => $rdftype, | |
61 'real_fields' => $real_fields, | |
62 'fake_fields' => $fake_fields, | |
63 ); | |
64 $render['tabs'][$entity_type][$bundle_name] = theme('rdfx_mapping_admin_overview', $variables); | |
65 } | |
66 } | |
67 return $render; | |
68 } | |
69 | |
70 /** | |
71 * Theme function to output the table for a bundle's mappings. | |
72 */ | |
73 function theme_rdfx_mapping_admin_overview($variables) { | |
74 $bundle_label = $variables['bundle']['label']; | |
75 $bundle = $variables['bundle']; | |
76 $real_fields = $variables['real_fields']; | |
77 $fake_fields = $variables['fake_fields']; | |
78 $rows = array(); | |
79 | |
80 // Add the table header for this bundle. | |
81 $header = array(t('Fields'), t('RDF predicates'), t('Mapping type'), t('Datatype')); | |
82 if (module_exists('rdfui')) { | |
83 $header[] = t('Operations'); | |
84 } | |
85 | |
86 // Display a title for each bundle above the mappings table. If RDF UI is | |
87 // enabled, also add an 'edit' link. | |
88 $title = "<h3>$bundle_label</h3>"; | |
89 $edit_link = (module_exists('rdfui') && !empty($bundle['edit_path'])) ? ' (' . l(t('edit'), $bundle['edit_path']) . ')' : ''; | |
90 $title .= "<p>" . t('RDF Types:') . ' ' . $variables['rdftype'] . $edit_link . '</p>'; | |
91 | |
92 // List all of the Field API fields and their mappings. | |
93 foreach ($real_fields as $name => $field) { | |
94 $rows[] = array( | |
95 'data' => theme('rdfx_mapping_admin_overview_row', array('field' => $field, 'field_name' => $name, 'edit_path' => $field['edit_path'])), | |
96 ); | |
97 } | |
98 | |
99 // Add any non-Field API entity attributes. | |
100 foreach ($fake_fields as $name => $field) { | |
101 $rows[] = array( | |
102 'data' => theme('rdfx_mapping_admin_overview_row', array('field' => $field, 'field_name' => $name, 'edit_path' => $field['edit_path'])), | |
103 ); | |
104 } | |
105 | |
106 // If there are no mappings, display a message inside the table. | |
107 if (!count($rows)) { | |
108 $rows[] = array( | |
109 'data' => array(array('data' => t('No mappings have been configured for this bundle.'), 'colspan' => 5)) | |
110 ); | |
111 } | |
112 | |
113 // Return the table for this bundle. | |
114 return array( | |
115 '#prefix' => $title, | |
116 '#theme' => 'table', | |
117 '#rows' => $rows, | |
118 '#header' => $header, | |
119 ); | |
120 } | |
121 | |
122 /** | |
123 * Theme function to output a field's row in the bundle mapping table. | |
124 */ | |
125 function theme_rdfx_mapping_admin_overview_row($variables) { | |
126 $field = $variables['field']; | |
127 | |
128 $field_label = '<strong>' . t($field['label']) . '</strong>'; | |
129 $predicates = isset($field['rdf_mapping']['predicates']) ? t(implode(', ', $field['rdf_mapping']['predicates'])) : ''; | |
130 $predicate_type = isset($field['rdf_mapping']['type']) ? check_plain($field['rdf_mapping']['type']) : 'property'; | |
131 $datatype = isset($field['rdf_mapping']['datatype']) ? $field['rdf_mapping']['datatype'] : ''; | |
132 | |
133 $row = array($field_label, $predicates, $predicate_type, $datatype); | |
134 | |
135 // Add operations links only if RDF UI is enabled. | |
136 if (module_exists('rdfui')) { | |
137 $operations = ''; | |
138 if (isset($variables['edit_path'])) { | |
139 // By adding the appropriate url fragment, we can open the corresponding | |
140 // vertical tab on the RDF mapping UI page. The fragment should correspond | |
141 // to the html id for each fieldset, which means certain characters need | |
142 // to be replaced. These replacement patterns are from drupal_html_id(). | |
143 $id = ($variables['field_name'] == 'title') ? 'rdf-title' : $variables['field_name']; | |
144 $id = strtr(drupal_strtolower($id), array(' ' => '-', '_' => '-', '[' => '-', ']' => '')); | |
145 $id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id); | |
146 $id = preg_replace('/\-+/', '-', $id); | |
147 $operations = l(t('edit'), $variables['edit_path'], array('fragment' => "edit-$id")); | |
148 } | |
149 $row[] = $operations; | |
150 } | |
151 | |
152 return $row; | |
153 } | |
154 | |
155 /** | |
156 * Menu callback for viewing all declared namespaces (conflicting and non-conflicting) | |
157 * and their prefixes. | |
158 */ | |
159 function rdfx_admin_namespaces() { | |
160 $output = ''; | |
161 | |
162 // List conflicting namespaces. | |
163 $conflicting_namespaces = rdfx_get_conflicting_namespaces(); | |
164 if ($conflicting_namespaces) { | |
165 $table_conflicting_namespaces = array(); | |
166 $table_conflicting_namespaces['header'] = array('Prefix', 'Conflicting Namespaces'); | |
167 foreach ($conflicting_namespaces as $prefix => $uris) { | |
168 $table_conflicting_namespaces['rows'][] = array($prefix, implode(", ", $uris)); | |
169 } | |
170 $output .= '<div class="messages warning">' . t("Warning: The following namespaces have conflicts") . '</div>'; | |
171 $output .= theme('table', $table_conflicting_namespaces); | |
172 } | |
173 | |
174 // List non-conflicting namespaces. | |
175 $table_namespaces = array(); | |
176 $table_namespaces['header'] = array('Prefix', 'Namespace'); | |
177 foreach (rdf_get_namespaces() as $prefix => $namespace) { | |
178 $table_namespaces['rows'][] = array($prefix, $namespace); | |
179 } | |
180 // Only show label if there were conflicting namespaces. | |
181 if ($conflicting_namespaces) { | |
182 $output .= '<div class="messages status">' . t("The following namespaces do not have conflicts") . '</div>'; | |
183 } | |
184 $output .= theme('table', $table_namespaces); | |
185 | |
186 // Form to add namespaces. | |
187 $form = drupal_get_form('rdfx_admin_namespaces_form'); | |
188 $output .= drupal_render($form); | |
189 | |
190 return $output; | |
191 } | |
192 | |
193 function rdfx_admin_namespaces_form($form, &$form_state) { | |
194 $form['prefix'] = array( | |
195 '#type' => 'textfield', | |
196 '#title' => t('Prefix'), | |
197 '#required' => TRUE, | |
198 '#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.'), | |
199 ); | |
200 $form['ns_uri'] = array( | |
201 '#type' => 'textfield', | |
202 '#title' => t('Namespace URI'), | |
203 '#required' => TRUE, | |
204 '#default_value' => isset($form_state['values']['ns_uri']) ? $form_state['values']['ns_uri'] : NULL, | |
205 '#description' => t("Enter the URI of the namespace. Make sure it ends with either / or #."), | |
206 ); | |
207 $form['submit'] = array( | |
208 '#type' => 'submit', | |
209 '#value' => t('Save'), | |
210 ); | |
211 return $form; | |
212 } | |
213 | |
214 function rdfx_admin_namespaces_form_validate($form, &$form_state) { | |
215 // Loads the XML Namespace regular expression patterns. | |
216 module_load_include('inc', 'rdfx'); | |
217 | |
218 $prefix = $form_state['values']['prefix']; | |
219 $ns_uri = $form_state['values']['ns_uri']; | |
220 | |
221 // Ensures that the namespace is a valid URI. | |
222 if (!valid_url($ns_uri, $absolute = TRUE)) { | |
223 form_set_error('ns_uri', t('The namespace URI must be a valid URI.')); | |
224 } | |
225 // Ensures the namespace URI ends in either / or #. | |
226 if (!preg_match('/(\/|\#)$/', $ns_uri)) { | |
227 form_set_error('ns_uri', t('The namespace URI must end in either a / or a #.')); | |
228 } | |
229 // Ensures the prefix is well formed according to the specification. | |
230 if (!preg_match('/^' . PREFIX .'$/', $prefix)) { | |
231 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>'))); | |
232 } | |
233 } | |
234 | |
235 function rdfx_admin_namespaces_form_submit($form, &$form_state) { | |
236 $prefix = $form_state['values']['prefix']; | |
237 $ns_uri = $form_state['values']['ns_uri']; | |
238 // Prepares a fake empty vocabulary for _rdfx_save_vocabulary() to save the | |
239 // namespace and prefix. | |
240 // @todo use API when http://drupal.org/node/1117646 is fixed. | |
241 $vocabulary = array( | |
242 'title' => array(), | |
243 'description' => array(), | |
244 'namespaces' => array(), | |
245 ); | |
246 _rdfx_save_vocabulary($ns_uri, $prefix, $vocabulary); | |
247 drupal_set_message(t('The namespace @namespace has been saved with the prefix @prefix.', array('@namespace' => $ns_uri, '@prefix' => $prefix))); | |
248 } |