<?php

/**
 * @file
 * Internationalization (i18n) submodule: Multilingual contact forms
 *
 * @author Jose A. Reyero, 2005
 */

/**
 * Implements hook_menu().
 *
 * Add translate tab to contact config.
 */
function i18n_contact_menu() {
  $items['admin/structure/contact/edit/%contact/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -100,
  );
  return $items;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function i18n_contact_form_contact_category_delete_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'i18n_contact_form_contact_category_delete_form_submit';
}

/**
 * Remove strings for deleted categories.
 */
function i18n_contact_form_contact_category_delete_form_submit(&$form, $form_state) {
  i18n_string_object_remove('contact_category', $form['contact']['#value']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function i18n_contact_form_contact_category_edit_form_alter(&$form, &$form_state) {
    $form['actions']['translate'] = array(
    '#type' => 'submit',
    '#name'   => 'save_translate',
    '#value' => t('Save and translate'),
  );
  $form['#submit'][] = 'i18n_contact_form_contact_category_edit_form_submit';
}

/**
 * Remove strings for edited/added categories.
 */
function i18n_contact_form_contact_category_edit_form_submit($form, &$form_state) {
  $contact = $form_state['values'];
  i18n_string_object_update('contact_category', $contact);
  // If the save and translate button was clicked, redirect to the translate
  // tab instead of the block overview.
  if ($form_state['triggering_element']['#name'] == 'save_translate') {
    $form_state['redirect'] = 'admin/structure/contact/edit/' . $contact['cid'] . '/translate';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function i18n_contact_form_contact_site_form_alter(&$form, &$form_state) {
  // Example of array translation. The placeholder '*' indicates the name part to be replace by the array keys
  $form['cid']['#options'] = i18n_string_translate("contact:category:*:category", $form['cid']['#options']);
}

/**
 * Implements hook_mail_alter().
 */
function i18n_contact_mail_alter(&$message) {
  if (in_array($message['id'], array('contact_page_mail', 'contact_page_copy', 'contact_page_autoreply'))) {
    // Alter the first part of the subject of emails going out if they need
    // translation.
    $contact = i18n_string_object_translate('contact_category', $message['params']['category'], array('langcode' => $message['language']->language));
    $message['subject'] = t(
      '[!category] !subject',
      array('!category' => $contact['category'], '!subject' => $message['params']['subject']),
      array('langcode' => $message['language']->language)
    );
  
    if ($message['id'] == 'contact_page_autoreply') {
      // Overwrite the whole message body. Maybe this is not entirely responsible
      // (it might overwrite other existing items altered in by others),
      // but unfortunately Drupal core cotact module does not make its item
      // identifiable easily.
      $message['body'] = array($contact['reply']);
    }
  }
}
