workflow_access.pages.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Provides pages for administrative UI.
  5. */
  6. /**
  7. * Implements hook_form().
  8. *
  9. * Add a form to set the weight fo the access module.
  10. */
  11. function workflow_access_priority_form($form, $form_state) {
  12. $form['workflow_access'] = array(
  13. '#type' => 'fieldset',
  14. '#title' => t('Workflow Access Settings'),
  15. '#collapsible' => FALSE,
  16. '#collapsed' => FALSE,
  17. );
  18. $form['workflow_access']['#tree'] = TRUE;
  19. $form['workflow_access']['workflow_access_priority'] = array(
  20. '#type' => 'weight',
  21. '#delta' => 10,
  22. '#title' => t('Workflow Access Priority'),
  23. '#default_value' => variable_get('workflow_access_priority', 0),
  24. '#description' => t('This sets the node access priority. Changing this
  25. setting can be dangerous. If there is any doubt, leave it at 0.
  26. <a href="@url">Read the manual.</a>', array('@url' => url('https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_access_records/7'))),
  27. );
  28. $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  29. return $form;
  30. }
  31. /**
  32. * Submit handler.
  33. */
  34. function workflow_access_priority_form_submit($form, &$form_state) {
  35. variable_set('workflow_access_priority', $form_state['values']['workflow_access']['workflow_access_priority']);
  36. $form_state['redirect'] = 'admin/config/workflow/workflow';
  37. }
  38. /**
  39. * Implements hook_form().
  40. *
  41. * Add a "three dimensional" (state, role, permission type) configuration
  42. * interface to the workflow edit form.
  43. */
  44. function workflow_access_form($form, $form_state, $workflow, $op) {
  45. if (!$workflow) {
  46. // Leave this page immediately.
  47. drupal_set_message(t('Unknown workflow'));
  48. drupal_goto('admin/config/workflow/workflow');
  49. }
  50. drupal_set_title(t('@name Access', array('@name' => $workflow->label()))); // No t() for Settings page.
  51. $form = array('#tree' => TRUE);
  52. $form['#wid'] = $workflow->wid;
  53. // A list of role names keyed by role ID, including the 'author' role.
  54. $roles = workflow_get_roles('participate in workflow');
  55. // Add a table for every workflow state.
  56. foreach ($workflow->getStates($all = TRUE) as $state) {
  57. if ($state->isCreationState()) {
  58. // No need to set perms on creation.
  59. continue;
  60. }
  61. $view = $update = $delete = array();
  62. $count = 0;
  63. foreach (workflow_access_get_workflow_access_by_sid($state->sid) as $access) {
  64. $count++;
  65. if ($access->grant_view) {
  66. $view[] = $access->rid;
  67. }
  68. if ($access->grant_update) {
  69. $update[] = $access->rid;
  70. }
  71. if ($access->grant_delete) {
  72. $delete[] = $access->rid;
  73. }
  74. }
  75. // Allow view grants by default for anonymous and authenticated users,
  76. // if no grants were set up earlier.
  77. if (!$count) {
  78. $view = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);
  79. }
  80. // @todo: better tables using a #theme function instead of direct #prefixing.
  81. $form[$state->sid] = array(
  82. '#type' => 'fieldset',
  83. '#title' => check_plain($state->label()),
  84. '#collapsible' => TRUE,
  85. '#collapsed' => FALSE,
  86. '#tree' => TRUE,
  87. );
  88. $form[$state->sid]['view'] = array(
  89. '#type' => 'checkboxes',
  90. '#options' => $roles,
  91. '#default_value' => $view,
  92. '#title' => t('Roles who can view posts in this state'),
  93. '#prefix' => '<table width="100%" style="border: 0;"><tbody style="border: 0;"><tr><td>',
  94. );
  95. $form[$state->sid]['update'] = array(
  96. '#type' => 'checkboxes',
  97. '#options' => $roles,
  98. '#default_value' => $update,
  99. '#title' => t('Roles who can edit posts in this state'),
  100. '#prefix' => "</td><td>",
  101. );
  102. $form[$state->sid]['delete'] = array(
  103. '#type' => 'checkboxes',
  104. '#options' => $roles,
  105. '#default_value' => $delete,
  106. '#title' => t('Roles who can delete posts in this state'),
  107. '#prefix' => "</td><td>",
  108. '#suffix' => "</td></tr></tbody></table>",
  109. );
  110. }
  111. $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  112. return $form;
  113. }
  114. /**
  115. * Stores permission settings for workflow states.
  116. */
  117. function workflow_access_form_submit($form, &$form_state) {
  118. foreach ($form_state['values'] as $sid => $access) {
  119. // Ignore irrelevant keys.
  120. if (!is_numeric($sid)) {
  121. continue;
  122. }
  123. foreach ($access['view'] as $rid => $checked) {
  124. $data = array(
  125. 'sid' => $sid,
  126. 'rid' => $rid,
  127. 'grant_view' => (!empty($checked)) ? (bool) $checked : 0,
  128. 'grant_update' => (!empty($access['update'][$rid])) ? (bool) $access['update'][$rid] : 0,
  129. 'grant_delete' => (!empty($access['delete'][$rid])) ? (bool) $access['delete'][$rid] : 0,
  130. );
  131. workflow_access_insert_workflow_access_by_sid($data);
  132. }
  133. // Update all nodes having same workflow state to reflect new settings.
  134. // just set a flag, which is working for both Workflow Field ánd Workflow Node.
  135. node_access_needs_rebuild(TRUE);
  136. }
  137. drupal_set_message(t('Workflow access permissions updated.'));
  138. $form_state['redirect'] = 'admin/config/workflow/workflow/' . $form['#wid'];
  139. }