<?php
// $Id: admin.inc,v 1.1.2.4 2007/03/31 19:02:51 z0rac Exp $

function _update_locale_admin_overview() {
  include_once './includes/install.inc';
  $langs = locale_supported_languages();
  $langs = array_map('t', $langs['name']);
  unset($langs['en']);

  $items = array();
  foreach ($langs as $code => $name) {
    $items[$code]['title'] = l($name, 'admin/settings/locale/string/update/'. $code .'/info');
    $items[$code]['value'] = t('Up to date');
    $result = db_query("SELECT count(*) FROM {update_locale_string} WHERE locale='%s'", $code);
    if ($n = db_result($result)) {
      $items[$code]['value'] = l(
        format_plural($n, '1 updated string', '@count updated strings'),
        'admin/settings/locale/string/update/'. $code
      );
      $items[$code]['severity'] = REQUIREMENT_WARNING;
    }
  }
  $form['status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Status'),
    '#collapsible' => TRUE,
    'items' => array('#value' => theme('status_report', $items)),
    'updatenow' => array('#type' => 'submit', '#value' => t('Update now'))
  );
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
    '#collapsible' => TRUE
  );
  $form['settings']['update_locale_moderation'] = array(
    '#type' => 'radios',
    '#title' => t('Moderation'),
    '#default_value' => variable_get('update_locale_moderation', 1),
    '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
    '#description' => t('When enabled, the updated strings will be applied after approval.')
  );
  $form['settings']['update_locale_moderation'] = array(
    '#type' => 'radios',
    '#title' => t('Moderation'),
    '#default_value' => variable_get('update_locale_moderation', 1),
    '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
    '#description' => t('When enabled, the updated strings will be applied after approval.')
  );
  $form['settings']['update_locale_cvs'] =  array(
    '#type' => 'textfield',
    '#title' => t('CVS repository for translations'),
    '#default_value' => variable_get('update_locale_cvs', ''),
    '#description' => t('Choose the repository of translation files.')
  );
  $period = drupal_map_assoc(array(0, 3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800), 'format_interval');
  $period[0] = t('none');
  $form['settings']['update_locale_interval'] = array(
    '#type' => 'select',
    '#title' => t('Update interval'),
    '#default_value' => variable_get('update_locale_interval', 0),
    '#options' => $period,
    '#description' => t('The interval indicating how often you want to update translated strings. Requires crontab.')
  );

  $form['#submit']['_update_locale_admin_updatenow'] = array();
  return system_settings_form($form);
}

function _update_locale_admin_updatenow($form_id, $form_values) {
  if ($form_values['op'] == $form_values['updatenow']) {
    update_locale_import_translations();
    return;
  }
  return system_settings_form_submit($form_id, $form_values);
}

function _update_locale_admin_language($lang, $kind = NULL) {
  $langs = locale_supported_languages();
  $langs = array_map('t', $langs['name']);
  unset($langs['en']);
  if (!isset($langs[$lang])) {
    return drupal_not_found();
  }
  if ($kind) {
    if ($kind == 'info') {
      $output = '<h3>'. t('@name - Translation information', array('@name' => $langs[$lang])) .'</h3>';
      $header = array(
        array('data' => t('Module'), 'field' => 'name', 'sort' => 'asc'),
        array('data' => t('Translator'), 'field' => 'translator'),
        array('data' => t('Team'), 'field' => 'team'),
        array('data' => t('Date'), 'field' => 'ftime')
      );
      $rows = array();
      $sql = "SELECT * FROM {update_locale_file} WHERE locale='". db_escape_string($lang) ."' ";
      $result = pager_query($sql . tablesort_sql($header), 50);
      while ($tr = db_fetch_object($result)) {
        $rows[] = array(
          array('data' => check_plain($tr->name)),
          array('data' => check_plain($tr->translator)),
          array('data' => check_plain($tr->team)),
          array('data' => format_date($tr->ftime, 'small'))
        );
      }
      if (count($rows) == 0) {
        $rows[][] = array(
          'data' => t('No information available.'),
          'colspan' => count($header)
        );
      }
      $output .= theme('table', $header, $rows);
      $output .= theme('pager', NULL, 50, 0);
      return $output;
    }
    return drupal_not_found();
  }
  return drupal_get_form('_update_locale_admin_string', $lang, $langs[$lang]);
}

