You are here

function spaces_update_1 in Spaces 5.2

File

./spaces.install, line 58

Code

function spaces_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':

      // Unweight spaces
      db_query("UPDATE {system} SET weight = 0 WHERE name = 'spaces'");

      // Rename gid to sid in spaces_features
      $ret[] = update_sql("ALTER TABLE {spaces_features} CHANGE COLUMN gid sid int(10) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {spaces_features} ADD KEY(sid)");

      // Drop deprecated spaces_features_custom table
      $ret[] = update_sql("DROP TABLE {spaces_features_custom}");

      // Add spaces table
      db_query("CREATE TABLE {spaces} (\n        sid int(10) NOT NULL default '0',\n        type varchar(64) NOT NULL,\n        preset varchar(64) NOT NULL,\n        customizer longtext NOT NULL,\n        KEY (sid)\n        ) /*!40100 DEFAULT CHARACTER SET UTF8 */");

      // Add spaces_presets table
      db_query("CREATE TABLE {spaces_presets} (\n        type varchar(64) NOT NULL,\n        id varchar(64) NOT NULL,\n        name varchar(255) NOT NULL,\n        description longtext NOT NULL,\n        value longtext NOT NULL,\n        KEY (type)\n        ) /*!40100 DEFAULT CHARACTER SET UTF8 */");

      // Add spaces_settings table
      db_query("CREATE TABLE {spaces_settings} (\n        sid int(10) NOT NULL default '0',                \n        type varchar(64) NOT NULL,\n        id longtext NOT NULL,\n        value longtext NOT NULL,\n        KEY (sid)\n        ) /*!40100 DEFAULT CHARACTER SET UTF8 */");

      // Convert all previous spaces context prefixes to use spaces_og
      // Set preset types based on the deprecated spaces_groupmask
      $result = db_query("SELECT cp.*, og.* FROM {context_prefix} cp JOIN {og} og ON cp.id = og.nid WHERE cp.provider = 'spaces'");
      while ($row = db_fetch_object($result)) {
        $og = array(
          'og_selective' => $row->selective,
          'og_directory' => $row->directory,
          'og_register' => $row->register,
          'og_private' => $row->private,
        );
        $preset = _spaces_groupmask('check', $og) ? _spaces_groupmask('check', $og) : 'private';
        db_query("UPDATE {context_prefix} SET provider = '%s' WHERE provider = 'spaces' AND prefix = '%s'", 'spaces_og', $row->prefix);
        db_query("INSERT INTO {spaces} (sid, type, preset, customizer) VALUES(%d, '%s', '%s', '%s')", $row->id, 'og', $preset, '');
      }

      // Move settings into the spaces_settings table
      $result = db_query("SELECT * FROM {spaces_features} WHERE type = %d", 1);
      while ($setting = db_fetch_object($result)) {
        if ($setting->id == 'spaces_home') {
          $values = array(
            $setting->sid,
            'og',
            'home',
            serialize($setting->value),
          );
          db_query("INSERT INTO {spaces_settings} (sid, type, id, value) VALUES(%d, '%s', '%s', '%s')", $values);
          db_query("DELETE FROM {spaces_features} WHERE sid = %d AND id = '%s' AND type = %d", $setting->sid, 'spaces_home', 1);
        }
        else {
          if ($setting->id != 'shoutbox') {
            $values = array(
              $setting->sid,
              'og',
              $setting->id,
              serialize($setting->value),
            );
            db_query("INSERT INTO {spaces_settings} (sid, type, id, value) VALUES(%d, '%s', '%s', '%s')", $values);
            db_query("DELETE FROM {spaces_features} WHERE sid = %d AND id = '%s' AND type = %d", $setting->sid, $setting->id, 1);
          }
        }
      }

      // Switch type from int to string
      $ret[] = update_sql("ALTER TABLE {spaces_features} CHANGE COLUMN type type varchar(64) NOT NULL");

      // Assume all features were provided for og spaces
      db_query("UPDATE {spaces_features} SET type = '%s'", 'og');
      break;
  }
  return $ret;
}