<?php

/**
 * Implementation of hook_schema().
 * @see http://api.drupal.org/api/function/hook_schema/6
 *
 * @return array
 */
function icl_content_schema() {
  $schema = array ();
  $schema ['icl_content_status'] = array (
      'description' => 'The ICanLocalize content translation statuses', 
      'fields' => array (
          'rid' => array (
              'description' => 'The request ID', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'nid' => array (
              'description' => 'The node id', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'timestamp' => array (
              'description' => 'The timestamp', 
              'type' => 'int', 
              'length' => 14, 
              'unsigned' => TRUE, 
              'not null' => FALSE ),
          'md5' => array (
              'description' => t ( 'md5 when last sent for translation' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'not null' => TRUE ) ) );

  $schema ['icl_node'] = array (
      'description' => 'Stores the md5 values of saved nodes', 
      'fields' => array (
          'nid' => array (
              'description' => 'The node id', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'md5' => array (
              'description' => t ( 'md5 when last sent for translation' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'not null' => TRUE ),
          'links_fixed' => array(
              'description' => t ( 'indicates if all links to other translated contant have been fixed.' ), 
              'type' => 'int', 
              'length' => 1, 
              'unsigned' => TRUE, 
              'not null' => FALSE ) ) );

  $schema ['icl_block_status'] = array (
      'description' => 'The ICanLocalize block content translation statuses', 
      'fields' => array (
          'rid' => array (
              'description' => 'The request ID', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'bid' => array (
              'description' => 'The block id', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'timestamp' => array (
              'description' => 'The timestamp', 
              'type' => 'int', 
              'length' => 14, 
              'unsigned' => TRUE, 
              'not null' => FALSE ),
          'md5' => array (
              'description' => t ( 'md5 when last sent for translation' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'not null' => TRUE ) ) );

  $schema ['icl_block'] = array (
      'description' => 'Stores the md5 values of saved blocks', 
      'fields' => array (
          'bid' => array (
              'description' => 'The block id', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'md5' => array (
              'description' => t ( 'md5 when last sent for translation' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'not null' => TRUE ),
          'links_fixed' => array(
              'description' => t ( 'indicates if all links to other translated contant have been fixed.' ), 
              'type' => 'int', 
              'length' => 1, 
              'unsigned' => TRUE, 
              'not null' => FALSE ) ) );

  $schema ['icl_comment_translation'] = array (
      'description' => 'Stores data for comment translation', 
      'fields' => array (
          'rid' => array (
              'description' => 'The request ID', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'cid' => array (
              'description' => 'The comment id', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ),
          'timestamp' => array (
              'description' => 'The timestamp', 
              'type' => 'int', 
              'length' => 14, 
              'unsigned' => TRUE, 
              'not null' => FALSE ) ) );
      
  $schema ['icl_string_status'] = array (
      'description' => 'The string translation statuses', 
      'fields' => array (
          'rid' => array (
              'description' => 'The request ID', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'translation_group_id' => array (
              'description' => 'The id of the string translation group that was sent for translation', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'timestamp' => array (
              'description' => 'The timestamp', 
              'type' => 'int', 
              'length' => 14, 
              'unsigned' => TRUE, 
              'not null' => FALSE ),
          'md5' => array (
              'description' => t ( 'md5 when last sent for translation' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'not null' => TRUE ) ) );

  $schema ['icl_string_group'] = array (
      'description' => 'Contains the groups of strings that were sent for translation together.', 
      'fields' => array (
          'translation_group_id' => array (
              'description' => 'The group ID', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'string_type' => array (
              'description' => t ( 'Where the string comes from, locale_source, variable, etc' ), 
              'type' => 'varchar', 
              'length' => 255, 
              'not null' => TRUE ), 
          'string_id' => array (
              'description' => 'The string id', 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ) ) );

  $schema ['icl_image_status'] = array (
      'description' => 'The ICanLocalize image translation statuses. Also for embedded objects', 
      'fields' => array (
        'id' => array(
          'description' => 'The primary identifier for an image.',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE),
        'data' => array(
          'description' => 'Contains the image path of other data for embedded objects.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big'),
        'type' => array(
          'description' => 'The type of data.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
          'default' => ''),
        'language' => array(
          'description' => 'The language of this data.',
          'type' => 'varchar',
          'length' => 12,
          'not null' => TRUE,
          'default' => ''),
        'tnid' => array(
          'description' => 'The translation set id for this data, which equals the data id of the source data in each set.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0),
        'md5' => array (
          'description' => t ( 'md5 of the data contents' ), 
          'type' => 'varchar', 
          'length' => 32, 
          'not null' => TRUE,
          'default' => 0),
        'strings' => array(
          'description' => 'strings in the image/data that need translation.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big',
          'default' => ''),
        ),

      'primary key' => array('id'),
      );

  $schema ['icl_image_replacement_history'] = array (
      'description' => 'Keeps a record of the image replacement performed so that we can undo it.', 
      'fields' => array (
        'id' => array(
          'description' => 'id of content changed, node or block',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE),
        'type' => array(
          'description' => 'The type of content, node or block.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
          'default' => ''),
        'original' => array(
          'description' => 'Original image source.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big'),
        'replacement' => array(
          'description' => 'Replace image source.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big'),
        ),

      );
	  
  $schema ['icl_link_status'] = array (
      'description' => 'The ICanLocalize link translation statuses. Also for embedded objects', 
      'fields' => array (
        'id' => array(
          'description' => 'The primary identifier for an link.',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE),
        'data' => array(
          'description' => 'Contains the link path of other data for embedded objects.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big'),
        'type' => array(
          'description' => 'The type of data.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
          'default' => ''),
        'language' => array(
          'description' => 'The language of this data.',
          'type' => 'varchar',
          'length' => 12,
          'not null' => TRUE,
          'default' => ''),
        'tnid' => array(
          'description' => 'The translation set id for this data, which equals the data id of the source data in each set.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0),
        ),

      'primary key' => array('id'),
      );

  $schema ['icl_link_replacement_history'] = array (
      'description' => 'Keeps a record of the link replacement performed so that we can undo it.', 
      'fields' => array (
        'id' => array(
          'description' => 'id of content changed, node or block',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE),
        'type' => array(
          'description' => 'The type of content, node or block.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
          'default' => ''),
        'original' => array(
          'description' => 'Original link path.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big'),
        'replacement' => array(
          'description' => 'Replace link path.',
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'big'),
        ),
	);

  return $schema;
}

/**
 * Implementation of hook_install().
 * @see http://api.drupal.org/api/function/hook_install/6
 *
 */
function icl_content_install() {
  drupal_install_schema ( 'icl_content' );
  
  // Set module weight for it to run after core modules.
  db_query("UPDATE {system} SET weight = 100 WHERE name = 'icl_content' AND type = 'module'");
  
}

/**
 * Implementation of hook_uninstall().
 * @see http://api.drupal.org/api/function/hook_uninstall/6 
 *
 */
function icl_content_uninstall() {
  variable_del ( 'icl_content_node_type_fields' );
  variable_del ( 'icl_content_send_options' );
  variable_del ( 'icl_content_delete_options' );
  variable_del ( 'icl_content_update_links_options' );
  drupal_uninstall_schema ( 'icl_content' );
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6003() {
  $ret = array ();
  
  db_add_field ( $ret, 'icl_content_status', 'timestamp', array (
      'type' => 'int', 
      'length' => 14, 
      'unsigned' => TRUE, 
      'not null' => FALSE ) );
  
  return $ret;
}

/**
 * Implementation of hook_update_N().
 * 
 * @return array
 */
function icl_content_update_6009() {
  $ret = array ();
    
  $default = array();
  foreach (array_keys(node_get_types()) as $type) {
    $default[$type] = array('title', 'body'); 
  }
  
  $settings = variable_get ( 'icl_content_node_type_fields', $default);  
  foreach ($settings as $node_type => $setting) {
    foreach ($setting as $key => $value) {
      $matches = array();
      if (preg_match('/taxonomy\[tags\[(\d+)\]\]/i', $value, $matches) > 0) {                
        $settings[$node_type]['taxonomy['.$matches[1]] = 'taxonomy['.$matches[1].']'; 
        unset($settings[$node_type][$key]);
      }
    }
  }
  variable_set('icl_content_node_type_fields', $settings);
  
  $ret[] = array('success' => true, 'query' => '***None***');
  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6011() {
  $ret = array ();
  
  db_add_field ( $ret, 'icl_content_status', 'md5', array (

              'description' => t ( 'md5 when last sent for translation' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'not null' => TRUE ) );

  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6012() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_node', $schema['icl_node'] );

  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6013() {
  $ret = array ();
  
  db_add_field ( $ret, 'icl_node', 'links_fixed', array (

              'description' => t ( 'indicates if all links to other translated contant have been fixed.' ), 
              'type' => 'int', 
              'length' => 1, 
              'unsigned' => TRUE, 
              'not null' => FALSE ) );

  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6014() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_comment_translation', $schema['icl_comment_translation'] );

  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6015() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_block_status', $schema['icl_block_status'] );
  db_create_table ( $ret, 'icl_block', $schema['icl_block'] );

  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6016() {
  
  $sql = "SELECT n.nid FROM {node} n join {icl_content_status} cs where n.nid != n.tnid and n.tnid != 0 and n.tnid = cs.nid";
  
  $query = db_query($sql);
  while ( $result = db_fetch_array ( $query ) ) {
    $nid = $result['nid'];
    $node = node_load($nid);
    $old_body = $node->body;
    _icl_content_fix_image_paths($node);
    if ($old_body != $node->body) {
      node_save($node);
    }
  }
    
  $ret[] = array('success' => true, 'query' => '***None***');
  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6017() {
  
  $sql = "SELECT n.nid FROM {node} n join {icl_content_status} cs where n.nid != n.tnid and n.tnid != 0 and n.tnid = cs.nid";
  
  $query = db_query($sql);
  while ( $result = db_fetch_array ( $query ) ) {
    $nid = $result['nid'];
    $node = node_load($nid);
    $old_body = $node->body;
    _icl_content_fix_relative_link_paths($node);
    if ($old_body != $node->body) {
      node_save($node);
    }
  }
    
  $ret[] = array('success' => true, 'query' => '***None***');
  return $ret;
}

/**
 * Implementation of hook_update_N().
 *
 * @return array
 */
function icl_content_update_6018() {
  
  $sql = "SELECT n.nid FROM {icl_node} i join {node} n where n.nid = i.nid";
  
  $query = db_query($sql);
  while ( $result = db_fetch_array ( $query ) ) {
    $nid = $result['nid'];
    $node = node_load($nid);
    $node_md5 = icl_content_calculate_node_md5($node);
    icl_content_save_node_md5($node, $node_md5);
  }
    
  $ret[] = array('success' => true, 'query' => '***None***');
  return $ret;
}

function icl_content_update_6019() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_string_status', $schema['icl_string_status'] );

  return $ret;
}


function icl_content_update_6022() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_image_status', $schema['icl_image_status'] );

  return $ret;
}

function icl_content_update_6024() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_image_replacement_history', $schema['icl_image_replacement_history'] );

  return $ret;
}

function icl_content_update_6025() {
  $ret = array ();
  // Set module weight for it to run after core modules.
  db_query("UPDATE {system} SET weight = 100 WHERE name = 'icl_content' AND type = 'module'");
  
  $ret[] = array('success' => true, 'query' => "UPDATE {system} SET weight = 100 WHERE name = 'icl_content' AND type = 'module'");
  return $ret;
}

function icl_content_update_6026() {
  $ret = array ();
  
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_link_status', $schema['icl_link_status'] );
  db_create_table ( $ret, 'icl_link_replacement_history', $schema['icl_link_replacement_history'] );

  return $ret;
}

function icl_content_update_6028() {
  $ret = array ();

  // delete the old string status table and create the new one  
  db_drop_table ( $ret, 'icl_string_status');
    
  $schema = icl_content_schema();
  
  db_create_table ( $ret, 'icl_string_status', $schema['icl_string_status'] );

  // creat the new string group table.
  db_create_table ( $ret, 'icl_string_group', $schema['icl_string_group'] );

  return $ret;
}

