You are here

book_access.install in Book access 6.2

Same filename and directory in other branches
  1. 5 book_access.install
  2. 6 book_access.install
  3. 7.2 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() {
  drupal_install_schema('book_access');
  db_query("UPDATE {system} SET weight = 2 WHERE name = 'book_access'");
  drupal_load('module', 'book_access');
  $vars = new BookAccessVars();
  $vars
    ->saveDefaults();
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6200() {
  $ret[] = update_sql("UPDATE {system} SET weight = 2 WHERE name = 'book_access'");
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6201() {
  $ret = array();
  $schema = array();
  $schema['book_access_user'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_view' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_update' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_delete' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'uid',
    ),
  );
  if (!db_table_exists('book_access_user')) {
    db_create_table($ret, 'book_access_user', $schema['book_access_user']);
  }
  if (!db_table_exists('book_access_role')) {
    db_rename_table($ret, 'book_access', 'book_access_role');
  }
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6203() {
  $ret = array();
  $ret[] = update_sql("UPDATE {node_access} SET realm = 'book_access_role' WHERE realm = 'book_access'");
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6204() {
  $ret = array();
  db_drop_primary_key($ret, 'book_access_role');
  db_change_field($ret, 'book_access_role', 'rid', 'rid', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'book_access_role', 'grant_view', 'grant_view', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'book_access_role', 'grant_update', 'grant_update', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'book_access_role', 'grant_delete', 'grant_delete', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_add_primary_key($ret, 'book_access_role', array(
    'nid',
    'rid',
  ));
  db_drop_primary_key($ret, 'book_access_user');
  db_change_field($ret, 'book_access_user', 'uid', 'uid', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'book_access_user', 'grant_view', 'grant_view', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'book_access_user', 'grant_update', 'grant_update', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'book_access_user', 'grant_delete', 'grant_delete', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_add_primary_key($ret, 'book_access_user', array(
    'nid',
    'uid',
  ));
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6206() {
  $fields = array(
    'grant_admin_access',
    'grant_add_child',
    'grant_edit_outline',
  );
  $ret = array();
  $schema = array();
  $spec = array(
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  $tables = array(
    'book_access_author',
    'book_access_role',
    'book_access_user',
  );
  $schema['book_access_author'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_view' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_update' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'grant_delete' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'uid',
    ),
  );
  if (!db_table_exists('book_access_author')) {
    db_create_table($ret, 'book_access_author', $schema['book_access_author']);
  }
  foreach ($tables as $table) {
    foreach ($fields as $field) {
      if (!db_column_exists($table, $field)) {
        db_add_field($ret, $table, $field, $spec);
      }
    }
  }
  drupal_load('module', 'book_access');
  $vars = new BookAccessVars();
  $vars
    ->saveDefaults();
  $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 = $vars[$variable];
    foreach ($grants as $old_value => $new_value) {
      if (isset($value[$old_value])) {
        if (!empty($value[$old_value])) {
          $value[$new_value] = $new_value;
        }
        unset($value[$old_value]);
      }
    }
    $vars[$variable] = $value;
  }
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Updated the persistent variables default values',
  );
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6207() {
  $ret = array();
  module_enable(array(
    'book_access_ui',
  ));
  drupal_rebuild_theme_registry();
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Enabled the module book_access_ui.module',
  );
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function book_access_update_6209() {
  $ret[] = update_sql("DELETE FROM {node_access} WHERE nid = 0");
  return $ret;
}

/**
 * Implements hook_uninstall().
 */
function book_access_uninstall() {
  drupal_uninstall_schema('book_access');
  drupal_load('module', 'book_access');
  $vars = new BookAccessVars();
  $vars
    ->removeDefaults();
}