<?php

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

/**
 * Implements hook_field_schema().
 */
function iframe_field_schema($field) {
  if ('iframe' == $field['type']) {
    $schema = array();

    $columns = array(
      'url' => array(
        'type' => 'varchar',
        'length' => 1024,
        'not null' => FALSE,
        'sortable' => TRUE,
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'sortable' => TRUE,
      ),
      'class' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'width' => array(
        'type' => 'varchar',
        'length' => 4,
        'not null' => FALSE,
      ),
      'height' => array(
        'type' => 'varchar',
        'length' => 4,
        'not null' => FALSE,
      ),
      'frameborder' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'scrolling' => array(
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,
        'default' => 'auto',
      ),
      'transparency' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'tokensupport' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    );
    $schema['columns'] = $columns;

    return $schema;
  }
}




/**
 * Helper: Returns all fields created on the system of the type defined in mymodule.
 */ 
function iframe_get_mymodule_fields() {
  module_load_include('module', 'iframe', 'iframe');
  $types = array_keys(iframe_field_info()); // field types defined in mymodule
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  return $fields;
}

/**
 * Helper: add or change one column
 */
function iframe_addorchange_column($field_suffix, $spec) {
  if (empty($field_suffix) || !is_array($spec) || !isset($spec['type'])) {
    die('Cannot add or change column!');
  }
  $fields = iframe_get_mymodule_fields();

  foreach ($fields as $field) {
    if (isset($field['storage']) && isset($field['storage']['type'])
      && 0 !== strcmp($field['storage']['type'], 'field_sql_storage')
    ) {
      $message = t('Iframe: Cannot change field name @name because of different storage_type: @type.',
        array('@name' => $field['field_name'], '@type' => $field['storage']['type']));
      drupal_set_message($message, 'warning');
      continue;
    }
    $table_prefixes = array(
      _field_sql_storage_tablename($field),
      _field_sql_storage_revision_tablename($field)
    );

    foreach ($table_prefixes as $table_prefix) {
      $field_name = $field['field_name']; // eg 'field_dimensions' ;
      $column = $field_name . '_' . $field_suffix;

      if (db_field_exists($table_prefix, $column)) {
        db_change_field($table_prefix, $column, $column, $spec);
      }
      else {
        db_add_field($table_prefix, $column, $spec);
      }
    }
  }
}

/**
 * Update 7100 : add new column "tokensupport"
 */
function iframe_update_7100() {
  $spec = array(
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 0,
  );
  iframe_addorchange_column('tokensupport', $spec);
}

/**
 * Update 7101 : adjust or synchronize length of "url" field for all users
 */
function iframe_update_7101() {
  $spec = array(
    'type' => 'varchar',
    'length' => 1024,
    'not null' => FALSE,
    'sortable' => TRUE,
  );

  iframe_addorchange_column('url', $spec);
}


