<?php
/**
 * @file
 * QBF module - installer
 *
 * Copyright 2008 Ouest Systèmes Informatiques SARL
 *
 * Author FG Marand
 *
 * License GPL2 or later
 */

// $Id: qbf.install,v 1.1.2.4 2008-10-13 13:43:18 marand Exp $

/**
 * Implement hook_install().
 *
 * - Create the QBF tables
 *   - qbf_queries
 * - Assign initial settings
 *   - none
 *
 * @todo support PGSQL
 * @ingroup hooks
 * @return void
 */
function qbf_install() {

  // Initialize settings
  $ar_const = get_defined_constants();
  $ar_const = array_keys($ar_const);
  $count = 0;
  $init_count = 0;
  foreach ($ar_const as $name) {
    if (strpos($name, 'QBF_VAR_') === 0) {
      $count++;
      $default = 'QBF_DEF_'. drupal_substr($name, drupal_strlen('QBF_VAR_'));
      if (defined($default)) {
        variable_set(constant($name), constant($default));
        $init_count++;
      }
    }
  }
  drupal_set_message(t('Initialized QBF module settings (@initCount/@count parameters). You can <a href="!link">complete setup</a>.',
    array
      (
      '@initCount' => $init_count,
      '@count'     => $count,
      '!link'      => url(QBF_PATH_SETTINGS),
      )));

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $sq = 'CREATE TABLE {qbf_queries} '
          .'  ( '
          ."  `qid`                   INT(10)          NOT NULL DEFAULT '0' COMMENT 'Query ID', "
          ."  `uid`                   INT(10)          NOT NULL DEFAULT '0' COMMENT 'User ID', "
          ."  `name`                  VARCHAR(40)      NOT NULL DEFAULT ''  COMMENT 'Query name', "
          ."  `query`                 TEXT             NOT NULL             COMMENT 'Query array', "
          ."  `created`               INT(10)          NOT NULL DEFAULT '0' COMMENT 'Creation timestamp', "
          ."  `updated`               INT(10)          NOT NULL DEFAULT '0' COMMENT 'Update timestamp', "
          .'  PRIMARY KEY          (`qid`) , '
          .'  KEY         `uid`    (`uid`), '
          .'  KEY         `updated`(`updated`) '
          .'  ) '
          .'ENGINE = MyISAM '
          .'CHARACTER SET = utf8 '
          ."COMMENT = 'QBF Query store' ";
      db_query($sq);
      break;

    case pgsql:
    default:
      drupal_set_message(t('Unsupported database backend for QBF module: @db',
        array('@db' => $GLOBALS['db_type'])), 'error');
      break;
  }
}

/**
 * Implement hook_uninstall().
 *
 * - Drop all the tables maintained by the module
 *   - qbf_queries
 * - Remove nodes of all the types maintained by the module
 *   - none
 * - Remove module settings
 *   - none
 * - Do NOT remove module schema version from {system}
 *
 * @ingroup hooks
 * @return void
 */
function qbf_uninstall() {

  // Remove settings
  $ar_const = get_defined_constants();
  $ar_const = array_keys($ar_const);
  foreach ($ar_const as $name) {
    if (strpos($name, 'QBF_VAR_') === 0) {
      variable_del($name);
    }
  }
  drupal_set_message(t('Removed QBF module settings.'));

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      db_query("DROP TABLE {qbf_queries}");
      break;

    default:
      drupal_set_message(t('Unsupported database backend for QBF module: @db',
        array('@db' => $GLOBALS['db_type'])), 'error');
      break;
  }
}

/**
 * Implement hook_update_N().
 *
 * For now, just define a proper initial schema version.
 *
 * @ingroup hooks
 * @return array
 */
function qbf_update_5000() {
  return array();
}