You are here

taxonomy_access.install in Taxonomy Access Control 5.2

File

taxonomy_access.install
View source
<?php

/**
 *  Implementations of hook_update (called by update.php)
 */

// Update#1: updating the tables to UTF-8
function taxonomy_access_update_1() {
  return _system_update_utf8(array(
    'term_access',
    'term_access_defaults',
  ));
}

// Update#2: adding field 'grant_list' to tables 'term_access' and 'term_access_defaults'.
function taxonomy_access_update_2() {
  switch ($GLOBALS['db_type']) {
    case 'pgsql':

      // Checking if column 'grant_list' exists
      if (db_result(db_query("SELECT a.attname FROM {pg_attribute} a LEFT JOIN {pg_class} c ON c.oid = a.attrelid WHERE c.relname = 'term_access' AND a.attname = 'grant_list'"))) {
        drupal_set_message(t("Taxonomy Access - Update #2: No queries executed. Field 'grant_list' already exists in tables 'term_access'."), 'error');
        $ret = array();
      }
      else {
        $ret[] = update_sql("ALTER TABLE {term_access} ADD grant_list smallint");
        $ret[] = update_sql("ALTER TABLE {term_access} ALTER COLUMN grant_list SET DEFAULT '0'");
        $ret[] = update_sql("ALTER TABLE {term_access} ALTER COLUMN grant_list SET NOT NULL ");
        $ret[] = update_sql("UPDATE {term_access} SET grant_list = grant_view");
        $ret[] = update_sql("ALTER TABLE {term_access_defaults} ADD grant_list smallint");
        $ret[] = update_sql("ALTER TABLE {term_access_defaults} ALTER COLUMN grant_list SET DEFAULT '0");
        $ret[] = update_sql("ALTER TABLE {term_access_defaults} ALTER COLUMN grant_list SET NOT NULL ");
        $ret[] = update_sql("UPDATE {term_access_defaults} SET grant_list = grant_view");
      }
      break;
    case 'mysql':
    case 'mysqli':

      // Checking if column 'grant_list' exists
      if (db_result(db_query("DESC {term_access} 'grant_list'"))) {
        drupal_set_message(t("Taxonomy Access - Update #2: No queries executed. Field 'grant_list' already exists in tables 'term_access'."), 'error');
        $ret = array();
      }
      else {
        $ret[] = update_sql("ALTER TABLE {term_access} ADD grant_list TINYINT(1) UNSIGNED DEFAULT '0'  NOT NULL");
        $ret[] = update_sql("UPDATE {term_access} SET grant_list = grant_view");
        $ret[] = update_sql("ALTER TABLE {term_access_defaults} ADD grant_list TINYINT(1) UNSIGNED DEFAULT '0'  NOT NULL");
        $ret[] = update_sql("UPDATE {term_access_defaults} SET grant_list = grant_view");
      }
      break;
  }
  return $ret;
}
function taxonomy_access_update_3() {

  // new module weights in core: put taxonomy_access to the bottom (but before the very last ones) in the chain.
  $ret[] = update_sql("UPDATE {system} SET weight = 9 WHERE name = 'taxonomy_access'");
  return $ret;
}

// Update#4: Delete variable 'taxonomy_access_enabled'
function taxonomy_access_update_4() {
  variable_del('taxonomy_access_enabled');
  return array();
}

// Update#5: Move global default records from term_access to term_access defaults
function taxonomy_access_update_5() {
  $result = db_query('SELECT rid, grant_view, grant_update, grant_delete, grant_create, grant_list FROM {term_access} WHERE tid=0');
  while ($row = db_fetch_array($result)) {
    if ($row['rid'] > 0) {

      // just in case we have a weird row with 0, 0
      db_query('INSERT INTO {term_access_defaults} (vid, rid, grant_view, grant_update, grant_delete, grant_create, grant_list) VALUES(0, %d, %d, %d, %d, %d, %d)', $row);
    }
  }
  db_query('DELETE FROM {term_access} WHERE tid=0');
  return array();
}

/**
 * Implementation of hook_install.
 * Adding tables to database: 'term_access', 'term_access_defaults'
 */
