/[drupal]/contributions/modules/oi/oi.module
ViewVC logotype

Contents of /contributions/modules/oi/oi.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Wed Aug 12 19:08:48 2009 UTC (3 months, 2 weeks ago) by pukku
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.2: +436 -317 lines
File MIME type: text/x-php
drupal 6 port
1 <?php
2
3 // $Id: oi.module,v 1.2 2009/06/24 18:27:59 pukku Exp $
4
5 /**
6 * @file
7 *
8 * OI provides a hierarchical access control system, among other things.
9 */
10
11 /**
12 * Constants for membership types
13 */
14 define('OI_REMOVE', -1);
15 define('OI_NONE', 0);
16 define('OI_MEMBER', 1);
17 define('OI_ACCESS_ONLY', 2);
18
19 /**
20 * Implementation of hook_perm().
21 */
22 function oi_perm () {
23 $ret = array(
24 'manage oi entities',
25 'manage oi entity membership',
26 'control access using oi',
27 'restrict access to any oi entity',
28 );
29
30 if (module_exists('site_user_list')) {
31 $ret[] = 'search site user list using any oi entity';
32 $ret[] = 'search site user list using own oi entities';
33 }
34
35 return $ret;
36 }
37
38 /**
39 * Implementation of hook_menu().
40 */
41 function oi_menu () {
42 $items = array();
43
44 $items['admin/settings/oi'] = array(
45 'title' => 'Organizational Infrastructure',
46 'page callback' => 'drupal_get_form',
47 'page arguments' => array('oi_admin_settings_form'),
48 'access arguments' => array('administer site configuration'),
49 'type' => MENU_NORMAL_ITEM,
50 );
51 $items['admin/settings/oi/entity_type_edit'] = array(
52 'title' => 'Edit entity type',
53 'page callback' => 'oi_settings_entity_type_form_dispatch',
54 'access arguments' => array('access administration pages'),
55 'type' => MENU_CALLBACK,
56 );
57 $items['admin/settings/oi/entity_type_delete'] = array(
58 'title' => 'Delete entity type',
59 'page callback' => 'oi_settings_entity_type_delete',
60 'access arguments' => array('access administration pages'),
61 'type' => MENU_CALLBACK,
62 );
63
64 $items['admin/settings/oi/entity_type_role_edit'] = array(
65 'title' => 'Edit entity type role',
66 'page callback' => 'oi_settings_entity_type_role_dispatch',
67 'access arguments' => array('access administration pages'),
68 'type' => MENU_CALLBACK,
69 );
70 $items['admin/settings/oi/entity_type_role_delete'] = array(
71 'title' => 'Delete entity type role',
72 'page callback' => 'oi_settings_entity_type_role_delete',
73 'access arguments' => array('access administration pages'),
74 'type' => MENU_CALLBACK,
75 );
76
77 $items['admin/settings/oi/entity_type_field_edit'] = array(
78 'title' => 'Edit entity type field',
79 'page callback' => 'oi_settings_entity_type_field_dispatch',
80 'access arguments' => array('access administration pages'),
81 'type' => MENU_CALLBACK,
82 );
83 $items['admin/settings/oi/entity_type_field_delete'] = array(
84 'title' => 'Delete entity type field',
85 'page callback' => 'oi_settings_entity_type_field_delete',
86 'access arguments' => array('access administration pages'),
87 'type' => MENU_CALLBACK,
88 );
89
90
91
92 $items['admin/user/oi'] = array(
93 'title' => 'OI Entities',
94 'page callback' => 'oi_entities_list',
95 'access arguments' => array('manage oi entities'),
96 'type' => MENU_NORMAL_ITEM,
97 );
98 $items['admin/user/oi/entity_add_edit'] = array(
99 'title' => 'Add or Edit OI Entity',
100 'page callback' => 'oi_entities_form_dispatch',
101 'access arguments' => array('manage oi entities'),
102 'type' => MENU_CALLBACK,
103 );
104 $items['admin/user/oi/entity_delete'] = array(
105 'title' => 'Delete OI Entity',
106 'page callback' => 'oi_entities_delete_dispatch',
107 'access arguments' => array('manage oi entities'),
108 'type' => MENU_CALLBACK,
109 );
110 $items['admin/user/oi/entity_data_edit'] = array(
111 'title' => 'Edit Entity Data',
112 'page callback' => 'oi_entities_data_edit',
113 'access arguments' => array('manage oi entities'),
114 'type' => MENU_CALLBACK,
115 );
116 $items['admin/user/oi/entity_role_edit'] = array(
117 'title' => 'Edit Entity Role',
118 'page callback' => 'oi_entities_role_form_dispatch',
119 'access arguments' => array('manage oi entities'),
120 'type' => MENU_CALLBACK,
121 );
122 $items['admin/user/oi/entity_field_edit'] = array(
123 'title' => 'Edit Entity Field',
124 'page callback' => 'oi_entities_field_form_dispatch',
125 'access arguments' => array('manage oi entities'),
126 'type' => MENU_CALLBACK,
127 );
128
129 return $items;
130 }
131
132 /**
133 * Implementation of hook_init().
134 */
135 function oi_init () {
136 drupal_add_css(drupal_get_path('module', 'oi') . '/oi.css');
137 }
138
139 /**
140 * @defgroup helper-functions
141 * @{
142 */
143
144 /**
145 * Equivalent to Perl DBIs $db->selectcol_array().
146 */
147 function oi_db_get_column ($query) {
148 $ret = array();
149 while($a = db_fetch_array($query)) {
150 $ret[] = array_shift($a);
151 }
152 return $ret;
153 }
154
155 function _oi_get_dashes ($count, $dashes = '--') {
156 $dash = str_repeat($dashes, $count);
157 return $dash;
158 }
159
160 /**
161 * @}
162 */
163
164 /**
165 * Implementation of hook_theme().
166 */
167 function oi_theme () {
168 return array(
169 'oi_settings_display_entity_type' => array(
170 'arguments' => array(
171 'etname' => NULL,
172 'roles' => array(),
173 'fields' => array(),
174 ),
175 ),
176 'oi_user_edit' => array(
177 'arguments' => array(
178 'form' => NULL,
179 ),
180 ),
181 'oi_restriction_note' => array(
182 'arguments' => array(
183 'eidisplays' => NULL,
184 ),
185 ),
186 'oi_roles_text' => array(
187 'arguments' => array(
188 'roles_text' => NULL,
189 ),
190 ),
191 );
192 }
193
194
195 /**
196 * @defgroup settings-form
197 * @{
198 */
199
200 /**
201 * Implementation of hook_settings().
202 */
203 function oi_admin_settings_form () {
204 $entity_types = _oi_get_entity_types();
205
206 $header = array(t('Type'), t('Operations'));
207
208 $rows = array();
209 _oi_entity_types_list_recurse_rows($entity_types['tree'], $entity_types['types'], $rows);
210
211 if (count($rows) == 0) {
212 $rows[] = array(array('colspan' => 2, 'data' => t('No entity types defined.')));
213 }
214
215 $form = array();
216
217 $node_access_status = variable_get('oi_node_access_enabled', false);
218 $form['toggle_oi_restrictions'] = array(
219 '#type' => 'fieldset',
220 '#title' => t('Toggle Access Restrictions'),
221 );
222 $form['toggle_oi_restrictions']['toggle_oi_restrictions_button'] = array(
223 '#type' => 'submit',
224 '#suffix' => '<p>' . t('OI node access permissions are currently ' . ($node_access_status ? 'on' : 'off') . '. You may change this by clicking on this button. Turning permissions off does not delete the information about permissions already assigned, it just prevents it from having any effect.') . '</p>',
225 '#value' => $node_access_status ? t('Turn Access Restrictions Off') : t('Turn Access Restrictions On'),
226 '#oi_toggle_curr_value' => $node_access_status,
227 );
228
229 $form['entity_types'] = array(
230 '#type' => 'fieldset',
231 '#title' => t('Entity Types'),
232 );
233 $form['entity_types'][] = array(
234 '#type' => 'markup',
235 '#value' => theme('table', $header, $rows)
236 . '<p>' . l(t('Add new entity type'), 'admin/settings/oi/entity_type_edit') . '</p>',
237 );
238
239 $form['oi_name_display'] = array(
240 '#type' => 'fieldset',
241 '#title' => t('Display settings'),
242 );
243 $form['oi_name_display']['oi_name_display_string'] = array(
244 '#type' => 'textfield',
245 '#title' => t('Name display settings'),
246 '#description' => t('Determine how OI will display user names. Use @field_name to indicate a field from profile.module. You may also use @uid, @name, @mail to get non-profile.module items'),
247 '#default_value' => variable_get('oi_name_display_string', '@name'),
248 );
249 $form['oi_name_display'][] = array(
250 '#type' => 'submit',
251 '#value' => t('Save display settings'),
252 );
253
254 return $form;
255 }
256
257 function oi_admin_settings_form_submit ($form, &$form_state) {
258 if (array_key_exists('#oi_toggle_curr_value', $form_state['clicked_button'])) {
259 oi_toggle_node_access();
260 $message = '';
261 if ($form_states['clicked_button']['#oi_toggle_curr_value']) {
262 $message = 'Access restrictions via OI disabled';
263 }
264 else {
265 $message = 'Access restrictions via OI enabled';
266 }
267 drupal_set_message($message);
268 }
269 else {
270 $display_string = $form_state['values']['oi_name_display_string'];
271 variable_set('oi_name_display_string', $display_string);
272
273 // precompute the select-from sql for speed...
274 preg_match_all('/@([A-Za-z_]+)/', $display_string, $matches);
275 $cols = array();
276 $joins = array();
277
278 $cols['uid'] = 'u.uid as "@uid"';
279 $cols['name'] = 'u.name as "@name"';
280 $cols['mail'] = 'u.mail as "@mail"';
281
282 foreach ($matches[1] as $field_name) {
283 if (!isset($cols[$field_name])) {
284 $fid = db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $field_name));
285 $tbl = 't_' . $fid;
286
287 $cols[$field_name] = "$tbl.value as \"@$field_name\"";
288 $joins[] = "LEFT OUTER JOIN {profile_values} as $tbl on (u.uid = $tbl.uid and $tbl.fid = $fid)";
289 }
290 }
291
292 $sql = 'SELECT ' . implode(', ', $cols) . ' '
293 . 'FROM {users} as u ' . implode(' ', $joins);
294
295 variable_set('oi_name_display_sql', $sql);
296
297 drupal_set_message('Changes saved.');
298 }
299 }
300
301 function _oi_entity_types_list_recurse_rows ($tree, $info, &$rows, $depth = 0) {
302 foreach ($tree as $etid => $children) {
303 $roles = array();
304 $rq = db_query("SELECT * FROM {oi_entity_type_roles} WHERE etid = %d ORDER BY rtweight", $etid);
305 while($r = db_fetch_array($rq)) {
306 $roles[] = $r['rtdisplay'];
307 }
308 $fields = array();
309 $fq = db_query("SELECT * FROM {oi_entity_type_fields} WHERE etid = %d ORDER BY ftweight", $etid);
310 while($f = db_fetch_array($fq)) {
311 $fields[] = $f['ftdisplay'];
312 }
313
314 $rows[] = array(
315 theme('oi_settings_display_entity_type', $info[$etid]['etname'], $roles, $fields),
316 l(t('edit'), "admin/settings/oi/entity_type_edit/$etid") . ' | ' . l(t('delete'), "admin/settings/oi/entity_type_delete/$etid"),
317 );
318 _oi_entity_types_list_recurse_rows($children, $info, $rows, $depth + 1);
319 }
320 }
321
322 function theme_oi_settings_display_entity_type ($etname, $roles = array(), $fields = array()) {
323 $roles_text = implode(', ', $roles);
324 if ($roles_text == '') {
325 $roles_text = 'Roles: <em>(no roles)</em>';
326 }
327 else {
328 $roles_text = 'Roles: ' . $roles_text;
329 }
330
331 $fields_text = implode(', ', $fields);
332 if ($fields_text == '') {
333 $fields_text = 'Fields: <em>(no fields)</em>';
334 }
335 else {
336 $fields_text = 'Fields: ' . $fields_text;
337 }
338
339 return '<div class="oi_settings_display_entity_type_name">' . $etname . '</div>'
340 . '<div class="oi_settings_display_entity_type_roles">' . $roles_text . '</div>'
341 . '<div class="oi_settings_display_entity_type_fields">' . $fields_text . '</div>';
342 }
343
344 /**
345 * @}
346 */
347
348
349
350
351 function oi_settings_entity_type_form_dispatch ($etid = 0) {
352 return drupal_get_form('oi_settings_entity_type_form', $etid);
353 }
354
355 function oi_settings_entity_type_form (&$form_state, $etid = 0) {
356 $defaults = array(
357 'etid' => $etid,
358 'etname' => '',
359 'parent_etid' => 0,
360 );
361
362 if ($etid != 0) {
363 $defaults = db_fetch_array(db_query("SELECT * FROM {oi_entity_types} WHERE etid = %d", $etid));
364 }
365
366 $form = array();
367
368 $form['etid'] = array(
369 '#type' => 'value',
370 '#value' => $etid,
371 );
372
373 $form['etname'] = array(
374 '#type' => 'textfield',
375 '#title' => t('Entity type name'),
376 '#description' => t('The name of this entity type'),
377 '#default_value' => $defaults['etname'],
378 '#size' => 60,
379 '#maxlength' => 100,
380 '#required' => true,
381 );
382
383 $form[] = array(
384 '#type' => 'submit',
385 '#value' => t('Save entity type'),
386 );
387
388 $form[] = array(
389 '#type' => 'markup',
390 '#value' => _oi_settings_entity_type_role_list($defaults['etid']),
391 );
392
393 $form[] = array(
394 '#type' => 'markup',
395 '#value' => _oi_settings_entity_type_field_list($defaults['etid']),
396 );
397
398 return $form;
399 }
400
401 function oi_settings_entity_type_form_submit ($form, &$form_state) {
402 if ($form_state['values']['etid'] == 0) {
403 // a new entity type
404 db_query("INSERT INTO {oi_entity_types} (etname) VALUES ('%s')", $form_state['values']['etname']);
405 drupal_set_message(t('The entity type %etname was added', array('%etname' => $form_state['values']['etname'])));
406 }
407 else {
408 // updating
409 db_query("UPDATE {oi_entity_types} SET etname = '%s' WHERE etid = %d", $form_state['values']['etname'], $form_state['values']['etid']);
410 drupal_set_message(t('The entity type %etname was updated', array('%etname' => $form_state['values']['etname'])));
411 }
412
413 $form_state['redirect'] = '/admin/settings/oi';
414 }
415
416
417 function oi_settings_entity_type_delete ($etid = 0) {
418 if ($etid == 0) {
419 drupal_set_message('You cannot delete a non-existant entity!');
420 drupal_goto('admin/settings/oi');
421 }
422 else {
423 return drupal_get_form('oi_settings_entity_type_delete_form', $etid);
424 }
425 }
426
427 function oi_settings_entity_type_delete_form (&$form_state, $etid) {
428 $etname = db_result(db_query("SELECT etname FROM {oi_entity_types} WHERE etid = %d", $etid));
429
430 //** TODO: put in a check here to make sure that this entity type is not in use!
431 $used = false;
432 if ($used) {
433 drupal_set_message(t('You cannot delete the entity type %etname because it is in use.', array('%etname' => $etname)));
434 drupal_goto('admin/settings/oi');
435 return;
436 }
437
438 $form = array();
439 $form['etid'] = array(
440 '#type' => 'value',
441 '#value' => $etid,
442 );
443 $form['etname'] = array(
444 '#type' => 'value',
445 '#value' => $etname,
446 );
447
448 return confirm_form($form,
449 t('Are you sure you want to delete the entity type %etname', array('%etname' => $etname)),
450 'admin/settings/oi',
451 '',
452 t('Delete'),
453 t('Cancel'));
454 }
455
456 function oi_settings_entity_type_delete_form_submit ($form, &$form_state) {
457 db_query("DELETE FROM {oi_entity_types} WHERE etid = %d", $form_state['values']['etid']);
458 db_query("DELETE FROM {oi_entity_type_roles} WHERE etid = %d", $form_state['values']['etid']);
459
460 drupal_set_message(t('The entity type %etname and all role definitions for that type were deleted.', array('%etname' => $form_state['values']['etname'])));
461
462 $form_state['redirect'] = 'admin/settings/oi';
463 }
464
465 /**
466 * @defgroup roles
467 * @{
468 */
469
470 function _oi_settings_entity_type_role_list ($etid) {
471 if (!isset($etid) or ($etid == '') or ($etid == 0)) {
472 return '';
473 }
474
475 $q = db_query("SELECT * FROM {oi_entity_type_roles} WHERE etid = %d ORDER BY rtweight", $etid);
476
477 $header = array(t('Role'), '', t('Operations'));
478 $rows = array();
479
480 while($r = db_fetch_array($q)) {
481 $rtid = $r['rtid'];
482 $rows[] = array($r['rtdisplay'],
483 $r['rtname'],
484 l(t('edit'), "admin/settings/oi/entity_type_role_edit/$etid/$rtid") . ' | ' . l(t('delete'), "admin/settings/oi/entity_type_role_delete/$etid/$rtid")
485 );
486 }
487
488 if (!count($rows)) {
489 $rows[] = array(array('colspan' => 3, 'data' => '<em>No roles for this entity type</em>'));
490 }
491
492 $output = theme('table', $header, $rows)
493 . '<p>' . l(t('Add new role'), "admin/settings/oi/entity_type_role_edit/$etid") . '</p>';
494
495 return $output;
496 }
497
498
499 function oi_settings_entity_type_role_dispatch ($etid = 0, $rtid = 0) {
500 $entity_type = db_fetch_array(db_query("SELECT * FROM {oi_entity_types} WHERE etid = %d", $etid));
501 if (!isset($entity_type) or !is_array($entity_type)) {
502 drupal_set_message("You must add roles to existing entity types.");
503 drupal_goto('admin/settings/oi');
504 return;
505 }
506 else {
507 return drupal_get_form('oi_settings_entity_type_role_form', $entity_type, $rtid);
508 }
509 }
510
511 function oi_settings_entity_type_role_form (&$form_state, $et, $rtid = 0) {
512
513 $defaults = array(
514 'etid' => $et['etid'],
515 'rtid' => $rtid,
516 'rtname' => '',
517 'rtdisplay' => '',
518 'rtweight' => 0,
519 );
520
521 if ($rtid != 0) {
522 $defaults = db_fetch_array(db_query("SELECT * FROM {oi_entity_type_roles} WHERE rtid = %d", $rtid));
523 }
524
525 $form = array();
526
527 $role_name = 'adding a new role';
528 if ($defaults['rtname'] != '') {
529 $role_name = 'editing role ' . theme('placeholder', $defaults['rtname']);
530 }
531 $form[] = array(
532 '#type' => 'markup',
533 '#value' => $role_name . ' to entity type ' . theme('placeholder', $entity_type['etname']),
534 );
535
536 $form['etid'] = array(
537 '#type' => 'value',
538 '#value' => $defaults['etid'],
539 );
540
541 $form['rtid'] = array(
542 '#type' => 'value',
543 '#value' => $defaults['rtid'],
544 );
545
546 $form['rtname'] = array(
547 '#type' => 'textfield',
548 '#title' => t('Role short name'),
549 '#description' => t('A short (internal) name for the role. No spaces or capitol letters, please.'),
550 '#default_value' => $defaults['rtname'],
551 '#required' => true,
552 );
553
554 $form['rtdisplay'] = array(
555 '#type' => 'textfield',
556 '#title' => t('Role display name'),
557 '#description' => t('A display name for the role.'),
558 '#default_value' => $defaults['rtdisplay'],
559 '#required' => true,
560 );
561
562 $form['rtweight'] = array(
563 '#type' => 'weight',
564 '#title' => 'Role Weight',
565 '#default_value' => $defaults['rtweight'],
566 );
567
568
569 $form[] = array(
570 '#type' => 'submit',
571 '#value' => t('Save entity type role'),
572 );
573
574 return $form;
575 }
576
577 function oi_settings_entity_type_role_form_submit ($form, &$form_state) {
578 if ($form_state['values']['rtid'] == 0) {
579 // new role
580 db_query("INSERT INTO {oi_entity_type_roles} (etid, rtname, rtdisplay, rtweight) VALUES (%d, '%s', '%s', %d)", $form_state['values']['etid'], $form_state['values']['rtname'], $form_state['values']['rtdisplay'], $form_state['values']['rtweight']);
581 drupal_set_message(t('The entity type role was added'));
582 }
583 else {
584 // updating
585 db_query("UPDATE {oi_entity_type_roles} SET rtname = '%s', rtdisplay = '%s', rtweight = %d WHERE rtid = %d", $form_state['values']['rtname'], $form_state['values']['rtdisplay'], $form_state['values']['rtweight'], $form_state['values']['rtid']);
586 drupal_set_message(t('The entity type role was updated'));
587 }
588
589 $form_state['redirect'] = "admin/settings/oi/entity_type_edit/" . $form_state['values']['etid'];
590 }
591
592
593 function oi_settings_entity_type_role_delete ($etid, $rtid) {
594 return drupal_get_form('oi_settings_entity_type_role_delete_form', $etid, $rtid);
595 }
596
597 function oi_settings_entity_type_role_delete_form (&$form_state, $etid, $rtid) {
598 $etname = db_result(db_query("SELECT etname FROM {oi_entity_types} WHERE etid = %d", $etid));
599 $rtname = db_result(db_query("SELECT rtname FROM {oi_entity_type_roles} WHERE rtid = %d", $rtid));
600
601 //** TODO: put a check in here to see if the role is being used
602 $used = false;
603 if ($used) {
604 drupal_set_message(t('You cannot delete the role %rtname because it is in use', array('%rtname' => $rtname)));
605 drupal_goto("admin/settings/oi/entity_type_edit/$etid");
606 return;
607 }
608
609 $form = array();
610 $form['rtid'] = array(
611 '#type' => 'value',
612 '#value' => $rtid,
613 );
614 $form['rtname'] = array(
615 '#type' => 'value',
616 '#value' => $rtname,
617 );
618 $form['etid'] = array(
619 '#type' => 'value',
620 '#value' => $etid,
621 );
622 $form['etname'] = array(
623 '#type' => 'value',
624 '#value' => $etname,
625 );
626
627 return confirm_form($form,
628 t('Are you sure you want to delete the role %role from entity type %type', array('%role' => $rtname, '%type' => $etname)),
629 "admin/settings/oi/entity_type_edit/$etid",
630 '',
631 t('Delete'),
632 t('Cancel'));
633 }
634
635 function oi_settings_entity_type_role_delete_form_submit ($form, &$form_state) {
636 db_query("DELETE FROM {oi_entity_type_roles} WHERE rtid = %d", $form_state['values']['rtid']);
637
638 drupal_set_message(t('The entity type role %role for type %type has been deleted.', array('%role' => $form_state['values']['rtname'], '%type' => $form_state['values']['etname'])));
639 $form_state['redirect'] = "admin/settings/oi/entity_type_edit/" . $form_state['values']['etid'];
640 }
641
642 /**
643 * @}
644 */
645
646
647 /**
648 * @defgroup fields
649 * @{
650 */
651
652 function _oi_settings_entity_type_field_list ($etid) {
653 if (!isset($etid) or ($etid == '') or ($etid == 0)) {
654 return '';
655 }
656
657 $q = db_query("SELECT * FROM {oi_entity_type_fields} WHERE etid = %d ORDER BY ftweight", $etid);
658
659 $header = array(t('Field'), '', '', t('Operations'));
660 $rows = array();
661
662 while($r = db_fetch_array($q)) {
663 $ftid = $r['ftid'];
664 $rows[] = array($r['ftdisplay'],
665 $r['ftname'],
666 $r['fttype'],
667 l(t('edit'), "admin/settings/oi/entity_type_field_edit/$etid/$ftid") . ' | ' . l(t('delete'), "admin/settings/oi/entity_type_field_delete/$etid/$ftid")
668 );
669 }
670
671 if (!count($rows)) {
672 $rows[] = array(array('colspan' => 4, 'data' => '<em>No fields for this entity type</em>'));
673 }
674
675 $output = theme('table', $header, $rows)
676 . '<p>' . l(t('Add new field'), "admin/settings/oi/entity_type_field_edit/$etid") . '</p>';
677
678 return $output;
679 }
680
681
682 function oi_settings_entity_type_field_dispatch ($etid = 0, $ftid = 0) {
683 $entity_type = db_fetch_array(db_query("SELECT * FROM {oi_entity_types} WHERE etid = %d", $etid));
684 if (!isset($entity_type) or !is_array($entity_type)) {
685 drupal_set_message("You must add fields to existing entity types.");
686 drupal_goto('admin/settings/oi');
687 return;
688 }
689 else {
690 return drupal_get_form('oi_settings_entity_type_field_form', $entity_type, $ftid);
691 }
692 }
693
694 function oi_settings_entity_type_field_form (&$form_state, $et, $ftid = 0) {
695
696 $defaults = array(
697 'etid' => $et['etid'],
698 'ftid' => $ftid,
699 'ftname' => '',
700 'ftdisplay' => '',
701 'fttype' => '',
702 'ftparameters' => '',
703 'ftweight' => 0,
704 );
705
706 if ($ftid != 0) {
707 $defaults = db_fetch_array(db_query("SELECT * FROM {oi_entity_type_fields} WHERE ftid = %d", $ftid));
708 }
709
710 $form = array();
711
712 $field_name = 'adding a new field';
713 if ($defaults['ftname'] != '') {
714 $field_name = 'editing field ' . theme('placeholder', $defaults['ftname']);
715 }
716 $form[] = array(
717 '#type' => 'markup',
718 '#value' => $field_name . ' to entity type ' . theme('placeholder', $entity_type['etname']),
719 );
720
721 $form['etid'] = array(
722 '#type' => 'value',
723 '#value' => $defaults['etid'],
724 );
725
726 $form['ftid'] = array(
727 '#type' => 'value',
728 '#value' => $defaults['ftid'],
729 );
730
731 $form['ftname'] = array(
732 '#type' => 'textfield',
733 '#title' => t('field short name'),
734 '#description' => t('A short (internal) name for the field. No spaces or capitol letters, please.'),
735 '#default_value' => $defaults['ftname'],
736 '#required' => true,
737 );
738
739 $form['ftdisplay'] = array(
740 '#type' => 'textfield',
741 '#title' => t('field display name'),
742 '#description' => t('A display name for the field.'),
743 '#default_value' => $defaults['ftdisplay'],
744 '#required' => true,
745 );
746
747 $form['fttype'] = array(
748 '#type' => 'select',
749 '#title' => t('field type'),
750 '#description' => t(''),
751 '#default_value' => $defaults['fttype'],
752 '#options' => array(
753 'text' => 'Text data',
754 'email' => 'Email address',
755 'url' => 'URL',
756 ),
757 );
758
759 $form['ftweight'] = array(
760 '#type' => 'weight',
761 '#title' => 'Field Weight',
762 '#default_value' => $defaults['ftweight'],
763 );
764
765 $form[] = array(
766 '#type' => 'submit',
767 '#value' => t('Save entity type field'),
768 );
769
770 return $form;
771 }
772
773 function oi_settings_entity_type_field_form_submit ($form, &$form_state) {
774 if ($form_state['values']['ftid'] == 0) {
775 // new field
776 db_query("INSERT INTO {oi_entity_type_fields} (etid, ftname, ftdisplay, fttype, ftweight) VALUES (%d, '%s', '%s', '%s', %d)", $form_state['values']['etid'], $form_state['values']['ftname'], $form_state['values']['ftdisplay'], $form_state['values']['fttype'], $form_state['values']['ftweight']);
777 drupal_set_message(t('The entity type field was added'));
778 }
779 else {
780 // updating
781 db_query("UPDATE {oi_entity_type_fields} SET ftname = '%s', ftdisplay = '%s', fttype = '%s', ftweight = %d WHERE ftid = %d", $form_state['values']['ftname'], $form_state['values']['ftdisplay'], $form_state['values']['fttype'], $form_state['values']['ftweight'], $form_state['values']['ftid']);
782 drupal_set_message(t('The entity type field was updated'));
783 }
784
785 $form_state['redirect'] = "admin/settings/oi/entity_type_edit/" . $form_state['values']['etid'];
786 }
787
788 function oi_settings_entity_type_field_delete ($etid, $ftid) {
789 return drupal_get_form('oi_settings_entity_type_field_delete_form', $etid, $ftid);
790 }
791
792 function oi_settings_entity_type_field_delete_form (&$form_state, $etid, $ftid) {
793 $ftname = db_result(db_query("SELECT ftname FROM {oi_entity_type_fields} WHERE ftid = %d", $ftid));
794 $etname = db_result(db_query("SELECT etname FROM {oi_entity_types} WHERE etid = %d", $etid));
795
796 //** TODO: put a check in here to see if the field is being used
797 $used = false;
798 if ($used) {
799 drupal_set_message(t('You cannot delete the field %ftname because it is in use', array('%ftname' => $ftname)));
800 drupal_goto("admin/settings/oi/entity_type_edit/$etid");
801 return;
802 }
803
804 $form = array();
805 $form['ftid'] = array(
806 '#type' => 'value',
807 '#value' => $ftid,
808 );
809 $form['ftname'] = array(
810 '#type' => 'value',
811 '#value' => $ftname,
812 );
813 $form['etid'] = array(
814 '#type' => 'value',
815 '#value' => $etid,
816 );
817 $form['etname'] = array(
818 '#type' => 'value',
819 '#value' => $etname,
820 );
821
822 return confirm_form($form,
823 t('Are you sure you want to delete the field %field from entity type %type', array('%field' => $ftname, '%type' => $etname)),
824 "admin/settings/oi/entity_type_edit/$etid",
825 '',
826 t('Delete'),
827 t('Cancel'));
828 }
829
830 function oi_settings_entity_type_field_delete_form_submit ($form, &$form_state) {
831 db_query("DELETE FROM {oi_entity_type_fields} WHERE ftid = %d", $form_state['values']['ftid']);
832
833 drupal_set_message(t('The entity type field %field for type %type has been deleted.', array('%field' => $form_state['values']['ftname'], '%type' => $form_state['values']['etname'])));
834 $form_state['redirect'] = "admin/settings/oi/entity_type_edit/" . $form_state['values']['etid'];
835 }
836
837 /**
838 * @}
839 */
840
841
842
843 /* Parent and recursive function to get entity types tree.
844 * the database has fields to allow hierarchical entity types, however
845 * I'm not sure that it's actually worth the bother of implementing them
846 */
847
848 function _oi_get_entity_types () {
849 $return = array();
850
851 $e_q = db_query("SELECT * FROM {oi_entity_types}");
852 while($e_r = db_fetch_array($e_q)) {
853 $return['types'][$e_r['etid']] = array(
854 'etid' => $e_r['etid'],
855 'etname' => $e_r['etname'],
856 'parent_etid' => $e_r['parent_etid'],
857 );
858 }
859
860 $return['tree'] = _oi_get_entity_types_tree();
861
862 return $return;
863 }
864
865 function _oi_get_entity_types_tree ($parent = 0) {
866 $e_q = db_query("SELECT etid FROM {oi_entity_types} WHERE parent_etid = %d", $parent);
867 $ret = array();
868 while($e_r = db_fetch_array($e_q)) {
869 $ret[$e_r['etid']] = _oi_get_entity_types_tree($e_r['etid']);
870 }
871 return $ret;
872 }
873
874
875 /* Parent and recursive function to create a "select" options array of entity types.
876 * this array is flat, but tries to indicate the hierarchy by prefixing
877 * the names of items with dashes. This would be used to create a pop-up menu
878 * of the entity types for a form item.
879 */
880
881 function oi_entity_types_select_array ($none = '<none>') {
882 $data = _oi_get_entity_types();
883 $select = array(0 => $none);
884 _oi_entity_types_select_array_recurse($data['tree'], $data['types'], $select);
885
886 return $select;
887 }
888
889 function _oi_entity_types_select_array_recurse ($tree, $data, &$select, $count = 0) {
890 foreach ($tree as $etid => $children) {
891 $select[$etid] = _oi_get_dashes($count) . ' ' . $data[$etid]['etname'];
892 _oi_entity_types_select_array_recurse($children, $data, $select, $count+1);
893 }
894 }
895
896
897 /**
898 * @defgroup entities
899 * @{
900 */
901
902 function oi_entities_list () {
903 $entities = _oi_get_entities();
904
905 $header = array(
906 t('Entities'),
907 t('Operations'),
908 );
909
910 $rows = array();
911 _oi_entities_list_recurse_rows($entities['tree'], $entities['entities'], $rows);
912
913 if (count($rows) == 0) {
914 $rows[] = array(array('colspan' => 2, 'data' => t('No entities defined')));
915 }
916
917 $output = theme('table', $header, $rows, array('class' => 'oi-entity-table'))
918 . '<p>' . l(t('Add new entity'), "admin/user/oi/entity_add_edit") . '</p>';
919
920 return $output;
921
922 }
923
924 function _oi_entities_list_recurse_rows ($tree, $info, &$rows, $depth = 0) {
925 foreach ($tree as $eid => $children) {
926 $rows[] = array(
927 _oi_get_dashes($depth, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
928 . $info[$eid]['display'],
929 l(t('edit data'), "admin/user/oi/entity_data_edit/$eid") . ' | ' .
930 l(t('edit entity'), "admin/user/oi/entity_add_edit/$eid") . ' | ' .
931 l(t('delete'), "admin/user/oi/entity_delete/$eid")
932 );
933 _oi_entities_list_recurse_rows($children, $info, $rows, $depth + 1);
934 }
935 }
936
937 function oi_entities_form_dispatch ($eid = 0) {
938 return drupal_get_form('oi_entities_form', $eid);
939 }
940
941 function oi_entities_form (&$form_state, $eid = 0) {
942 $defaults = array(
943 'eid' => 0,
944 'etid' => null,
945 'name' => null,
946 'display' => null,
947 'parent_eid' => 0,
948 'propagate_membership' => 1,
949 'weight' => 0,
950 );
951
952 if ($eid != 0) {
953 $defaults = db_fetch_array(db_query("SELECT * FROM {oi_entities} WHERE eid = %d", $eid));
954 }
955
956 $form = array();
957
958 $form['eid'] = array(
959 '#type' => 'value',
960 '#value' => $eid,
961 );
962 $form['name'] = array(
963 '#type' => 'textfield',
964 '#title' => t('Internal entity name'),
965 '#description' => t('A short name for the entity. This is used for internal purposes, and should be lowercase letters, numbers, and underscores only.'),
966 '#default_value' => $defaults['name'],
967 '#maxlength' => 100,
968 '#required' => true,
969 );
970
971 $form['display'] = array(
972 '#type' => 'textfield',
973 '#title' => t('Display entity name'),
974 '#description' => t('A display name for the entity. This may contain spaces, capitol letters, punctuation, etc...'),
975 '#default_value' => $defaults['display'],
976 '#required' => true,
977 );
978
979 if ($defaults['etid'] == null) {
980 $form['etid'] = array(
981 '#type' => 'select',
982 '#title' => t('Entity type'),
983 '#description' => t('What type of entity this is.'),
984 '#default_value' => $defaults['etid'],
985 '#options' => oi_entity_types_select_array(''),
986 '#required' => true,
987 );
988 }
989 else {
990 $form['etid'] = array(
991 '#type' => 'value',
992 '#value' => $defaults['etid'],
993 );
994 $form[] = array(
995 '#type' => 'markup',
996 '#value' => '<p>' . t('This entity is of type %type', array('%type' => db_result(db_query('SELECT etname FROM {oi_entity_types} WHERE etid = %d', $defaults['etid'])))) . '</p>',
997 );
998 }
999
1000 $form['parent_eid'] = array(
1001 '#type' => 'select',
1002 '#title' => t('Parent entity'),
1003 '#default_value' => $defaults['parent_eid'],
1004 '#options' => oi_entities_select_array('<root>'),
1005 '#required' => true,
1006 );
1007
1008 $form['propagate_membership'] = array(
1009 '#type' => 'checkbox',
1010 '#title' => t('Propagate membership up the tree'),
1011 '#default_value' => $defaults['propagate_membership'],
1012 '#description' => t("If you don't know what this setting does, you should probably leave it alone"),
1013 );
1014
1015 $form['weight'] = array(
1016 '#type' => 'weight',
1017 '#title' => t('Weight'),
1018 '#default_value' => $defaults['weight'],
1019 '#delta' => 30,
1020 );
1021
1022 $form[] = array(
1023 '#type' => 'submit',
1024 '#value' => 'Save entity',
1025 );
1026
1027 return $form;
1028 }
1029
1030 function oi_entities_form_validate ($form, &$form_state) {
1031 if ($form_state['values']['etid'] == 0) {
1032 form_set_error('etid', 'You must supply an entity type for this entity');
1033 }
1034 }
1035
1036 function oi_entities_form_submit ($form, &$form_state) {
1037 if (!isset($form_state['values']['eid']) or ($form_state['values']['eid'] == 0)) {
1038 // new record
1039 db_query("INSERT INTO {oi_entities} (etid, name, display, parent_eid, propagate_membership, weight) values (%d, '%s', '%s', %d, %d, %d)",
1040 $form_state['values']['etid'], $form_state['values']['name'], $form_state['values']['display'], $form_state['values']['parent_eid'], $form_state['values']['propagate_membership'], $form_state['values']['weight']);
1041 drupal_set_message(t('The entity %display was created.', array('%display' => $form_state['values']['display'])));
1042 }
1043 else {
1044 // updating
1045 db_query("UPDATE {oi_entities} SET name = '%s', display = '%s', parent_eid = %d, propagate_membership = %d, weight = %d WHERE eid = %d",
1046 $form_state['values']['name'], $form_state['values']['display'], $form_state['values']['parent_eid'], $form_state['values']['propagate_membership'], $form_state['values']['weight'], $form_state['values']['eid']);
1047 drupal_set_message(t('The entity %display was updated.', array('%display' => $form_state['values']['display'])));
1048 }
1049
1050 $form_state['redirect'] = 'admin/user/oi';
1051 }
1052
1053 function oi_entities_delete_dispatch ($eid) {
1054 return drupal_get_form('oi_entities_delete_form', $eid);
1055 }
1056
1057 function oi_entities_delete_form (&$form_state, $eid) {
1058 $form = array();
1059
1060 $display = db_result(db_query("SELECT display FROM {oi_entities} WHERE eid = %d", $eid));
1061
1062 $form['eid'] = array(
1063 '#type' => 'value',
1064 '#value' => $eid,
1065 );
1066 $form['display'] = array(
1067 '#type' => 'value',
1068 '#value' => $display,
1069 );
1070
1071 return confirm_form($form,
1072 t('Are you sure you want to delete the entity %display and all of the information associated with it?',
1073 array('%display' => $display)),
1074 'admin/user/oi',
1075 t('This will delete the entity, any field information for that entity, and remove any users from the entity. If there is any content which is restricted to the view of this entity, it will be set to the default restriction entity.'),
1076 t('Delete'),
1077 t('Cancel'));
1078 }
1079
1080 function oi_entities_delete_form_submit ($form, &$form_state) {
1081 db_query("DELETE FROM {oi_entities} WHERE eid = %d", $form_state['values']['eid']);
1082 db_query("DELETE FROM {oi_entity_roles} WHERE eid = %d", $form_state['values']['eid']);
1083 db_query("DELETE FROM {oi_entity_fields} WHERE eid = %d", $form_state['values']['eid']);
1084 // more deletes here...
1085 // need to do recursive delete?
1086
1087 drupal_set_message(t('The entity %display and associted information has been deleted.',
1088 array('%display' => $form_state['values']['display'])));
1089 $form_state['redirect'] = 'admin/user/oi';
1090 }
1091
1092
1093 function oi_entities_data_edit ($eid) {
1094 $output = '';
1095
1096 $entity = db_fetch_array(db_query("SELECT * FROM {oi_entities} WHERE eid = %d", $eid));
1097
1098 $output .= _oi_entities_data_edit_get_roles_table($entity);
1099 $output .= _oi_entities_data_edit_get_fields_table($entity);
1100
1101 return $output;
1102 }
1103
1104 function _oi_entities_data_edit_get_roles_table ($entity) {
1105 // get a list of all the roles for this entity
1106
1107 $roles = array();
1108 $roles_q = db_query("SELECT * FROM {oi_entity_type_roles} WHERE etid = %d ORDER BY rtweight", $entity['etid']);
1109 while($r_a = db_fetch_array($roles_q)) {
1110 $roles[$r_a['rtid']] = $r_a['rtdisplay'];
1111 }
1112
1113 if (count($roles) == 0) {
1114 // there are no roles for this entity type
1115 return theme('box', t('Roles for %entity', array('%entity' => $entity['display'])),
1116 '<p>There are no roles defined for this entity type</p>');
1117 }
1118
1119 $header = array(
1120 t('Role'),
1121 t('User(s)'),
1122 ''
1123 );
1124
1125 $rows = array();
1126
1127 foreach ($roles as $rtid => $rtdisplay) {
1128 // get a list of all the existing users
1129 $uid_q = db_query("SELECT uid FROM {oi_entity_roles} WHERE eid = %d and rtid = %d", $entity['eid'], $rtid);
1130 $uids = o