You are here

book_access_ui.module in Book access 7.2

Same filename and directory in other branches
  1. 6.2 book_access_ui.module

User interface for the Book access module.

File

book_access_ui.module
View source
<?php

/**
 * @file
 *
 * User interface for the Book access module.
 */

/**
 * Implements hook_admin_paths().
 */
function book_access_ui_admin_paths() {
  $paths = array();
  if (variable_get('node_admin_theme', TRUE)) {
    $paths['node/*/book_access'] = TRUE;
    $paths['book_access/*'] = TRUE;
  }
  return $paths;
}

/**
 * Implements hook_form_FORM_ID_alter() for book_admin_settings().
 *
 * @see theme_book_access_ui_settings()
 */
function book_access_ui_form_book_admin_settings_alter(&$form, &$form_state) {
  $roles = user_roles();
  $options = array(
    'grant_admin_access' => '',
    'grant_update' => '',
    'grant_delete' => '',
    'grant_add_child' => '',
    'grant_edit_outline' => '',
    'grant_view' => '',
  );
  $form['book_access_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Book access settings'),
    '#collapsible' => TRUE,
    '#theme' => 'book_access_ui_settings',
  );
  $form['book_access_settings']['#options'] = array(
    'grant_view' => t('View book'),
    'grant_update' => t('Edit pages'),
    'grant_delete' => t('Delete pages'),
    'grant_admin_access' => t('Administer access'),
    'grant_add_child' => t('Add child pages'),
    'grant_edit_outline' => t('Edit book outlines'),
  );
  $form['book_access_settings']['info'] = array(
    '#markup' => '<em>' . t('These are the default access rules for authors and roles.  Users can only be specified on a per-book basis.') . '<br/><br/>' . t('<strong>Warning:</strong> these defaults only apply to books that have yet to be created.  Books that already exist must be manually changed.') . '</em><br/><br/>',
  );
  $form['book_access_settings']['grants']['book_access_default_author_access'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Default author\'s access'),
    '#default_value' => variable_get('book_access_default_author_access'),
    '#options' => $options,
  );
  foreach ($roles as $rid => $rname) {
    $form['book_access_settings']['grants']["book_access_default_role_{$rid}_access"] = array(
      '#type' => 'checkboxes',
      '#title' => t('Default access for <em>@role</em>', array(
        '@role' => $rname,
      )),
      '#default_value' => variable_get("book_access_default_role_{$rid}_access"),
      '#options' => $options,
    );
  }
  $form['buttons']['#weight'] = 100;
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Save and reset all books to defaults'),
    '#submit' => array(
      'book_access_ui_reset_all_submit',
    ),
    '#weight' => 110,
  );
}

/**
 * Form submission callback for book_access_ui_grants_form().
 *
 * @see book_access_ui_grants_form()
 */
function book_access_ui_reset_all_submit($form, &$form_state) {
  $destination = array();
  if (isset($_GET['destination'])) {
    $destination = drupal_get_destination();
    unset($_GET['destination']);
  }
  $form_state['redirect'] = array(
    'book_access/reset_all',
    array(
      'query' => $destination,
    ),
  );
}

/**
 * Implements hook_help().
 */
function book_access_ui_help($path, $arg) {
  switch ($path) {
    case 'admin/help#book_access':
      $help = '<p>' . t('Allow access control for book nodes on a per book basis.</p>
        <p><a href="@permissions">Permissions enabled</a> will override the
        module access settings. For example, if you would like a role to be able
        to edit all book pages, enable <q>edit any book content</q> in
        <a href="@permissions">Permissions</a>.
        If you would like to control edit permission on a per book basis,
        disable that permission in <a href="@permissions">Permissions</a> and
        configure the module accordingly.', array(
        '@permissions' => url('admin/user/permissions'),
      )) . '</p>' . '<p>' . t('Certain access control modules can impact functionality of this
        module. Broad reaching modules such as "taxonomy access" and "content
        access" can override the values set in the <em>Book access</em> settings page.
        You must turn off all enabled access controls in such modules.</p>
        <p>If you are using additional access control modules, be
        certain that none of them are allowing access to book nodes. The simplest
        way to do this is to limit the types of pages that a book may contain to
        a single node type (such as <q>book page</q>) and unset any access grants
        provided by other modules on that node type configuration page.') . '</p>';
      break;
    case 'node/%/book_access':
      $help = '<p>' . t('Configure access control per book based on users or roles. Settings
        affect all pages within the given book.
        These settings will have no effect for roles with permission to administer nodes.') . '</p>';
      break;
    default:
      $help = '';
  }
  return $help;
}

/**
 * Implements hook_menu().
 */
function book_access_ui_menu() {
  $access_permission = array(
    'administer book access',
  );
  $item = array();

  // We create an additional tab in each top-level book view page.
  $items['node/%node/book_access'] = array(
    'title' => 'Book access',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'book_access_ui_grants_form',
      1,
    ),
    'access callback' => 'book_access_ui_grants_form_access',
    'access arguments' => array(
      1,
    ),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
    'file' => 'book_access_ui.admin.inc',
  );
  $items['book_access/delete/user_permission/%node/%user'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'book_access_ui_user_permissions_remove',
      3,
      4,
    ),
    'access arguments' => $access_permission,
    'type' => MENU_CALLBACK,
    'file' => 'book_access_ui.admin.inc',
  );

  // special admin-only to rebuild all book permissions
  $items['book_access/rebuild_all'] = array(
    'title' => t('Rebuild all book permissions'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'book_access_ui_rebuild_all',
    ),
    'access arguments' => array(
      'administer book access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'book_access_ui.admin.inc',
  );

  // special admin-only to reset all book permissions
  $items['book_access/reset_all'] = array(
    'title' => t('Reset all book permissions to defaults'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'book_access_ui_reset_all_to_defaults',
    ),
    'access arguments' => array(
      'administer book access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'book_access_ui.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function book_access_ui_theme() {
  return array(
    'book_access_ui_grants_form' => array(
      'render element' => 'form',
      'file' => 'book_access_ui.admin.inc',
    ),
    'book_access_ui_settings' => array(
      'render element' => 'form',
      'file' => 'book_access_ui.admin.inc',
    ),
  );
}

/**
 * Determines when the book access tab can be shown in the node edit page.
 *
 * @see book_access_ui_menu()
 */
function book_access_ui_grants_form_access($node) {
  global $user;
  if (!empty($node->book['bid'])) {
    if (user_access('administer nodes') || user_access('administer book access')) {
      return TRUE;
    }
    if (user_access('administer access of any book')) {
      return TRUE;
    }
    if ($node->uid == $user->uid && user_access('administer access of own books')) {
      return TRUE;
    }
    return BookAccess::checkGrant($node->book['bid'], 'admin_access');
  }
  return FALSE;
}

Functions

Namesort descending Description
book_access_ui_admin_paths Implements hook_admin_paths().
book_access_ui_form_book_admin_settings_alter Implements hook_form_FORM_ID_alter() for book_admin_settings().
book_access_ui_grants_form_access Determines when the book access tab can be shown in the node edit page.
book_access_ui_help Implements hook_help().
book_access_ui_menu Implements hook_menu().
book_access_ui_reset_all_submit Form submission callback for book_access_ui_grants_form().
book_access_ui_theme Implements hook_theme().