You are here

function workbench_access_role_form in Workbench Access 7

Generate a role overview form for a section.

Parameters

$access_type: The type of access requested (e.g.g taxonomy).

$access_type_id: The id for this specific access (here, a taxnomy term tid).

Return value

A form.

1 string reference to 'workbench_access_role_form'
workbench_access_roles in ./workbench_access.admin.inc
Display the roles for a section.

File

./workbench_access.admin.inc, line 780
Workbench Access admin file.

Code

function workbench_access_role_form($form, &$form_state, $access_type, $access_type_id) {

  // Set proper breadcrumb trails.
  $breadcrumb[] = l(t('Roles'), 'admin/config/workbench/access/roles');
  workbench_access_breadcrumb($breadcrumb);
  $active = workbench_access_get_active_tree();
  $active['access_scheme']['access_id'] = $access_type_id;
  if ($active['access_scheme']['access_type'] != $access_type || !isset($active['active'][$access_type_id])) {
    drupal_access_denied();
    drupal_exit();
  }

  // Build the list of user roles that can be assigned workbench access.
  $roles = workbench_access_get_roles('access workbench access by role');

  // Form markup elements.
  $access_info = workbench_access_load($access_type, $access_type_id);
  $output = '<h2>' . t('%name editors by role', array(
    '%name' => $access_info['name'],
  )) . '</h2>';
  $output .= '<p>' . t('Active editors for the %section section, as determined by role. <a href="!url">View editors by account</a>.', array(
    '%section' => $access_info['name'],
    '!url' => url('admin/config/workbench/access/editors/' . $active['access_scheme']['access_type'] . '/' . $access_type_id),
  ));
  $header = array(
    t('Editors'),
    t('Roles'),
  );
  $rows = array();
  $access_rids = array();
  if (!empty($roles)) {
    $access_rids = db_query("SELECT war.rid FROM {workbench_access_role} war WHERE war.rid IN (:rids) AND war.access_scheme = :access_scheme AND war.access_id = :access_type_id", array(
      ':rids' => array_keys($roles),
      ':access_scheme' => $access_type,
      ':access_type_id' => $access_type_id,
    ))
      ->fetchAllAssoc('rid');
  }
  $users = array();
  if (!empty($access_rids)) {
    if (!isset($access_rids[DRUPAL_AUTHENTICATED_RID])) {
      $users = db_query("SELECT u.name, u.uid, r.name AS roles FROM {users} u INNER JOIN {users_roles} ur ON ur.uid = u.uid LEFT JOIN {role} r ON r.rid = ur.rid WHERE ur.rid IN (:rids) AND u.status > 0", array(
        ':rids' => array_keys($access_rids),
      ));
    }
    else {
      $uids = db_query("SELECT u.uid FROM {users} u WHERE u.status > 0 AND u.uid > 0")
        ->fetchCol();
      $users = user_load_multiple($uids);
    }
  }
  $users_by_role = array();
  foreach ($users as $data) {
    $users_by_role[$data->uid]['name'] = format_username($data);
    if (is_array($data->roles)) {
      array_walk($data->roles, 'check_plain');
      $users_by_role[$data->uid]['roles'] = array_values($data->roles);
    }
    else {
      $users_by_role[$data->uid]['roles'][] = check_plain($data->roles);
    }
  }

  // Add anonymous users, if selected.
  if (isset($access_rids[DRUPAL_ANONYMOUS_RID])) {
    $users_by_role[0] = array(
      'name' => variable_get('anonymous', t('Anonymous')),
      'roles' => array(
        'anonymous user',
      ),
    );
  }
  foreach ($users_by_role as $uid => $item) {
    $rows[] = array(
      l($item['name'], 'user/' . $uid),
      theme('item_list', array(
        'items' => $item['roles'],
        'type' => 'ul',
      )),
    );
  }
  $table = theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'empty' => t('No active roles have been found.'),
  ));
  $form['content'] = array(
    '#weight' => -5,
    '#markup' => $output,
  );
  $form['table'] = array(
    '#weight' => -3,
    '#markup' => $table,
  );

  // User role form.
  $default = array_keys(db_query("SELECT rid FROM {workbench_access_role} WHERE access_scheme = :access_scheme AND access_id = :access_type_id", array(
    ':access_scheme' => $access_type,
    ':access_type_id' => $access_type_id,
  ))
    ->fetchAllAssoc('rid'));
  $form['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#options' => $roles,
    '#default_value' => $default,
    '#description' => empty($roles) ? t('There are no roles with the proper permissions.') : format_plural(count($roles), t('Select the role that should have all users assigned to this section.'), t('Select the roles that should have all users assigned to this section.')),
  );

  // TODO: replace with fancy jQuery.
  if (isset($roles[DRUPAL_AUTHENTICATED_RID])) {
    $form['roles']['#description'] .= '<p>' . t('Selecting the %auth role will select all registered users.', array(
      '%auth' => $roles[DRUPAL_AUTHENTICATED_RID],
    )) . '</p>';
  }
  $form['default_roles'] = array(
    '#type' => 'value',
    '#value' => !empty($roles) ? $default : array(),
  );
  $form['workbench_access'] = array(
    '#type' => 'value',
    '#value' => $active['access_scheme'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update roles'),
  );
  return $form;
}