You are here

function l10n_update_client_save_string in Localization update 7.2

Same name and namespace in other branches
  1. 6 l10n_update.module \l10n_update_client_save_string()
  2. 7 l10n_update.module \l10n_update_client_save_string()

Menu callback. Saves a string translation coming as POST data.

1 string reference to 'l10n_update_client_save_string'
l10n_update_menu_alter in ./l10n_update.module
Implements hook_menu_alter().

File

./l10n_update.module, line 426
Download translations from remote localization server.

Code

function l10n_update_client_save_string() {
  global $user, $language;
  if (l10n_client_access()) {
    if (isset($_POST['source']) && isset($_POST['target']) && !empty($_POST['textgroup']) && !empty($_POST['form_token']) && drupal_valid_token($_POST['form_token'], 'l10n_client_form')) {

      // Ensure we have this source string before we attempt to save it.
      // @todo: add actual context support.
      $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = :context AND textgroup = :textgroup", array(
        ':source' => $_POST['source'],
        ':context' => '',
        ':textgroup' => $_POST['textgroup'],
      ))
        ->fetchField();
      if (!empty($lid)) {
        module_load_include('translation.inc', 'l10n_update');
        $report = array(
          'skips' => 0,
          'additions' => 0,
          'updates' => 0,
          'deletes' => 0,
        );

        // @todo: add actual context support.
        _l10n_update_locale_import_one_string_db($report, $language->language, '', $_POST['source'], $_POST['target'], $_POST['textgroup'], NULL, LOCALE_IMPORT_OVERWRITE, L10N_UPDATE_STRING_CUSTOM);
        cache_clear_all('locale:', 'cache', TRUE);
        _locale_invalidate_js($language->language);
        if (!empty($report['skips'])) {
          $message = theme('l10n_client_message', array(
            'message' => t('Not saved locally due to invalid HTML content.'),
          ));
        }
        elseif (!empty($report['additions']) || !empty($report['updates'])) {
          $message = theme('l10n_client_message', array(
            'message' => t('Translation saved locally.'),
            'level' => WATCHDOG_INFO,
          ));
        }
        elseif (!empty($report['deletes'])) {
          $message = theme('l10n_client_message', array(
            'message' => t('Translation successfully removed locally.'),
            'level' => WATCHDOG_INFO,
          ));
        }
        else {
          $message = theme('l10n_client_message', array(
            'message' => t('Unknown error while saving translation locally.'),
          ));
        }

        // Submit to remote server if enabled.
        if (variable_get('l10n_client_use_server', FALSE) && user_access('submit translations to localization server') && $_POST['textgroup'] == 'default') {
          if (!empty($user->data['l10n_client_key'])) {
            $remote_result = l10n_client_submit_translation($language->language, $_POST['source'], $_POST['target'], $user->data['l10n_client_key'], l10n_client_user_token($user));
            $message .= theme('l10n_client_message', array(
              'message' => $remote_result[1],
              'level' => $remote_result[0] ? WATCHDOG_INFO : WATCHDOG_ERROR,
            ));
          }
          else {
            $server_url = variable_get('l10n_client_server', 'https://localize.drupal.org');
            $user_edit_url = url('user/' . $user->uid . '/edit', array(
              'absolute' => TRUE,
            ));
            $message .= theme('l10n_client_message', array(
              'message' => t('You could share your work with !l10n_server if you set your API key at !user_link.', array(
                '!l10n_server' => l($server_url, $server_url),
                '!user_link' => l($user_edit_url, 'user/' . $user->uid . '/edit'),
              )),
              'level' => WATCHDOG_WARNING,
            ));
          }
        }
      }
      else {
        $message = theme('l10n_client_message', array(
          'message' => t('Not saved due to source string missing.'),
        ));
      }
    }
    else {
      $message = theme('l10n_client_message', array(
        'message' => t('Not saved due to missing form values.'),
      ));
    }
  }
  else {
    $message = theme('l10n_client_message', array(
      'message' => t('Not saved due to insufficient permissions.'),
    ));
  }
  drupal_json_output($message);
  exit;
}