<?php
// $Id: flashvideo.install,v 1.15.2.9 2009/07/31 02:56:52 attheshow Exp $

/**
 * @file
 * This file handles installation and uninstallation of FlashVideo DB tables.
 */

/**
 * Implementation of hook_install().
 */
function flashvideo_install() {
  // Create tables.
  drupal_install_schema('flashvideo');

  // Give the flashvideo module a high weight so that it can override [video] and [thumbnail] tags.
  db_query("UPDATE {system} SET weight=11 WHERE type='module' AND name='flashvideo'");
  
  if (!module_exists('upload')) {
    // Automatically turn on FileField integration option for the user since they're not using core Upload.
    variable_set('flashvideo_filefield', 1);
  }
}

/**
 * Implementation of hook_uninstall().
 */
function flashvideo_uninstall() {
  // Remove tables.
  drupal_uninstall_schema('flashvideo');

  // Delete all variables.
  db_query("DELETE FROM {variable} WHERE name LIKE 'flashvideo_%%'");
}

/**
 * Implementation of hook_schema().
 */
function flashvideo_schema() {
  $schema['flashvideo'] = array(
    'fields' => array(
      'fid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'nid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'oid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'status'     => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'video_index'  => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'width'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 450),
      'height'     => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 337),
      'play_counter' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'flags'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
    ),
    'indexes' => array(
      'fid' => array('fid'),
      'nid' => array('nid'),
      'oid' => array('oid'),
    ),
    'primary key' => array('fid'),
  );

  $schema['ffmpeg_data'] = array(
    'fields' => array(
      'did'       => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
      'fid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'created'    => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'input_file'  => array('type' => 'text', 'not null' => FALSE),
      'output_file'  => array('type' => 'text', 'not null' => FALSE),
      'status'     => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'data'      => array('type' => 'text', 'not null' => FALSE),
    ),
    'indexes' => array(
      'did' => array('did'),
      'fid' => array('fid'),
    ),
    'primary key' => array('did'),
  );

  return $schema;
}

/**
 * Implementation of hook_requirements().
 */
function flashvideo_requirements($phase) {
  switch ($phase) {
    case 'install':
    case 'runtime':
      if (!module_exists('upload') && !module_exists('filefield')) {
        $requirements['mymodule'] = array(
          'title' => t('FlashVideo Additional Requirements'),
          'severity' => REQUIREMENT_ERROR,
          'description' => t('The FlashVideo module requires either the <a href="http://drupal.org/project/filefield">FileField</a> or Upload module to be enabled before it can function. Please enable one of these two modules before proceeding.'),
        );
        return $requirements;
      }
      break;
  }
}

function flashvideo_update_6001() {
  $ret = array();
  if (module_exists('filefield') && flashvideo_variable_get(NULL, 'flashvideo_filefield', 0)) { // Using FileField
    $ret[] = update_sql("DELETE FROM {variable} WHERE (name LIKE 'flashvideo_%_userootpath' OR name LIKE 'flashvideo_%_originaldir' OR name LIKE 'flashvideo_%_outputdir')");
  }
  return $ret;
}