PDF import with new type API

This commit is contained in:
Roland Gruber 2017-01-05 21:40:48 +01:00
parent 8774583532
commit 61647ed008
3 changed files with 118 additions and 36 deletions

View File

@ -218,8 +218,8 @@ function copyStructure($sourceType, $sourceStructureName, $targetType) {
$targetConfig = $targetType->getTypeManager()->getConfig()->getName(); $targetConfig = $targetType->getTypeManager()->getConfig()->getName();
$targetTypeId = $targetType->getId(); $targetTypeId = $targetType->getId();
$basePath = dirname(__FILE__) . '/../config/pdf/'; $basePath = dirname(__FILE__) . '/../config/pdf/';
$src = $basePath . $sourceConfig . '/' . $sourceStructureName . '.' . $sourceTypeId; $src = $basePath . $sourceConfig . '/' . $sourceStructureName . '.' . $sourceTypeId . '.xml';
$dst = $basePath . $targetConfig . '/' . $sourceStructureName . '.' . $targetTypeId; $dst = $basePath . $targetConfig . '/' . $sourceStructureName . '.' . $targetTypeId . '.xml';
if (!@copy($src, $dst)) { if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceStructureName); throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceStructureName);
} }
@ -240,8 +240,8 @@ function copyStructureToTemplates($sourceType, $sourceName) {
$sourceTypeId = $sourceType->getId(); $sourceTypeId = $sourceType->getId();
$basePath = dirname(__FILE__) . '/../config/pdf/'; $basePath = dirname(__FILE__) . '/../config/pdf/';
$templatePath = dirname(__FILE__) . '/../config/templates/pdf/'; $templatePath = dirname(__FILE__) . '/../config/templates/pdf/';
$src = $basePath . $sourceConfig . '/' . $sourceName . '.' . $sourceTypeId; $src = $basePath . $sourceConfig . '/' . $sourceName . '.' . $sourceTypeId . '.xml';
$dst = $templatePath . $sourceName . '.' . $sourceType->getScope(); $dst = $templatePath . $sourceName . '.' . $sourceType->getScope() . '.xml';
if (!@copy($src, $dst)) { if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceName); throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceName);
} }

View File

