You are here

function book_access_update_6206 in Book access 6.2

Implements hook_update_N().

File

./book_access.install, line 400

Code

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;
}