You are here

nodehierarchy.install in Node Hierarchy 5

File

nodehierarchy.install
View source
<?php

// Create the database table on install (MySQL only for now)
function nodehierarchy_install() {
  if ($GLOBALS['db_type'] == 'mysqli' || $GLOBALS['db_type'] == 'mysql') {
    drupal_set_message("Creating required nodehierarchy.module MySQL tables for first install");
    db_query("CREATE TABLE {nodehierarchy} (\n      nid int(10) unsigned NOT NULL default '0',\n      parent int(10) unsigned NOT NULL default '0',\n      order_by float unsigned NOT NULL default '0',\n      PRIMARY KEY (nid)\n    ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
  }
  nodehierarchy_add_default_parents();
}
function nodehierarchy_uninstall() {
  db_query("DROP TABLE {nodehierarchy}");
  foreach (node_get_types() as $key => $type) {
    variable_del('nh_child_' . $key);
    variable_del('nh_parent_' . $key);
    variable_del('nh_defaultparent_' . $key);
  }
}
function nodehierarchy_update_1() {
  return nodehierarchy_add_default_parents();
}

// Add nodehierarchy records for pre-existing nodes.
function nodehierarchy_add_default_parents() {
  $out = array();
  $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {nodehierarchy} h ON n.nid = h.nid WHERE h.parent IS NULL ORDER BY n.nid");
  $order_by = 1;
  while ($node = db_fetch_object($result)) {
    if (function_exists("update_sql")) {
      $out[] = update_sql("INSERT INTO {nodehierarchy} VALUES ({$node->nid}, 0, {$order_by})");
    }
    else {
      db_query("INSERT INTO {nodehierarchy} VALUES ({$node->nid}, 0, {$order_by})");
    }
    $order_by++;
  }
  return $out;
}