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;
}
// get possible PDF structures
$pdf_structures = \LAM\PDF\getPDFStructureDefinitions($this->type->getId());
$pdf_structures = \LAM\PDF\getPDFStructures($this->type->getId());
$this->listPrintHeader();

View File

@ -67,7 +67,7 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$account_type = $accounts[0]->get_type();
// 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'];
// get list of PDF keys
$pdfKeys = array();

View File

@ -2,6 +2,7 @@
namespace LAM\PDF;
use \htmlStatusMessage;
use \LAMException;
/*
$Id$
@ -49,7 +50,7 @@ include_once("ldap.inc");
* scope. Each entry is a string being the filename that may be passed to the
* createModulePDF() function as second argument.
*/
function getPDFStructureDefinitions($scope = "user", $profile = null) {
function getPDFStructures($scope = "user", $profile = null) {
$return = array();
if (!isset($profile)) {
@ -70,17 +71,19 @@ function getPDFStructureDefinitions($scope = "user", $profile = null) {
}
/**
* This function is used to get pdf structure from xml file.
* Used in createModulePDF.
* This function is used to get the PDF structure from XML file.
*
* @param string $scope The account scope for wich the PDF structure should be returned.
* @param string $pdf_structure Structure name of selected scope wich should be returned.
* @param string $typeId the account type
* @param string $name structure name
*
* @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();
$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);
$border = 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 $definition Name of definition
* @param string $typeId account type
* @param string $name name of structure
* @return string "no perms" if access denied or "ok".
*/
function savePDFStructureDefinitions($scope, $definition) {
if (!preg_match('/[a-zA-Z0-9\-\_]+/',$definition)) return 'no perms';
if (!preg_match('/[a-zA-Z]+/',$scope)) return 'no perms';
$struct_file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $definition . '.' . $scope . '.xml';
function savePDFStructure($typeId, $name) {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
return 'no perms';
}
$struct_file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
if(!is_writable(dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName())) {
return 'no perms';
}
@ -157,15 +161,16 @@ function savePDFStructureDefinitions($scope, $definition) {
/**
* Deletes XML file with PDF structure definitions.
*
* @param string $scope account type
* @param string $definition Name of definition to delete
* @param string $typeId account type
* @param string $name Name of definition to delete
*
* @return boolean True if file was deleted or false if a problem occured.
*/
function deletePDFStructureDefinition($scope, $definition) {
if (!preg_match('/[a-zA-Z0-9\-\_]+/',$definition)) return false;
if (!preg_match('/[a-zA-Z]+/',$scope)) return false;
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $definition . '.' . $scope . '.xml';
function deletePDFStructure($typeId, $name) {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/',$typeId)) {
return false;
}
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
if(is_file($file) && is_writable($file)) {
return unlink($file);
}
@ -196,47 +201,51 @@ function getAvailableLogos() {
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 String $scope account scope
* @param array $dests destinations
*
* @return boolean operation succeeded
* @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param string $sourceName structure name
* @throws Exception
*/
function copyPdfProfiles($pdfProfiles, $scope, $dests = array()) {
$state = true;
$profilePath = substr(__FILE__, 0, strlen(__FILE__) - 17) . 'config/pdf/';
foreach ($pdfProfiles as $profile) {
//part 1: server profile
//part 2: account profile
$tmpArr = explode('##', $profile);
$src = $profilePath . $tmpArr[0] . '/' . $tmpArr[1] . '.' . $scope . '.xml';
if (!empty($dests)) {
foreach ($dests as $dest) {
if ($dest == 'templates*') {
$dst = substr(__FILE__, 0, strlen(__FILE__) - 17) . 'config/templates/pdf/' . $tmpArr[1] . '.' . $scope . '.xml';
} else {
$dst = $profilePath . $dest . '/' . $tmpArr[1] . '.' . $scope . '.xml';
function copyStructureToTemplates($sourceType, $sourceName) {
if (!isValidPDFStructureName($sourceName)) {
throw new LAMException(_('Failed to copy'));
}
$sourceConfig = $sourceType->getTypeManager()->getConfig()->getName();
$sourceTypeId = $sourceType->getId();
$basePath = dirname(__FILE__) . '/../config/pdf/';
$templatePath = dirname(__FILE__) . '/../config/templates/pdf/';
$src = $basePath . $sourceConfig . '/' . $sourceName . '.' . $sourceTypeId;
$dst = $templatePath . $sourceName . '.' . $sourceType->getScope();
if (!@copy($src, $dst)) {
StatusMessage('ERROR', _('Failed to export!'), $tmpArr[1] . '.' . $scope . '.xml');
$state = false;
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceName);
}
}
} 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;
}
/**
* Uploads a PDF logo file for the current server profile.
@ -285,9 +294,9 @@ function deletePDFLogo($name) {
// check if still in use
$activeTypes = $_SESSION['config']->get_ActiveTypes();
foreach ($activeTypes as $type) {
$structures = getPDFStructureDefinitions($type);
$structures = getPDFStructures($type);
foreach ($structures as $structure) {
$data = loadPDFStructureDefinitions($type, $structure);
$data = loadPDFStructure($type, $structure);
if ($data['page_definitions']['filename'] == $name) {
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)));
@ -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')) {
// 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->colspan = 10;
$container->addElement($message, true);
@ -171,7 +171,7 @@ foreach ($sortedTypes as $typeId => $title) {
}
// get list of templates for each account type
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
@ -286,7 +286,7 @@ include '../main_header.php';
$tmpArr = array();
foreach ($configProfiles as $profile) {
if ($profile != $_SESSION['config']->getName()) {
$accountProfiles = \LAM\PDF\getPDFStructureDefinitions($typeId, $profile);
$accountProfiles = \LAM\PDF\getPDFStructures($typeId, $profile);
for ($p = 0; $p < sizeof($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\',\'_\',\'-\'.'));
}
else {
$return = \LAM\PDF\savePDFStructureDefinitions($type->getId(), $_GET['pdfname']);
$return = \LAM\PDF\savePDFStructure($type->getId(), $_GET['pdfname']);
if($return == 'ok') {
metaRefresh('pdfmain.php?savedSuccessfully=' . $_GET['pdfname']);
exit;
@ -356,14 +356,14 @@ foreach ($_GET as $key => $value) {
if(!isset($_SESSION['currentPDFStructure'])) {
// Load structure file to be 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['currentPageDefinitions'] = $load['page_definitions'];
$_GET['pdfname'] = $_GET['edit'];
}
// Load default structure file when creating a new one
else {
$load = \LAM\PDF\loadPDFStructureDefinitions($type->getId());
$load = \LAM\PDF\loadPDFStructure($type->getId());
$_SESSION['currentPDFStructure'] = $load['structure'];
$_SESSION['currentPageDefinitions'] = $load['page_definitions'];
}

View File

@ -145,9 +145,9 @@ foreach ($configProfiles as $profileName) {
if (!empty($_POST['import'])) {
$cfg = new LAMCfgMain();
// check master password
$impMessage = null;
$errMessage = null;
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'])) {
$options = array();
@ -155,20 +155,20 @@ if (!empty($_POST['import'])) {
$parts = explode('##', $importProfiles);
$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) {
$impMessage->colspan = 10;
$container->addElement($impMessage, true);
if ($errMessage != null) {
$errMessage->colspan = 10;
$container->addElement($errMessage, true);
}
}
// export profiles
if (!empty($_POST['export'])) {
$cfg = new LAMCfgMain();
// check master password
$impMessage = null;
$errMessage = null;
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'])) {
$options = array();
@ -178,11 +178,11 @@ if (!empty($_POST['export'])) {
}
$typeId = $_POST['typeId'];
$name = $_POST['name_' . $typeId];
$impMessage = exportProfiles($typeId, $name, $options, $serverProfiles, $typeManager);
$errMessage = exportProfiles($typeId, $name, $options, $serverProfiles, $typeManager);
}
if ($impMessage != null) {
$impMessage->colspan = 10;
$container->addElement($impMessage, true);
if ($errMessage != null) {
$errMessage->colspan = 10;
$container->addElement($errMessage, true);
}
}

View File

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