<?php
// $Id: custom_breadcrumbs.install,v 1.3.2.2 2008/09/19 01:00:12 eaton Exp $

/**
 * Implementation of hook_install().
 */
function custom_breadcrumbs_install() {
  drupal_install_schema('custom_breadcrumbs');
}

function custom_breadcrumbs_schema() {
  $schema['custom_breadcrumb'] = array(
    'description' => t('Stores custom breadcrumb trail overrides.'),
    'fields' => array(
      'bid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => t('Unique identifier for the {custom_breadcrumb}.'),
      ),
      'titles' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => t("A return-delimited list of titles for the breadcrumb links.")
      ),
      'paths' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => t("A return-delimited list of url paths for the breadcrumb links."),
      ),
      'visibility_php' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
        'default' => '',
        'description' => t('An optional PHP snippet to control the {custom_breadcrumb} visibility.'),
      ),
      'node_type' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => FALSE,
        'default' => 'AND',
        'description' => t("Node types the {custom_breadcrumb} should apply to."),
      ),
      'set_active_menu' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'description' => t("Whether this {custom_breadcrumb} should override the node's default menu location."),
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => t('The language this breadcrumb is for; if blank, the breadcrumb will be used for unknown languages.'),
      ),
    ),
    'primary key' => array('bid'),
  );
  return $schema;
}

// Update old-style tokens from early versions of token.module.
// Most users aren't using them, but we might as well handle them
// properly.

function custom_breadcrumbs_update_1() {
  $stuff = array(
    '%author_id'      => '[author-uid]',
    '%author_name'    => '[author-name]',
    '%user_id'        => '[user-id]',
    '%user_name'      => '[user-name]',
    '%node_id'        => '[nid]',
    '%node_type'      => '[type]',
    '%node_type_name' => '[type-name]',
    '%top_term'       => '[term]',
    '%top_tname'      => '[term-id]',
    '%created_d'      => '[dd]',
    '%created_D'      => '[ddd]',
    '%created_j'      => '[d]',
    '%created_l'      => '[day]',
    '%created_F'      => '[month]',
    '%created_m'      => '[mm]',
    '%created_M'      => '[mon]',
    '%created_n'      => '[m]',
    '%created_y'      => '[yy]',
    '%created_Y'      => '[yyyy]',
  );

  $search = array_keys($stuff);
  $replace = array_values($stuff);

  $result = db_query("SELECT * FROM {custom_breadcrumb} cb");
  while ($crumb = db_fetch_object($result)) {
    $crumb->titles = str_replace($search, $replace, $crumb->titles);
    $crumb->paths = str_replace($search, $replace, $crumb->paths);
    custom_breadcrumbs_save_breadcrumb($crumb);
  }

  return array(t('Custom Breadcrumb replacement strings updated for use with Token module.'));
}

function custom_breadcrumbs_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {custom_breadcrumb} ADD visibility_php text");
      break;
    case 'pgsql':
      db_add_column($ret, 'custom_breadcrumb', 'visibility_php', 'text', array('default' => ''));
      break;
  }
  return $ret;
}

// Ensure this module's weight is larger than other modules, like views, so custom breadcrumbs page callback is used.
function custom_breadcrumbs_update_3() {
  $ret[] = update_sql("UPDATE {system} SET weight = 12 WHERE name = 'custom_breadcrumbs'");
  return $ret;
}

// Add the menu flag
function custom_breadcrumbs_update_6001() {
  $ret = array();
  db_add_field($ret, 'custom_breadcrumb', 'set_active_menu', 'int', array('default' => 1, 'NOT NULL' => TRUE));
  return $ret;
}

/**
 * Add language support to breadcrumbs.
 */
function custom_breadcrumbs_update_6002() {
  $ret = array();
  db_add_field($ret, 'custom_breadcrumb', 'language', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''));
  return $ret;
}

function custom_breadcrumbs_uninstall() {
  drupal_uninstall_schema('custom_breadcrumbs');
}
