new type API for PDF

This commit is contained in:
Roland Gruber 2017-01-04 20:52:51 +01:00
parent 22cfb56f60
commit a9234c1537
8 changed files with 70 additions and 66 deletions

View File

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

View File

@ -1,5 +1,6 @@
<?php
use LAM\TYPES\ConfiguredType;
use function LAM\TYPES\getScopeFromTypeId;
/*
$Id$
@ -401,14 +402,14 @@ function getHelp($module,$helpID,$scope='') {
/**
* Returns a list of available PDF entries.
*
* @param string $scope account type (user, group, host)
* @param string $typeId account type (user, group, host)
* @return array PDF entries (field ID => field label)
*/
function getAvailablePDFFields($scope) {
$mods = $_SESSION['config']->get_AccountModules($scope);
function getAvailablePDFFields($typeId) {
$mods = $_SESSION['config']->get_AccountModules($typeId);
$return = array();
for ($i = 0; $i < sizeof($mods); $i++) {
$module = moduleCache::getModule($mods[$i], $scope);
$module = moduleCache::getModule($mods[$i], getScopeFromTypeId($typeId));
$fields = $module->get_pdfFields();
$return[$mods[$i]] = array();
if (is_array($fields)) {

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 = loadPDFStructureDefinitions($account_type->getId(), $pdf_structure);
$load = \LAM\PDF\loadPDFStructureDefinitions($account_type->getId(), $pdf_structure);
$structure = $load['structure'];
// get list of PDF keys
$pdfKeys = array();

View File

@ -1,4 +1,7 @@
<?php
namespace LAM\PDF;
use \htmlStatusMessage;
/*
$Id$
@ -75,14 +78,14 @@ function getPDFStructureDefinitions($scope = "user", $profile = null) {
*
* @return array PDF structure
*/
function loadPDFStructureDefinitions($scope='user',$pdf_structure='default') {
function loadPDFStructureDefinitions($scope='user', $pdf_structure='default') {
$parser = new xmlParser();
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $pdf_structure . '.' . $scope . '.xml';
$xml = $parser->parse($file);
$border = array();
$structure = array();
$complete_page_definitions = array('filename' => 'printLogo.jpg', 'headline' => 'LDAP Account Manager');
if($xml[0][$xml[1]['PDF'][0]]['attributes']['TYPE'] == $scope) {
if (!empty($xml)) {
$border['start'] = $xml[1]['PDF'][0];
$page_definitions = $xml[0][$xml[1]['PDF'][0]]['attributes'];
foreach($page_definitions as $key => $value) {
@ -90,8 +93,8 @@ function loadPDFStructureDefinitions($scope='user',$pdf_structure='default') {
unset($page_definitions[$key]);
}
$border['end'] = $xml[1]['PDF'][1];
$structure = array_slice($xml[0],$border['start'] + 1,$border['end'] - ($border['start'] + 1));
}
$structure = array_slice($xml[0],$border['start'] + 1,$border['end'] - ($border['start'] + 1));
return array('structure' => $structure, 'page_definitions' => $complete_page_definitions);
}
@ -103,7 +106,7 @@ function loadPDFStructureDefinitions($scope='user',$pdf_structure='default') {
* @param string $definition Name of definition
* @return string "no perms" if access denied or "ok".
*/
function savePDFStructureDefinitions($scope,$definition) {
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';
@ -115,11 +118,9 @@ function savePDFStructureDefinitions($scope,$definition) {
if (!$handle) return 'no perms';
$pdf_attributes = '';
foreach($_SESSION['currentPageDefinitions'] as $key => $value) {
if($key != 'type') {
$pdf_attributes .= ' ' . $key . '="' . $value . '"';
}
$pdf_attributes .= ' ' . $key . '="' . $value . '"';
}
$file = '<pdf type="' . $scope . "\"" . $pdf_attributes . ">\n";
$file = '<pdf' . $pdf_attributes . ">\n";
foreach($_SESSION['currentPDFStructure'] as $entry) {
$ident = '';
for($i=0;$i<$entry['level'] -1;$i++) {
@ -289,7 +290,7 @@ function deletePDFLogo($name) {
$data = loadPDFStructureDefinitions($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)));
sprintf(_('Logo is still in use by PDF structure "%s" in account type "%s".'), $structure, \LAM\TYPES\getTypeAlias($type)));
}
}
}

View File

@ -1,9 +1,11 @@
<?php
namespace LAM\PDF;
/*
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Michael Duergner
2017 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -43,7 +45,7 @@ class xmlParser {
*/
function __construct() {
$this->xmlParser = xml_parser_create();
xml_set_object($this->xmlParser,$this);
xml_set_object($this->xmlParser, $this);
xml_parser_set_option($this->xmlParser, XML_OPTION_CASE_FOLDING, 1);
xml_parser_set_option($this->xmlParser, XML_OPTION_SKIP_WHITE, 1);
}
@ -58,8 +60,8 @@ class xmlParser {
if(file_exists($filename)) {
$xmlStructure = array();
$xmlIndex = array();
xml_parse_into_struct($this->xmlParser,implode("\n",file($filename)),$xmlStructure,$xmlIndex);
return array($xmlStructure,$xmlIndex);
xml_parse_into_struct($this->xmlParser, implode("\n", file($filename)), $xmlStructure, $xmlIndex);
return array($xmlStructure, $xmlIndex);
}
return array();
}

View File

@ -75,7 +75,6 @@ setlanguage();
// Unset pdf structure definitions in session if set
if(isset($_SESSION['currentPDFStructure'])) {
unset($_SESSION['currentPDFStructure']);
unset($_SESSION['availablePDFFields']);
unset($_SESSION['currentPageDefinitions']);
}
@ -107,7 +106,7 @@ $container->addElement(new htmlTitle(_('PDF editor')), true);
if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) {
// delete structure
if (deletePDFStructureDefinition($_POST['profileDeleteType'], $_POST['profileDeleteName'])) {
if (\LAM\PDF\deletePDFStructureDefinition($_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);
@ -127,7 +126,7 @@ if (isset($_POST['importexport']) && ($_POST['importexport'] === '1')) {
if (!$cfg->checkPassword($_POST['passwd_' . $_POST['typeId']])) {
$impExpMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!'));
}
elseif (copyPdfProfiles($_POST['importProfiles_' . $_POST['typeId']], $_POST['typeId'])) {
elseif (\LAM\PDF\copyPdfProfiles($_POST['importProfiles_' . $_POST['typeId']], $_POST['typeId'])) {
$impExpMessage = new htmlStatusMessage('INFO', _('Import successful'));
}
} else if (isset($_POST['exportProfiles'])) {
@ -135,7 +134,7 @@ if (isset($_POST['importexport']) && ($_POST['importexport'] === '1')) {
if (!$cfg->checkPassword($_POST['passwd'])) {
$impExpMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!'));
}
elseif (copyPdfProfiles($_POST['exportProfiles'], $_POST['typeId'], $_POST['destServerProfiles'])) {
elseif (\LAM\PDF\copyPdfProfiles($_POST['exportProfiles'], $_POST['typeId'], $_POST['destServerProfiles'])) {
$impExpMessage = new htmlStatusMessage('INFO', _('Export successful'));
}
}
@ -149,13 +148,13 @@ if (isset($_POST['importexport']) && ($_POST['importexport'] === '1')) {
if (isset($_POST['uploadLogo']) && !empty($_FILES['logoUpload']) && !empty($_FILES['logoUpload']['size'])) {
$file = $_FILES['logoUpload']['tmp_name'];
$filename = $_FILES['logoUpload']['name'];
$container->addElement(uploadPDFLogo($file, $filename), true);
$container->addElement(\LAM\PDF\uploadPDFLogo($file, $filename), true);
}
// delete logo file
if (isset($_POST['delLogo'])) {
$toDel = $_POST['logo'];
$container->addElement(deletePDFLogo($toDel), true);
$container->addElement(\LAM\PDF\deletePDFLogo($toDel), true);
}
// get list of account types
@ -172,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'] = getPDFStructureDefinitions($templateClasses[$i]['typeId']);
$templateClasses[$i]['templates'] = \LAM\PDF\getPDFStructureDefinitions($templateClasses[$i]['typeId']);
}
// check if a template should be edited
@ -255,7 +254,7 @@ include '../main_header.php';
$logoContainer = new htmlTable();
$logoContainer->addElement(new htmlSpacer(null, '30px'), true);
$logoContainer->addElement(new htmlSubTitle(_('Manage logos')), true);
$logos = getAvailableLogos();
$logos = \LAM\PDF\getAvailableLogos();
$logoOptions = array();
foreach ($logos as $logo) {
$file = $logo['filename'];
@ -287,7 +286,7 @@ include '../main_header.php';
$tmpArr = array();
foreach ($configProfiles as $profile) {
if ($profile != $_SESSION['config']->getName()) {
$accountProfiles = getPDFStructureDefinitions($typeId, $profile);
$accountProfiles = \LAM\PDF\getPDFStructureDefinitions($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 = savePDFStructureDefinitions($type->getId(), $_GET['pdfname']);
$return = \LAM\PDF\savePDFStructureDefinitions($type->getId(), $_GET['pdfname']);
if($return == 'ok') {
metaRefresh('pdfmain.php?savedSuccessfully=' . $_GET['pdfname']);
exit;
@ -356,23 +356,20 @@ foreach ($_GET as $key => $value) {
if(!isset($_SESSION['currentPDFStructure'])) {
// Load structure file to be edit
if(isset($_GET['edit'])) {
$load = loadPDFStructureDefinitions($type->getId(), $_GET['edit']);
$load = \LAM\PDF\loadPDFStructureDefinitions($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 = loadPDFStructureDefinitions($type->getId());
$load = \LAM\PDF\loadPDFStructureDefinitions($type->getId());
$_SESSION['currentPDFStructure'] = $load['structure'];
$_SESSION['currentPageDefinitions'] = $load['page_definitions'];
}
}
// Load available fields from modules when not set in session
if(!isset($_SESSION['availablePDFFields'])) {
$_SESSION['availablePDFFields'] = getAvailablePDFFields($type->getId());
}
$availablePDFFields = getAvailablePDFFields($type->getId());
// Create the values for the dropdown boxes for section headline defined by
// value entries and fetch all available modules
@ -380,7 +377,7 @@ $modules = array();
$section_items_array = array();
$section_items = '';
$sortedModules = array();
foreach($_SESSION['availablePDFFields'] as $module => $fields) {
foreach($availablePDFFields as $module => $fields) {
if ($module != 'main') {
$title = getModuleAlias($module, $type->getScope());
}
@ -391,7 +388,7 @@ foreach($_SESSION['availablePDFFields'] as $module => $fields) {
}
natcasesort($sortedModules);
foreach($sortedModules as $module => $title) {
$values = $_SESSION['availablePDFFields'][$module];
$values = $availablePDFFields[$module];
if (!is_array($values) || (sizeof($values) < 1)) {
continue;
}
@ -419,7 +416,7 @@ if (sizeof($saveErrors) > 0) {
$newFieldFieldElements = array();
foreach($sortedModules as $module => $title) {
$fields = $_SESSION['availablePDFFields'][$module];
$fields = $availablePDFFields[$module];
if (isset($fields) && is_array($fields) && (sizeof($fields) > 0)) {
$moduleFields = array();
foreach ($fields as $field => $fieldLabel) {
@ -446,7 +443,7 @@ if (isset($_SESSION['currentPageDefinitions']['headline'])) {
$headline = $_SESSION['currentPageDefinitions']['headline'];
}
// logo
$logoFiles = getAvailableLogos();
$logoFiles = \LAM\PDF\getAvailableLogos();
$logos = array(_('No logo') => 'none');
foreach($logoFiles as $logoFile) {
$logos[$logoFile['filename'] . ' (' . $logoFile['infos'][0] . ' x ' . $logoFile['infos'][1] . ")"] = $logoFile['filename'];
@ -459,7 +456,7 @@ if (isset($_SESSION['currentPageDefinitions']['filename'])) {
?>
<form id="inputForm" action="pdfpage.php" method="post" onSubmit="saveScrollPosition('inputForm')">
<?php
$sectionElements = array(_('Beginning') => 0);
$sectionElements = array();
$nonTextSectionElements = array();
$container = new htmlTable();
@ -498,10 +495,10 @@ for ($key = 0; $key < sizeof($_SESSION['currentPDFStructure']); $key++) {
$linkRemove->setTitle(_("Remove"));
$emptyBox = new htmlOutputText('');
// We have a new section to start
if($entry['tag'] == "SECTION" && $entry['type'] == "open") {
if(($entry['tag'] == "SECTION") && ($entry['type'] == 'open')) {
$name = $entry['attributes']['NAME'];
if(preg_match("/^_[a-zA-Z0-9_]+_[a-zA-Z0-9_]+/",$name)) {
$section_headline = translateFieldIDToName(substr($name,1), $type->getScope());
$section_headline = translateFieldIDToName(substr($name,1), $type->getScope(), $availablePDFFields);
}
else {
$section_headline = $name;
@ -513,7 +510,7 @@ for ($key = 0; $key < sizeof($_SESSION['currentPDFStructure']); $key++) {
if(preg_match("/^_[a-zA-Z0-9_]+_[a-zA-Z0-9_]+/",$name)) {
$headlineElements = array();
foreach($section_items_array as $item) {
$headlineElements[translateFieldIDToName($item, $type->getScope())] = '_' . $item;
$headlineElements[translateFieldIDToName($item, $type->getScope(), $availablePDFFields)] = '_' . $item;
}
$sectionHeadlineSelect = new htmlSelect('section_' . $key, $headlineElements, array($name));
$sectionHeadlineSelect->setHasDescriptiveElements(true);
@ -583,7 +580,7 @@ for ($key = 0; $key < sizeof($_SESSION['currentPDFStructure']); $key++) {
// Get name of current entry
$name = $entry['attributes']['NAME'];
$structureContent->addElement(new htmlSpacer('10px', null));
$fieldOutput = new htmlOutputText(translateFieldIDToName($name, $type->getScope()));
$fieldOutput = new htmlOutputText(translateFieldIDToName($name, $type->getScope(), $availablePDFFields));
$structureContent->addElement($fieldOutput);
if ($_SESSION['currentPDFStructure'][$key - 1]['tag'] != 'SECTION') {
$structureContent->addElement($linkUp);
@ -600,6 +597,7 @@ for ($key = 0; $key < sizeof($_SESSION['currentPDFStructure']); $key++) {
$structureContent->addElement($linkRemove, true);
}
}
$sectionElements[_('End')] = sizeof($_SESSION['currentPDFStructure']);
$structureContent->colspan = 3;
$mainContent->addElement($structureContent);
$container->addElement(new htmlFieldset($mainContent), true);
@ -638,19 +636,21 @@ $newTextFieldContent->addElement(new htmlButton('add_text', _('Add')));
$container->addElement(new htmlFieldset($newTextFieldContent, _("Text field")), true);
// new field
$container->addElement(new htmlSubTitle(_('New field')), true);
$newFieldContainer = new htmlTable();
$newFieldFieldSelect = new htmlSelect('new_field', $newFieldFieldElements);
$newFieldFieldSelect->setHasDescriptiveElements(true);
$newFieldFieldSelect->setContainsOptgroups(true);
$newFieldContainer->addElement($newFieldFieldSelect);
$newFieldContainer->addElement(new htmlSpacer('10px', null));
$newFieldSectionSelect = new htmlTableExtendedSelect('add_field_position', $nonTextSectionElements, array(), _('Position'));
$newFieldSectionSelect->setHasDescriptiveElements(true);
$newFieldContainer->addElement($newFieldSectionSelect);
$newFieldContainer->addElement(new htmlButton('add_new_field', _('Add')));
$container->addElement(new htmlFieldset($newFieldContainer), true);
$container->addElement(new htmlSpacer(null, '20px'), true);
if (!empty($nonTextSectionElements)) {
$container->addElement(new htmlSubTitle(_('New field')), true);
$newFieldContainer = new htmlTable();
$newFieldFieldSelect = new htmlSelect('new_field', $newFieldFieldElements);
$newFieldFieldSelect->setHasDescriptiveElements(true);
$newFieldFieldSelect->setContainsOptgroups(true);
$newFieldContainer->addElement($newFieldFieldSelect);
$newFieldContainer->addElement(new htmlSpacer('10px', null));
$newFieldSectionSelect = new htmlTableExtendedSelect('add_field_position', $nonTextSectionElements, array(), _('Position'));
$newFieldSectionSelect->setHasDescriptiveElements(true);
$newFieldContainer->addElement($newFieldSectionSelect);
$newFieldContainer->addElement(new htmlButton('add_new_field', _('Add')));
$container->addElement(new htmlFieldset($newFieldContainer), true);
$container->addElement(new htmlSpacer(null, '20px'), true);
}
// buttons
$buttonContainer = new htmlTable();
@ -688,9 +688,10 @@ include '../main_footer.php';
*
* @param String $id field ID
* @param String $scope account type
* @param array $availablePDFFields available PDF fields
*/
function translateFieldIDToName($id, $scope) {
foreach ($_SESSION['availablePDFFields'] as $module => $fields) {
function translateFieldIDToName($id, $scope, $availablePDFFields) {
foreach ($availablePDFFields as $module => $fields) {
if (!(strpos($id, $module . '_') === 0)) {
continue;
}

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 = getPDFStructureDefinitions($scope);
$pdfStructures = \LAM\PDF\getPDFStructureDefinitions($scope);
$pdfSelected = array();
if (isset($_POST['pdfStructure'])) {
$pdfSelected = array($_POST['pdfStructure']);