new type API for PDF

This commit is contained in:
Roland Gruber 2017-01-05 21:05:17 +01:00
parent a9234c1537
commit 8774583532
7 changed files with 97 additions and 78 deletions

View File

@ -690,7 +690,7 @@ class lamList {
$selAccounts[] = $id; $selAccounts[] = $id;
} }
// get possible PDF structures // get possible PDF structures
$pdf_structures = \LAM\PDF\getPDFStructureDefinitions($this->type->getId()); $pdf_structures = \LAM\PDF\getPDFStructures($this->type->getId());
$this->listPrintHeader(); $this->listPrintHeader();

View File

@ -67,7 +67,7 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$account_type = $accounts[0]->get_type(); $account_type = $accounts[0]->get_type();
// Get PDF structure from xml file // Get PDF structure from xml file
$load = \LAM\PDF\loadPDFStructureDefinitions($account_type->getId(), $pdf_structure); $load = \LAM\PDF\loadPDFStructure($account_type->getId(), $pdf_structure);
$structure = $load['structure']; $structure = $load['structure'];
// get list of PDF keys // get list of PDF keys
$pdfKeys = array(); $pdfKeys = array();

View File

@ -2,6 +2,7 @@
namespace LAM\PDF; namespace LAM\PDF;
use \htmlStatusMessage; use \htmlStatusMessage;
use \LAMException;
/* /*
$Id$ $Id$
@ -49,7 +50,7 @@ include_once("ldap.inc");
* scope. Each entry is a string being the filename that may be passed to the * scope. Each entry is a string being the filename that may be passed to the
* createModulePDF() function as second argument. * createModulePDF() function as second argument.
*/ */
function getPDFStructureDefinitions($scope = "user", $profile = null) { function getPDFStructures($scope = "user", $profile = null) {
$return = array(); $return = array();
if (!isset($profile)) { if (!isset($profile)) {
@ -70,17 +71,19 @@ function getPDFStructureDefinitions($scope = "user", $profile = null) {
} }
/** /**
* This function is used to get pdf structure from xml file. * This function is used to get the PDF structure from XML file.
* Used in createModulePDF.
* *
* @param string $scope The account scope for wich the PDF structure should be returned. * @param string $typeId the account type
* @param string $pdf_structure Structure name of selected scope wich should be returned. * @param string $name structure name
* *
* @return array PDF structure * @return array PDF structure
*/ */
function loadPDFStructureDefinitions($scope='user', $pdf_structure='default') { function loadPDFStructure($typeId, $name='default') {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
return null;
}
$parser = new xmlParser(); $parser = new xmlParser();
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $pdf_structure . '.' . $scope . '.xml'; $file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
$xml = $parser->parse($file); $xml = $parser->parse($file);
$border = array(); $border = array();
$structure = array(); $structure = array();
@ -100,16 +103,17 @@ function loadPDFStructureDefinitions($scope='user', $pdf_structure='default') {
/** /**
* Saves PDF structure definitions to XML file in format: <name>.<scope>.xml * Saves PDF structure to XML file in format: <name>.<typeId>.xml
* *
* @param string $scope account type * @param string $typeId account type
* @param string $definition Name of definition * @param string $name name of structure
* @return string "no perms" if access denied or "ok". * @return string "no perms" if access denied or "ok".
*/ */
function savePDFStructureDefinitions($scope, $definition) { function savePDFStructure($typeId, $name) {
if (!preg_match('/[a-zA-Z0-9\-\_]+/',$definition)) return 'no perms'; if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
if (!preg_match('/[a-zA-Z]+/',$scope)) return 'no perms'; return 'no perms';
$struct_file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $definition . '.' . $scope . '.xml'; }
$struct_file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
if(!is_writable(dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName())) { if(!is_writable(dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName())) {
return 'no perms'; return 'no perms';
} }
@ -157,15 +161,16 @@ function savePDFStructureDefinitions($scope, $definition) {
/** /**
* Deletes XML file with PDF structure definitions. * Deletes XML file with PDF structure definitions.
* *
* @param string $scope account type * @param string $typeId account type
* @param string $definition Name of definition to delete * @param string $name Name of definition to delete
* *
* @return boolean True if file was deleted or false if a problem occured. * @return boolean True if file was deleted or false if a problem occured.
*/ */
function deletePDFStructureDefinition($scope, $definition) { function deletePDFStructure($typeId, $name) {
if (!preg_match('/[a-zA-Z0-9\-\_]+/',$definition)) return false; if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/',$typeId)) {
if (!preg_match('/[a-zA-Z]+/',$scope)) return false; return false;
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $definition . '.' . $scope . '.xml'; }
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
if(is_file($file) && is_writable($file)) { if(is_file($file) && is_writable($file)) {
return unlink($file); return unlink($file);
} }
@ -196,46 +201,50 @@ function getAvailableLogos() {
return $return; return $return;
} }
/**
* Copies a PDF structure from the given source to target.
*
* @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param string $sourceStructureName structure name
* @param \LAM\TYPES\ConfiguredType $targetType target type
* @throws Exception
*/
function copyStructure($sourceType, $sourceStructureName, $targetType) {
if (!isValidPDFStructureName($sourceStructureName)) {
throw new LAMException(_('Failed to copy'));
}
$sourceConfig = $sourceType->getTypeManager()->getConfig()->getName();
$sourceTypeId = $sourceType->getId();
$targetConfig = $targetType->getTypeManager()->getConfig()->getName();
$targetTypeId = $targetType->getId();
$basePath = dirname(__FILE__) . '/../config/pdf/';
$src = $basePath . $sourceConfig . '/' . $sourceStructureName . '.' . $sourceTypeId;
$dst = $basePath . $targetConfig . '/' . $sourceStructureName . '.' . $targetTypeId;
if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceStructureName);
}
}
/** /**
* Copies PDF profiles to other server profiles. * Copies a PDF structure from the given source to global templates.
* *
* @param array $pdfProfiles PDF profile names * @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param String $scope account scope * @param string $sourceName structure name
* @param array $dests destinations * @throws Exception
*
* @return boolean operation succeeded
*/ */
function copyPdfProfiles($pdfProfiles, $scope, $dests = array()) { function copyStructureToTemplates($sourceType, $sourceName) {
$state = true; if (!isValidPDFStructureName($sourceName)) {
$profilePath = substr(__FILE__, 0, strlen(__FILE__) - 17) . 'config/pdf/'; throw new LAMException(_('Failed to copy'));
foreach ($pdfProfiles as $profile) { }
//part 1: server profile $sourceConfig = $sourceType->getTypeManager()->getConfig()->getName();
//part 2: account profile $sourceTypeId = $sourceType->getId();
$tmpArr = explode('##', $profile); $basePath = dirname(__FILE__) . '/../config/pdf/';
$src = $profilePath . $tmpArr[0] . '/' . $tmpArr[1] . '.' . $scope . '.xml'; $templatePath = dirname(__FILE__) . '/../config/templates/pdf/';
if (!empty($dests)) { $src = $basePath . $sourceConfig . '/' . $sourceName . '.' . $sourceTypeId;
foreach ($dests as $dest) { $dst = $templatePath . $sourceName . '.' . $sourceType->getScope();
if ($dest == 'templates*') { if (!@copy($src, $dst)) {
$dst = substr(__FILE__, 0, strlen(__FILE__) - 17) . 'config/templates/pdf/' . $tmpArr[1] . '.' . $scope . '.xml'; throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceName);
} else {
$dst = $profilePath . $dest . '/' . $tmpArr[1] . '.' . $scope . '.xml';
}
if (!@copy($src, $dst)) {
StatusMessage('ERROR', _('Failed to export!'), $tmpArr[1] . '.' . $scope . '.xml');
$state = false;
}
}
} else {
$dst = $profilePath . $_SESSION['config']->getName() . '/' . $tmpArr[1] . '.' . $scope . '.xml';
if (!@copy($src, $dst)) {
StatusMessage('ERROR', _('Failed to import!'), $tmpArr[1] . '.' . $scope . '.xml');
$state = false;
}
}
} }
return $state;
} }
/** /**
@ -285,9 +294,9 @@ function deletePDFLogo($name) {
// check if still in use // check if still in use
$activeTypes = $_SESSION['config']->get_ActiveTypes(); $activeTypes = $_SESSION['config']->get_ActiveTypes();
foreach ($activeTypes as $type) { foreach ($activeTypes as $type) {
$structures = getPDFStructureDefinitions($type); $structures = getPDFStructures($type);
foreach ($structures as $structure) { foreach ($structures as $structure) {
$data = loadPDFStructureDefinitions($type, $structure); $data = loadPDFStructure($type, $structure);
if ($data['page_definitions']['filename'] == $name) { if ($data['page_definitions']['filename'] == $name) {
return new htmlStatusMessage('ERROR', _('Unable to delete logo file.'), return new htmlStatusMessage('ERROR', _('Unable to delete logo file.'),
sprintf(_('Logo is still in use by PDF structure "%s" in account type "%s".'), $structure, \LAM\TYPES\getTypeAlias($type))); sprintf(_('Logo is still in use by PDF structure "%s" in account type "%s".'), $structure, \LAM\TYPES\getTypeAlias($type)));
@ -305,4 +314,14 @@ function deletePDFLogo($name) {
} }
} }
/**
* Returns if the give structure name is valid.
*
* @param string $name structure name
* @return boolean is valid
*/
function isValidPDFStructureName($name) {
return preg_match('/[a-zA-Z0-9\-\_]+/',$name) === 1;
}
?> ?>

View File

@ -106,7 +106,7 @@ $container->addElement(new htmlTitle(_('PDF editor')), true);
if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) { if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) {
// delete structure // delete structure
if (\LAM\PDF\deletePDFStructureDefinition($_POST['profileDeleteType'], $_POST['profileDeleteName'])) { if (\LAM\PDF\deletePDFStructure($_POST['profileDeleteType'], $_POST['profileDeleteName'])) {
$message = new htmlStatusMessage('INFO', _('Deleted PDF structure.'), \LAM\TYPES\getTypeAlias($_POST['profileDeleteType']) . ': ' . htmlspecialchars($_POST['profileDeleteName'])); $message = new htmlStatusMessage('INFO', _('Deleted PDF structure.'), \LAM\TYPES\getTypeAlias($_POST['profileDeleteType']) . ': ' . htmlspecialchars($_POST['profileDeleteName']));
$message->colspan = 10; $message->colspan = 10;
$container->addElement($message, true); $container->addElement($message, true);
@ -171,7 +171,7 @@ foreach ($sortedTypes as $typeId => $title) {
} }
// get list of templates for each account type // get list of templates for each account type
for ($i = 0; $i < sizeof($templateClasses); $i++) { for ($i = 0; $i < sizeof($templateClasses); $i++) {
$templateClasses[$i]['templates'] = \LAM\PDF\getPDFStructureDefinitions($templateClasses[$i]['typeId']); $templateClasses[$i]['templates'] = \LAM\PDF\getPDFStructures($templateClasses[$i]['typeId']);
} }
// check if a template should be edited // check if a template should be edited
@ -286,7 +286,7 @@ include '../main_header.php';
$tmpArr = array(); $tmpArr = array();
foreach ($configProfiles as $profile) { foreach ($configProfiles as $profile) {
if ($profile != $_SESSION['config']->getName()) { if ($profile != $_SESSION['config']->getName()) {
$accountProfiles = \LAM\PDF\getPDFStructureDefinitions($typeId, $profile); $accountProfiles = \LAM\PDF\getPDFStructures($typeId, $profile);
for ($p = 0; $p < sizeof($accountProfiles); $p++) { for ($p = 0; $p < sizeof($accountProfiles); $p++) {
$tmpArr[$profile][$accountProfiles[$p]] = $profile . '##' . $accountProfiles[$p]; $tmpArr[$profile][$accountProfiles[$p]] = $profile . '##' . $accountProfiles[$p];
} }

View File

@ -129,7 +129,7 @@ if(isset($_GET['submit'])) {
$saveErrors[] = array('ERROR', _('PDF structure name not valid'), _('The name for that PDF-structure you submitted is not valid. A valid name must consist of the following characters: \'a-z\',\'A-Z\',\'0-9\',\'_\',\'-\'.')); $saveErrors[] = array('ERROR', _('PDF structure name not valid'), _('The name for that PDF-structure you submitted is not valid. A valid name must consist of the following characters: \'a-z\',\'A-Z\',\'0-9\',\'_\',\'-\'.'));
} }
else { else {
$return = \LAM\PDF\savePDFStructureDefinitions($type->getId(), $_GET['pdfname']); $return = \LAM\PDF\savePDFStructure($type->getId(), $_GET['pdfname']);
if($return == 'ok') { if($return == 'ok') {
metaRefresh('pdfmain.php?savedSuccessfully=' . $_GET['pdfname']); metaRefresh('pdfmain.php?savedSuccessfully=' . $_GET['pdfname']);
exit; exit;
@ -356,14 +356,14 @@ foreach ($_GET as $key => $value) {
if(!isset($_SESSION['currentPDFStructure'])) { if(!isset($_SESSION['currentPDFStructure'])) {
// Load structure file to be edit // Load structure file to be edit
if(isset($_GET['edit'])) { if(isset($_GET['edit'])) {
$load = \LAM\PDF\loadPDFStructureDefinitions($type->getId(), $_GET['edit']); $load = \LAM\PDF\loadPDFStructure($type->getId(), $_GET['edit']);
$_SESSION['currentPDFStructure'] = $load['structure']; $_SESSION['currentPDFStructure'] = $load['structure'];
$_SESSION['currentPageDefinitions'] = $load['page_definitions']; $_SESSION['currentPageDefinitions'] = $load['page_definitions'];
$_GET['pdfname'] = $_GET['edit']; $_GET['pdfname'] = $_GET['edit'];
} }
// Load default structure file when creating a new one // Load default structure file when creating a new one
else { else {
$load = \LAM\PDF\loadPDFStructureDefinitions($type->getId()); $load = \LAM\PDF\loadPDFStructure($type->getId());
$_SESSION['currentPDFStructure'] = $load['structure']; $_SESSION['currentPDFStructure'] = $load['structure'];
$_SESSION['currentPageDefinitions'] = $load['page_definitions']; $_SESSION['currentPageDefinitions'] = $load['page_definitions'];
} }

View File

@ -145,9 +145,9 @@ foreach ($configProfiles as $profileName) {
if (!empty($_POST['import'])) { if (!empty($_POST['import'])) {
$cfg = new LAMCfgMain(); $cfg = new LAMCfgMain();
// check master password // check master password
$impMessage = null; $errMessage = null;
if (!$cfg->checkPassword($_POST['passwd_i_' . $_POST['typeId']])) { if (!$cfg->checkPassword($_POST['passwd_i_' . $_POST['typeId']])) {
$impMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); $errMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!'));
} }
elseif (!empty($_POST['importProfiles'])) { elseif (!empty($_POST['importProfiles'])) {
$options = array(); $options = array();
@ -155,20 +155,20 @@ if (!empty($_POST['import'])) {
$parts = explode('##', $importProfiles); $parts = explode('##', $importProfiles);
$options[] = array('conf' => $parts[0], 'typeId' => $parts[1], 'name' => $parts[2]); $options[] = array('conf' => $parts[0], 'typeId' => $parts[1], 'name' => $parts[2]);
} }
$impMessage = importProfiles($_POST['typeId'], $options, $serverProfiles, $typeManager); $errMessage = importProfiles($_POST['typeId'], $options, $serverProfiles, $typeManager);
} }
if ($impMessage != null) { if ($errMessage != null) {
$impMessage->colspan = 10; $errMessage->colspan = 10;
$container->addElement($impMessage, true); $container->addElement($errMessage, true);
} }
} }
// export profiles // export profiles
if (!empty($_POST['export'])) { if (!empty($_POST['export'])) {
$cfg = new LAMCfgMain(); $cfg = new LAMCfgMain();
// check master password // check master password
$impMessage = null; $errMessage = null;
if (!$cfg->checkPassword($_POST['passwd_e_' . $_POST['typeId']])) { if (!$cfg->checkPassword($_POST['passwd_e_' . $_POST['typeId']])) {
$impMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); $errMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!'));
} }
elseif (!empty($_POST['exportProfiles'])) { elseif (!empty($_POST['exportProfiles'])) {
$options = array(); $options = array();
@ -178,11 +178,11 @@ if (!empty($_POST['export'])) {
} }
$typeId = $_POST['typeId']; $typeId = $_POST['typeId'];
$name = $_POST['name_' . $typeId]; $name = $_POST['name_' . $typeId];
$impMessage = exportProfiles($typeId, $name, $options, $serverProfiles, $typeManager); $errMessage = exportProfiles($typeId, $name, $options, $serverProfiles, $typeManager);
} }
if ($impMessage != null) { if ($errMessage != null) {
$impMessage->colspan = 10; $errMessage->colspan = 10;
$container->addElement($impMessage, true); $container->addElement($errMessage, true);
} }
} }

View File

@ -267,7 +267,7 @@ function showMainPage($scope, $selectedModules) {
$pdfCheckbox = new htmlTableExtendedInputCheckbox('createPDF', $createPDF, _('Create PDF files')); $pdfCheckbox = new htmlTableExtendedInputCheckbox('createPDF', $createPDF, _('Create PDF files'));
$pdfCheckbox->setTableRowsToShow(array('pdfStructure')); $pdfCheckbox->setTableRowsToShow(array('pdfStructure'));
$inputContainer->addElement($pdfCheckbox, true); $inputContainer->addElement($pdfCheckbox, true);
$pdfStructures = \LAM\PDF\getPDFStructureDefinitions($scope); $pdfStructures = \LAM\PDF\getPDFStructures($scope);
$pdfSelected = array(); $pdfSelected = array();
if (isset($_POST['pdfStructure'])) { if (isset($_POST['pdfStructure'])) {
$pdfSelected = array($_POST['pdfStructure']); $pdfSelected = array($_POST['pdfStructure']);