<?php

/**
 * @file
 * FAQ module install file.
 */

/**
 * Define the 'faq_weights' and 'faq_questions' table structures.
 *
 * @return
 *   The schema which contains the structure for the faq module's tables.
 */
function faq_schema() {
  $schema['faq_weights'] = array(
    'description' => 'A table containing the weight of each faq node by category.',
    'fields' => array(
      'tid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The primary identifier for a term or category.  This will be 0 for non-categorized nodes.'),
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The primary identifier for a node.'),
      'weight' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'description' => 'A number representing the weight of a node.  Nodes with lower weight values will appear above those with higher weight values.'),
    ),
    'primary key' => array('nid', 'tid'),
  );

  $schema['faq_questions'] = array(
    'description' => 'A table containing the long question text of each faq node revision.',
    'fields' => array(
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The primary identifier for a node.'),
      'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The primary identifier for a node revision.'),
      'question' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE, 'description' => 'The faq short question text.'),
      'detailed_question' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE, 'description' => 'The faq long question text.'),
    ),
    'primary key' => array('nid', 'vid'),
  );


  return $schema;
}

/**
 * Implementation of hook_install().
 *
 * Inserts the FAQ module's schema in the SQL database.
 */
function faq_install() {
  drupal_install_schema('faq');
  variable_set('node_options_faq', array('status'));
}

/**
 * Implementation of hook_uninstall().
 *
 * Remove the variables, nodes and schema corresponding to the FAQ module.
 */
function faq_uninstall() {
  // Delete the variables we created.
  // General settings.
  variable_del('faq_title');
  variable_del('faq_description');
  variable_del('faq_description_format');
  // Questions page.
  variable_del('faq_display');
  variable_del('faq_question_listing');
  variable_del('faq_qa_mark');
  variable_del('faq_question_label');
  variable_del('faq_answer_label');
  variable_del('faq_question_length');
  variable_del('faq_hide_qa_accordion');
  variable_del('faq_show_expand_all');
  variable_del('faq_use_teaser');
  variable_del('faq_show_node_links');
  variable_del('faq_back_to_top');
  variable_del('faq_disable_node_links');
  variable_del('faq_default_sorting');
  // Categories page.
  variable_del('faq_use_categories');
  variable_del('faq_category_display');
  variable_del('faq_category_listing');
  variable_del('faq_category_hide_qa_accordion');
  variable_del('faq_count');
  variable_del('faq_answer_category_name');
  variable_del('faq_group_questions_top');
  variable_del('faq_hide_child_terms');
  variable_del('faq_show_term_page_children');
  variable_del('faq_omit_vocabulary');
  variable_del('faq_enable_term_links');
  // Block settings.
  variable_del('faq_block_recent_faq_count');
  variable_del('faq_block_random_faq_count');
  // Custom breadcrumbs control
  variable_del('faq_custom_breadcrumbs');

  // Deprecated.
  variable_del('faq_more_link');

  drupal_uninstall_schema('faq');

  // Clear the cache tables.
  cache_clear_all('*', 'cache', TRUE);
  cache_clear_all('*', 'cache_filter', TRUE);
  cache_clear_all('*', 'cache_menu', TRUE);
  cache_clear_all('*', 'cache_page', TRUE);
}

/**
 * Create 'faq_weights' table in order to upgrade from older installations.
 *
 * Should have been created with faq_update_600x() naming convention, but too
 * late now.
 *
 * @return
 *   An array containing the structure of the SQL schema.
 */
function faq_update_1() {
  $schema['faq_weights'] = array(
    'description' => 'A table containing the weight of each faq node by category.',
    'fields' => array(
      'tid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'weight' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
    ),
    'primary key' => array('nid', 'tid'),
  );

  $ret = array();
  db_create_table($ret, 'faq_weights', $schema['faq_weights']);

  return $ret;
}

/**
 * Create 'faq_questions' table in order to upgrade from older installations.
 *
 * Should have been created with faq_update_600x() naming convention, but too
 * late now.
 *
 * @return
 *   An array containing the results of the update.
 */
function faq_update_2() {
  $schema['faq_questions'] = array(
    'description' => 'A table containing the long question text of each faq node revision.',
    'fields' => array(
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The primary identifier for a node.'),
      'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'The primary identifier for a node revision.'),
      'question' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE, 'description' => 'The faq long question text.'),
    ),
    'primary key' => array('nid', 'vid'),
  );

  $ret = array();
  db_create_table($ret, 'faq_questions', $schema['faq_questions']);

  $ret[] = update_sql("INSERT INTO {faq_questions} (nid, vid, question) SELECT r.nid, r.vid, r.title FROM {node_revisions} r, {node} n WHERE n.nid = r.nid AND n.type = 'faq'");

  return $ret;
}

/**
 * Add the 'detailed_question' column to the 'faq_questions' table.
 *
 * @return
 *   An array containing the results of the update.
 */
function faq_update_6003() {
  $ret = array();
  db_add_field($ret, 'faq_questions', 'detailed_question', array('type' => 'text', 'size' => 'normal', 'not null' => TRUE));
  $ret[] = update_sql("UPDATE {faq_questions} SET detailed_question = question");
  return $ret;
}
