<?php

/**
 * @file
 * Install hooks for uc_catalog.module.
 */

/**
 * Implements hook_schema().
 */
function uc_catalog_schema() {
  $schema = array();

  $schema['uc_catalog_images'] = array(
    'description' => 'Provides a relation between a catalog taxonomy term in {term_data} and an image in the {files} table.',
    'fields' => array(
      'fid' => array(
        'description' => 'Primary key: The {files}.fid. of the file',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'tid' => array(
        'description' => 'The {term_data}.tid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'filename' => array(
        'description' => 'The {files}.filename of the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'filepath' => array(
        'description' => 'The {files}.filepath of the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'filemime' => array(
        'description' => 'The {files}.filemime of the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'filesize' => array(
        'description' => 'The {files}.filesize of the file.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('fid'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 *
 * Creates a Catalog vocabulary or uses one by that name that may already exist.
 */
function uc_catalog_install() {
  $t = get_t();
  // Find possible Product Catalog vocabulary.
  $result = db_query("SELECT vid FROM {vocabulary} WHERE name = 'Catalog'");
  if ($vocab = db_fetch_object($result)) {
    $vid = $vocab->vid;
  }
  else { // If none, create one.
    db_query("INSERT INTO {vocabulary} (name, description, help, relations, hierarchy, multiple, required, tags, module, weight)
      VALUES ('%s', '', '%s', 0, 1, 1, 0, 0, 'uc_catalog', 0)", $t('Catalog'), $t('Hold Ctrl while clicking to select multiple categories.'));
    $vid = db_last_insert_id('vocabulary', 'vid');
  }
  variable_set('uc_catalog_vid', $vid);
  variable_set('uc_catalog_name', $t('Catalog'));

  drupal_install_schema('uc_catalog');
}

/**
 * Implements hook_uninstall().
 */
function uc_catalog_uninstall() {
  drupal_uninstall_schema('uc_catalog');
  if ($vid = variable_get('uc_catalog_vid', 0)) {
    drupal_set_message(t('The Ubercart Catalog vocabulary has not been deleted. If you need to delete it, <a href="!url">please do so manually</a>.',
      array('!url' => url('admin/content/taxonomy/edit/vocabulary/'. $vid))), 'notice');
  }
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_catalog_%%'");
}

/**
 * Implements hook_update_last_removed().
 */
function uc_catalog_update_last_removed() {
  return 8;
}
