You are here

function ad_update_4 in Advertisement 5

Same name and namespace in other branches
  1. 5.2 ad.install \ad_update_4()

Move ad groups from custom tables into taxonomy tables.

File

./ad.install, line 327

Code

function ad_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    default:

      // Move groups from ad_groups into taxonomy.
      $result = db_query('SELECT * FROM {ad_groups}');
      while ($group = db_fetch_object($result)) {

        // The default group is now no group at all.
        if ($group->gid == 1) {
          continue;
        }
        $edit = array(
          'vid' => _ad_get_vid(),
          'name' => $group->name,
          'description' => $group->description,
        );
        taxonomy_save_term($edit);
        $tid[$group->gid] = $edit['tid'];
      }

      // Assign ads to new ad groups.
      $result = db_query('SELECT aid,gid FROM {ads}');
      while ($ad = db_fetch_object($result)) {
        if ($tid[$ad->gid]) {
          $ret[] = update_sql("INSERT INTO {term_node} (nid, tid) VALUES ({$ad->aid}, " . $tid[$ad->gid] . ')');
        }
      }

      // Fix the deltas of any group blocks.
      $result = db_query("SELECT * from {blocks} WHERE module = 'ad'");
      $ret[] = update_sql("DELETE FROM {blocks} WHERE module = 'ad'");
      while ($block = db_fetch_object($result)) {
        if ($block->delta == 1) {

          // The old "default" group block now as a delta of 0.
          $ret[] = update_sql("INSERT INTO {blocks} VALUES('ad', 0, '{$block->theme}', {$block->status}, {$block->weight}, '{$block->region}', {$block->custom}, {$block->throttle}, {$block->visibility}, '{$block->pages}', '{$block->title}')");
        }
        else {

          // Switch from a delta of "gid" to a delta of "tid".
          $ret[] = update_sql("INSERT INTO {blocks} VALUES('ad', " . $tid[$block->delta] . ", '{$block->theme}', {$block->status}, {$block->weight}, '{$block->region}', {$block->custom}, {$block->throttle}, {$block->visibility}, '{$block->pages}', '{$block->title}')");
        }
      }

      // Remove old groups table and column.
      $ret[] = update_sql("DROP TABLE {ad_groups}");
      $ret[] = update_sql("ALTER TABLE {ads} DROP gid");
  }
  return $ret;
}