You are here

book_access.install in Book access 7.2

Same filename and directory in other branches
  1. 5 book_access.install
  2. 6.2 book_access.install
  3. 6 book_access.install

File

book_access.install
View source
<?php

/**
 * Implements hook_enable().
 */
function book_access_enable() {
  node_access_needs_rebuild();
}

/**
 * Implements hook_disable().
 */
function book_access_disable() {
  node_access_needs_rebuild();
}

/**
 * Implements hook_schema().
 */
function book_access_schema() {
  $schema = array();
  $schema['book_access_author'] = array(
    'description' => 'Table for tracking book access by author.',
    'fields' => array(
      'nid' => array(
        'description' => 'Primary key: The node ID of the book.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'Primary key: The user ID of the book author.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_view' => array(
        'description' => 'View book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_update' => array(
        'description' => 'Edit book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_delete' => array(
        'description' => 'Delete book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_admin_access' => array(
        'description' => 'Administer book access permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_add_child' => array(
        'description' => 'Add child pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_edit_outline' => array(
        'description' => 'Edit book outlines permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'uid',
    ),
  );
  $schema['book_access_role'] = array(
    'description' => 'Table for tracking book access.',
    'fields' => array(
      'nid' => array(
        'description' => 'Primary key: The node ID of the book.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'rid' => array(
        'description' => 'Primary key: The role ID associated with a book node ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_view' => array(
        'description' => 'View book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_update' => array(
        'description' => 'Edit book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_delete' => array(
        'description' => 'Delete book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_admin_access' => array(
        'description' => 'Administer book access permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_add_child' => array(
        'description' => 'Add child pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_edit_outline' => array(
        'description' => 'Edit book outlines permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'rid',
    ),
  );
  $schema['book_access_user'] = array(
    'description' => 'Table for tracking book access by user.',
    'fields' => array(
      'nid' => array(
        'description' => 'Primary key: The node ID of the book.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'Primary key: The user ID associated with a book node ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_view' => array(
        'description' => 'View book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_update' => array(
        'description' => 'Edit book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_delete' => array(
        'description' => 'Delete book pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_admin_access' => array(
        'description' => 'Administer book access permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_add_child' => array(
        'description' => 'Add child pages permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_edit_outline' => array(
        'description' => 'Edit book outlines permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'uid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function book_access_install() {
  db_update('system')
    ->fields(array(
    'weight' => 15,
  ))
    ->condition('name', 'book_access')
    ->execute();
}

/**
 * Update the existing tables from previous versions to match the new schema.
 */
function book_access_update_7201() {
  $fields = array(
    'grant_view',
    'grant_update',
    'grant_delete',
    'grant_admin_access',
    'grant_add_child',
    'grant_edit_outline',
  );
  $schema = book_access_schema();
  $spec = array(
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  if (!db_table_exists('book_access_author')) {
    db_create_table('book_access_author', $schema['book_access_author']);
  }
  if (!db_table_exists('book_access_role')) {
    if (db_table_exists('book_access')) {
      @db_drop_primary_key('book_access');
      db_rename_table('book_access', 'book_access_role');
      db_add_primary_key('book_access_role', array(
        'nid',
        'rid',
      ));
    }
    else {
      db_create_table('book_access_role', $schema['book_access_role']);
    }
  }
  else {
    @db_drop_primary_key('book_access_role');
    db_change_field('book_access_role', 'rid', 'rid', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ));
    foreach ($fields as $field) {
      if (db_field_exists('book_access_role', $field)) {
        db_change_field('book_access_role', $field, $field, $spec);
      }
      else {
        db_add_field('book_access_role', $field, $spec);
      }
    }
    db_add_primary_key('book_access_role', array(
      'nid',
      'rid',
    ));
  }
  if (!db_table_exists('book_access_user')) {
    db_create_table('book_access_user', $schema['book_access_user']);
  }
  else {
    @db_drop_primary_key('book_access_user');
    db_change_field('book_access_user', 'uid', 'uid', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ));
    foreach ($fields as $field) {
      if (db_field_exists('book_access_user', $field)) {
        db_change_field('book_access_user', $field, $field, $spec);
      }
      else {
        db_add_field('book_access_user', $field, $spec);
      }
    }
    db_add_primary_key('book_access_user', array(
      'nid',
      'uid',
    ));
  }
  return $ret;
}

/**
 * Remove spurious entries in the node access tables that cause users to see
 * books they should not see.
 */
function book_access_update_7202() {
  db_delete('node_access')
    ->condition('nid', 0)
    ->execute();
}

/**
 * Update the value of some Drupal variables used by the module.
 */
function book_access_update_7203() {
  $grants = array(
    'view' => 'grant_view',
    'update' => 'grant_update',
    'delete' => 'grant_delete',
  );
  $variables = array(
    'book_access_default_authors_access',
    'book_access_default_roles_access',
    'book_access_default_users_access',
  );
  foreach ($variables as $variable) {
    $value = variable_get($variable, NULL);
    foreach ($grants as $old_value => $new_value) {
      if (!empty($value[$old_value])) {
        $value[$new_value] = $new_value;
      }
      unset($value[$old_value]);
    }
    variable_set($variable, $value);
  }
}

/**
 * Get rid of variables no longer being used.
 */
function book_access_update_7204() {
  variable_del('book_access_default_roles_access');
  variable_del('book_access_default_users_access');
}

/**
 * Implements hook_uninstall().
 */
function book_access_uninstall() {
  $roles = user_roles();
  $variables = array();
  $variables[] = 'book_access_default_authors_access';
  foreach ($roles as $rid => $rname) {
    $variables[] = "book_access_default_role_{$rid}_access";
  }
  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

Functions

Namesort descending Description
book_access_disable Implements hook_disable().
book_access_enable Implements hook_enable().
book_access_install Implements hook_install().
book_access_schema Implements hook_schema().
book_access_uninstall Implements hook_uninstall().
book_access_update_7201 Update the existing tables from previous versions to match the new schema.
book_access_update_7202 Remove spurious entries in the node access tables that cause users to see books they should not see.
book_access_update_7203 Update the value of some Drupal variables used by the module.
book_access_update_7204 Get rid of variables no longer being used.