You are here

function workflow_access_form_alter in Workflow 5.2

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

Implementation of hook_form_alter().

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

File

./workflow_access.module, line 88
Provides node access permissions based on workflow states.

Code

function workflow_access_form_alter($form_id, &$form) {

  // We only work with the workflow edit form.
  if ($form_id != 'workflow_edit_form') {
    return;
  }

  // A list of roles available on the site and our
  // special -1 role used to represent the node author.
  // TODO i think there is an API call for this -- user_roles() perhaps?
  $rids = array(
    '-1' => t('author'),
  );
  $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  while ($obj = db_fetch_object($result)) {
    $rids[$obj->rid] = check_plain($obj->name);
  }
  $form['workflow_access'] = array(
    '#type' => 'fieldset',
    '#title' => t('Access control'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );

  // Add a table for every workflow state.
  $states = workflow_get_states($form['wid']['#value']);
  foreach ($states as $sid => $state) {
    if (workflow_is_system_state($state)) {
      continue;

      // no need to set perms on creation
    }
    $view = $update = $delete = array();
    $result = db_query("SELECT * from {workflow_access} where sid = %d", $sid);

    // Allow view grants by default for anonymous and authenticated users,
    // if no grants were set up earlier.
    if (db_num_rows($result) == 0) {
      $view = array(
        DRUPAL_ANONYMOUS_RID,
        DRUPAL_AUTHENTICATED_RID,
      );
    }
    while ($access = db_fetch_object($result)) {
      if ($access->grant_view) {
        $view[] = $access->rid;
      }
      if ($access->grant_update) {
        $update[] = $access->rid;
      }
      if ($access->grant_delete) {
        $delete[] = $access->rid;
      }
    }

    // TODO better tables using a #theme function instead of direct #prefixing
    $form['workflow_access'][$sid] = array(
      '#type' => 'fieldset',
      '#title' => check_plain(t($state)),
      '#collapsible' => TRUE,
      '#tree' => TRUE,
    );
    $form['workflow_access'][$sid]['view'] = array(
      '#type' => 'checkboxes',
      '#options' => $rids,
      '#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['workflow_access'][$sid]['update'] = array(
      '#type' => 'checkboxes',
      '#options' => $rids,
      '#default_value' => $update,
      '#title' => t('Roles who can edit posts in this state'),
      '#prefix' => "</td><td>",
    );
    $form['workflow_access'][$sid]['delete'] = array(
      '#type' => 'checkboxes',
      '#options' => $rids,
      '#default_value' => $delete,
      '#title' => t('Roles who can delete posts in this state'),
      '#prefix' => "</td><td>",
      '#suffix' => "</td></tr></tbody></table>",
    );
  }

  // Place our block comfortably down the page.
  $form['submit']['#weight'] = 10;
  $form['#submit'] += array(
    'workflow_access_form_submit' => array(),
  );
}