<?php

/**
 * @file
 *   Install, update and uninstall functions for the quicktabs module.
 */

/**
 * Implements hook_schema().
 */
function quicktabs_schema() {
  $schema['quicktabs'] = array(
    'description' => 'The quicktabs table.',
    'export' => array(
      'key' => 'machine_name',
      'identifier' => 'quicktabs',
      'default hook' => 'quicktabs_default_quicktabs',
      'api' => array(
        'owner' => 'quicktabs',
        'api' => 'quicktabs',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
      'export callback' => 'quicktabs_export',
    ),
    'fields' => array(
      'machine_name' => array(
        'description' => 'The primary identifier for a qt block.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'ajax' => array(
        'description' => 'Whether this is an ajax views block.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'hide_empty_tabs' => array(
        'description' => 'Whether this tabset hides empty tabs.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'default_tab' => array(
        'description' => 'Default tab.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'title' => array(
        'description' => 'The title of this quicktabs block.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'tabs' => array(
        'description' => 'A serialized array of the contents of this qt block.',
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'renderer' => array(
        'description' => 'The rendering mechanism.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'style' => array(
        'description' => 'The tab style.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'options' => array(
        'description' => 'A serialized array of the options for this qt instance.',
        'type' => 'text',
        'size' => 'medium',
        'not null' => FALSE,
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array('machine_name'),
  );
  return $schema;
}


/**
 * Update to 7.x-3.x
 */
function quicktabs_update_7300() {

  if (!db_field_exists('quicktabs', 'machine_name')) {
    // Pull all existing quicktabs, and then delete existing quicktabs. We will reinsert.
    $result = db_query("SELECT * FROM {quicktabs}");
    if (!db_query("DELETE FROM {quicktabs}")) {
      throw new DrupalUpdateException(t('Could not complete the update.'));
    }
  
    db_drop_field('quicktabs', 'qtid');
    $name_field = array(
      'description' => 'The primary identifier for a qt block.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
    );
    db_add_field('quicktabs', 'machine_name', $name_field);
    db_add_primary_key('quicktabs', array('machine_name'));
  
    $output = $used = array();
    foreach ($result as $qt) {
      $row = (array)$qt;
      // Generate a machine-readable string
      $qt_name = strtolower(preg_replace('/[^a-zA-Z0-9_]+/', '_', $row['title']));
      $i = 0;
      while (in_array($i == 0 ? $qt_name : "{$qt_name}_{$i}", $used)) {
        $i++;
      }
      $row['machine_name'] = $used[] = $i == 0 ? $qt_name : "{$qt_name}_{$i}";
      unset($row['qtid']);
      unset($row['style']);
      $row['renderer'] = 'tabs';
      $placeholders = implode(', ', array_keys($row));
      $values = array();
      // Ugh - really?? Somebody tell me there's a better way to do this :-/
      foreach ($row as $name => $value) {
        $values[':' . $name] = $value;
      }
      $tokens = implode(', ', array_keys($values));
      db_query("INSERT INTO {quicktabs} ($placeholders) VALUES($tokens)", $values);
  
      $output[] = "Converted quicktab {$row['machine_name']}.";
    }
  }

  // Add the renderer field
  $renderer_field = array(
    'description' => 'The rendering mechanism.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => 'quicktabs',
  );
  db_add_field('quicktabs', 'renderer', $renderer_field);
  $output[] = "Added the renderer field";

  
  return implode('<br />', $output);
}

/**
 * Add the options field which will hold renderer-specific options.
 */
function quicktabs_update_7301() {
  $options_field = array(
    'description' => 'A serialized array of the options for this qt instance.',
    'type' => 'text',
    'size' => 'medium',
    'not null' => FALSE,
    'serialize' => TRUE,
  );
  db_add_field('quicktabs', 'options', $options_field);
  return "Added the options field";
}

/**
 * Rebuild the registry because of changed method name.
 */
function quicktabs_update_7302() {
  registry_rebuild();
}
