<?php

/**
 * @file
 * Brings Internationalization to Custom Search
 */

/**
 * Implements hook_menu().
 */
function custom_search_i18n_menu() {
  $items['admin/config/search/custom_search/i18n'] = array(
    'title'             => 'Languages',
    'description'       => 'Manage search languages.',
    'page arguments'    => array('custom_search_i18n_admin'),
    'access arguments'  => array('administer custom search'),
    'file'              => 'custom_search_i18n.admin.inc',
    'type'              => MENU_LOCAL_TASK,
    'weight'            => 10,
  );
  return $items;
}

/**
 * Implements hook_custom_search_box().
 */
function custom_search_i18n_custom_search_box(&$form, $form_id, $delta = '') {

  // Criteria.
  $criteria = array('or', 'phrase', 'negative');
  foreach ($criteria as $c) {
    if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_display', FALSE)) {
      if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_region', 'block') == 'popup') {
        $form['popup']['custom_search_criteria_' . $c]['#title'] = i18n_string_translate('custom_search:criterion:1:' . $delta . $c . '_label', variable_get('custom_search_' . $delta . 'criteria_' . $c . '_label', constant('CUSTOM_SEARCH_CRITERIA_' . strtoupper($c) . '_LABEL_DEFAULT')));
      }
      else {
        $form['custom_search_criteria_' . $c]['#title'] = i18n_string_translate('custom_search:criterion:1:' . $delta . $c . '_label', variable_get('custom_search_' . $delta . 'criteria_' . $c . '_label', constant('CUSTOM_SEARCH_CRITERIA_' . strtoupper($c) . '_LABEL_DEFAULT')));
      }
    }
  }

  // Title.
  $form[$form_id]['#title'] = i18n_string_translate('custom_search:common:1:' . $delta . 'label', variable_get('custom_search_' . $delta . 'label', CUSTOM_SEARCH_LABEL_DEFAULT));

  // Default text.
  $form[$form_id]['#attributes']['placeholder'] = i18n_string_translate('custom_search:common:1:' . $delta . 'text', variable_get('custom_search_' . $delta . 'text', ''));

  // Hint text.
  $form[$form_id]['#attributes']['title'] = i18n_string_translate('custom_search:common:1:' . $delta . 'hint_text', variable_get('custom_search_' . $delta . 'hint_text', CUSTOM_SEARCH_HINT_TEXT_DEFAULT));

  // Submit button.
  $form['actions']['submit']['#value'] = i18n_string_translate('custom_search:common:1:' . $delta . 'submit_text', variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT));
  if ($form['actions']['submit']['#value'] == '') {
    $form['actions']['submit']['#attributes'] = array('style' => 'display:none;');
  }

  // Type selector.
  if ((!empty($form['custom_search_types']) && is_array($form['custom_search_types'])) || (!empty($form['popup']['custom_search_types']) && is_array($form['popup']['custom_search_types']))) {
    if (variable_get('custom_search_' . $delta . 'content_types_region', 'block') == 'popup') {
      $form['popup']['custom_search_types']['#title'] = i18n_string_translate('custom_search:content_types:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT));
      if (array_key_exists('c-all', $form['popup']['custom_search_types']['#options'])) {
        $form['popup']['custom_search_types']['#options']['c-all'] = i18n_string_translate('custom_search:content_types:1:' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
      }
    }
    else {
      $form['custom_search_types']['#title'] = i18n_string_translate('custom_search:content_types:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT));
      if (array_key_exists('c-all', $form['custom_search_types']['#options'])) {
        $form['custom_search_types']['#options']['c-all'] = i18n_string_translate('custom_search:content_types:1:' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
      }
    }
  }

  // Taxonomy.
  if (module_exists('custom_search_taxonomy')) {
    $vocabularies = taxonomy_get_vocabularies();
    // Translate label and -all-.
    foreach ($vocabularies as $voc) {
      if ((!empty($form['custom_search_vocabulary_' . $voc->vid]) && is_array($form['custom_search_vocabulary_' . $voc->vid])) || (!empty($form['popup']['custom_search_vocabulary_' . $voc->vid]) && is_array($form['popup']['custom_search_vocabulary_' . $voc->vid]))) {
        if (variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block') == 'popup') {
          $form['popup']['custom_search_vocabulary_' . $voc->vid]['#title'] = i18n_string_translate('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label', $voc->name));
          if (is_array($form['popup']['custom_search_vocabulary_' . $voc->vid]['#options']) && array_key_exists('c-all', $form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'])) {
            $form['popup']['custom_search_vocabulary_' . $voc->vid]['#options']['c-all'] = i18n_string_translate('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
          }
        }
        else {
          $form['custom_search_vocabulary_' . $voc->vid]['#title'] = i18n_string_translate('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label', $voc->name));
          if (!empty($form['custom_search_vocabulary_' . $voc->vid]['#options']) && array_key_exists('c-all', $form['custom_search_vocabulary_' . $voc->vid]['#options'])) {
            $form['custom_search_vocabulary_' . $voc->vid]['#options']['c-all'] = i18n_string_translate('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
          }
        }
      }
    }
    // Translate terms.
    if (module_exists('i18n_taxonomy')) {
      foreach ($vocabularies as $voc) {
        if ((!empty($form['custom_search_vocabulary_' . $voc->vid]) && !empty($form['custom_search_vocabulary_' . $voc->vid]['#options'])) || (!empty($form['popup']['custom_search_vocabulary_' . $voc->vid]) && !empty($form['popup']['custom_search_vocabulary_' . $voc->vid]['#options']))) {
          if (variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block') == 'popup') {
            foreach ($form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'] as $tid => $name) {
              if ($tid != 'c-all') {
                $real_tid = substr($tid, 2);
                $translated_term = i18n_string_translate('taxonomy:term:' . $real_tid . ':name', $name);
                $form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'][$tid] = ($translated_term[0] != '-') ? str_repeat('-', _custom_search_i18n_term_depth($real_tid)) . ' ' . $translated_term : $translated_term;
              }
            }
          }
          else {
            foreach ($form['custom_search_vocabulary_' . $voc->vid]['#options'] as $tid => $name) {
              if ($tid != 'c-all') {
                $real_tid = substr($tid, 2);
                $translated_term = i18n_string_translate('taxonomy:term:' . $real_tid . ':name', $name);
                $form['custom_search_vocabulary_' . $voc->vid]['#options'][$tid] = ($translated_term[0] != '-') ? str_repeat('-', _custom_search_i18n_term_depth($real_tid)) . ' ' . $translated_term : $translated_term;
              }
            }
          }
        }
      }
    }
  }

  // Custom Paths.
  if ((!empty($form['custom_search_paths']) && is_array($form['custom_search_paths'])) || (!empty($form['popup']['custom_search_paths']) && is_array($form['popup']['custom_search_paths']))) {
    if (variable_get('custom_search_' . $delta . 'custom_paths_region', 'block') == 'popup') {
      $form['popup']['custom_search_paths']['#title'] = i18n_string_translate('custom_search:paths:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT));
    }
    else {
      $form['custom_search_paths']['#title'] = i18n_string_translate('custom_search:paths:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT));
    }
  }

}

/**
 * Implements hook_form_alter().
 */
function custom_search_i18n_form_alter(&$form, &$form_state, $form_id) {

  switch ($form_id) {

    case 'custom_search_admin':
      $form['#submit'][] = 'custom_search_i18n_admin_submit';
      break;

    case 'custom_search_content_admin':
      $form['#submit'][] = 'custom_search_i18n_content_admin_submit';
      break;

    case 'custom_search_taxonomy_admin':
      $form['#submit'][] = 'custom_search_i18n_taxonomy_admin_submit';
      break;

    case 'custom_search_results_admin':
      $form['#submit'][] = 'custom_search_i18n_results_admin_submit';
      break;

    case 'block_admin_configure':
      // Custom Search Blocks.
      if (isset($form['module']) && $form['module']['#value'] == 'custom_search_blocks') {
        $form['#submit'][] = 'custom_search_i18n_block_admin_configure_submit';
      }
      break;

  }
}

function custom_search_i18n_preprocess_search_results(&$variables) {
  if (user_access('use custom search') && variable_get('custom_search_filter', 'disabled') != 'disabled') {
    $variables['filter-title'] = i18n_string_translate('custom_search:filter:1:label', variable_get('custom_search_filter_label', CUSTOM_SEARCH_FILTER_LABEL_DEFAULT));
  }
}

/**
 * Callbacks for updating translations.
 */
function custom_search_i18n_admin_submit($form, &$form_state) {
  i18n_string_update('custom_search:common:1:label', $form_state['values']['custom_search_label']);
  i18n_string_update('custom_search:common:1:text', $form_state['values']['custom_search_text']);
  i18n_string_update('custom_search:common:1:hint_text', $form_state['values']['custom_search_hint_text']);
  i18n_string_update('custom_search:common:1:submit_text', $form_state['values']['custom_search_submit_text']);
  i18n_string_update('custom_search:criterion:1:or_label', $form_state['values']['custom_search_criteria_or_label']);
  i18n_string_update('custom_search:criterion:1:phrase_label', $form_state['values']['custom_search_criteria_phrase_label']);
  i18n_string_update('custom_search:criterion:1:negative_label', $form_state['values']['custom_search_criteria_negative_label']);
  i18n_string_update('custom_search:paths:1:selector_label', $form_state['values']['custom_search_paths_selector_label']);
}

function custom_search_i18n_content_admin_submit($form, &$form_state) {
  i18n_string_update('custom_search:content_types:1:selector_label', $form_state['values']['custom_search_type_selector_label']);
  i18n_string_update('custom_search:content_types:1:selector_all', $form_state['values']['custom_search_type_selector_all']);
}

function custom_search_i18n_taxonomy_admin_submit($form, &$form_state) {
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $voc) {
    i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':selector_label', $form_state['values']['custom_search_voc' . $voc->vid . '_selector_label']);
    i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':selector_all', $form_state['values']['custom_search_voc' . $voc->vid . '_selector_all']);
  }
}

function custom_search_i18n_results_admin_submit($form, &$form_state) {
  i18n_string_update('custom_search:filter:1:label', $form_state['values']['custom_search_filter_label']);
}

function custom_search_i18n_block_admin_configure_submit($form, &$form_state) {
  $delta = $form_state['values']['delta'];
  i18n_string_update('custom_search:common:1:blocks_' . $delta . '_label', $form_state['values']['custom_search_blocks_' . $delta . '_label']);
  i18n_string_update('custom_search:common:1:blocks_' . $delta . '_text', $form_state['values']['custom_search_blocks_' . $delta . '_text']);
  i18n_string_update('custom_search:common:1:blocks_' . $delta . '_hint_text', $form_state['values']['custom_search_blocks_' . $delta . '_hint_text']);
  i18n_string_update('custom_search:common:1:blocks_' . $delta . '_submit_text', $form_state['values']['custom_search_blocks_' . $delta . '_submit_text']);
  i18n_string_update('custom_search:criterion:1:blocks_' . $delta . '_or_label', $form_state['values']['custom_search_blocks_' . $delta . '_criteria_or_label']);
  i18n_string_update('custom_search:criterion:1:blocks_' . $delta . '_phrase_label', $form_state['values']['custom_search_blocks_' . $delta . '_criteria_phrase_label']);
  i18n_string_update('custom_search:criterion:1:blocks_' . $delta . '_negative_label', $form_state['values']['custom_search_blocks_' . $delta . '_criteria_negative_label']);
  i18n_string_update('custom_search:content_types:1:blocks_' . $delta . '_selector_label', $form_state['values']['custom_search_blocks_' . $delta . '_type_selector_label']);
  i18n_string_update('custom_search:content_types:1:blocks_' . $delta . '_selector_all', $form_state['values']['custom_search_blocks_' . $delta . '_type_selector_all']);
  i18n_string_update('custom_search:paths:1:blocks_' . $delta . '_selector_label', $form_state['values']['custom_search_blocks_' . $delta . '_paths_selector_label']);
  if (module_exists('custom_search_taxonomy')) {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $voc) {
      i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':blocks_' . $delta . '_selector_label', $form_state['values']['custom_search_blocks_' . $delta . '_voc' . $voc->vid . '_selector_label']);
      i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':blocks_' . $delta . '_selector_all', $form_state['values']['custom_search_blocks_' . $delta . '_voc' . $voc->vid . '_selector_all']);
    }
  }
}