function _update_locale_admin_string($lang, $name) {
  $operations = array(
    '_update_locale_update_strings' => t('Update the selected strings'),
    '_update_locale_ignore_strings' => t('Ignore the selected strings')
  );

  $form = array();
  $form['lang'] = array('#type' => 'value', '#value' => $lang);
  $form['title'] = array(
    '#prefix' => '<h3>', '#value' => check_plain($name), '#suffix' => '</h3>'
  );
  $form['update'] = array(
    '#type' => 'fieldset',
    '#title' => t('Update options'),
    'operations' => array('#type' => 'value', '#value' => $operations),
    'operation' => array(
      '#prefix' => '<div class="container-inline">',
      '#type' => 'select',
      '#options' => $operations
    ),
    'op' => array(
      '#type' => 'submit',
      '#value' => t('Update'),
      '#suffix' => '</div>'
    )
  );

  $form['strings'] = array('#theme' => 'update_locale_admin_string_table');
  $form['strings']['header'] = array(
    '#type' => 'value',
    '#value' => array(
      theme('table_select_header_cell'),
      array('data' => t('New string'), 'field' => 'u.translation'),
      array('data' => t('Old string'), 'field' => 't.translation'),
      array('data' => t('Original string'), 'field' => 's.source')
    )
  );
  $form['strings']['status'] = array('#tree' => TRUE);

  $sql = "SELECT u.lid, u.translation new, t.translation old, s.source";
  $sql .= " FROM {update_locale_string} u";
  $sql .= " LEFT JOIN {locales_target} t USING (lid, locale)";
  $sql .= " LEFT JOIN {locales_source} s USING (lid)";
  $sql .= " WHERE u.locale='". db_escape_string($lang) ."' ";
  $result = pager_query($sql . tablesort_sql($form['strings']['header']['#value']), 20);
  while ($st = db_fetch_object($result)) {
    $form['strings']['status'][$st->lid] = array('#type' => 'checkbox');
    $form['strings'][$st->lid][] = array('#value' => check_plain($st->new));
    $form['strings'][$st->lid][] = array('#value' => check_plain($st->old));
    $form['strings'][$st->lid][] = array(
      '#value' => '<small>'. check_plain($st->source) .'</small>'
    );
  }
  $form['pager'] = array('#value' => theme('pager', NULL, 20, 0));

  return $form;
}

function _update_locale_admin_string_validate($form_id, $form_values) {
  $form_values['status'] = array_diff((array)$form_values['status'], array(0));
  if (count($form_values['status']) == 0) {
    form_set_error('', t('Please select one or more strings to perform the update on.'));
    drupal_goto($_GET['q']);
  }
}

function _update_locale_admin_string_submit($form_id, $form_values) {
  if (isset($form_values['operations'][$form_values['operation']])) {
    $lang = $form_values['lang'];
    $function = $form_values['operation'];
    foreach ($form_values['status'] as $lid => $value) {
      if ($value && $function($lid, $lang)) {
        $refresh = TRUE;
      }
    }
    if ($refresh) {
      cache_clear_all("locale:$lang", 'cache');
      menu_rebuild();
    }
    drupal_set_message(t('The update has been performed.'));
  }
}

function theme_update_locale_admin_string_table($form) {
  $rows = array();
  foreach (element_children($form['status']) as $key) {
    $row = array(drupal_render($form['status'][$key]));
    foreach (element_children($form[$key]) as $column) {
      $row[] = drupal_render($form[$key][$column]);
    }
    $rows[] = $row;
  }
  $header = $form['header']['#value'];
  if (count($rows) == 0) {
    $rows[][] = array(
      'data' => t('No strings available.'),
      'colspan' => count($header)
    );
  }
  return theme('table', $header, $rows);
}

function _update_locale_update_strings($lid, $lang) {
  $u = db_result(db_query("SELECT translation FROM {update_locale_string} WHERE lid=%d AND locale='%s'", $lid, $lang));
  db_query("UPDATE {locales_target} SET translation='%s' WHERE lid=%d AND locale='%s'", $u, $lid, $lang);
  db_query("DELETE FROM {update_locale_string} WHERE lid=%d AND locale='%s'", $lid, $lang);
  return TRUE;
}

function _update_locale_ignore_strings($lid, $lang) {
  db_query("DELETE FROM {update_locale_string} WHERE lid=%d AND locale='%s'", $lid, $lang);
}
