<?php

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

/**
 * Implements hook_install().
 */
function i18n_field_install() {
  // If updating from D6, module changed name
  if (variable_get('i18n_drupal6_update')) {
    i18n_field_update_7000();
  }
}


/**
 * Implements hook_update_dependencies()
 */
function i18n_field_update_dependencies() {
  $dependencies['i18n_field'][7000] = array(
    'i18n_string' => 7001,
  );
  return $dependencies;
}

/**
 * Implements hook_i18n_drupal6_update().
 *
 * Update old string names
 */
function i18n_field_update_7000() {
  // @todo
  module_load_install('i18n_string');
  // Old CCK label and description
  $query = db_select('i18n_string', 's')
    ->fields('s')
    ->condition('textgroup', 'cck')
    ->condition('type', 'field');
  foreach ($query->execute() as $string) {
    $string->textgroup = 'field';
    list($bundle, $field) = explode('-', $string->objectid);
    $string->type = $field;
    $string->objectid = $bundle;
    $string->property = str_replace('widget_', '', $string->property);
    i18n_string_install_update_string($string);
  }
  // @todo Field groups ??
  // Old Profile fields
  $query = db_select('i18n_string', 's')
    ->fields('s')
    ->condition('textgroup', 'profile')
    ->condition('type', 'field');
  foreach ($query->execute() as $string) {
    $string->textgroup = 'field';
    $string->type = $string->property;
    if ($string->objectid == 'options') {
      // @todo Handle field options
      $string->objectid = '#allowed_values';
    }
    else {
      $string->objectid = 'user'; // Bundle for profile fields
      i18n_string_install_update_string($string);
    }
  }
  // @todo Profile categories ??
}

/**
 * Old strings to update. All these will be handled by i18n_field module
 *
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_label'
 *  --> 'field:$field_name:$bundle:label' (though not used atm)
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_description'
 *  --> 'field:$field_name:$bundle:description'
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':display_description'
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':form_description', $group['settings']['form']['description']);
 *
 * Profile:
 * profile:field:$field_name:title|explanation|options
 * "profile:category", $field->category
 *
 */
