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_num_rows(db_query("SELECT * FROM {book}"));
// Do the update
if ($count > 0) {
include_once './modules/node/node.module';
include_once './modules/node/content_types.inc';
// Create a temporary book table
$schema['book_temp'] = array(
'fields' => array(
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'parent' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'weight' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
),
'primary key' => array('nid'),
'name' => 'book_temp',
'module' => 'book',
);
db_create_table($ret, $schema['book_temp']);
$ret[] = update_sql("INSERT INTO {book_temp} (nid, parent, weight) SELECT nid, parent, weight FROM {book}");
$nodes = array();
$book_nodes = db_query("SELECT b.nid, b.weight, b.parent, n.title FROM {book_temp} b INNER JOIN {node} n 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]->depth = 1;
$nodes[$node->nid]->book_id = _book_get_top_level($node, $nodes);
}
// Make sure that parent nodes get links before their children
$nodes = _book_reorder_by_depth($nodes);
$book_node_type = array(
'type' => 'book',
'name' => t('Book page'),
'module' => 'node', // This is cheating, but it works
'description' => t("A book is a collaborative writing effort: users can collaborate writing the pages of the book, positioning the pages in the right order, and reviewing or modifying pages previously written. So when you have some information to share or when you read a page of the book and you didn't like it, or if you think a certain page could have been written better, you can do something about it."),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);
$book_node_type = (object) _node_type_set_defaults($book_node_type);
node_type_save($book_node_type);
$ret[] = update_sql("DROP TABLE {book}");
include_once './modules/book/book.schema';
$schema = book_schema();
_drupal_initialize_schema('book', $schema);
db_create_table($ret, $schema['book']);
foreach($nodes as $node) {
if($node->depth == 1) {
$plid = 0;
}
else {
$mlid = db_fetch_array(db_query("SELECT mlid FROM {book} WHERE nid = %d", $node->book_id));
$plid = $mlid['mlid'];
}
$link = array('link_title' => $node->title,
'link_path' => 'node/'. $node->nid,
'router_path' => 'node/%',
'menu_name' => 'book-toc-'. $node->book_id,
'module' => 'book',
'plid' => $plid,
);
if(menu_link_save($link)) {
$ret[] = update_sql("INSERT INTO {book} (nid, mlid, book_id, child_type) VALUES (%d, %d, %d, '%s')", $node->nid, $link['mlid'], $node->book_id, 'book');
}
}
}
else {
// No exising nodes in the hierarchy, so might as well drop the table
// and re-create it
$ret[] = 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);
return $nodes_new;
}