@ -118,32 +118,37 @@ if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) {
} }
} }
if (isset($_POST['importexport']) && ($_POST['importexport'] === '1')) { $configProfiles = getConfigProfiles();
$serverProfiles = array();
foreach ($configProfiles as $profileName) {
$serverProfiles[$profileName] = new \LAMConfig($profileName);
}
// import profiles
if (!empty($_POST['import'])) {
$cfg = new LAMCfgMain(); $cfg = new LAMCfgMain();
$impExpMessage = null; $typeId = $_POST['typeId'];
if (isset($_POST['importProfiles_' . $_POST['typeId']])) { // check master password
// check master password $errMessage = null;
if (!$cfg->checkPassword($_POST['passwd_' . $_POST['typeId']])) { if (!$cfg->checkPassword($_POST['passwd_i_' . $_POST['typeId']])) {
$impExpMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); $errMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!'));
}
elseif (\LAM\PDF\copyPdfProfiles($_POST['importProfiles_' . $_POST['typeId']], $_POST['typeId'])) {
$impExpMessage = new htmlStatusMessage('INFO', _('Import successful'));
}
} else if (isset($_POST['exportProfiles'])) {
// check master password
if (!$cfg->checkPassword($_POST['passwd'])) {
$impExpMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!'));
}
elseif (\LAM\PDF\copyPdfProfiles($_POST['exportProfiles'], $_POST['typeId'], $_POST['destServerProfiles'])) {
$impExpMessage = new htmlStatusMessage('INFO', _('Export successful'));
}
} }
if ($impExpMessage != null) { elseif (!empty($_POST['importProfiles_' . $typeId])) {
$impExpMessage->colspan = 10; $options = array();
$container->addElement($impExpMessage, true); foreach ($_POST['importProfiles_' . $typeId] as $importProfiles) {
$parts = explode('##', $importProfiles);
$options[] = array('conf' => $parts[0], 'typeId' => $parts[1], 'name' => $parts[2]);
}
$errMessage = importStructures($_POST['typeId'], $options, $serverProfiles, $typeManager);
}
if ($errMessage != null) {
$errMessage->colspan = 10;
$container->addElement($errMessage, true);
} }
} }
// upload logo file // upload logo file
if (isset($_POST['uploadLogo']) && !empty($_FILES['logoUpload']) && !empty($_FILES['logoUpload']['size'])) { if (isset($_POST['uploadLogo']) && !empty($_FILES['logoUpload']) && !empty($_FILES['logoUpload']['size'])) {
$file = $_FILES['logoUpload']['tmp_name']; $file = $_FILES['logoUpload']['tmp_name'];
@ -209,8 +214,6 @@ include '../main_header.php';
} }
// existing templates // existing templates
$configProfiles = getConfigProfiles();
$container->addElement(new htmlSubTitle(_("Manage existing PDF structures")), true); $container->addElement(new htmlSubTitle(_("Manage existing PDF structures")), true);
$existingContainer = new htmlTable(); $existingContainer = new htmlTable();
for ($i = 0; $i < sizeof($templateClasses); $i++) { for ($i = 0; $i < sizeof($templateClasses); $i++) {
@ -283,12 +286,19 @@ include '../main_header.php';
for ($i = 0; $i < sizeof($templateClasses); $i++) { for ($i = 0; $i < sizeof($templateClasses); $i++) {
$typeId = $templateClasses[$i]['typeId']; $typeId = $templateClasses[$i]['typeId'];
$tmpArr = array(); $scope = $templateClasses[$i]['scope'];
$importOptions = array();
foreach ($configProfiles as $profile) { foreach ($configProfiles as $profile) {
if ($profile != $_SESSION['config']->getName()) { $typeManagerImport = new \LAM\TYPES\TypeManager($serverProfiles[$profile]);
$accountProfiles = \LAM\PDF\getPDFStructures($typeId, $profile); $typesImport = $typeManagerImport->getConfiguredTypesForScope($scope);
for ($p = 0; $p < sizeof($accountProfiles); $p++) { foreach ($typesImport as $typeImport) {
$tmpArr[$profile][$accountProfiles[$p]] = $profile . '##' . $accountProfiles[$p]; if (($profile != $_SESSION['config']->getName()) || ($typeImport->getId() != $typeId)) {
$accountProfiles = \LAM\PDF\getPDFStructures($typeImport->getId(), $profile);
if (!empty($accountProfiles)) {
foreach ($accountProfiles as $accountProfile) {
$importOptions[$profile][$typeImport->getAlias() . ': ' . $accountProfile] = $profile . '##' . $typeImport->getId() . '##' . $accountProfile;
}
}
} }
} }
} }
@ -300,7 +310,7 @@ include '../main_header.php';
$container = new htmlTable(); $container = new htmlTable();
$container->addElement(new htmlOutputText(_('PDF structures')), true); $container->addElement(new htmlOutputText(_('PDF structures')), true);
$select = new htmlSelect('importProfiles_' . $typeId, $tmpArr, array(), count($tmpArr, 1) < 15 ? count($tmpArr, 1) : 15); $select = new htmlSelect('importProfiles_' . $typeId, $importOptions, array(), count($importOptions, 1) < 15 ? count($importOptions, 1) : 15);
$select->setMultiSelect(true); $select->setMultiSelect(true);
$select->setHasDescriptiveElements(true); $select->setHasDescriptiveElements(true);
$select->setContainsOptgroups(true); $select->setContainsOptgroups(true);
@ -312,11 +322,11 @@ include '../main_header.php';
$container->addElement(new htmlSpacer(null, '10px'), true); $container->addElement(new htmlSpacer(null, '10px'), true);
$container->addElement(new htmlOutputText(_("Master password")), true); $container->addElement(new htmlOutputText(_("Master password")), true);
$exportPasswd = new htmlInputField('passwd_' . $typeId); $exportPasswd = new htmlInputField('passwd_i_' . $typeId);
$exportPasswd->setIsPassword(true); $exportPasswd->setIsPassword(true);
$container->addElement($exportPasswd); $container->addElement($exportPasswd);
$container->addElement(new htmlHelpLink('236')); $container->addElement(new htmlHelpLink('236'));
$container->addElement(new htmlHiddenInput('importexport', '1')); $container->addElement(new htmlHiddenInput('import', '1'));
$container->addElement(new htmlHiddenInput('typeId', $typeId), true); $container->addElement(new htmlHiddenInput('typeId', $typeId), true);
addSecurityTokenToMetaHTML($container); addSecurityTokenToMetaHTML($container);
@ -384,4 +394,77 @@ echo '<div id="deleteProfileDialog" class="hidden"><form id="deleteProfileForm"
echo '</form></div>'; echo '</form></div>';
include '../main_footer.php'; include '../main_footer.php';
/**
* Imports the selected PDF structures.
*
* @param string $typeId type id
* @param array $options options
* @param \LAMConfig[] $serverProfiles server profiles (name => profile object)
* @param \LAM\TYPES\TypeManager $typeManager type manager
* @return \htmlStatusMessage message or null
*/
function importStructures($typeId, $options, &$serverProfiles, &$typeManager) {
foreach ($options as $option) {
$sourceConfName = $option['conf'];
$sourceTypeId = $option['typeId'];
$sourceName = $option['name'];
$sourceTypeManager = new \LAM\TYPES\TypeManager($serverProfiles[$sourceConfName]);
$sourceType = $sourceTypeManager->getConfiguredType($sourceTypeId);
$targetType = $typeManager->getConfiguredType($typeId);
if (($sourceType != null) && ($targetType != null)) {
try {
\LAM\PDF\copyStructure($sourceType, $sourceName, $targetType);
}
catch (\LAMException $e) {
return new \htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage());
}
}
}
return new \htmlStatusMessage('INFO', _('Import successful'));
}
/**
* Exports the selected account profile.
*
* @param string $typeId source type id
* @param string $name profile name
* @param array $options options
* @param \LAMConfig[] $serverProfiles server profiles (name => profile object)
* @param \LAM\TYPES\TypeManager $typeManager type manager
* @return \htmlStatusMessage message or null
*/
function exportStructures($typeId, $name, $options, &$serverProfiles, &$typeManager) {
$sourceType = $typeManager->getConfiguredType($typeId);
if ($sourceType == null) {
return null;
}
foreach ($options as $option) {
$targetConfName = $option['conf'];
if ($targetConfName == 'templates*') {
try {
\copyAccountProfileToTemplates($sourceType, $name);
}
catch (\LAMException $e) {
return new \htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage());
}
}
else {
$targetTypeId = $option['typeId'];
$targetTypeManager = new \LAM\TYPES\TypeManager($serverProfiles[$targetConfName]);
$targetType = $targetTypeManager->getConfiguredType($targetTypeId);
if ($targetType != null) {
try {
\copyAccountProfile($sourceType, $name, $targetType);
}
catch (\LAMException $e) {
return new \htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage());
}
}
}
}
return new \htmlStatusMessage('INFO', _('Export successful'));
}
?> ?>

View File

@ -14,7 +14,6 @@ use \htmlOutputText;
use \htmlHelpLink; use \htmlHelpLink;
use \htmlHiddenInput; use \htmlHiddenInput;
use \htmlInputField; use \htmlInputField;
use \htmlDiv;
/* /*
$Id$ $Id$