You are here

function stringoverrides_admin_import_submit in String Overrides 5

Same name and namespace in other branches
  1. 6 stringoverrides.admin.inc \stringoverrides_admin_import_submit()

Triggered when the user imports data

File

./stringoverrides.admin.inc, line 152
Admin page callbacks for the String Overrides module.

Code

function stringoverrides_admin_import_submit($form_id, $form_values) {

  // Check if the file uploaded correctly
  if ($file = file_check_upload('file')) {
    $overrides = array();

    // Start reading the file
    $handle = @fopen($file->filepath, "r");
    if ($handle) {
      $currentoverride = NULL;
      $currentstring = NULL;
      $operation = 'msgstr';

      // Loop through the whole file
      while (!feof($handle)) {

        // Retrieve a single line
        $buffer = trim(fgets($handle));

        // Skip empty or comment lines
        if (empty($buffer) || $buffer[0] == '#') {
          continue;
        }

        // Continued string
        if ($buffer[0] == '"') {

          // See what we're reading in
          $string = trim(substr($buffer, 1, -1));
          if ($operation == 'msgstr' && !empty($string)) {
            $currentstring .= $string;
          }
          else {
            $currentoverride .= $string;
          }
        }
        else {
          if (substr($buffer, 0, 6) == 'msgstr') {

            // Retrieve the override string
            $operation = 'msgstr';
            $currentstring = substr($buffer, 8, -1);
          }
          else {
            if (substr($buffer, 0, 5) == 'msgid') {

              // New string
              // Save old string
              if (!empty($currentstring) && !empty($currentoverride)) {
                $overrides[$currentoverride] = $currentstring;
                $currentoverride = $currentstring = '';
              }

              // Read what's next
              $operation = 'msgid';
              $currentoverride = substr($buffer, 7, -1);
            }
          }
        }
      }

      // Save old string
      if (!empty($currentstring) && !empty($currentoverride)) {
        $overrides[$currentoverride] = $currentstring;
      }

      // Clean up and save the imported data
      fclose($handle);
      file_delete($file->filepath);
      variable_set('locale_custom_strings_en', $overrides);
      drupal_set_message(t('The overrides have been imported.'));
    }
  }
  else {
    form_set_error('file', t('A file to import is required.'));
  }
}