fpa.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. function fpa_menu() {
  3. $items = array();
  4. $items['fpa_modal/%fpa_js/permissions'] = array(
  5. 'title' => 'Permissions',
  6. 'page callback' => '_fpa_ctools',
  7. 'page arguments' => array(1),
  8. 'access callback' => TRUE,
  9. 'type' => MENU_CALLBACK,
  10. );
  11. return $items;
  12. }
  13. function fpa_admin_paths() {
  14. return array(
  15. 'fpa_modal/*/permissions' => FALSE,
  16. );
  17. }
  18. function fpa_js_load($arg) {
  19. if (module_exists('ctools')) {
  20. return ctools_js_load($arg);
  21. }
  22. return 0;
  23. }
  24. function fpa_l($permission = '', $text = NULL, $options = array()) {
  25. // available modal modules in order of priority
  26. $supported_modules = array(
  27. //'dialog', // dialog api is buggy
  28. 'ctools',
  29. 'modalframe',
  30. );
  31. if (is_null($text)) {
  32. $text = t('Manage Permissions');
  33. }
  34. // gets the most prefered modal method from the available methods
  35. $modules = array_intersect($supported_modules, array_keys(module_list()));
  36. $method = array_shift($modules);
  37. $path = 'fpa_modal/nojs/permissions';
  38. $attributes = array();
  39. $query = array('fpa_perm' => $permission, 'fpa_method' => $method);
  40. switch ($method) {
  41. case 'dialog':
  42. dialog_add_js();
  43. $attributes['class'] = 'ctools-use-dialog';
  44. break;
  45. case 'ctools':
  46. ctools_include('ajax');
  47. ctools_include('modal');
  48. ctools_modal_add_js();
  49. $attributes['class'] = 'ctools-use-modal';
  50. break;
  51. case 'modalframe':
  52. modalframe_parent_js();
  53. drupal_add_js(drupal_get_path('module', 'fpa') . '/js/fpa.js');
  54. $attributes['class'] = 'fpa_modalframe';
  55. break;
  56. default:
  57. $path = 'admin/people/permissions';
  58. $attributes['target'] = '_blank';
  59. $query['fpa_method'] = '_blank';
  60. break;
  61. }
  62. $options = array_merge_recursive(array('query' => $query, 'attributes' => $attributes), $options);
  63. return l($text, $path, $options);
  64. }
  65. function fpa_fieldset($permission, &$parent_item, $group = array()) {
  66. $parent_item['fpa_fieldset'] = array(
  67. '#type' => 'fieldset',
  68. '#title' => t('Permissions'),
  69. '#collapsible' => TRUE,
  70. '#collapsed' => TRUE,
  71. '#description' => t('Clicking on this link will launch a modal/tab/window displaying the appropriate permissions.'),
  72. );
  73. $parent_item['fpa_fieldset'] = array_merge_recursive($parent_item['fpa_fieldset'], $group);
  74. $parent_item['fpa_fieldset']['fpa'] = array(
  75. '#type' => 'markup',
  76. '#markup' => fpa_l($permission),
  77. );
  78. }
  79. function fpa_form_node_type_form_alter(&$form, &$form_state) {
  80. if (!empty($form['type']['#default_value']) && user_access('administer permissions')) {
  81. fpa_fieldset($form['#node_type']->type . ':', $form, array('#group' => 'additional_settings'));
  82. }
  83. }
  84. function fpa_form_user_admin_permissions_alter(&$form, &$form_state) {
  85. if (isset($_GET['fpa_method']) && $_GET['fpa_method'] == 'modalframe') {
  86. modalframe_child_js();
  87. $form['#submit'][] = '_fpa_modalframe_submit';
  88. }
  89. $fpa_module_path = drupal_get_path('module', 'fpa');
  90. drupal_add_css($fpa_module_path .'/css/fpa.css');
  91. drupal_add_js($fpa_module_path .'/js/fpa.js');
  92. drupal_add_js(array('fpa' => array('perm' => isset($_GET['fpa_perm']) && $_GET['fpa_perm'] ? check_plain($_GET['fpa_perm']) : '')), array('type' => 'setting'));
  93. }
  94. function _fpa_modalframe_submit() {
  95. modalframe_close_dialog();
  96. }
  97. function _fpa_ctools($js = NULL) {
  98. // Need to include the file that contains the permissions form.
  99. module_load_include('inc', 'user', 'user.admin');
  100. // Fall back if $js is not set.
  101. if (!$js) {
  102. return drupal_get_form('user_admin_permissions');
  103. }
  104. ctools_include('modal');
  105. ctools_include('ajax');
  106. $form_state = array(
  107. 'title' => t('Permissions'),
  108. 'ajax' => TRUE,
  109. );
  110. switch ($_GET['fpa_method']) {
  111. case 'dialog':
  112. $output = dialog_get_form('user_admin_permissions', $form_state);
  113. break;
  114. case 'ctools':
  115. $output = ctools_modal_form_wrapper('user_admin_permissions', $form_state);
  116. break;
  117. }
  118. if (!empty($form_state['executed'])) {
  119. $output = array();
  120. $output[] = ctools_ajax_command_reload();
  121. }
  122. echo ajax_render($output);
  123. exit;
  124. }