You are here

public function SmartlingTranslator::getFileName in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::getFileName()
  2. 8.2 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::getFileName()
  3. 8.3 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::getFileName()

Returns file name.

Parameters

\Drupal\tmgmt\JobInterface $job:

Return value

string

1 call to SmartlingTranslator::getFileName()
SmartlingTranslator::requestTranslation in src/Plugin/tmgmt/Translator/SmartlingTranslator.php
@abstract

File

src/Plugin/tmgmt/Translator/SmartlingTranslator.php, line 509
Contains \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator.

Class

SmartlingTranslator
Smartling translator plugin.

Namespace

Drupal\tmgmt_smartling\Plugin\tmgmt\Translator

Code

public function getFileName(JobInterface $job) {

  // TODO: identical filename task.
  // $extension = $job->getSetting('export_format');
  //
  // try {
  //   // Try to load existing file name from tmgmt_job table.
  //   $filename = $job->get('job_file_name');
  //
  //   if (!empty($filename->getValue())) {
  //     $filename = $filename->getValue()[0]['value'];
  //   }
  //   // Job item title should be included into a filename only if there is a
  //   // single JobItem in a Job. If there are 3 JobItems in a file - file name
  //   // should be "@entity_type_@entity_id>". And finally for every Job with
  //   // more than 3 JobItems - standard "JobId@id"
  //   elseif ($extension == 'xml') {
  //     $file_names = [];
  //     $job_items = $job->getItems();
  //     $job_items_count = count($job_items);
  //
  //     if ($job_items_count == 1) {
  //       $file_name_type = 'expanded';
  //     }
  //     else if ($job_items_count > 1 && $job_items_count <= 3) {
  //       $file_name_type = 'simplified';
  //     }
  //     else {
  //       $file_name_type = 'default';
  //     }
  //
  //     foreach ($job_items as $job_item) {
  //       $job_item_id = $job_item->getItemId();
  //       $job_item_type = $job_item->getItemType();
  //
  //       switch ($file_name_type) {
  //         case 'expanded':
  //           $temp_name = $job_item->getSourceLabel() . '_' . $job_item_type . '_' . $job_item_id;
  //
  //           break;
  //
  //         case 'simplified':
  //           $temp_name = $job_item_type . '_' . $job_item_id;
  //
  //           break;
  //
  //         default:
  //           $file_names[$job_item_id] = 'JobID' . $job->id() . '_' . $job->getSourceLangcode() . '_' . $job->getTargetLangcode();
  //
  //           break 2;
  //       }
  //
  //       $file_names[$job_item_id] = $temp_name;
  //     }
  //
  //     ksort($file_names);
  //     $filename = $this->cleanFileName(implode('_', $file_names) . '.' . $extension);
  //   }
  //   else {
  //     $filename = '';
  //   }
  // } catch (\Exception $e) {
  //   $filename = '';
  // }
  //
  // // Fallback to default file name.
  // if (empty($filename) || !$job->getSetting('identical_file_name')) {
  //   $filename = "JobID" . $job->id() . '_' . $job->getSourceLangcode() . '_' . $job->getTargetLangcode() . '.' . $extension;
  // }
  //
  // return $filename;
  try {
    $filename = $job
      ->get('job_file_name');
    $filename = !empty($filename
      ->getValue()) ? $filename
      ->getValue()[0]['value'] : '';
  } catch (\Exception $e) {
    $filename = '';
  }
  if (empty($filename)) {
    $extension = $job
      ->getSetting('export_format');
    $name = "JobID" . $job
      ->id() . '_' . $job
      ->getSourceLangcode() . '_' . $job
      ->getTargetLangcode();

    // Alter name before saving it into database.
    $cloned_job = clone $job;
    \Drupal::moduleHandler()
      ->alter('tmgmt_smartling_filename', $name, $cloned_job);
    $filename = $name . '.' . $extension;
  }
  return $filename;
}