16 use Translate\Controller\ProcessParams;
32 private int $tabId = 0;
36 private bool $convertEncoding;
38 private string $encodingOut;
40 private bool $collectUntranslated;
42 private bool $appendSamples;
43 private int $samplesCount = 10;
44 private array $samplesRestriction = [];
47 private array $languages;
50 private array $export;
51 private array $samples;
60 $configureActions = parent::configureActions();
69 'class' => Translate\Controller\Export\ExportPath::class,
75 'class' => Translate\Controller\Export\ExportFile::class,
81 'class' => Translate\Controller\Export\ExportFileList::class,
115 Main\Engine\ActionFilter\Csrf::class,
122 return $configureActions;
135 if ($this->request->get(
'tabId') !==
null)
137 $this->tabId = (int)$this->request->get(
'tabId');
142 $this->collectUntranslated = ($this->request->get(
'collectUntranslated') ===
'Y');
145 $this->appendSamples = ($this->request->get(
'appendSamples') ===
'Y');
146 $this->samplesCount = (int)$this->request->get(
'samplesCount') ?: 10;
148 $this->samplesRestriction = [];
149 if (!empty($this->request->get(
'samplesRestriction')))
151 $this->samplesRestriction = array_filter($this->request->get(
'samplesRestriction'),
'intVal');
155 $this->convertEncoding = ($this->request->get(
'convertEncoding') ===
'Y');
157 $this->encodingOut =
'';
158 if ($this->convertEncoding)
160 $this->encodingOut =
'utf-8';
164 $enabledLanguages = Translate\Config::getEnabledLanguages();
165 $languages = $this->request->get(
'languages');
166 if (\is_array($languages) && !\in_array(
'all', $languages))
168 $languages = \array_intersect($languages, $enabledLanguages);
169 $sortLang = \array_flip($enabledLanguages);
172 function ($a, $b) use ($sortLang)
176 return (($a == $b) ? 1 : ($a < $b ? -1 : 1));
182 $languages = $enabledLanguages;
184 $this->languages = $languages;
197 public function exportAction($tabId, $path =
''): array
199 if (empty($tabId) || (
int)$tabId <= 0)
205 $path = Translate\Config::getDefaultPath();
209 $action = $this->detectAction($path);
211 $result = $action->run($path,
true);
213 if (\count($action->getErrors()) > 0)
218 if ($action->hasProcessCompleted() && $result[
'TOTAL_ITEMS'] == 0)
222 elseif ($action->hasProcessCompleted())
224 $fileProperties = $action->getDownloadingParameters();
225 $result[
'FILE_NAME'] = $fileProperties[
'fileName'];
226 $result[
'DOWNLOAD_LINK'] = $this->generateDownloadLink($fileProperties,
'export');
228 $messagePlaceholders = [
229 '#TOTAL_PHRASES#' => $result[
'TOTAL_PHRASES'],
230 '#FILE_SIZE_FORMAT#' => \CFile::formatSize($fileProperties[
'fileSize']),
231 '#FILE_NAME#' => $result[
'FILE_NAME'],
232 '#FILE_LINK#' => $result[
'DOWNLOAD_LINK'],
236 .
"\n".
Loc::getMessage(
'TR_EXPORT_ACTION_EXPORT', $messagePlaceholders)
240 if ($this->appendSamples)
242 if ($result[
'TOTAL_SAMPLES'] > 0)
244 $fileSamplesProperties = $action->getDownloadingSamplesParameters();
245 $result[
'SAMPLES_LINK'] = $this->generateDownloadLink($fileSamplesProperties,
'samples');
246 $result[
'SAMPLES_FILE'] = $fileSamplesProperties[
'fileName'];
248 $messagePlaceholders = [
249 '#TOTAL_SAMPLES#' => $result[
'TOTAL_SAMPLES'],
250 '#FILE_SIZE_FORMAT#' => \CFile::formatSize($fileSamplesProperties[
'fileSize']),
251 '#FILE_NAME#' => $result[
'SAMPLES_FILE'],
252 '#FILE_LINK#' => $result[
'SAMPLES_LINK'],
254 $result[
'SUMMARY'] .=
"\n" .
Loc::getMessage(
'TR_EXPORT_SAMPLES', $messagePlaceholders);
255 $result[
'SUMMARY'] .=
" " .
Loc::getMessage(
'TR_EXPORT_DOWNLOAD', $messagePlaceholders);
259 $result[
'SUMMARY'] .=
"\n" .
Loc::getMessage(
'TR_EXPORT_SAMPLES_NOT_FOUND');
275 private function detectAction($path): ExportAction
279 $exporterClass = ExportFileList::class;
283 !empty($this->filter[
'FILE_NAME'])
284 || !empty($this->filter[
'FOLDER_NAME'])
285 || !empty($this->filter[
'INCLUDE_PATHS'])
286 || !empty($this->filter[
'EXCLUDE_PATHS'])
290 $exporterClass = ExportFileSearch::class;
294 $pathList = $this->request->get(
'pathList');
295 if (!empty($pathList))
298 $exporterClass = ExportPath::class;
303 !empty($this->filter[
'PHRASE_CODE'])
304 || !empty($this->filter[
'INCLUDE_PHRASE_CODES'])
305 || !empty($this->filter[
'EXCLUDE_PHRASE_CODES'])
306 || !empty($this->filter[
'PHRASE_TEXT'])
310 $exporterClass = ExportPhraseSearch::class;
313 $codeList = $this->request->get(
'codeList');
314 if (!empty($pathList) && !empty($codeList))
317 $exporterClass = ExportPath::class;
322 if (\preg_match(
"/\.php$/", $path))
325 $exporterClass = ExportFile::class;
330 $action =
new $exporterClass(
334 'tabId' => $this->tabId,
335 'collectUntranslated' => $this->collectUntranslated,
336 'appendSamples' => $this->appendSamples,
337 'samplesCount' => $this->samplesCount,
338 'samplesRestriction' => $this->samplesRestriction,
339 'convertEncoding' => $this->convertEncoding,
340 'encodingOut' => $this->encodingOut,
341 'languages' => $this->languages,
342 'filter' => $this->filter,
398 ->keepField(
'export')
399 ->keepField(
'samples')
400 ->restoreProgressParameters();
402 foreach ([
'export',
'samples'] as $type)
404 if (!empty($this->{$type}[
'filePath']))
407 if ($path->isExists())
418 'STATUS' => Translate\Controller\STATUS_COMPLETED
430 private function generateDownloadLink(array $params,
string $type): string
432 $this->{$type} = $params;
433 $this->
keepField($type)->saveProgressParameters();
435 return $this->
getActionUri(self::ACTION_DOWNLOAD, [
'tabId' => $this->tabId,
'type' => $type])->getUri();
452 if (empty($type) || !in_array($type, [
'export',
'samples'],
true))
457 $this->
keepField($type)->restoreProgressParameters();
459 if (!empty($this->{$type}[
'filePath']) && !empty($this->{$type}[
'fileName']))
462 if ($path->isExists())
466 $this->{$type}[
'fileName'],
467 $this->{$type}[
'fileType']
getActionUri(string $actionName, array $params=[], bool $absolute=false)
static getMessage($code, $replace=null, $language=null)