/**
 * Implements hook_locale().
 */
function custom_search_i18n_locale($op = 'groups', $group = NULL) {
  $value = array();
  switch ($op) {
    case 'groups':
      $value['custom_search'] = t('Custom Search');
      break;

    case 'info':
      $value['custom_search']['refresh callback'] = 'custom_search_i18n_locale_refresh';
      break;
  }
  return $value;

}

function custom_search_i18n_locale_refresh() {
  i18n_string_update('custom_search:common:1:label', variable_get('custom_search_label', CUSTOM_SEARCH_LABEL_DEFAULT));
  i18n_string_update('custom_search:common:1:text', variable_get('custom_search_text', ''));
  i18n_string_update('custom_search:common:1:hint_text', variable_get('custom_search_hint_text', CUSTOM_SEARCH_HINT_TEXT_DEFAULT));
  i18n_string_update('custom_search:common:1:submit_text', variable_get('custom_search_submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT));
  i18n_string_update('custom_search:content_types:1:selector_label', variable_get('custom_search_type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT));
  i18n_string_update('custom_search:content_types:1:selector_all', variable_get('custom_search_type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
  i18n_string_update('custom_search:paths:1:selector_label', variable_get('custom_search_paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT));
  i18n_string_update('custom_search:criterion:1:or_label', variable_get('custom_search_criteria_or_label', CUSTOM_SEARCH_CRITERIA_OR_LABEL_DEFAULT));
  i18n_string_update('custom_search:criterion:1:phrase_label', variable_get('custom_search_criteria_phrase_label', CUSTOM_SEARCH_CRITERIA_PHRASE_LABEL_DEFAULT));
  i18n_string_update('custom_search:criterion:1:negative_label', variable_get('custom_search_criteria_negative_label', CUSTOM_SEARCH_CRITERIA_NEGATIVE_LABEL_DEFAULT));
  i18n_string_update('custom_search:filter:1:label', variable_get('custom_search_filter_label', CUSTOM_SEARCH_FILTER_LABEL_DEFAULT));
  if (module_exists('custom_search_taxonomy')) {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $voc) {
      i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':selector_label', variable_get('custom_search_voc' . $voc->vid . '_selector_label', $voc->name));
      i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':selector_all', variable_get('custom_search_voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
      if (module_exists('custom_search_blocks')) {
        for ($a = 1; $a <= variable_get('custom_search_blocks_number', 1); $a++) {
          i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':blocks_' . $a . '_selector_label', variable_get('custom_search_blocks_' . $a . '_voc' . $voc->vid . '_selector_label', $voc->name));
          i18n_string_update('custom_search:vocabulary:' . $voc->vid . ':blocks_' . $a . '_selector_all', variable_get('custom_search_blocks_' . $a . '_voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
        }
      }
    }
  }
  if (module_exists('custom_search_blocks')) {
    for ($a = 1; $a <= variable_get('custom_search_blocks_number', 1); $a++) {
      i18n_string_update('custom_search:common:1:blocks_' . $a . '_label', variable_get('custom_search_blocks_' . $a . '_label', CUSTOM_SEARCH_LABEL_DEFAULT));
      i18n_string_update('custom_search:common:1:blocks_' . $a . '_text', variable_get('custom_search_blocks_' . $a . '_text', ''));
      i18n_string_update('custom_search:common:1:blocks_' . $a . '_hint_text', variable_get('custom_search_blocks_' . $a . '_hint_text', CUSTOM_SEARCH_HINT_TEXT_DEFAULT));
      i18n_string_update('custom_search:common:1:blocks_' . $a . '_submit_text', variable_get('custom_search_blocks_' . $a . '_submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT));
      i18n_string_update('custom_search:criterion:1:blocks_' . $a . '_or_label', variable_get('custom_search_blocks_' . $a . '_criteria_or_label', CUSTOM_SEARCH_CRITERIA_OR_LABEL_DEFAULT));
      i18n_string_update('custom_search:criterion:1:blocks_' . $a . '_phrase_label', variable_get('custom_search_blocks_' . $a . '_criteria_phrase_label', CUSTOM_SEARCH_CRITERIA_PHRASE_LABEL_DEFAULT));
      i18n_string_update('custom_search:criterion:1:blocks_' . $a . '_negative_label', variable_get('custom_search_blocks_' . $a . '_criteria_negative_label', CUSTOM_SEARCH_CRITERIA_NEGATIVE_LABEL_DEFAULT));
      i18n_string_update('custom_search:content_types:1:blocks_' . $a . '_selector_label', variable_get('custom_search_blocks_' . $a . '_type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT));
      i18n_string_update('custom_search:content_types:1:blocks_' . $a . '_selector_all', variable_get('custom_search_blocks_' . $a . '_type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
      i18n_string_update('custom_search:paths:1:blocks_' . $a . '_selector_label', variable_get('custom_search_blocks_' . $a . '_paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT));
    }
  }
  return TRUE;
}

function _custom_search_i18n_term_depth($tid) {
  $limit = 99;
  $depth = -1;
  while ($parent = db_query_range('SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid', 0, 1, array(':tid' => $tid))->fetchObject()) {
    $tid = $parent->parent;
    $depth++;
    if ($depth > $limit) {
      break;
    }
  }
  return $depth;
}
