View source
<?php
function book_access_enable() {
node_access_needs_rebuild();
}
function book_access_disable() {
node_access_needs_rebuild();
}
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;
}
function book_access_install() {
db_update('system')
->fields(array(
'weight' => 15,
))
->condition('name', 'book_access')
->execute();
}
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;
}
function book_access_update_7202() {
db_delete('node_access')
->condition('nid', 0)
->execute();
}
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);
}
}
function book_access_update_7204() {
variable_del('book_access_default_roles_access');
variable_del('book_access_default_users_access');
}
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);
}
}