You are here

og_subgroups.install in Subgroups for Organic groups 5.4

Same filename and directory in other branches
  1. 5 og_subgroups.install
  2. 6 og_subgroups.install

File

og_subgroups.install
View source
<?php

function og_subgroups_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {og_subgroups} (\n        gid int(11) NOT NULL,\n        parent int(11) NOT NULL,\n        PRIMARY KEY (gid, parent)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {og_subgroups} (\n       gid int NOT NULL,\n       parent int NOT NULL,\n       PRIMARY KEY (gid, parent));");
      db_query("CREATE INDEX {og_subgroups}_gid_idx ON {og_subgroups} (gid);");
      db_query("CREATE INDEX {og_subgroups}_parent_idx ON {og_subgroups} (parent);");
      break;
  }

  // Make sure no records are kept in {og_ancestry}.
  $ret = og_subgroups_migrate();
  drupal_set_message($ret['query']);
}
function og_subgroups_uninstall() {

  // Drop database table
  db_query('DROP TABLE {og_subgroups}');

  // Delete variables
  $variables = array(
    'og_subgroups_propagate_content',
    'og_subgroups_propagate_members',
    'og_subgroups_propagate_demote',
  );
  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

/**
 * Helper function migrate users with og_subgroups 5.x-3 to 5.x-4.
 * 
 * @param $clean_only
 *  When enabled new records will not be written, only deleted from {og_ancestry}.
 */
function og_subgroups_migrate($clean_only = FALSE) {

  // Get all nodes from og_ancestry that nid and group_nid are both group nodes.
  $all_groups = db_query("SELECT oga.nid AS gid, oga.group_nid AS parent FROM {og_ancestry} oga INNER JOIN {og} og ON og.nid = oga.nid");
  $migrate = FALSE;
  while ($group = db_fetch_object($all_groups)) {
    $migrate = TRUE;

    // Don't insert new records, only clean older records.
    if (!$clean_only) {
      db_query('INSERT INTO {og_subgroups} (gid, parent) VALUES (%d, %d)', $group->gid, $group->parent);
    }
    db_query('DELETE FROM {og_ancestry} WHERE nid = %d AND group_nid = %d', $group->gid, $group->parent);
  }
  if ($migrate) {
    $ret = array(
      'query' => t('Groups from versions prior to 5.x-4 were migrated, and old records from the {og_ancestry} were removed.'),
      'success' => TRUE,
    );
  }
  else {
    $ret = array(
      'query' => t('OG Subgroups didn\'t migrate older records from {og_ancestry} since there were none.'),
      'success' => TRUE,
    );
  }
  return $ret;
}

/**
 * Update path for versions prior to 5.x-4.
 */
function og_subgroups_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {og_subgroups} (\n        gid int(11) NOT NULL,\n        parent int(11) NOT NULL,\n        PRIMARY KEY (gid, parent)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {og_subgroups} (\n       gid int NOT NULL,\n       parent int NOT NULL,\n       PRIMARY KEY (gid, parent));");
      db_query("CREATE INDEX {og_subgroups}_gid_idx ON {og_subgroups} (gid);");
      db_query("CREATE INDEX {og_subgroups}_parent_idx ON {og_subgroups} (parent);");
      break;
  }
  $ret[] = array(
    'query' => t('{og_subgroups} table was installed.'),
    'success' => TRUE,
  );
  $ret[] = og_subgroups_migrate();
  return $ret;
}

/**
 * Remove old records from {og_ancestry}.
 */
function og_subgroups_update_2() {
  $ret = array();
  $ret[] = og_subgroups_migrate(TRUE);
  return $ret;
}

Functions

Namesort descending Description
og_subgroups_install
og_subgroups_migrate Helper function migrate users with og_subgroups 5.x-3 to 5.x-4.
og_subgroups_uninstall
og_subgroups_update_1 Update path for versions prior to 5.x-4.
og_subgroups_update_2 Remove old records from {og_ancestry}.