All pastes #516425 Raw Edit

Mine

public text v1 · immutable
#516425 ·published 2007-05-28 17:58 UTC
rendered paste body
<?php
// $Id: book.install,v 1.7 2007/05/25 12:46:43 dries Exp $

/**
 * Implementation of hook_install().
 */
function book_install() {
  // Create tables.
  drupal_install_schema('book');
}

/**
 * Implementation of hook_uninstall().
 */
function book_uninstall() {
  // Delete menu links
  db_query("DELETE FROM {menu_links} WHERE module = 'book'");
  menu_cache_clear_all();
  // Remove tables.
  drupal_uninstall_schema('book');
}


function book_update_6000() {
  $ret = array();

// Killswitch for doing the update
  $count = db_result(db_query("SELECT count(*) AS count FROM {book}"));

  if ($count['count'] < 0) {  // Do the update
    $nodes = array();
    $book_nodes = db_query("SELECT b.nid, b.weight, b.parent, n.title FROM {book} b INNER JOIN {node} ON b.nid = n.nid");
    while($book_node = db_fetch_object($book_nodes)) {
      $nodes[$book_node->nid] = $book_node;
    }
    foreach($nodes as $node) {
      $nodes[$node->nid]->book_id = _book_get_top_level($node, $nodes);
      $node->depth = 1;
    }
    // Make sure that parent nodes get links before their children
    _book_reorder_by_depth($nodes);
    foreach($nodes as $node) {
      if($node->depth == 1) {
        $plid = 0;
      }
      menu_link_save(array('link_title' => $node->title, 
        'link_path'   => 'node/'. $node->nid,
        'router_path' => 'node/%',
        
    Array ( [link_title] => zxcv [link_path] => node/2 [router_path] => node/% [menu_name] => book-toc-1 [module] => book [plid] => 97 ) 
    }
  }
  else {  // no exising nodes in the hierarchy
     $ret = db_update_sql("DROP TABLE {book}");
     drupal_install_schema('book');
  }
  return $ret;
}

function _book_get_top_level($node, $nodes) {
  if($node->parent == 0) {
    return $node->nid;
  }
  else {
    $node->depth++;
    return _book_get_top_level($nodes[$node->parent], $nodes);
  }
}

function _book_reorder_by_depth(&$nodes) {
  $nodes_new = array();
  foreach($nodes as $node) {
    $nodes_new[$node->depth] = $node;
  }
  ksort($nodes_new);
  $nodes = $nodes_new;
}