Files
2025-03-12 17:06:23 +01:00

366 lines
16 KiB
PHP

<?php if ($this->getParameterValue('export')) : ?>
public function executeExport()
{
$this->getResponse()->addStylesheet('backend/stImportExportPlugin.css');
stAuthUsersListener::checkAccessCredentials($this, $this->getRequest(), $this->getModuleName());
$i18n = $this->getContext()->getI18N();
$this->processExportForwardParameters();
$this->errors = false;
$this->export = false;
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
$data = $this->getRequestParameter('export');
$exportType = $data['type'];
$exportProfile = isset($data['profile']) ? $data['profile'] : null;
$customParameters = isset($data['custom_parameters']) ? $data['custom_parameters'] : array();
if (class_exists($exportType))
{
if (!is_dir(sfConfig::get('sf_data_dir').'/export'))
{
mkdir(sfConfig::get('sf_data_dir').'/export', 0755);
}
$filename = $i18n->__('<?php echo $this->getParameterValue('export.filename', 'export') ?>').' '.date('Y-m-d').'T'.date('H-i-s');
$filepath = sfConfig::get('sf_data_dir').'/export/'.str_replace(' ', '-', $filename).'.tmp';
$exporter = new <?php echo $this->getModuleName() ?>ImportExport('export', $exportType, $filepath, $exportProfile, $filepath);
$exporter->setI18nCatalogue('<?php echo $this->getModuleName() ?>ImportExport');
if ($this->hasRequestParameter('sample_file'))
{
$filename = $i18n->__('<?php echo $this->getParameterValue('export.filename', 'export') ?>').' - '.$i18n->__('przykladowy plik', null, 'stImportExportBackend');
stImportExportPropel::fileCleanup();
$this->setLayout(false);
$response = $this->getContext()->getResponse();
$response->setContentType("application/octet-stream");
$response->setHttpHeader('Content-Disposition', 'attachment; filename="'.$filename.'.'.$exporter->class_handle->output_file_extension.'"');
return $this->renderText($exporter->sampleFile());
}
$dataCount = $exporter->getDataCount();
$this->pb = new stProgressBar('<?php echo $this->getModuleName() ?>','doExport',$dataCount);
$this->pb->setParam("exporter", $exportType);
$this->pb->setParam("filename", $filename);
$this->pb->setParam("profile", $exportProfile);
$this->pb->setParam("custom_parameters", $customParameters);
$exporter->setCustomParameters($customParameters);
stImportExportLog::clearLogs('export');
$this->actual_step = $exporter->doProcess(0);
$this->export = true;
if ($dataCount==0 || $this->actual_step>=$dataCount)
{
$this->errors = $exporter->getImporterExporter()->getLogger()->hasLog();
$this->logFile = basename($exporter->getImporterExporter()->getLogger()->getFilename());
$link = $this->getController()->genUrl("<?php echo $this->getModuleName() ?>/exportDownload?filename=".$filename."&ext=".$exporter->class_handle->output_file_extension , true);
sfLoader::loadHelpers('Partial');
$this->pb->setMsg(get_partial('export_link', array('link'=>$link)));
stImportExportPropel::fileCleanup();
}
}
}
if (!$this->export)
{
<?php if ($this->getParameterValue('export.show_profiles', true)): ?>
$this->profiles = stImportExportPropel::getProfiles('<?php echo $this->getModuleName() ?>', '<?php echo $this->getClassName() ?>');
<?php else: ?>
$this->profiles = null;
<?php endif ?>
$this->export_types = $this->getExportTypes();
$this->custom_parameters = $this->getExportCustomParameters();
$this->default_export_type = '<?php echo $this->getParameterValue('export.default', key($this->getParameterValue('exporters'))) ?>';
}
$this->getBreadcrumbsBuilder()->getExportBreadcrumbs();
}
public function executeDoExport()
{
$this->errors = false;
$this->actual_step = $this->getRequestParameter('step');
$this->pb = new stProgressBar('<?php echo $this->getModuleName() ?>','doExport');
$filename = $this->pb->getParam('filename');
$filepath = sfConfig::get('sf_data_dir').'/export/'.str_replace(' ', '-', $filename).'.tmp';
$exporter = new <?php echo $this->getModuleName() ?>ImportExport('export', $this->pb->getParam('exporter'), $filepath, $this->pb->getParam('profile'));
$exporter->setI18nCatalogue('<?php echo $this->getModuleName() ?>ImportExport');
$exporter->setCustomParameters($this->pb->getParam('custom_parameters'));
$this->actual_step = $exporter->doProcess($this->actual_step);
if ($this->actual_step>=$this->pb->getParam('steps'))
{
$this->errors = $exporter->getImporterExporter()->getLogger()->hasLog();
$this->logFile = basename($exporter->getImporterExporter()->getLogger()->getFilename());
$link = $this->getController()->genUrl("<?php echo $this->getModuleName() ?>/exportDownload?filename=".$filename."&ext=".$exporter->class_handle->output_file_extension , true);
sfLoader::loadHelpers('Partial');
$this->pb->setMsg(get_partial('export_link', array('link'=>$link)));
stImportExportPropel::fileCleanup();
}
}
public function executeExportLog()
{
$logFile = $this->getRequestParameter('file');
$logger = new stImportExportLog(sfConfig::get('sf_log_dir').DIRECTORY_SEPARATOR.$logFile);
$this->logs = $logger->getLog();
}
public function executeExportDownload()
{
$filename = $this->getRequestParameter('filename');
$file_extension = $this->getRequestParameter('ext');
$tmp = sfConfig::get('sf_data_dir').'/export/'.str_replace(' ', '-', $filename).'.tmp';
$this->setLayout(false);
$response = $this->getContext()->getResponse();
$response->setContentType("application/octet-stream");
$response->setHttpHeader('Content-Disposition', 'attachment; filename="'.$filename.'.'.$file_extension.'"');
$response->sendHttpHeaders();
$handle = fopen($tmp,'r');
if ($handle)
{
while (!feof($handle))
{
print fread($handle, 8192);
ob_flush();
flush();
}
fclose($handle);
}
throw new sfStopException();
}
public function getExportTypes()
{
return array(
<?php foreach ($this->getParameterValue('exporters', array()) as $type => $label): ?>
'<?php echo $type ?>' => '<?php echo addcslashes($label, "'") ?>',
<?php endforeach ?>
);
}
public function getExportCustomParameters()
{
return array(
<?php foreach ($this->getParameterValue('export.custom_parameters', array()) as $name => $options): ?>
'<?php echo $name ?>' => <?php echo strtr(var_export($options, true), "\n", "") ?>,
<?php endforeach ?>
);
}
<?php endif; ?>
<?php if ($this->getParameterValue('import')) : ?>
public function executeImport()
{
$this->getResponse()->addStylesheet('backend/stImportExportPlugin.css');
stAuthUsersListener::checkModificationCredentials($this, $this->getRequest(), $this->getModuleName());
$this->import = false;
$i18n = $this->getContext()->getI18N();
$this->processImportForwardParameters();
// obluga wyslanego formualrza
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
if($this->getRequest()->getFileName('import[filename]') || $this->hasRequestParameter('sample_file') || $this->hasRequestParameter('server_file'))
{
$importerType = $this->getRequestParameter('import[type]');
if (class_exists($importerType))
{
if (!is_dir(sfConfig::get('sf_data_dir').'/import'))
{
mkdir(sfConfig::get('sf_data_dir').'/import', 0755);
}
$filename = '<?php echo str_replace(" ", "-", $this->getParameterValue('import.filename', 'import')) ?>-'.date('Y-m-d').'T'.date('H-i-s').'-'.uniqid();
$filepath = sfConfig::get('sf_data_dir').'/import/'.$filename.'.tmp';
if ($this->getRequestParameter('server_file'))
{
copy(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'import'.DIRECTORY_SEPARATOR.$this->getRequestParameter('server_file'), $filepath);
}
else
{
$this->getRequest()->moveFile('import[filename]', $filepath);
}
$importer = new <?php echo $this->getModuleName() ?>ImportExport('import', $importerType, $filepath);
$importer->setI18nCatalogue('<?php echo $this->getModuleName() ?>ImportExport');
if ($this->hasRequestParameter('sample_file'))
{
stImportExportPropel::fileCleanup();
$filename = $i18n->__('<?php echo $this->getParameterValue('import.filename', 'import') ?>').' - '.$i18n->__('przykladowy plik', null, 'stImportExportBackend');
$this->setLayout(false);
$response = $this->getContext()->getResponse();
$response->setContentType("application/octet-stream");
$response->setHttpHeader('Content-Disposition', 'attachment; filename="'.$filename.'.'.$importer->class_handle->input_file_extension.'"');
return $this->renderText($importer->sampleFile());
}
$valid = $importer->validateFile($errors);
$items = $importer->getDataCount();
if ($items && $valid)
{
$this->pb = new stProgressBar('<?php echo $this->getModuleName() ?>','doImport',$items);
$this->pb->setParam("importer",$importerType);
$this->pb->setParam("filename",$filename);
stImportExportLog::clearLogs('import');
$this->import = true;
}
else
{
if ($errors)
{
$this->getRequest()->setError('import{filename}', implode("<br>", $errors));
}
else
{
$this->getRequest()->setError('import{filename}', sfContext::getInstance()->getI18n()->__('Nieprawidłowy format pliku', array(), 'stImportExportBackend'));
}
$this->import = false;
}
}
}
}
if (!$this->import)
{
$this->import_types = $this->getImportTypes();
$this->default_import_type = '<?php echo $this->getParameterValue('import.default', key($this->getParameterValue('importers'))) ?>';
}
$this->getBreadcrumbsBuilder()->getImportBreadcrumbs();
}
public function executeDoImport()
{
stFastCacheManager::disableClearCache();
stFunctionCache::disableClearCache(array('stTax'));
stPartialCache::disableClearCache();
$this->setLayout(false);
$this->errors = false;
$this->actual_step = $this->getRequestParameter('step');
$this->pb = new stProgressBar('<?php echo $this->getModuleName() ?>','doImport');
$filename = $this->pb->getParam("filename");
$filepath = sfConfig::get('sf_data_dir').'/import/'.$filename.'.tmp';
$importer = new <?php echo $this->getModuleName() ?>ImportExport('import', $this->pb->getParam('importer'), $filepath);
$importer->setI18nCatalogue('<?php echo $this->getModuleName() ?>ImportExport');
$this->actual_step = $importer->doProcess($this->actual_step);
if ($this->actual_step>=$this->pb->getParam('steps'))
{
if ($importer->getImporterExporter()->getLogger()->hasLog())
{
$this->errors = $importer->getImporterExporter()->getLogger()->hasLog();
$this->logFile = basename($importer->getImporterExporter()->getLogger()->getFilename());
$this->pb->setMsg(sfContext::getInstance()->getI18n()->__('Wystąpiły błędy podczas importu danych.', array(), 'stImportExportBackend'));
}
else
{
if (is_callable(array('<?php echo $this->getModuleName() ?>ImportExport', 'onImportFinished')))
{
call_user_func(array('<?php echo $this->getModuleName() ?>ImportExport', 'onImportFinished'));
}
$this->pb->setMsg(sfContext::getInstance()->getI18n()->__('Dane zostały zaimportowane pomyślnie', array(), 'stImportExportBackend'));
}
stFunctionCache::enableClearCache();
stPartialCache::enableClearCache();
stFastCacheManager::enableClearCache();
stFunctionCache::clearAll();
stPartialCache::clearAll('frontend');
stFastCacheManager::clearCache();
stImportExportPropel::fileCleanup();
}
}
public function executeImportLog()
{
$logFile = $this->getRequestParameter('file');
$logger = new stImportExportLog(sfConfig::get('sf_log_dir').DIRECTORY_SEPARATOR.$logFile);
$this->logs = $logger->getLog();
}
public function handleErrorImport()
{
$this->processImportForwardParameters();
$i18n = $this->getContext()->getI18N();
$this->import = false;
$this->import_types = $this->getImportTypes();
$this->default_import_type = '<?php echo $this->getParameterValue('import.default', key($this->getParameterValue('importers'))) ?>';
$this->getBreadcrumbsBuilder()->getDefaultBreadcrumbs()->add($i18n->__('<?php echo $this->getParameterValue('import.title', 'Eksport') ?>', null, '<?php echo $this->getModuleName() ?>'));
return sfView::SUCCESS;
}
public function validateImport()
{
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
$type = $this->getRequestParameter('import[type]');
// The name field is required
if (!$type)
{
$this->getRequest()->setError('import{type}', sfContext::getInstance()->getI18n()->__('Proszę wybrać format pliku', array(), 'stImportExportBackend'));
return false;
}
$filename = $this->getRequest()->getFileName('import[filename]');
// The name field is required
if (!$filename && !$this->hasRequestParameter('sample_file'))
{
if (!$this->hasRequestParameter('server_file')) {
$this->getRequest()->setError('import{filename}', sfContext::getInstance()->getI18n()->__('Nie wybrano pliku z danymi', array(), 'stImportExportBackend'));
return false;
}
}
}
return true;
}
public function getImportTypes()
{
return array(
<?php foreach ($this->getParameterValue('importers', array()) as $type => $label): ?>
'<?php echo $type ?>' => '<?php echo addcslashes($label, "'") ?>',
<?php endforeach ?>
);
}
protected function processExportForwardParameters()
{
$this->forward_parameters = $this->processForwardParameters(array(<?php echo $this->getForwardParameters('export') ? "'".implode("', '", $this->getForwardParameters('export'))."'" : '' ?>), '<?php echo $this->getGeneratedModuleName() ?>', 'export');
}
protected function processImportForwardParameters()
{
$this->forward_parameters = $this->processForwardParameters(array(<?php echo $this->getForwardParameters('import') ? "'".implode("', '", $this->getForwardParameters('import'))."'" : '' ?>), '<?php echo $this->getGeneratedModuleName() ?>', 'import');
}
<?php endif; ?>