forked from rosiel/islandora_relationship_editor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Add.inc
343 lines (283 loc) · 9.74 KB
/
Add.inc
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
function islandora_relationship_editor_add_relationships(array $form, array &$form_state, $object_pid) {
$options_relationship = get_possible_relationships($object_pid);
$value_relationship = !empty($form_state['values']['relationship']) ? $form_state['values']['relationship'] : "";
$form = array();
$form['this'] = array(
'#title' => t('This Object'),
'#type' => 'textfield',
'#value' => $object_pid,
'#disabled' => TRUE,
);
$form['rels'] = array(
'#type' => 'fieldset',
'#title' => 'Relationship',
);
$form['rels']['relationship'] = array(
'#type' => 'select',
'#title' => t('Relationship'),
'#options' => $options_relationship,
'#required' => TRUE,
'#ajax' => array(
'method' => 'replace',
'effect' => 'fade',
'event' => 'change',
'callback' => 'relationship_ajax_callback',
),
);
$form['rels']['description'] = array(
'#markup' => textfield_description_value($value_relationship),
'#prefix' => '<div id="textfield_description_replace">',
'#suffix' => '</div>',
);
$form['ns'] = array(
'#type' => 'fieldset',
'#title' => 'Namespace',
'#prefix' => '<div id="fieldset_ns_replace">',
'#suffix' => '</div>',
);
$form['ns']['namespace'] = array(
'#type' => 'textfield',
'#title' => t('Namespace'),
'#disabled' => TRUE,
'#value' => textfield_namespace_value($value_relationship),
);
$form['ns']['curr_alias'] = array(
'#type' => 'textfield',
'#title' => t('Namespace alias'),
'#disabled' => TRUE,
'#value' => isset($form_state['values']['relationship']) ? ns_has_alias($value_relationship, $object_pid) : "",
);
$form['ns']['add_new_alias'] = array(
'#type' => 'checkbox',
'#disabled' => TRUE,
'#title' => t('Add NEW alias'),
'#default_value' => FALSE,
'#value' => (isset($form_state['values']['relationship']) && !(ns_has_alias($value_relationship, $object_pid))) ? TRUE : FALSE,
);
$form['ns']['alias'] = array(
'#type' => 'textfield',
'#title' => t('NEW Namespace alias (blank for inline namespace)'),
'#disabled' => (isset($form_state['values']['relationship']) && !(ns_has_alias($value_relationship, $object_pid))) ? FALSE : TRUE,
);
$form['obj'] = array(
'#type' => 'fieldset',
'#title' => 'Object',
);
$form['obj']['target'] = array(
'#type' => 'textfield',
'#title' => t('Target object'),
'#required' => TRUE,
);
$form['symmetric'] = array(
'#type' => 'fieldset',
'#title' => 'Symmetry',
'#prefix' => '<div id="fieldset_symm_replace">',
'#suffix' => '</div>',
);
$form['symmetric']['inv-relationship'] = array(
'#type' => 'textfield',
'#title' => t('Symmetric relationship'),
'#disabled' => TRUE,
'#value' => textfield_inverseof_value($value_relationship),
);
$form['symmetric']['inv-relationship_ns'] = array(
'#type' => 'textfield',
'#title' => t('Symmetric namespace'),
'#disabled' => TRUE,
'#value' => textfield_inverseof_ns($value_relationship),
);
$form['symmetric']['sym-relationship'] = array(
'#markup' => description_inverseof_value($value_relationship),
);
$form['symmetric']['is-symmetric'] = array(
'#type' => 'checkbox',
'#disabled' => !(has_inverseof_value($value_relationship)),
'#title' => t('Add the corresponding relationship?'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('submit'),
);
return $form;
}
function relationship_ajax_callback(&$form, $form_state) {
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#textfield_description_replace", render($form['rels']['description'])),
ajax_command_replace("#fieldset_symm_replace", render($form['symmetric'])),
ajax_command_replace("#fieldset_ns_replace", render($form['ns']))
)
);
}
function ns_has_alias($value_relationship, $object_pid) {
if (empty($value_relationship)) {
return FALSE;
}
list($value_ns, $value_prop) = rdfx_split_uri($value_relationship);
$object = islandora_object_load($object_pid);
if (!$object) {
drupal_set_message("Fedora object doesn't exist.");
drupal_not_found();
return FALSE;
}
$rels = $object->relationships->get();
foreach ($rels as $rel){
if ($rel['predicate']['namespace'] == $value_ns) {
if (!empty($rel['predicate']['alias'])){
return $rel['predicate']['alias'];
}
}
}
return FALSE;
}
function alias_exists($alias, $object_pid) {
if (empty($alias)) {
return TRUE;
}
$object = islandora_object_load($object_pid);
if (!$object) {
drupal_set_message("Fedora object doesn't exist.");
drupal_not_found();
return TRUE;
}
$rels = $object->relationships->get();
foreach ($rels as $rel){
if ($rel['predicate']['alias'] == $alias) {
return TRUE;
}
}
return FALSE;
}
function textfield_namespace_value($value_relationship) {
if (!empty($value_relationship)) {
list($value_ns, $value_prop) = rdfx_split_uri($value_relationship);
}
return empty($value_ns) ? "No namespace" : $value_ns;
}
function textfield_description_value($value_relationship) {
if (!empty($value_relationship)) {
$vocabulary = get_current_vocabulary();
$term = $vocabulary['terms'][$value_relationship];
if (isset($term['comment'])) {
if (isset($term['comment']['en'])) {
$servoc = $term['comment']['en'];
} else {
$servoc = reset($term['comment']);
}
}
$value_description = $servoc;
}
return empty($value_description) ? "No description" : $value_description;
}
function textfield_inverseof_value($value_relationship) {
if (!empty($value_relationship)) {
$vocabulary = get_current_vocabulary();
$term = $vocabulary['terms'][$value_relationship];
if (isset($term['inverse'][0])) {
list($value_ns, $value_inverse) = rdfx_split_uri($term['inverse'][0]);
}
}
return empty($value_inverse) ? "No inverse" : $value_inverse;
}
function textfield_inverseof_ns($value_relationship) {
if (!empty($value_relationship)) {
$vocabulary = get_current_vocabulary();
$term = $vocabulary['terms'][$value_relationship];
if (isset($term['inverse'][0])) {
list($value_ns, $value_inverse) = rdfx_split_uri($term['inverse'][0]);
}
}
return empty($value_ns) ? "No inverse ns" : $value_ns;
}
function description_inverseof_value($value_relationship) {
if (!empty($value_relationship)) {
$vocabulary = get_current_vocabulary();
$term = $vocabulary['terms'][$value_relationship];
if (isset($term['inverse'][0])) {
$term1 = $vocabulary['terms'][$term['inverse'][0]];
if (isset($term1['label'])) {
if (isset($term1['label']['en'])) {
$servoc = $term1['label']['en'];
} else {
$servoc = reset($term1['label']);
}
}
$value_inverse = "<B>".$servoc."</B>";
}
}
return empty($value_inverse) ? "No inverse<HR>" : $value_inverse."<HR>";
}
function has_inverseof_value($value_relationship) {
if (!empty($value_relationship)) {
$vocabulary = get_current_vocabulary();
$term = $vocabulary['terms'][$value_relationship];
if (isset($term['inverse'][0])) {
return TRUE;
}
}
return FALSE;
}
function islandora_relationship_editor_add_relationships_submit($form, &$form_state) {
$subject = islandora_object_load($form_state['values']['this']);
if (!$subject) {
form_set_error('target', "Subject error: Fedora object doesn't exist.");
$form_state['redirect'] = FALSE;
return ;
}
$object = islandora_object_load($form_state['values']['target']);
if (!$object) {
form_set_error('target', "Object error: Fedora object doesn't exist.");
$form_state['redirect'] = FALSE;
return ;
}
list($rel_ns, $rel_name) = rdfx_split_uri($form_state['values']['relationship']);
$relOk = 0;
if ($form_state['values']['add_new_alias']){
if (!(empty($form_state['values']['alias']))){
if (alias_exists($form_state['values']['alias'], $form_state['values']['this'])) {
form_set_error('alias', "Duplicated alias");
$form_state['redirect'] = FALSE;
return ;
} else {
$relOk = 1; // Add Alias and Insert Rel
}
} else {
$relOk = 2; // Insert Rel with ns inline
}
} else {
$relOk = 3; // Insert Rel with alias exists
}
$invOk = 0;
if ($form_state['values']['is-symmetric']) {
if (ns_has_alias($form_state['values']['inv-relationship_ns'].$form_state['values']['inv-relationship'], $form_state['values']['target'])) {
$invOk = 3; // Insert inv_rel with alias exists
} else {
if (($form_state['values']['inv-relationship_ns'] == $rel_ns) && (($relOk == 1) || ($relOk == 3))) {
$invOk = 1; // Add Alias and Insert Inv_Rel
} elseif (($form_state['values']['inv-relationship_ns'] == $rel_ns) && ($relOk == 2)) {
$invOk = 2; // Insert Inv_Rel with ns inline
} else {
form_set_error('is-symmetric', "Inverse namespace alias error");
$form_state['redirect'] = FALSE;
return ;
}
}
}
// Insert rel in RELS-EXT of subject
if ($relOk == 1) {
$subject->relationships->registerNamespace($form_state['values']['alias'], $rel_ns);
$subject->relationships->add($rel_ns, $rel_name, $form_state['values']['target']);
} elseif ($relOk > 1) {
$subject->relationships->add($rel_ns, $rel_name, $form_state['values']['target']);
}
// Insert inverse rel in RELS-EXT of object
if ($invOk == 1) {
$object->relationships->registerNamespace(empty($form_state['values']['curr_alias']) ? $form_state['values']['alias'] : $form_state['values']['curr_alias'], $form_state['values']['inv-relationship_ns']);
$object->relationships->add($form_state['values']['inv-relationship_ns'], $form_state['values']['inv-relationship'], $form_state['values']['this']);
} elseif ($invOk >1) {
$object->relationships->add($form_state['values']['inv-relationship_ns'], $form_state['values']['inv-relationship'], $form_state['values']['this']);
}
$form_state['redirect'] = "islandora/object/{$subject->id}/manage/relationships";
}