You are here

book_copy.install in Book Copy 6

Same filename and directory in other branches
  1. 7 book_copy.install

File

book_copy.install
View source
<?php

// @file

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

  // Create tables.
  drupal_install_schema('book_copy');
  db_query("UPDATE {system} SET weight = 15 WHERE name = 'book_copy'");

  // our module should run after any book modules
}

/**
 * Implementation of hook_uninstall().
 */
function book_copy_uninstall() {

  // Remove tables.
  drupal_uninstall_schema('book_copy');
}

/**
 * Implementation of hook_schema().
 * 
 * An entry in the {book_copy} table indicates that a
 * book is to be considered personal, meaning that only the
 * owner may add pages to it.
 */
function book_copy_schema() {
  $schema['book_copy_history'] = array(
    'description' => t('This table maintains source history for books derived via the copy feature'),
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The new nid of the node copied'),
      ),
      'bid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The target book (when initially copied)'),
      ),
      'sbid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The source book'),
      ),
      'snid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The source nid.'),
      ),
      'copied' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The datetime this was copied'),
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The new uid of the user initiating the copy.'),
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  return $schema;
}
function book_copy_update_6000() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {book_copy_history} ADD uid int(11) NOT NULL");
      $result = db_query("SELECT distinct b.bid, u.uid FROM {book} b LEFT JOIN {node} n ON n.nid = b.nid LEFT JOIN {users} u ON n.uid = u.uid;");

      // set copy uid to user who owns the book now
      while ($row = db_fetch_array($result)) {
        db_query("UPDATE {book_copy_history} SET uid = %d WHERE bid = %d", $row['uid'], $row['bid']);
      }

      // fix any nodes that may have since been deleted (we will still have some orphans)
      $result = db_query("SELECT n.nid, u.uid FROM {book_copy_history} bch LEFT JOIN {node} n ON n.nid = bch.nid LEFT JOIN {users} u ON u.uid = n.uid WHERE bch.uid = 0");
      while ($row = db_fetch_array($result)) {
        db_query("UPDATE {book_copy_history} SET uid = %d WHERE nid = %d", $row['uid'], $row['nid']);
      }
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
book_copy_install Implementation of hook_install().
book_copy_schema Implementation of hook_schema().
book_copy_uninstall Implementation of hook_uninstall().
book_copy_update_6000