workflow_access.pages.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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'] = WORKFLOW_ADMIN_UI_PATH;
  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 $workflow) {
  45. // If we don't have a workflow that goes with this, return to the admin pg.
  46. if (!is_object($workflow)) {
  47. drupal_set_message(t('Improper worklow ID provided.'), 'error');
  48. drupal_goto(WORKFLOW_ADMIN_UI_PATH);
  49. }
  50. // @todo: Let's get a better page title.
  51. drupal_set_title(t('@name Access', array('@name' => $workflow->label())));
  52. $form['#tree'] = TRUE;
  53. $form['#wid'] = $wid = $workflow->getWorkflowId();
  54. // A list of role names keyed by role ID, including the 'author' role.
  55. $roles = workflow_get_roles('participate in workflow');
  56. // Add a table for every workflow state.
  57. foreach ($workflow->getStates($all = TRUE) as $state) {
  58. if ($state->isCreationState()) {
  59. // No need to set perms on creation.
  60. continue;
  61. }
  62. $view = $update = $delete = array();
  63. $count = 0;
  64. foreach (workflow_access_get_workflow_access_by_sid($state->sid) as $access) {
  65. $count++;
  66. if ($access->grant_view) {
  67. $view[] = $access->rid;
  68. }
  69. if ($access->grant_update) {
  70. $update[] = $access->rid;
  71. }
  72. if ($access->grant_delete) {
  73. $delete[] = $access->rid;
  74. }
  75. }
  76. // Allow view grants by default for anonymous and authenticated users,
  77. // if no grants were set up earlier.
  78. if (!$count) {
  79. $view = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);
  80. }
  81. // @todo: better tables using a #theme function instead of direct #prefixing.
  82. $form[$state->sid] = array(
  83. '#type' => 'fieldset',
  84. '#title' => $state->label(),
  85. '#collapsible' => TRUE,
  86. '#collapsed' => FALSE,
  87. '#tree' => TRUE,
  88. );
  89. $form[$state->sid]['view'] = array(
  90. '#type' => 'checkboxes',
  91. '#options' => $roles,
  92. '#default_value' => $view,
  93. '#title' => t('Roles who can view posts in this state'),
  94. '#prefix' => '<table width="100%" style="border: 0;"><tbody style="border: 0;"><tr><td>',
  95. );
  96. $form[$state->sid]['update'] = array(
  97. '#type' => 'checkboxes',
  98. '#options' => $roles,
  99. '#default_value' => $update,
  100. '#title' => t('Roles who can edit posts in this state'),
  101. '#prefix' => "</td><td>",
  102. );
  103. $form[$state->sid]['delete'] = array(
  104. '#type' => 'checkboxes',
  105. '#options' => $roles,
  106. '#default_value' => $delete,
  107. '#title' => t('Roles who can delete posts in this state'),
  108. '#prefix' => "</td><td>",
  109. '#suffix' => "</td></tr></tbody></table>",
  110. );
  111. }
  112. $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  113. return $form;
  114. }
  115. /**
  116. * Stores permission settings for workflow states.
  117. */
  118. function workflow_access_form_submit($form, &$form_state) {
  119. $wid = $form['#wid'];
  120. foreach ($form_state['values'] as $sid => $access) {
  121. // Ignore irrelevant keys.
  122. if (!is_numeric($sid)) {
  123. continue;
  124. }
  125. foreach ($access['view'] as $rid => $checked) {
  126. $data = array(
  127. 'sid' => $sid,
  128. 'rid' => $rid,
  129. 'grant_view' => (!empty($checked)) ? (bool) $checked : 0,
  130. 'grant_update' => (!empty($access['update'][$rid])) ? (bool) $access['update'][$rid] : 0,
  131. 'grant_delete' => (!empty($access['delete'][$rid])) ? (bool) $access['delete'][$rid] : 0,
  132. );
  133. workflow_access_insert_workflow_access_by_sid($data);
  134. }
  135. // Update all nodes having same workflow state to reflect new settings.
  136. // just set a flag, which is working for both Workflow Field ánd Workflow Node.
  137. node_access_needs_rebuild(TRUE);
  138. }
  139. drupal_set_message(t('Workflow access permissions updated.'));
  140. $form_state['redirect'] = WORKFLOW_ADMIN_UI_PATH;
  141. }