You are here

function book_access_permissions_form in Book access 6

Book access configuration page.

1 string reference to 'book_access_permissions_form'
book_access_menu in ./book_access.module
Implements hook_menu().

File

./book_access.admin.inc, line 13
Administration interface for the Book access module.

Code

function book_access_permissions_form(&$form_state, $node) {
  $grants = array(
    'view' => array(),
    'update' => array(),
    'delete' => array(),
  );
  $grant_ids = array(
    'grant_view' => 'view',
    'grant_update' => 'update',
    'grant_delete' => 'delete',
  );
  $bid = $node->book['bid'];
  $form = array(
    '#bid' => $bid,
    '#rids' => array(),
    '#uids' => array(),
    '#theme' => array(
      'book_access_permissions_form',
    ),
    '#tree' => TRUE,
  );
  $roles = user_roles();
  $rids = array_keys($roles);
  $result = db_query("SELECT * FROM {book_access_role} WHERE nid = %d AND rid IN (" . db_placeholders($rids) . ")", array_merge(array(
    $bid,
  ), $rids));

  // Build the role access permissions for the book.
  while ($book_access = db_fetch_object($result)) {
    unset($roles[$book_access->rid]);
    foreach ($grant_ids as $id => $var) {
      $grants[$var][$book_access->rid] = !empty($book_access->{$id});
    }
  }
  $default_roles_access = array_filter(variable_get('book_access_default_roles_access', array()));

  // Set the default role access permissions for the roles that don't have
  // access permissions already set.
  foreach ($grant_ids as $id => $var) {
    foreach ($roles as $rid => $name) {
      $grants[$var][$rid] = !empty($default_roles_access[$var]);
    }
  }
  foreach (user_roles() as $rid => $name) {
    $form['#rids'][] = $rid;
    $form['roles']['names'][$rid] = array(
      '#value' => check_plain($name),
    );
    $form['roles']['view'][$rid] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($grants['view'][$rid]),
    );
    $form['roles']['update'][$rid] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($grants['update'][$rid]),
    );
    $form['roles']['delete'][$rid] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($grants['delete'][$rid]),
    );
  }
  $grants = array(
    'view' => array(),
    'update' => array(),
    'delete' => array(),
  );
  $uids = array();
  $result = db_query("SELECT * FROM {book_access_user} bau INNER JOIN {users} u ON u.uid = bau.uid WHERE bau.nid = %d", $bid);
  while ($book_access = db_fetch_object($result)) {
    $uid = $book_access->uid;
    $uids[$uid] = $uid;
    foreach ($grant_ids as $id => $var) {
      $grants[$var][$uid] = !empty($book_access->{$id});
    }
  }
  foreach ($uids as $uid) {
    $form['#uids'][] = $uid;
    $form['users']['names'][$uid] = array(
      '#value' => ($user = user_load($uid)) ? check_plain($user->name) : t('User ID %id', array(
        '%id' => $uid,
      )),
    );
    $form['users']['view'][$uid] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($grants['view'][$uid]),
    );
    $form['users']['update'][$uid] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($grants['update'][$uid]),
    );
    $form['users']['delete'][$uid] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($grants['delete'][$uid]),
    );
  }
  $form['user']['username'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#autocomplete_path' => 'user/autocomplete',
  );
  $grants = array_filter(variable_get('book_access_default_users_access', array()));
  $form['user']['view'] = array(
    '#type' => 'checkbox',
    '#default_value' => !empty($grants['view']),
  );
  $form['user']['update'] = array(
    '#type' => 'checkbox',
    '#default_value' => !empty($grants['update']),
  );
  $form['user']['delete'] = array(
    '#type' => 'checkbox',
    '#default_value' => !empty($grants['delete']),
  );
  $form['user']['add_user'] = array(
    '#type' => 'submit',
    '#value' => t('Add user'),
    '#submit' => array(
      'book_access_user_add_submit',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  return $form;
}