170 lines
5.4 KiB
PHP
170 lines
5.4 KiB
PHP
<?php
|
|
|
|
class stTaskSchedulerImportProgressBarTask extends stTaskScheluderProgressBar
|
|
{
|
|
private $errors = [];
|
|
|
|
private $mainTask = null;
|
|
|
|
/**
|
|
*
|
|
* @var stEventDispatcher
|
|
*/
|
|
private $dispatcher;
|
|
|
|
public function getTitle()
|
|
{
|
|
return $this->getContext()->getI18N()->__('Import w trakcie...');
|
|
}
|
|
|
|
public function isReadyToExecute(): bool
|
|
{
|
|
return $this->getMainTask()->getStatus() == stTask::STATUS_PENDING;
|
|
}
|
|
|
|
public function initialize($offset = 0)
|
|
{
|
|
$this->errors = [];
|
|
$this->dispatcher = stEventDispatcher::getInstance();
|
|
$this->dispatcher->connect('task.logger.log', [$this, 'logErrors']);
|
|
|
|
if (null === $this->getParameter('task_pool'))
|
|
{
|
|
$taskPool = [];
|
|
$totalCount = 0;
|
|
$task = $this->getMainTask();
|
|
$taskConfiguration = stTaskSchedulerImportConfiguration::get($task->getId());
|
|
|
|
if (false !== $task->doInitialize(true))
|
|
{
|
|
$taskPool[] = ['id' => $task->getId(), 'offset' => $totalCount, 'parameters' => [
|
|
'import_file' => $this->getUploadedFile($task->getId()),
|
|
]];
|
|
|
|
$totalCount += $task->doCount();
|
|
|
|
foreach ($taskConfiguration->getChildren() as $child)
|
|
{
|
|
$childTask = stTaskScheluder::getTask($child->getId());
|
|
$childTask->setParameter('import_file', $this->getUploadedFile($child->getId()));
|
|
|
|
if (false === $childTask->doInitialize(true))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
$taskPool[] = [
|
|
'id' => $child->getId(),
|
|
'offset' => $totalCount,
|
|
'parameters' => [
|
|
'import_file' => $this->getUploadedFile($child->getId()),
|
|
]
|
|
];
|
|
|
|
$totalCount += $childTask->doCount();
|
|
}
|
|
|
|
if (null !== $taskConfiguration->getOption('preloaded_data_import'))
|
|
{
|
|
$taskPool[] = [
|
|
'id' => 'st_task_scheduler_import_preloaded_data',
|
|
'offset' => $totalCount,
|
|
'parameters' => [
|
|
'import_hash_id' => $taskConfiguration->getHashId(),
|
|
]
|
|
];
|
|
|
|
$totalCount += $task->doCount();
|
|
}
|
|
|
|
$taskPool = array_reverse($taskPool);
|
|
|
|
$this->setParameter('task_pool', $taskPool);
|
|
$this->setParameter('count', $totalCount);
|
|
}
|
|
}
|
|
|
|
parent::initialize($offset);
|
|
}
|
|
|
|
public function finished()
|
|
{
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
sfLoader::loadHelpers(['Helper', 'stUrl', 'stAdminGenerator']);
|
|
|
|
if (!$this->hasErrors())
|
|
{
|
|
$message = $i18n->__('Import został zakończony pomyślnie.', null, 'stTaskSchedulerImportBackend');
|
|
}
|
|
else
|
|
{
|
|
$message = $i18n->__('Wystąpiły błędy podczas importu. Sprawdź %logs%, aby dowiedzieć się wiecej.', [
|
|
'%logs%' => st_link_to($i18n->__('logi', null, 'stTaskSchedulerImportBackend'), '@stTaskScheluderBackend?action=logList&filters[task_id]='.$this->getMainTask()->getTask()->getId(), [
|
|
'target' => '_blank',
|
|
]),
|
|
], 'stTaskSchedulerImportBackend');
|
|
}
|
|
|
|
$content = content_tag('p', $message, ['class' => 'bs-mb-2']).st_get_admin_button('default', $i18n->__('Powrót', null, 'stTaskSchedulerImportBackend'), $this->getParameter('return_url'));
|
|
$this->setMessage($content);
|
|
|
|
$this->dispatcher->disconnect('task.logger.log', [$this, 'logErrors']);
|
|
}
|
|
|
|
public function logErrors(sfEvent $event): void
|
|
{
|
|
if ($event['type'] == stTaskLogger::TYPE_ERROR)
|
|
{
|
|
$this->errors[] = [
|
|
'message' => $event['message'],
|
|
'message_params' => $event['message_params'],
|
|
];
|
|
|
|
$this->setParameter('has_errors', true);
|
|
}
|
|
}
|
|
|
|
public function hasErrors(): bool
|
|
{
|
|
return $this->getParameter('has_errors', false);
|
|
}
|
|
|
|
public function getLastErrors(): array
|
|
{
|
|
return $this->errors;
|
|
}
|
|
|
|
public function getLastErrorsAsString($separator = '<br>'): string
|
|
{
|
|
$results = [];
|
|
|
|
foreach ($this->errors as $error)
|
|
{
|
|
$message = $this->getContext()->getI18N()->__($error['message'], $error['message_params']);
|
|
$results[] = stTextUtility::create($message)->markdown();
|
|
}
|
|
|
|
return implode($separator, $results);
|
|
}
|
|
|
|
public function getMainTask(): stTaskInterface
|
|
{
|
|
if (null === $this->mainTask)
|
|
{
|
|
$id = $this->getParameter('import_id');
|
|
$this->mainTask = stTaskScheluder::getTask($id);
|
|
$this->mainTask->setParameter('import_file', $this->getUploadedFile($id));
|
|
}
|
|
|
|
return $this->mainTask;
|
|
}
|
|
|
|
public function getUploadedFile($taskId): ?string
|
|
{
|
|
$files = $this->getParameter('uploaded_files', []);
|
|
|
|
return isset($files[$taskId]) ? sfConfig::get('sf_data_dir') . '/task-scheduler-import/upload/' . $files[$taskId] : null;
|
|
}
|
|
}
|