function taxonomy_access_install() {
  switch ($GLOBALS['db_type']) {
    case 'pgsql':

      /*
       * Not using pg_version() because it is only available in PHP 5 and with
       * PostgreSQL library: 7.4.  More importantly, the 'server_version'
       * is missing, at least in PHP 5.1.2.
       */
      $row = db_fetch_object(db_query('SELECT version() AS version'));
      $version = preg_replace('/^[^0-9]+([^ ]+).*/i', '\\1', $row->version);
      if (version_compare($version, '8.0', '<')) {

        // PRIOR TO POSTGRESQL 8.0: making a BIT_OR aggregate function
        db_query("CREATE AGGREGATE BIT_OR (\n          basetype = smallint,\n          sfunc = int2or,\n          stype = smallint\n        );");
      }
      db_query("CREATE TABLE {term_access} (\n        tid integer  NOT NULL default '0',\n        rid integer  NOT NULL default '0',\n        grant_view smallint NOT NULL default '0',\n        grant_update smallint NOT NULL default '0',\n        grant_delete smallint NOT NULL default '0',\n        grant_create smallint NOT NULL default '0',\n        grant_list smallint NOT NULL default '0',\n        PRIMARY KEY  (tid,rid)\n      );");
      db_query("CREATE TABLE {term_access_defaults} (\n        vid integer  NOT NULL default '0',\n        rid integer  NOT NULL default '0',\n        grant_view smallint NOT NULL default '0',\n        grant_update smallint NOT NULL default '0',\n        grant_delete smallint NOT NULL default '0',\n        grant_create smallint NOT NULL default '0',\n        grant_list smallint NOT NULL default '0',\n        PRIMARY KEY  (vid,rid)\n      );");

      // new module weights in core: put taxonomy_access to the bottom (but before the very last ones) in the chain.
      db_query("UPDATE {system} SET weight = 9 WHERE name = 'taxonomy_access'");

      // default global perms for roles 1 and 2
      db_query('INSERT INTO {term_access_defaults} (vid, rid, grant_view, grant_update, grant_delete, grant_create, grant_list) VALUES(0, 1, 1, 0, 0, 1, 1)');
      db_query('INSERT INTO {term_access_defaults} (vid, rid, grant_view, grant_update, grant_delete, grant_create, grant_list) VALUES(0, 2, 1, 0, 0, 1, 1)');
      $success = TRUE;
      break;
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {term_access} (\n        tid int(10) unsigned NOT NULL default '0',\n        rid int(10) unsigned NOT NULL default '0',\n        grant_view tinyint(1) unsigned NOT NULL default '0',\n        grant_update tinyint(1) unsigned NOT NULL default '0',\n        grant_delete tinyint(1) unsigned NOT NULL default '0',\n        grant_create tinyint(1) unsigned NOT NULL default '0',\n        grant_list tinyint(1) unsigned NOT NULL default '0',\n        PRIMARY KEY  (tid,rid)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {term_access_defaults} (\n        vid int(10) unsigned NOT NULL default '0',\n        rid int(10) unsigned NOT NULL default '0',\n        grant_view tinyint(1) unsigned NOT NULL default '0',\n        grant_update tinyint(1) unsigned NOT NULL default '0',\n        grant_delete tinyint(1) unsigned NOT NULL default '0',\n        grant_create tinyint(1) unsigned NOT NULL default '0',\n        grant_list tinyint(1) unsigned NOT NULL default '0',\n        PRIMARY KEY  (vid,rid)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      // new module weights in core: put taxonomy_access to the bottom (but before the very last ones) in the chain.
      db_query("UPDATE {system} SET weight = 9 WHERE name = 'taxonomy_access'");

      // default global perms for roles 1 and 2
      db_query('INSERT INTO {term_access_defaults} (vid, rid, grant_view, grant_update, grant_delete, grant_create, grant_list) VALUES(0, 1, 1, 0, 0, 1, 1)');
      db_query('INSERT INTO {term_access_defaults} (vid, rid, grant_view, grant_update, grant_delete, grant_create, grant_list) VALUES(0, 2, 1, 0, 0, 1, 1)');
      $success = TRUE;
      break;
  }

  // End case
  // Notify of changes
  if ($success) {
    drupal_set_message(t('Taxonomy Access module installed tables successfully.'));
  }
  else {
    drupal_set_message(t('The installation of Taxonomy Access module was unsuccessful.'), 'error');
  }
}
function taxonomy_access_uninstall() {
  db_query('DROP TABLE {term_access}');
  db_query('DROP TABLE {term_access_defaults}');

  //variable_del('taxonomy_access_enabled');
  drupal_set_message(t('Taxonomy Access have been successfully uninstalled.'));
}

Functions

Namesort descending Description
taxonomy_access_install Implementation of hook_install. Adding tables to database: 'term_access', 'term_access_defaults'
taxonomy_access_uninstall
taxonomy_access_update_1
taxonomy_access_update_2
taxonomy_access_update_3
taxonomy_access_update_4
taxonomy_access_update_5