forked from roblib/islandora_solution_pack_chemistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_chem_sp_home.inc
465 lines (393 loc) · 14.5 KB
/
islandora_chem_sp_home.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* @file
*/
function islandora_chem_sp_group_form(&$form_state) {
$form['first_tab'] = array(
'#type' => 'tabpage',
);
$form['first_tab']['tabs'] = array(
'#type' => 'tabset',
);
$form['first_tab']['tabs']['new_groups'] = array(
'#type' => 'tabpage',
'#title' => t('Group content')
);
$form['first_tab']['tabs']['new_groups']['content'] = array(
'#value' => views_embed_view('og_mytracker'),
);
$form['first_tab']['tabs']['unread_groups'] = array(
'#type' => 'tabpage',
'#title' => t('Unread group content')
);
$form['first_tab']['tabs']['unread_groups']['content'] = array(
'#value' => views_embed_view('og_unread'),
);
$form['first_tab']['tabs']['new_content'] = array(
'#type' => 'tabpage',
'#title' => t('Recent site content')
);
$form['first_tab']['tabs']['new_content']['content'] = array(
'#value' => views_embed_view('og_tracker'),
);
return $form;
}
/**
* Form to display the objects that the user owns
* Also allows the permissions to be changed on the object by
* editing the XACML policy
* @todo Possibly add in individual users to assign permissions to
* @global type $base_url
* @global type $user
* @param type $form_state
* @param type $username
* @return string
*/
function islandora_chem_sp_object_form(&$form_state) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'islandora_xacml_editor', 'api/Xacml');
global $base_url;
global $user;
$username = $user->name;
$xacml_dsid = 'POLICY';
$groups = islandora_chem_sp_og_users_groups();
$page = isset($_GET['page']) ? $_GET['page'] : '0';
$items_per_page = 5;
$offset = $page * $items_per_page;
$calculations_list = array();
$solr_url = variable_get('islandora_solr_search_block_url', 'localhost:8080/solr');
$solr_query = 'http://' . $solr_url . '/select?q=fgs.ownerId:"' . urlencode($username) . '"%20AND%20rel.hasModel:sp_chem_CM&fl=PID,dc.title,cml.name&qt=standard&start=' . $offset . '&rows=' . $items_per_page;
$solr_result = @file_get_contents($solr_query);
if ($solr_result != NULL) {
$calc_search = new SimpleXMLElement($solr_result);
$pid_results = $calc_search->result;
$pid_list = array();
$rows2 = array();
$open_read_pids = array();
$open_write_pids = array();
$group_headers = array();
$total = $calc_search->result['numFound'];
foreach ($pid_results->children() as $child) {
$pid = (string) $child->str;
$pid_list[] = $pid;
$object = new Fedora_Item($pid);
$datastreams = $object->get_datastreams_list_as_array();
if (array_key_exists('POLICY', $datastreams)) {
module_load_include('inc', 'islandora_xacml_api', 'XacmlException');
// here we populate the form
$xml = $object->get_datastream_dissemination($xacml_dsid);
// some basic error handling
try {
$xacml = new Xacml($xml);
} catch (XacmlException $e) {
drupal_set_message($e->getMessage());
drupal_set_message("Xacml Parser failed to parse $object_pid/$xacml_dsid. It is likely this POLICY wasn't written by the islandora XACML editor, it will have to be modified by hand.");
drupal_not_found();
exit();
}
}
else {
$xacml = new Xacml();
}
$read_roles = $xacml->viewingRule->getRoles();
$write_roles = $xacml->managementRule->getUsers();
// var_dump($read_roles);
// var_dump($write_roles);
$cml = $object->get_datastream_dissemination('CML');
if ($cml != NULL) {
$cml_xml = new SimpleXMLElement($cml);
$name = $cml_xml->xpath('//molecule/name');
}
else {
$name = array('Error retrieving name');
}
$dc = $object->get_datastream_dissemination('DC');
if ($dc != NULL) {
$dc_xml = new SimpleXMLElement($dc);
$dc_xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$title = $dc_xml->xpath('//dc:title');
if ($title == NULL) {
$title = array('Unknown');
}
}
else {
$title = array('Error retrieving title');
}
$calc_title = l((string) $title[0] . ' ', $base_url . '/fedora/repository/' . $pid);
$calc_name = (string) $name[0];
// if (strlen($calc_name) > 100) {
// $calc_name = substr($calc_name, 0, 100);
// }
$tick_box1 = array(
'#type' => 'checkbox',
'#name' => 'chem_user_read__' . $pid,
'#value' => $xacml->viewingRule->isPopulated(),
);
$tick_box2 = array(
'#type' => 'checkbox',
'#name' => 'chem_user_write__' . $pid,
'#value' => $xacml->managementRule->isPopulated(),
);
$group_checkboxes = array();
$group_headers = array();
foreach ($groups as $group) {
$group_headers[] = $group['title'];
$tick_box3 = $tick_box_group_read_ . $group['nid'];
$tick_box3 = array(
'#type' => 'checkbox',
'#name' => 'chem_group-' . $group['nid'] . '-read__' . $pid,
'#value' => in_array($group['title'], $xacml->viewingRule->getRoles()),
);
$group_checkboxes_string = drupal_render($tick_box3);
$tick_box4 = $tick_box_group_write_ . $group['nid'];
$tick_box4 = array(
'#type' => 'checkbox',
'#name' => 'chem_group-' . $group['nid'] . '-write__' . $pid,
'#value' => in_array($group['title'], $xacml->managementRule->getRoles()),
);
$group_checkboxes_string .= drupal_render($tick_box4);
$group_checkboxes[] = $group_checkboxes_string;
}
$tick_box5 = array(
'#type' => 'checkbox',
'#name' => 'chem_all_read__' . $pid,
'#title' => t('Read'),
'#value' => !$xacml->viewingRule->isPopulated(),
);
$tick_box6 = array(
'#type' => 'checkbox',
'#name' => 'chem_all_write__' . $pid,
'#title' => t('Write'),
'#value' => !$xacml->managementRule->isPopulated(),
);
if (!$xacml->viewingRule->isPopulated()) {
$open_read_pids[] = $pid;
}
if (!$xacml->managementRule->isPopulated()) {
$open_write_pids[] = $pid;
}
$rows2_1 = array($calc_title, ucfirst(ltrim($calc_name)), drupal_render($tick_box1) . drupal_render($tick_box2));
$rows2_3 = array(drupal_render($tick_box5) . drupal_render($tick_box6));
$rows2[] = array_merge($rows2_1, $group_checkboxes, $rows2_3);
}
$no_of_items = count($rows2);
$element = 0;
$GLOBALS['pager_page_array'][$element] = max(0, min((int) $pager_page_array[$element], ((int) $pager_total[$element]) - 1));
if ($total == 0) {
$GLOBALS['pager_total'][$element] = 0;
}
else {
$GLOBALS['pager_total'][$element] = ceil($no_of_items / $total);
}
$header2_1 = array('Title', 'Name', 'User');
$header_2_3 = array('All');
$header2 = array_merge($header2_1, $group_headers, $header_2_3);
// var_dump($header2);
if (!empty($rows2)) {
sort($rows2);
$content1 .= theme('table', $header2, $rows2);
}
else {
$content1 .= "</BR>None found";
}
//Pretend we're actually doing an SQL query so paging will work
pager_query("SELECT %d", $items_per_page, 0, NULL, $total);
$output = $content1 . theme('pager');
}
else {
$output = "</BR>None found";
}
$form['content'] = array(
'#value' => $output,
);
$form['pids'] = array(
'#type' => 'hidden',
'#value' => implode(' ', $pid_list),
);
$form['open_read_pids'] = array(
'#type' => 'hidden',
'#value' => implode(' ', $open_read_pids),
);
$form['open_write_pids'] = array(
'#type' => 'hidden',
'#value' => implode(' ', $open_write_pids),
);
$form['save'] = array(
'#type' => 'submit',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#value' => t('Save changes'),
'#name' => 'save_permissions',
);
return $form;
}
/**
* Submit function for home page form
* Checks which submit button was clicked and acts appropriately
* @todo When the return key i s pressed work out which button the user wanted to click
* from elements that the user had filled in
* @global type $user
* @param type $form
* @param type $form_state
*/
function islandora_chem_sp_object_form_submit($form, &$form_state) {
module_load_include('inc', 'islandora_chem_sp', 'islandora_chem_sp_search');
module_load_include('module', 'islandora_xacml_editor', 'islandora_xacml_editor');
module_load_include('inc', 'islandora_xacml_editor', 'api/Xacml');
global $user;
$dsid = 'POLICY';
$permissions = array();
$pid_list = explode(' ', $form_state['clicked_button']['#post']['pids']);
$open_read_pids = explode(' ', $form_state['clicked_button']['#post']['open_read_pids']);
$open_write_pids = explode(' ', $form_state['clicked_button']['#post']['open_write_pids']);
drupal_set_message('Button: ' . $form_state['clicked_button']['#name']);
if ($form_state['clicked_button']['#name'] == 'save_permissions') {
foreach ($form_state['clicked_button']['#post'] as $key => $value) {
if (strstr($key, 'chem')) {
$tasks = explode('__', $key);
$permissions[$tasks[1]][$tasks[0]] = $value;
}
}
}
// krumo($form_state);
foreach ($pid_list as $pid) {
$read_open = TRUE;
$write_open = TRUE;
$xacml = new Xacml();
$object = new Fedora_Item($pid);
$datastreams = $object->get_datastreams_list_as_array();
// drupal_set_message('PID: ' . $pid);
foreach ($permissions[$pid] as $key => $pid_permission) {
// drupal_set_message('Pid permission: ' . $key . ' - ' . $pid_permission);
if ($key == 'chem_user_read' && $pid_permission == 1) {
// drupal_set_message('Chem user read');
$xacml->viewingRule->addUser($user->name);
$read_open = FALSE;
}
if ($key == 'chem_user_write' && $pid_permission == 1) {
// drupal_set_message('Chem user write');
$xacml->managementRule->addUser($user->name);
$write_open = FALSE;
}
if (stristr($key, 'chem_group') != FALSE) {
$key_array = explode('-', $key);
// drupal_set_message('Group id: ' . $key_array[1]);
// drupal_set_message('Level: ' . $key_array[2]);
$query = db_query("SELECT title FROM {node} WHERE nid = '%d'", $key_array[1]);
$result = db_fetch_object($query);
$role = $result->title;
if ($key_array[2] == 'read') {
// drupal_set_message('Adding viewing role ' . $role);
$xacml->viewingRule->addRole($role);
$read_open = FALSE;
}
if ($key_array[2] == 'write') {
// drupal_set_message('Adding management role ' . $role);
$xacml->viewingRule->addRole($role);
$xacml->managementRule->addRole($role);
$write_open = FALSE;
}
}
}
// drupal_set_message('Open read pids: ' . implode(', ', $open_read_pids));
// drupal_set_message('In read: ' . (string) in_array($pid, $open_read_pids));
if ($key == 'chem_all_read' && $pid_permission == 1) {
if ($read_open == TRUE || !in_array($pid, $open_read_pids)) {
// drupal_set_message('Chem all read');
$read_roles = $xacml->viewingRule->getRoles();
foreach ($read_roles as $read_role) {
$xacml->viewingRule->removeRole($read_role);
}
$read_users = $xacml->viewingRule->getUsers();
foreach ($read_users as $read_user) {
$xacml->viewingRule->removeUser($read_user);
}
}
}
if ($key == 'chem_all_write' && $pid_permission == 1) {
if ($write_open == TRUE || !in_array($pid, $open_write_pids)) {
// drupal_set_message('Chem all write');
$write_roles = $xacml->managementRule->getRoles();
foreach ($write_roles as $write_role) {
$xacml->managementRule->removeRole($write_role);
}
$write_users = $xacml->managementRule->getUsers();
foreach ($write_users as $write_user) {
$xacml->managementRule->removeUser($write_user);
}
}
}
$xml = $xacml->getXmlString();
if ($xml != NULL) {
if (array_key_exists($dsid, $datastreams)) {
$object->modify_datastream_by_value($xml, $dsid, 'Xacml Policy Stream', 'text/xml');
}
else {
$object->add_datastream_from_string($xml, $dsid, 'Xacml Policy Stream', 'text/xml', 'X');
}
}
else {
$result = $object->purge_datastream($dsid);
if ($result) {
drupal_set_message(t('Security policy removed from @pid', array('@pid' => $pid)));
}
}
// drupal_set_message('XACML: ' . $xml);
}
}
/**
* Helper function to bring back the organic groups that
* the user is a member of. Strangely, OG doesn't provide this in their
* API
* @global type $user
* @return type array of organic groups that the user is a member of
*/
function islandora_chem_sp_og_users_groups() {
global $user;
$rows = array();
$query = db_query("SELECT a.nid,title FROM {node} a INNER JOIN {og_uid} b ON a.nid = b.nid WHERE b.uid = '%s'", $user->uid);
if ($query == FALSE) {
return FALSE;
}
else {
while ($result = db_fetch_array($query)) {
$rows[] = $result;
}
return $rows;
}
}
function islandora_chem_sp_people_form(&$form_state) {
global $user;
$content = '';
$interests_query = db_query("SELECT v.value FROM {profile_values} v INNER JOIN {profile_fields} f ON v.fid=f.fid WHERE v.uid = '%d' AND f.name = 'profile_interests'", $user->uid);
while ($result = db_fetch_array($interests_query)) {
$interests_array[] = $result;
}
$interests_array = (explode("\n", $interests_array[0]['value']));
$interesting_people = array();
foreach ($interests_array as $interest_array) {
$query = db_query("SELECT v.uid FROM {profile_values} v INNER JOIN {profile_fields} f ON v.fid=f.fid WHERE v.value LIKE '%%%s%%'", trim($interest_array));
while ($result = db_fetch_array($query)) {
$users_array[] = $result;
}
if ($users_array != NULL) {
foreach ($users_array as $user_array) {
$user_query = db_query("SELECT users.name FROM {users} where uid = '%d'", $user_array);
while ($result = db_fetch_array($user_query)) {
$interesting_people[$user_array['uid']] = $result['name'];
}
}
}
}
$content .= '<h3>People with similar interests</h3>';
$content .= '<ul>';
foreach ($interesting_people as $uid => $person) {
$content .= '<li>' . l($person, 'user/' . $uid) . '</li>';
}
$content .= '</ul>';
$form['people_list'] = array(
'#value' => $content,
);
return $form;
}