499 lines
19 KiB
PHP
499 lines
19 KiB
PHP
<?php
|
|
|
|
class stTaskSchedulerImportBackendActions extends autoStTaskSchedulerImportBackendActions
|
|
{
|
|
public function initialize($context)
|
|
{
|
|
$result = parent::initialize($context);
|
|
|
|
$this->checkSubscriptionTypes('stTaskSchedulerImportPlugin', [stCommunication::SUBSCRIPTION_STANDARD, stCommunication::SUBSCRIPTION_PRO, stCommunication::SUBSCRIPTION_VIP], [
|
|
'install_version' => '7.4.4',
|
|
]);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function executeExcludeYes()
|
|
{
|
|
stAuthUsersListener::checkModificationCredentials($this, $this->getRequest(), $this->getModuleName());
|
|
|
|
foreach (stTaskSchedulerImportCategoryMappingPeer::retrieveByPKs($this->getRequestParameter('st_task_scheduler_import_category_mapping[selected]', array())) as $mapping)
|
|
{
|
|
$mapping->setIsExcluded(true);
|
|
$mapping->save();
|
|
}
|
|
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
public function executeExcludeNo()
|
|
{
|
|
stAuthUsersListener::checkModificationCredentials($this, $this->getRequest(), $this->getModuleName());
|
|
|
|
foreach (stTaskSchedulerImportCategoryMappingPeer::retrieveByPKs($this->getRequestParameter('st_task_scheduler_import_category_mapping[selected]', array())) as $mapping)
|
|
{
|
|
$mapping->setIsExcluded(false);
|
|
$mapping->save();
|
|
}
|
|
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
protected function updateListItem($item, $data)
|
|
{
|
|
parent::updateListItem($item, $data);
|
|
|
|
if (isset($data['price_margin_type']))
|
|
{
|
|
$item->setPriceMarginType($data['price_margin_type']);
|
|
}
|
|
|
|
if (isset($data['price_margin']))
|
|
{
|
|
$item->setPriceMargin($data['price_margin']);
|
|
}
|
|
else
|
|
{
|
|
$item->setPriceMargin(null);
|
|
}
|
|
}
|
|
|
|
|
|
public function executeImportList()
|
|
{
|
|
stTaskScheluder::initialize();
|
|
|
|
stEventDispatcher::getInstance()->connect('Task.isAdminGeneratorActionVisible', [stTaskSchedulerImportListener::class, 'isImportListActionVisible']);
|
|
|
|
return parent::executeImportList();
|
|
}
|
|
|
|
public function executeUpload()
|
|
{
|
|
if (!sfConfig::get('sf_debug'))
|
|
{
|
|
return $this->forward404();
|
|
}
|
|
|
|
/**
|
|
* @var \stAdminFileUpload[]
|
|
*/
|
|
$uploaders = [];
|
|
$files = [];
|
|
$labels = [];
|
|
$request = $this->getRequest();
|
|
$i18n = $this->getContext()->getI18N();
|
|
$id = $this->getRequestParameter('id');
|
|
$task = stTaskFactory::createTaskById($id);
|
|
$factory = new stTaskSchedulerImportFactory();
|
|
$import = $factory->createImport($task);
|
|
|
|
if (!$import->getConfiguration()->isActive())
|
|
{
|
|
$this->setFlash('warning', $i18n->__('Import <b>%name%</b> jest wyłączony w konfiguracji.', [
|
|
'%name%' => $i18n->__($import->getConfiguration()->getLabel(), null, $import->getConfiguration()->getClass()),
|
|
]));
|
|
return $this->redirect('@stTaskSchedulerImportBackend?action=importList');
|
|
}
|
|
|
|
/**
|
|
* @var \stTaskSchedulerImportConfigurationInterface[]
|
|
*/
|
|
$imports = array_merge([$import->getConfiguration()], $import->getConfiguration()->getChildren());
|
|
|
|
foreach ($imports as $current)
|
|
{
|
|
$labels['upload{'. $current->getId() .'}{file}'] = $i18n->__($current->getLabel(), null, $current->getClass());
|
|
$uploaders[$current->getId()] = $this->createFileUpoader('upload['. $current->getId() .'][file]', null, [
|
|
'upload_dir' => false,
|
|
'accept' => [sfMimeType::extension2MimeType($import->getFiletypeExtension())],
|
|
'filename' => $current->getId(),
|
|
'required' => true,
|
|
]);
|
|
}
|
|
|
|
if ($request->getMethod() == sfRequest::POST)
|
|
{
|
|
foreach ($imports as $current)
|
|
{
|
|
$errors = [];
|
|
$uploader = $uploaders[$current->getId()];
|
|
$filename = $uploader->process($errors);
|
|
|
|
if (false !== $filename)
|
|
{
|
|
$files[$current->getId()] = $filename;
|
|
}
|
|
else
|
|
{
|
|
$request->setError('upload{'. $current->getId() .'}{file}', implode('<br>', $errors));
|
|
}
|
|
}
|
|
|
|
if (!$request->hasErrors())
|
|
{
|
|
foreach ($uploaders as $uploader)
|
|
{
|
|
$uploader->doMoveFiles(sfConfig::get('sf_data_dir'). '/task-scheduler-import/upload');
|
|
}
|
|
|
|
$request->setParameter('uploaded_files', $files);
|
|
$request->setParameter('view', 'upload');
|
|
$this->setTemplate('import');
|
|
return $this->forward($this->getModuleName(), 'import');
|
|
}
|
|
}
|
|
|
|
$this->getBreadcrumbsBuilder()->getDefaultBreadcrumbs()
|
|
->add($i18n->__('Importy z hurtowni'), '@stTaskSchedulerImportBackend?action=importList')
|
|
->add($i18n->__($import->getConfiguration()->getLabel(), null, $import->getConfiguration()->getClass()), '@stTaskSchedulerImportBackend?action=importConfiguration&id='.$id)
|
|
->add($i18n->__('Załącz plik'));
|
|
|
|
$this->configuration = $import->getConfiguration();
|
|
$this->labels = $labels;
|
|
$this->imports = $imports;
|
|
$this->uploaders = $uploaders;
|
|
$this->id = $id;
|
|
}
|
|
|
|
public function executeImport()
|
|
{
|
|
$id = $this->getRequestParameter('id');
|
|
$view = $this->getRequestParameter('view');
|
|
$returnUrl = $this->getReturnUrl($view, $id);
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
$progressBarTask = new stTaskSchedulerImportProgressBarTask($this->getContext());
|
|
$progressBarTask->clearParameters();
|
|
$progressBarTask->setParameter('import_id', $id);
|
|
$progressBarTask->setParameter('return_url', $returnUrl);
|
|
$progressBarTask->setParameter('uploaded_files', $this->getRequestParameter('uploaded_files'));
|
|
|
|
$importConfiguration = stTaskSchedulerImportConfiguration::get($id);
|
|
|
|
if (!$importConfiguration->isActive())
|
|
{
|
|
$this->setFlash('warning', $i18n->__('Import <b>%name%</b> jest wyłączony w konfiguracji.', [
|
|
'%name%' => $i18n->__($importConfiguration->getLabel(), null, $importConfiguration->getClass()),
|
|
]));
|
|
return $this->redirect($returnUrl);
|
|
}
|
|
|
|
if (!$progressBarTask->getMainTask()->isReadyToExecute(false))
|
|
{
|
|
$this->setFlash('info', $i18n->__('Import <b>%name%</b> jest już w trakcie wykonywania', [
|
|
'%name%' => $importConfiguration->getLabel(),
|
|
], 'stTaskSchedulerImportBackend'));
|
|
|
|
return $this->redirect($returnUrl);
|
|
}
|
|
|
|
$progressBarTask->getMainTask()->getTask()->doStart();
|
|
|
|
$progressBarTask->initialize();
|
|
|
|
$progressBarTask->getMainTask()->getTask()->doFinish();
|
|
|
|
if ($progressBarTask->hasErrors())
|
|
{
|
|
sfLoader::loadHelpers(['Helper', 'stUrl']);
|
|
|
|
$this->setFlash('error', $i18n->__('Wystąpiły błędy podczas importu:') . '<br>' . $progressBarTask->getLastErrorsAsString());
|
|
$this->setFlash('success', null);
|
|
|
|
return $this->redirect($returnUrl);
|
|
}
|
|
|
|
$this->progress_bar_task = $progressBarTask;
|
|
$this->title = $importConfiguration->getLabel();
|
|
}
|
|
|
|
public function executeImportConfiguration()
|
|
{
|
|
stTaskScheluder::initialize();
|
|
|
|
$i18n = $this->getContext()->getI18N();
|
|
$id = $this->getRequestParameter('id');
|
|
$config = stConfig::getInstance('stTaskSchedulerImportBackend');
|
|
$import = $config->get('import', []);
|
|
$importConfig = stTaskSchedulerImportConfiguration::get($id);
|
|
|
|
if ($this->getRequest()->getMethod() == sfRequest::POST)
|
|
{
|
|
$import[$id] = stConfig::trim($this->getRequestParameter('import', []));
|
|
|
|
$childImport = $this->getRequestParameter('child_import', []);
|
|
|
|
foreach ($importConfig->getChildren() as $child)
|
|
{
|
|
if (isset($childImport[$child->getId()]))
|
|
{
|
|
$import[$child->getId()] = stConfig::trim($childImport[$child->getId()]);
|
|
}
|
|
}
|
|
|
|
/*echo "<pre>";
|
|
print_r($import);
|
|
echo "<pre>";
|
|
die();*/
|
|
|
|
$config->set('import', $import);
|
|
|
|
if (!$this->getRequest()->hasErrors())
|
|
{
|
|
$config->save();
|
|
|
|
$task = TaskPeer::retrieveByTaskId($id);
|
|
$task->setIsActive(isset($import[$id]['enabled']));
|
|
$task->setTimeInterval($import[$id]['time_interval']);
|
|
$task->save();
|
|
|
|
foreach ($importConfig->getChildren() as $child)
|
|
{
|
|
$task = TaskPeer::retrieveByTaskId($child->getId());
|
|
$task->setIsActive(isset($import[$id]['enabled']));
|
|
$task->setTimeInterval($import[$id]['time_interval']);
|
|
$task->save();
|
|
}
|
|
|
|
$this->setFlash('success', $i18n->__('Twoje zmiany zostały zapisane'));
|
|
|
|
if ($this->hasRequestParameter('execute'))
|
|
{
|
|
return $this->redirect('@stTaskSchedulerImportBackend?action=import&id='.$id.'&view=config');
|
|
}
|
|
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
}
|
|
|
|
$this->id = $this->getRequestParameter('id');
|
|
$this->import = isset($import[$id]) ? $import[$id] : [];
|
|
$this->child_import = $import;
|
|
$this->import_config = $importConfig;
|
|
$this->code_prefix_disabled = $this->isCodePrefixDisabled($importConfig);
|
|
|
|
$this->getBreadcrumbsBuilder()->getDefaultBreadcrumbs()->add($i18n->__('Import - %name%', [
|
|
'%name%' => $importConfig->getLabel(),
|
|
]));
|
|
}
|
|
|
|
public function handleErrorImportConfiguration()
|
|
{
|
|
$this->executeImportConfiguration();
|
|
|
|
return sfView::SUCCESS;
|
|
}
|
|
|
|
public function validateImportConfiguration()
|
|
{
|
|
$request = $this->getRequest();
|
|
$i18n = $this->getContext()->getI18N();
|
|
$id = $this->getRequestParameter('id');
|
|
|
|
if ($request->getMethod() == sfRequest::POST)
|
|
{
|
|
$this->labels = [];
|
|
|
|
$import = stConfig::trim($request->getParameter('import', []));
|
|
$childImport = stConfig::trim($request->getParameter('child_import', []));
|
|
$importConfig = stTaskSchedulerImportConfiguration::get($id);
|
|
|
|
if (!isset($import['enabled']))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (!$importConfig->getOption('ignore_limit') && !stTaskSchedulerImportLimit::checkLimit() && !$importConfig->isActive())
|
|
{
|
|
$request->setError('import{enabled}', $i18n->__('Przekroczyłeś limit aktywnych importów'));
|
|
$this->labels['import{enabled}'] = $i18n->__("Włącz");
|
|
}
|
|
|
|
$urlValidator = new sfUrlValidator();
|
|
$urlValidator->initialize($this->getContext(), [
|
|
'url_error' => $i18n->__('Adres url jest niepoprawny'),
|
|
]);
|
|
|
|
|
|
if ($importConfig->with('product_price_array') && $import['price_array_enabled'] == 1 && $importConfig->isActive() )
|
|
{
|
|
$arr = $import['price_array'];
|
|
|
|
$prevTo = null;
|
|
$i = 0;
|
|
|
|
foreach($arr as $key => $value){
|
|
$i++;
|
|
|
|
if(empty($value['from'])){
|
|
|
|
$request->setError('import{price_array}{'.$key.'}{from}', $i18n->__("Jedna lub więcej wartości jest pusta w przedziale ").$i);
|
|
$this->labels['import{price_array}{'.$key.'}{from}'] = $i18n->__("Popraw przedział tabeli narzutów");
|
|
}
|
|
|
|
elseif(empty($value['to'])){
|
|
|
|
$request->setError('import{price_array}{'.$key.'}{to}', $i18n->__("Jedna lub więcej wartości jest pusta w przedziale ").$i);
|
|
$this->labels['import{price_array}{'.$key.'}{to}'] = $i18n->__("Popraw przedział tabeli narzutów");
|
|
}
|
|
|
|
elseif(empty($value['multiplier'])){
|
|
|
|
$request->setError('import{price_array}{'.$key.'}{multiplier}', $i18n->__("Jedna lub więcej wartości jest pusta w przedziale ").$i);
|
|
$this->labels['import{price_array}{'.$key.'}{multiplier}'] = $i18n->__("Popraw przedział tabeli narzutów");
|
|
}
|
|
|
|
elseif(empty($value['add'])){
|
|
|
|
$request->setError('import{price_array}{'.$key.'}{add}', $i18n->__("Jedna lub więcej wartości jest pusta w przedziale ").$i);
|
|
$this->labels['import{price_array}{'.$key.'}{add}'] = $i18n->__("Popraw przedział tabeli narzutów");
|
|
}
|
|
|
|
elseif ($value['from'] >= $value['to']){
|
|
$request->setError('import{price_array}{'.$key.'}{from}', $i18n->__('Wartość "Cena od" jest większa lub równa wartości "Cena do" w przedziale ').$i);
|
|
$this->labels['import{price_array}{'.$key.'}{from}'] = $i18n->__("Popraw przedział tabeli narzutów");
|
|
|
|
$request->setError('import{price_array}{'.$key.'}{to}','');
|
|
}
|
|
|
|
elseif($prevTo !== null && $value['from'] <= $prevTo) {
|
|
|
|
$request->setError('import{price_array}{'.$key.'}{from}', $i18n->__('Przedziały nachodzą na siebie. Sprawdź przedziały ') . ($i-1) . $i18n->__(' i ').($i));
|
|
$this->labels['import{price_array}{'.$key.'}{from}'] = $i18n->__("Popraw przedział tabeli narzutów");
|
|
}
|
|
|
|
$prevTo = $value['to'];
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
$error = null;
|
|
|
|
// if (!$urlValidator->execute($import['url'], $error))
|
|
// {
|
|
// $request->setError('import{url}', $error);
|
|
// $this->labels['import{url}'] = $i18n->__("Adres pliku");
|
|
// }
|
|
|
|
foreach ($importConfig->getChildren() as $child)
|
|
{
|
|
$error = null;
|
|
|
|
if ($child->with('url'))
|
|
{
|
|
// if (!$urlValidator->execute($childImport[$child->getId()]['url'], $error))
|
|
// {
|
|
// $request->setError('child_import{'.$child->getId().'}{url}', $error);
|
|
// $this->labels['child_import{'.$child->getId().'}{url}'] = $i18n->__("Adres pliku");
|
|
// }
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return !$request->hasErrors();
|
|
}
|
|
|
|
protected function updatestTaskSchedulerImportCategoryMappingFromRequest()
|
|
{
|
|
parent::updatestTaskSchedulerImportCategoryMappingFromRequest();
|
|
|
|
$data = $this->getRequestParameter('st_task_scheduler_import_category_mapping');
|
|
|
|
if (isset($data['price_margin_type']))
|
|
{
|
|
$this->st_task_scheduler_import_category_mapping->setPriceMarginType($data['price_margin_type']);
|
|
}
|
|
|
|
if (!isset($data['price_margin']))
|
|
{
|
|
$this->st_task_scheduler_import_category_mapping->setPriceMargin(null);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dodaje kryteria filtrowania dla listy mapowania kategorii
|
|
*
|
|
* @param Criteria $c
|
|
* @return void
|
|
*/
|
|
protected function addFiltersCriteria($c)
|
|
{
|
|
parent::addFiltersCriteria($c);
|
|
|
|
if (isset($this->filters['target_category_path']) && $this->filters['target_category_path'] !== '')
|
|
{
|
|
if ($this->filters['target_category_path'] == 'assigned')
|
|
{
|
|
$c->add(stTaskSchedulerImportCategoryMappingPeer::CATEGORY_ID, null, Criteria::ISNOTNULL);
|
|
}
|
|
else
|
|
{
|
|
$c->add(stTaskSchedulerImportCategoryMappingPeer::CATEGORY_ID, null, Criteria::ISNULL);
|
|
}
|
|
}
|
|
|
|
if (isset($this->filters['import_label']) && $this->filters['import_label'] !== '')
|
|
{
|
|
$import = stTaskSchedulerImportConfiguration::get($this->filters['import_label']);
|
|
$c->add(stTaskSchedulerImportCategoryMappingPeer::IMPORT_HASH_ID, $import->getHashId());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dodaje kryteria filtrowania dla listy produktów bez zmapowanych kategorii
|
|
*
|
|
* @param Criteria $c
|
|
* @return void
|
|
*/
|
|
protected function addProductMappingFiltersCriteria($c)
|
|
{
|
|
parent::addProductMappingFiltersCriteria($c);
|
|
|
|
$c->add(stTaskSchedulerImportCategoryMappingPeer::CATEGORY_ID, null, Criteria::ISNULL);
|
|
}
|
|
|
|
protected function addImportFiltersCriteria($c)
|
|
{
|
|
parent::addImportFiltersCriteria($c);
|
|
|
|
$ids = [];
|
|
|
|
foreach (stTaskSchedulerImportConfiguration::getAll() as $config)
|
|
{
|
|
if (!$config->getParent())
|
|
{
|
|
$ids[$config->getLabel()] = $config->getId();
|
|
}
|
|
}
|
|
|
|
ksort($ids, SORT_NATURAL);
|
|
|
|
$ids = array_values($ids);
|
|
|
|
$c->add(TaskPeer::TASK_ID, $ids, Criteria::IN);
|
|
$c->addOrderByField(TaskPeer::TASK_ID, $ids);
|
|
}
|
|
|
|
protected function isCodePrefixDisabled(stTaskSchedulerImportConfigurationInterface $config): bool
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(stTaskSchedulerImportProductPeer::IMPORT_HASH_ID, $config->getHashId());
|
|
return stTaskSchedulerImportProductPeer::doCount($c) > 0;
|
|
}
|
|
|
|
protected function getReturnUrl(string $type, string $importId): string
|
|
{
|
|
$urls = array(
|
|
'list' => '@stTaskSchedulerImportBackend?action=importList',
|
|
'config' => '@stTaskSchedulerImportBackend?action=importConfiguration&id='.$importId,
|
|
'upload' => '@stTaskSchedulerImportBackend?action=upload&id='.$importId,
|
|
);
|
|
|
|
return isset($urls[$type]) ? $urls[$type] : $urls['list'];
|
|
}
|
|
} |