<?php

/**
 * @file
 * Installation options for TableField
 */

/**
 * Implements hook_install().
 */
function tablefield_install() {
}

/**
 * Implements hook_uninstall().
 */
function tablefield_uninstall() {
}

/**
 * Implements hook_enable().
 *
 * Notify content module when this module is enabled.
 */
function tablefield_enable() {
}

/**
 * Implements hook_disable().
 *
 * Notify content module when this module is disabled.
 */
function tablefield_disable() {
}

/**
 * Implements hook_update_dependencies().
 */
function tablefield_update_dependencies() {
  // Ensure that format columns are only changed after Filter module has changed
  // the primary records.
  $dependencies['text'][7000] = array(
    'filter' => 7010,
  );

  return $dependencies;
}

function tablefield_repair_schema() {
  $spec = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
  );
  $fields = _update_7000_field_read_fields(array(
    'module' => 'tablefield',
    'storage_type' => 'field_sql_storage',
  ));
  foreach ($fields as $field_name => $field) {
    if ($field['deleted']) {
      $table = "field_deleted_data_{$field['id']}";
      $revision_table = "field_deleted_revision_{$field['id']}";
    }
    else {
      $table = "field_data_{$field['field_name']}";
      $revision_table = "field_revision_{$field['field_name']}";
    }
    $column = $field['field_name'] . '_' . 'format';
    db_change_field($table, $column, $column, $spec);
    db_change_field($revision_table, $column, $column, $spec);
  }
}

/**
 * Update schema to handle machine names of input filter formats.
 */
function tablefield_update_7000() {
  tablefield_repair_schema();
}

/**
 * Fix colums created by versions prior to beta1.
 */
function tablefield_update_7001() {
  tablefield_repair_schema();
}
