You are here

function nodehierarchy_update_6300 in Node Hierarchy 6.3

Convert valid variables and clean up unused variables for use with 6.x-3.x schema

File

./nodehierarchy.install, line 142
Install file for nodehierarchy module.

Code

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;
}