You are here

nodehierarchy.install in Node Hierarchy 6.3

Install file for nodehierarchy module.

File

nodehierarchy.install
View source
<?php

/**
 * @file
 *   Install file for nodehierarchy module.
 */

/**
 * Implementation of hook_install().
 */
function nodehierarchy_install() {

  // Create tables.
  drupal_install_schema('nodehierarchy');
}

/**
 * Implementation of hook_enable().
 */
function nodehierarchy_enable() {
}

/**
 * Implementation of hook_schema().
 */
function nodehierarchy_schema() {
  $schema['nodehierarchy_menu_links'] = array(
    'fields' => array(
      'mlid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => "The book page's {menu_links}.mlid.",
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {node}.nid whose parent is being defined.'),
      ),
    ),
    'primary key' => array(
      'mlid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
    ),
  );
  return $schema;
}

/**
 * Update from the 5.x or 6.x-1.x branches.
 */
function nodehierarchy_update_6200() {
  require_once './' . drupal_get_path('module', 'nodehierarchy') . '/nodehierarchy.module';
  $out = array();
  $schema = nodehierarchy_schema();
  db_create_table($ret, 'nodehierarchy_menu_links', $schema['nodehierarchy_menu_links']);
  $result = db_query("SELECT nh.*, n.title FROM {nodehierarchy} nh LEFT JOIN {node} n ON n.nid = nh.nid ORDER BY nh.parent");
  while ($node = db_fetch_object($result)) {
    $plid = (int) _nodehierarchy_get_node_mlid($node->parent);
    if ($menu_link = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE plid = %d AND link_path = 'node/%d'", $plid, $node->nid))) {
      $menu_link = _nodehierarchy_prepare_menu_link($menu_link);
      $menu_link['module'] = 'nodehierarchy';
    }
    else {
      $menu_link = _nodehierarchy_default_menu_link($node->nid, $plid);
      $menu_link['link_title'] = $node->title;
    }
    menu_link_save($menu_link);
    _nodehierarchy_create_nodehierarchy_menu_link_reference($menu_link);
    update_sql("DELETE FROM {nodehierarchy} WHERE nid = %d", $node->nid);
  }

  // Update the old can-be-parent can-be-child settings.
  $types = node_get_types();
  $can_be_children = array();
  foreach ($types as $type => $info) {
    if (variable_get('nh_child_' . $type, FALSE)) {
      $can_be_children[$type] = $type;
    }
    variable_del('nh_child_' . $type);
  }
  foreach ($types as $type => $info) {
    if (variable_get('nh_parent_' . $type, FALSE)) {
      variable_set('nh_allowchild_' . $type, $can_be_children);
    }
    variable_del('nh_parent_' . $type);
  }

  // Update the default parents.
  foreach ($types as $type => $info) {
    if ($pnid = variable_get('nh_defaultparent_' . $type, 0)) {
      $parent_menus = _nodehierarchy_get_node_menu_links($pnid);
      if ($parent_menus && @$parent_menus[0]['mlid']) {
        variable_set('nh_defaultparent_' . $type, $parent_menus[0]['mlid']);
      }
    }
  }

  // Update view handlers etc.
  $view_translation = array(
    'order_by' => array(
      'nh_menu_links',
      'weight',
    ),
    'antecedent' => array(
      'nh_ancestor',
      'nid',
    ),
    'parent' => array(
      'nh_parent',
      'nid',
    ),
    'actions' => NULL,
  );
  if (module_exists('views')) {
    $views = views_get_all_views();
    foreach ($views as $view_name => $view) {
      $changed = FALSE;
      foreach ($view->display as $display_id => $display) {
        foreach (array(
          'arguments',
          'filters',
          'sorts',
          'fields',
        ) as $item) {
          foreach ((array) @$display->display_options[$item] as $key => $info) {
            if ($info['table'] == 'nodehierarchy' && ($trans = @$view_translation[$info['id']])) {
              if ($trans !== NULL) {
                $info['table'] = $trans[0];
                $info['id'] = $info['field'] = $trans[1];
                unset($view->display[$display_id]->display_options[$item][$key]);
                $view->display[$display_id]->display_options[$item][$info['id']] = $info;
              }
              else {
                unset($view->display[$display_id]->display_options[$item][$key]);
              }
              $changed = TRUE;
            }
          }
        }
      }
      if ($changed) {
        $view
          ->save();
      }
    }
  }
  return $out;
}

/**
 * Convert valid variables and clean up unused variables for use with 6.x-3.x schema
 */
function nodehierarchy_update_6300() {
  $ret = array();
  $renamed = array();
  $remove = array();
  $types = array_keys(node_get_types());
  $query = db_query('SELECT * FROM {variable} WHERE name LIKE \'nh_%\'');
  while ($row = db_fetch_object($query)) {

    // Rename accepted variables
    $pattern = array(
      // Children
      '^nh_allowchild_(.*)$' => 'nh_children_allowed_types_',
      // Parents
      '^nh_defaultparent_(.*)$' => 'nh_parent_node_',
      '^nh_multiple_(.*)$' => 'nh_parent_multiple_',
      // Menus
      '^nh_createmenu_(.*)$' => 'nh_menu_create_',
      //Views
      '^nh_default_children_view_(.*)$' => 'nh_view_default_',
    );
    foreach ($pattern as $variable => $replace) {
      $match = array();
      $regex = '/' . $variable . '/i';
      if (preg_match($regex, $row->name, $match)) {

        // Add the matched patterns so they are removed
        $remove[] = $row->name;

        // Rename only valid node types and remove the node types that no longer exist.
        // After this update, Node Hierarchy settings for the respected node type are deleted when the node type is deleted or this module is uninstalled.
        if (in_array($match[1], $types)) {
          $renamed[$replace . $match[1]] = $row->value;
        }
        break;
      }
    }
  }
  foreach ($renamed as $name => $value) {
    if (!db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $value)) {
      $ret[] = array(
        'success' => FALSE,
        'query' => check_plain('Could not rename Node Hierarchy variables.'),
      );
      return $ret;
    }
  }
  if (!empty($remove)) {
    db_query('DELETE FROM {variable} WHERE name IN (' . db_placeholders($remove, 'varchar') . ')', $remove);
  }
  $ret[] = array(
    'success' => TRUE,
    'query' => check_plain('INSERT: ' . count($renamed) . ' Node Hierarchy variables renamed.'),
  );
  return $ret;
}
function nodehierarchy_uninstall() {
  drupal_uninstall_schema('nodehierarchy');

  // Run a LIKE query to delete all variables that start with 'nh_', this way we don't have to update this function in the future
  db_query("DELETE FROM {variable} WHERE name LIKE 'nh_%'");
}

Functions

Namesort descending Description
nodehierarchy_enable Implementation of hook_enable().
nodehierarchy_install Implementation of hook_install().
nodehierarchy_schema Implementation of hook_schema().
nodehierarchy_uninstall
nodehierarchy_update_6200 Update from the 5.x or 6.x-1.x branches.
nodehierarchy_update_6300 Convert valid variables and clean up unused variables for use with 6.x-3.x schema