<?php

/**
 * @file
 * Internationalization (i18n) module - Path translation
 */

/**
 * Implements hook_menu()
 */
function i18n_path_menu() {
  $items['admin/config/regional/i18n_translation/path'] = array(
    'title' => 'Paths',
    'description' => 'Path translation.',
    'page callback' => 'i18n_path_admin_overview',
    'access arguments' => array('administer site configuration'),
    'file' => 'i18n_path.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/config/regional/i18n_translation/path/list'] = array(
    'title' => 'Paths',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/regional/i18n_translation/path/add'] = array(
    'title' => 'Add path translation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('i18n_path_admin_form'),
    'access arguments' => array('administer site configuration'),
    'file' => 'i18n_path.admin.inc',
    'type' => MENU_LOCAL_ACTION,
    'parent' => 'admin/config/regional/i18n_translation',
  );
  $items['admin/config/regional/i18n_translation/path/edit/%i18n_path_translation_set'] = array(
    'title' => 'Edit path translation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('i18n_path_admin_form', 6),
    'access arguments' => array('administer site configuration'),
    'file' => 'i18n_path.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
  );
  $items['admin/config/regional/i18n_translation/path/delete/%i18n_path_translation_set'] = array(
    'title' => 'Delete path translation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('i18n_translation_set_delete_confirm', 6),
    'access arguments' => array('administer site configuration'),
    'file' => 'i18n_path.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
  );
  return $items;
}

/**
 * Implements hook_url_outbound_alter()
 */
/*
function i18n_path_url_outbound_alter(&$path, &$options, $original_path) {
  if (!empty($options['language'])) {
    $langcode = $options['language']->language;
    $original = $options['alias'] ? drupal_get_normal_path($path, $langcode) : $original_path;
    if (($translations = i18n_path_get_translations($path)) && !empty($translations[$langcode])) {
      $path = $options['alias'] ? drupal_get_path_alias($translations[$langcode], $langcode) : $translations[$langcode];
    }
  }
}
*/

/**
 * Get translations for path
 */
function i18n_path_get_translations($path) {
  static $translations;

  if (!isset($translations)) {
    $translations = drupal_static(__FUNCTION__, array());
  }
  if (!isset($translations[$path])) {
    $translations[$path] = db_query('SELECT p.language, p.path FROM {i18n_path} p INNER JOIN {i18n_path} ps ON p.tsid = ps.tsid WHERE ps.path = :path',
      array(':path' => $path)
    )->fetchAllKeyed();
  }
  return $translations[$path];
}


/**
 * Implements hook_i18n_object_info().
 */
function i18n_path_i18n_object_info() {
  return array(
    'path' => array(
      'title' => t('Path'),
      'class' => 'i18n_path_object',
      'key' => array('path', 'language'),
      'translation set' => array(
        'class' => 'i18n_path_translation_set',
        'table' => 'i18n_path',
        'field' => 'tsid',
        'placeholder' => '%i18n_path_translation_set',
        'edit path' => 'admin/config/regional/i18n_translation/path/edit/%i18n_path_translation_set',
        'delete path' => 'admin/config/regional/i18n_translation/path/delete/%i18n_path_translation_set',
        'list path' => 'admin/config/regional/i18n_translation/path',
      ),
    )
  );
}

/**
 * Implements hook_i18n_translate_path()
 */
function i18n_path_i18n_translate_path($path) {
  if ($translations = i18n_path_get_translations($path)) {
    $result = array();
    foreach ($translations as $langcode => $translated) {
      $result[$langcode] = array(
        'href' => $translated,
      );
    }
    return $result;
  }
}

/**
 * Load translation set. Menu loading callback.
 */
function i18n_path_translation_set_load($tsid) {
  return i18n_translation_set_load($tsid, 'path');
}
