You are here

function access_admin_grants in Access Control Kit 7

Form constructor for the grant admin form.

See also

access_admin_grants_validate()

access_admin_grants_submit()

1 call to access_admin_grants()
access_overview_grants in ./access_grants.admin.inc
Menu page callback; the access grants overview page.

File

./access_grants.admin.inc, line 29
Access grants administrative UI for the access control kit module.

Code

function access_admin_grants() {

  // Build the delete button.
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => -10,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Delete selected grants'),
    '#validate' => array(
      'access_admin_grants_validate',
    ),
    '#submit' => array(
      'access_admin_grants_submit',
    ),
  );

  // Build the sortable table header.
  $header = array(
    'username' => array(
      'data' => t('Username'),
      'field' => 'u.name',
    ),
    'role' => array(
      'data' => t('Role'),
      'field' => 'r.name',
    ),
    'realms' => t('Realms'),
    'scheme' => array(
      'data' => t('Scheme'),
      'field' => 's.name',
    ),
    'operations' => array(
      'data' => t('Operations'),
    ),
  );
  $query = db_select('access_grant', 'g')
    ->extend('PagerDefault')
    ->extend('TableSort');
  access_admin_grants_build_filter_query($query);
  $query
    ->join('users', 'u', 'g.uid = u.uid');
  $query
    ->join('role', 'r', 'g.rid = r.rid');
  $query
    ->join('access_scheme', 's', 'g.scheme = s.machine_name');
  $query
    ->addField('g', 'gid', 'gid');
  $query
    ->addField('r', 'name', 'role');
  $query
    ->addField('s', 'name', 'scheme');
  $result = $query
    ->fields('u', array(
    'uid',
    'name',
    'status',
    'created',
    'access',
  ))
    ->limit(50)
    ->orderByHeader($header)
    ->execute()
    ->fetchAllAssoc('gid');
  $grants = access_grant_load_multiple(array_keys($result));

  // Prepare the list of grants.
  $destination = drupal_get_destination();
  $options = array();
  foreach ($result as $gid => $fields) {

    // Get the realms list, truncated for length.
    $realms = implode(', ', $grants[$gid]->realms);
    if (drupal_strlen($realms) > 30) {
      $realms = drupal_substr($realms, 0, 25) . '...';
    }

    // Add the main columns.
    $options[$gid] = array(
      'username' => theme('username', array(
        'account' => $fields,
      )),
      'role' => check_plain($fields->role),
      // We don't need to check_plain($realms) because $grant->realms is already
      // sanitized in AccessGrantEntityController::attachLoad().
      'realms' => $realms,
      'scheme' => check_plain($fields->scheme),
    );

    // Add the operations links.
    $url = 'admin/access/grant/' . $gid;
    $operations = array();
    $operations['view'] = array(
      'title' => t('view'),
      'href' => $url,
    );
    $operations['edit'] = array(
      'title' => t('edit'),
      'href' => $url . '/edit',
      'query' => $destination,
    );
    $operations['delete'] = array(
      'title' => t('delete'),
      'href' => $url . '/delete',
      'query' => $destination,
    );
    $options[$gid]['operations'] = array(
      'data' => array(
        '#theme' => 'links',
        '#links' => $operations,
        '#attributes' => array(
          'class' => array(
            'links',
            'inline',
          ),
        ),
      ),
    );
  }

  // Format as a table with pager.
  $form['grants'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No access grants available.'),
  );
  $form['pager'] = array(
    '#markup' => theme('pager'),
  );
  return $form;
}