You are here

function workflow_access_form in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow_access/workflow_access.module \workflow_access_form()

Implements hook_form().

Add a "three dimensional" (state, role, permission type) configuration interface to the workflow edit form.

1 string reference to 'workflow_access_form'
workflow_access_menu in workflow_access/workflow_access.module
Implements hook_menu().

File

workflow_access/workflow_access.pages.inc, line 49
Provides pages for administrative UI.

Code

function workflow_access_form($form, $form_state, Workflow $workflow) {

  // If we don't have a workflow that goes with this, return to the admin pg.
  if (!is_object($workflow)) {
    drupal_set_message(t('Improper worklow ID provided.'), 'error');
    drupal_goto(WORKFLOW_ADMIN_UI_PATH);
  }

  // @todo: Let's get a better page title.
  drupal_set_title(t('@name Access', array(
    '@name' => $workflow
      ->label(),
  )));
  $form['#tree'] = TRUE;
  $form['#wid'] = $wid = $workflow
    ->getWorkflowId();

  // A list of role names keyed by role ID, including the 'author' role.
  $roles = workflow_get_roles('participate in workflow');

  // Add a table for every workflow state.
  foreach ($workflow
    ->getStates($all = TRUE) as $state) {
    if ($state
      ->isCreationState()) {

      // No need to set perms on creation.
      continue;
    }
    $view = $update = $delete = array();
    $count = 0;
    foreach (workflow_access_get_workflow_access_by_sid($state->sid) as $access) {
      $count++;
      if ($access->grant_view) {
        $view[] = $access->rid;
      }
      if ($access->grant_update) {
        $update[] = $access->rid;
      }
      if ($access->grant_delete) {
        $delete[] = $access->rid;
      }
    }

    // Allow view grants by default for anonymous and authenticated users,
    // if no grants were set up earlier.
    if (!$count) {
      $view = array(
        DRUPAL_ANONYMOUS_RID,
        DRUPAL_AUTHENTICATED_RID,
      );
    }

    // @todo: better tables using a #theme function instead of direct #prefixing.
    $form[$state->sid] = array(
      '#type' => 'fieldset',
      '#title' => $state
        ->label(),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    $form[$state->sid]['view'] = array(
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#default_value' => $view,
      '#title' => t('Roles who can view posts in this state'),
      '#prefix' => '<table width="100%" style="border: 0;"><tbody style="border: 0;"><tr><td>',
    );
    $form[$state->sid]['update'] = array(
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#default_value' => $update,
      '#title' => t('Roles who can edit posts in this state'),
      '#prefix' => "</td><td>",
    );
    $form[$state->sid]['delete'] = array(
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#default_value' => $delete,
      '#title' => t('Roles who can delete posts in this state'),
      '#prefix' => "</td><td>",
      '#suffix' => "</td></tr></tbody></table>",
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  return $form;
}