<?php
// $Id: qbf.install,v 1.1.4.2 2009-03-17 11:43:04 marand Exp $
/**
 * @file
 * QBF module - installer
 *
 * Copyright 2008-2009 Ouest Systèmes Informatiques SARL
 *
 * Author FG Marand
 *
 * Licensed under the CeCILL 2.0 and the General Public Licence version 2 or later
 */

// We need to import the module to access its named constants
require_once(drupal_get_path('module', 'qbf') .'/qbf.module');

/**
 * Implement hook_schema().
 *
 * @ingroup hooks
 *
 * @return array
 */
function qbf_schema()
  {
  $schema['qbf_queries'] = array
    (
    'description' => t('Saved QBF queries.'),
    'fields' => array
      (
      'qid' => array
        (
        'description' => t('Query ID'),
        'type'        => 'serial',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
        ),
      'uid' => array
        (
        'description' => t('User ID'),
        'type'        => 'int',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
        'default'     => 0,
        ),
      'name' => array
        (
        'description' => t('Query name'),
        'type'        => 'varchar',
        'length'      => 40,
        'not null'    => TRUE,
        'default'     => '',
        ),
      'query' => array
        (
        'description' => t('Query array'),
        'type'        => 'text',
        'not null'    => TRUE,
        ),
      'created' => array
        (
        'description' => 'Query creation timestamp.',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
        ),
      'updated' => array
        (
        'description' => 'Query update timestamp',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
        ),
      ),

    'primary key' => array('qid'),

    'indexes' => array
      (
      'query_updated' => array('updated'),
      'query_created' => array('created'),
      ),

    'unique keys' => array(),
    );

  return $schema;
  }

/**
 * Implement hook_install().
 *
 * - Install schema
 * - Assign initial settings
 *
 * @ingroup hooks
 * @return void
 */
function qbf_install()
  {
  drupal_install_schema('qbf');

  // 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),
      )
    ));
  }

/**
 * Implement hook_uninstall().
 *
 * - Uninstall schema
 * - 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()
  {
  drupal_uninstall_schema('qbf');

  // 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.'));
  }

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

/**
 * Implement hook_update_N().
 *
 * No data structure change from 5000 to 6000, but we need a 6xxx schema version.
 *
 * @ingroup hooks
 * @return array
 */
function qbf_update_6000()
  {
  return array();
  }