You are here

function book_access_admin_form in Book access 5

Book access configuration page.

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

File

./book_access.module, line 155
Allows access control for Drupal book nodes.

Code

function book_access_admin_form() {
  $form = array();
  $rids = array();
  $books = array();
  drupal_add_css(drupal_get_path('module', 'book_access') . '/book_access.css');

  // Get a list of roles (which act as grant IDs).
  $results = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  while ($result = db_fetch_object($results)) {
    $rids[$result->rid] = $result->name;
  }

  // Get listing of books, each of which will have it's own access settings.
  $sql = "\n    SELECT n.nid, n.title\n    FROM {node} n\n    LEFT JOIN {book} b ON n.vid = b.vid\n    WHERE b.parent = 0\n    ORDER BY b.weight ASC\n  ";
  $book_results = db_query($sql);
  while ($book = db_fetch_object($book_results)) {
    $books[$book->nid] = $book->title;
  }

  // Each book has its own access control settings.
  foreach ($books as $book_nid => $book_name) {

    // Used to store existing grants for this book.
    $view = array();
    $update = array();
    $delete = array();
    $result = db_query("SELECT * FROM {book_access} where nid = %d", $book_nid);

    // if no existing grants, use some safe defaults
    if (db_num_rows($result) == 0) {
      $view = array(
        1,
        2,
      );
      $update = array();
      $delete = array();
    }
    else {
      while ($book_access = db_fetch_object($result)) {
        if ($book_access->grant_view) {
          $view[] = $book_access->rid;
        }
        if ($book_access->grant_update) {
          $update[] = $book_access->rid;
        }
        if ($book_access->grant_delete) {
          $delete[] = $book_access->rid;
        }
      }
    }
    $form['#tree'] = TRUE;
    $form['access'][$book_nid] = array(
      '#type' => 'fieldset',
      '#title' => $book_name,
      '#collapsible' => TRUE,
    );
    $form['access'][$book_nid]['view'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div class="book-access-div">',
      '#suffix' => '</div>',
      '#options' => $rids,
      '#title' => t('View this book'),
      '#default_value' => $view,
    );
    $form['access'][$book_nid]['update'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div class="book-access-div">',
      '#suffix' => '</div>',
      '#options' => $rids,
      '#title' => t('Edit pages in this book'),
      '#default_value' => $update,
    );
    $form['access'][$book_nid]['delete'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div class="book-access-div">',
      '#suffix' => '</div>',
      '#options' => $rids,
      '#title' => t('Delete pages in this book'),
      '#default_value' => $delete,
    );
  }
  $form['clearer'] = array(
    '#value' => '<div class="book-access-clearer"></div>',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  $form['notice'] = array(
    '#type' => 'markup',
    '#value' => '<p>' . t('Node access tables must be rebuilt when these changes are
      submitted. This may take a few moments.') . '</p>',
  );
  return $form;
}