1<?php
2namespace LAM\TOOLS\PDF_EDITOR;
3use \htmlResponsiveRow;
4use \htmlResponsiveSelect;
5use \htmlResponsiveInputField;
6use \htmlTitle;
7use \htmlButton;
8use \htmlOutputText;
9use \htmlGroup;
10use \htmlSelect;
11use \htmlInputField;
12use \htmlSubTitle;
13use \htmlResponsiveInputTextarea;
14use \htmlHiddenInput;
15use \htmlSpacer;
16use LAM\PDF\PDFStructureReader;
17use LAM\PDF\PDFTextSection;
18use LAM\PDF\PDFEntrySection;
19use LAM\PDF\PDFStructure;
20use LAM\PDF\PDFSectionEntry;
21use LAM\PDF\PDFStructureWriter;
22/*
23  This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
24  Copyright (C) 2003 - 2006  Michael Duergner
25                2007 - 2020  Roland Gruber
26
27  This program is free software; you can redistribute it and/or modify
28  it under the terms of the GNU General Public License as published by
29  the Free Software Foundation; either version 2 of the License, or
30  (at your option) any later version.
31
32  This program is distributed in the hope that it will be useful,
33  but WITHOUT ANY WARRANTY; without even the implied warranty of
34  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  GNU General Public License for more details.
36
37  You should have received a copy of the GNU General Public License
38  along with this program; if not, write to the Free Software
39  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
40
41  Manages creating/changing of pdf structures.
42*/
43
44/**
45* Displays the main page of the PDF editor where the user can select the displayed entries.
46*
47* @author Michael Duergner
48* @author Roland Gruber
49* @package PDF
50*/
51
52/** security functions */
53include_once(__DIR__ . "/../../lib/security.inc");
54/** access to PDF configuration files */
55include_once(__DIR__ . '/../../lib/pdfstruct.inc');
56/** LDAP object */
57include_once(__DIR__ . '/../../lib/ldap.inc');
58/** LAM configuration */
59include_once(__DIR__ . '/../../lib/config.inc');
60/** module functions */
61include_once(__DIR__ . '/../../lib/modules.inc');
62
63// start session
64startSecureSession();
65enforceUserIsLoggedIn();
66
67// die if no write access
68if (!checkIfWriteAccessIsAllowed()) {
69	die();
70}
71
72checkIfToolIsActive('toolPDFEditor');
73
74setlanguage();
75
76if (!empty($_POST)) {
77	validateSecurityToken();
78}
79
80// check if user is logged in, if not go to login
81if (!$_SESSION['ldap'] || !$_SESSION['ldap']->server()) {
82	metaRefresh("../login.php");
83	exit;
84}
85
86// Write $_POST variables to $_GET when form was submitted via post
87if (isset($_POST['type'])) {
88	$_GET = $_POST;
89}
90
91$typeManager = new \LAM\TYPES\TypeManager();
92$type = $typeManager->getConfiguredType($_GET['type']);
93if ($type->isHidden() || !checkIfWriteAccessIsAllowed($type->getId())) {
94	logNewMessage(LOG_ERR, 'User tried to access hidden PDF structure: ' . $type->getId());
95	die();
96}
97
98
99// Abort and go back to main pdf structure page
100if(isset($_GET['abort'])) {
101	metarefresh('pdfmain.php');
102	exit;
103}
104
105// Load PDF structure from file if it is not defined in session
106if(!isset($_SESSION['currentPDFStructure'])) {
107	// Load structure file to be edit
108	$reader = new PDFStructureReader($_SESSION['config']->getName());
109	try {
110		if(isset($_GET['edit'])) {
111			$_SESSION['currentPDFStructure'] = $reader->read($type->getId(), $_GET['edit']);
112		}
113		// Load default structure file when creating a new one
114		else {
115			$_SESSION['currentPDFStructure'] = $reader->read($type->getId(), 'default');
116		}
117	}
118	catch (\LAMException $e) {
119		metaRefresh('pdfmain.php?loadFailed=1&name=' . $_GET['edit']);
120		exit;
121	}
122}
123
124if (!empty($_POST['form_submit'])) {
125	updateBasicSettings($_SESSION['currentPDFStructure']);
126	updateSectionTitles($_SESSION['currentPDFStructure']);
127	addSection($_SESSION['currentPDFStructure']);
128	addSectionEntry($_SESSION['currentPDFStructure']);
129	removeItem($_SESSION['currentPDFStructure']);
130	moveUp($_SESSION['currentPDFStructure']);
131	moveDown($_SESSION['currentPDFStructure']);
132}
133
134// Check if pdfname is valid, then save current structure to file and go to
135// main pdf structure page
136$saveErrors = array();
137if(isset($_GET['submit'])) {
138	$writer = new PDFStructureWriter($_SESSION['config']->getName());
139	try {
140		$writer->write($type->getId(), $_POST['pdfname'], $_SESSION['currentPDFStructure']);
141		unset($_SESSION['currentPDFStructure']);
142		metaRefresh('pdfmain.php?savedSuccessfully=' . $_POST['pdfname']);
143		exit;
144	}
145	catch (\LAMException $e) {
146		$saveErrors[] = array('ERROR', $e->getTitle(), $e->getMessage());
147	}
148}
149
150$availablePDFFields = getAvailablePDFFields($type->getId());
151
152// Create the values for the dropdown boxes for section headline defined by
153// value entries and fetch all available modules
154$modules = array();
155$section_items_array = array();
156$section_items = '';
157$sortedModules = array();
158foreach($availablePDFFields as $module => $fields) {
159	if ($module != 'main') {
160		$title = getModuleAlias($module, $type->getScope());
161	}
162	else {
163		$title = _('Main');
164	}
165	$sortedModules[$module] = $title;
166}
167natcasesort($sortedModules);
168foreach($sortedModules as $module => $title) {
169	$values = $availablePDFFields[$module];
170	if (!is_array($values) || (sizeof($values) < 1)) {
171		continue;
172	}
173	$modules[] = $module;
174	$section_items .= "<optgroup label=\"" . $title . "\"\n>";
175	natcasesort($values);
176	foreach($values as $attribute => $attributeLabel) {
177		$section_items_array[] = $module . '_' . $attribute;
178		$section_items .= "<option value=\"" . $module . '_' . $attribute . "\">" . $attributeLabel . "</option>\n";
179	}
180	$section_items .= "</optgroup>\n";
181}
182$modules = join(',',$modules);
183
184// print header
185include __DIR__ . '/../../lib/adminHeader.inc';
186?>
187	<div class="user-bright smallPaddingContent">
188<?php
189
190// print error messages if any
191if (sizeof($saveErrors) > 0) {
192	foreach ($saveErrors as $saveError) {
193		call_user_func_array('StatusMessage', $saveError);
194	}
195	echo "<br>\n";
196}
197
198$newFieldFieldElements = array();
199foreach($sortedModules as $module => $title) {
200	$fields = $availablePDFFields[$module];
201	if (isset($fields) && is_array($fields) && (sizeof($fields) > 0)) {
202		$moduleFields = array();
203		foreach ($fields as $field => $fieldLabel) {
204			$moduleFields[$fieldLabel] = $module . "_" . $field;
205		}
206		$newFieldFieldElements[$title] = $moduleFields;
207	}
208}
209
210// structure name
211$structureName = '';
212if (isset($_GET['edit'])) {
213	$structureName = $_GET['edit'];
214}
215else if (isset($_POST['pdfname'])) {
216	$structureName = $_POST['pdfname'];
217}
218// headline
219$headline = $_SESSION['currentPDFStructure']->getTitle();
220// logo
221$logoFiles = \LAM\PDF\getAvailableLogos($_SESSION['config']->getName());
222$logos = array(_('No logo') => 'none');
223foreach($logoFiles as $logoFile) {
224	$logos[$logoFile['filename'] . ' (' . $logoFile['infos'][0] . ' x ' . $logoFile['infos'][1] . ")"] = $logoFile['filename'];
225}
226$selectedLogo = array('printLogo.jpg');
227if (isset($_SESSION['currentPDFStructure'])) {
228	$selectedLogo = array($_SESSION['currentPDFStructure']->getLogo());
229}
230
231?>
232	<form id="inputForm" action="pdfpage.php" method="post" onSubmit="saveScrollPosition('inputForm')">
233<?php
234$sectionElements = array();
235$nonTextSectionElements = array();
236
237$container = new htmlResponsiveRow();
238$container->add(new htmlTitle(_('PDF editor')), 12);
239
240// main content
241$mainContent = new htmlResponsiveRow();
242$structureNameInput = new htmlResponsiveInputField(_("Structure name"), 'pdfname', $structureName, '360');
243$structureNameInput->setRequired(true);
244$mainContent->add($structureNameInput, 12);
245$mainContent->add(new htmlResponsiveInputField(_('Headline'), 'headline', $headline), 12);
246$logoSelect = new htmlResponsiveSelect('logoFile', $logos, $selectedLogo, _('Logo'));
247$logoSelect->setHasDescriptiveElements(true);
248$mainContent->add($logoSelect, 12);
249$foldingMarks = 'no';
250if (isset($_SESSION['currentPDFStructure'])) {
251	$foldingMarks = $_SESSION['currentPDFStructure']->getFoldingMarks();
252}
253$possibleFoldingMarks = array(_('No') => 'no', _('Yes') => 'standard');
254$foldingMarksSelect = new htmlResponsiveSelect('foldingmarks', $possibleFoldingMarks, array($foldingMarks), _('Folding marks'));
255$foldingMarksSelect->setHasDescriptiveElements(true);
256$mainContent->add($foldingMarksSelect, 12);
257$mainContent->addVerticalSpacer('3rem');
258// PDF structure
259$structure = $_SESSION['currentPDFStructure'];
260// print every entry in the current structure
261$structureContent = new htmlResponsiveRow();
262$sections = $structure->getSections();
263foreach ($sections as $key => $section) {
264	// create the up/down/remove links
265	$linkUp = new htmlButton('up_section_' . $key, 'up.gif', true);
266	$linkUp->setTitle(_("Up"));
267	$linkDown = new htmlButton('down_section_' . $key, 'down.gif', true);
268	$linkDown->setTitle(_("Down"));
269	$linkRemove = new htmlButton('remove_section_' . $key, 'delete.gif', true);
270	$linkRemove->setTitle(_("Remove"));
271	$emptyBox = new htmlSpacer('19px', null);
272	// We have a new section to start
273	if($section instanceof PDFEntrySection) {
274		if($section->isAttributeTitle()) {
275			$section_headline = translateFieldIDToName($section->getPdfKey(), $type->getScope(), $availablePDFFields);
276			if ($section_headline === null) {
277				continue;
278			}
279		}
280		else {
281			$section_headline = $section->getTitle();
282		}
283		$nonTextSectionElements[$section_headline] = $key;
284		$sectionElements[$section_headline] = $key;
285		$structureContent->addVerticalSpacer('2rem');
286		// Section headline is a value entry
287		if($section->isAttributeTitle()) {
288			$headlineElements = array();
289			foreach($section_items_array as $item) {
290				$headlineElements[translateFieldIDToName($item, $type->getScope(), $availablePDFFields)] = '_' . $item;
291			}
292			$sectionHeadlineSelect = new htmlSelect('section_' . $key, $headlineElements, array('_' . $section->getPdfKey()));
293			$sectionHeadlineSelect->setHasDescriptiveElements(true);
294			$structureContent->addLabel($sectionHeadlineSelect);
295		}
296		// Section headline is a user text
297		else {
298			$sectionHeadlineInput = new htmlInputField('section_' . $key, $section_headline);
299			$structureContent->addLabel($sectionHeadlineInput);
300		}
301		$actionGroup = new htmlGroup();
302		if ($key != 0) {
303			$actionGroup->addElement($linkUp);
304		}
305		else {
306			$actionGroup->addElement($emptyBox);
307		}
308		$hasAdditionalSections = $key < (sizeof($sections) - 1);
309		if ($hasAdditionalSections) {
310			$actionGroup->addElement($linkDown);
311		}
312		else {
313			$actionGroup->addElement($emptyBox);
314		}
315		$actionGroup->addElement($linkRemove);
316		$structureContent->addField($actionGroup);
317		// add section entries
318		$sectionEntries = $section->getEntries();
319		foreach ($sectionEntries as $e => $sectionEntry) {
320			$fieldLabel = translateFieldIDToName($sectionEntry->getKey(), $type->getScope(), $availablePDFFields);
321			if ($fieldLabel === null) {
322				continue;
323			}
324			$structureContent->addVerticalSpacer('1rem');
325			$fieldOutput = new htmlOutputText($fieldLabel);
326			$structureContent->addLabel($fieldOutput);
327			$actionGroup = new htmlGroup();
328			if ($e != 0) {
329				$entryLinkUp = new htmlButton('up_entry_' . $key . '_' . $e, 'up.gif', true);
330				$entryLinkUp->setTitle(_("Up"));
331				$actionGroup->addElement($entryLinkUp);
332			}
333			else {
334				$actionGroup->addElement($emptyBox);
335			}
336			if ($e < (sizeof($sectionEntries) - 1)) {
337				$linkDown = new htmlButton('down_entry_' . $key . '_' . $e, 'down.gif', true);
338				$linkDown->setTitle(_("Down"));
339				$actionGroup->addElement($linkDown);
340			}
341			else {
342				$actionGroup->addElement($emptyBox);
343			}
344			$entryLinkRemove = new htmlButton('remove_entry_' . $key . '_' . $e, 'delete.gif', true);
345			$entryLinkRemove->setTitle(_("Remove"));
346			$actionGroup->addElement($entryLinkRemove, true);
347			$structureContent->addField($actionGroup);
348		}
349	}
350	// We have to include a static text.
351	elseif($section instanceof PDFTextSection) {
352		// Add current satic text for dropdown box needed for the position when inserting a new
353		// section or static text entry
354		$textSnippet = $section->getText();
355		$textSnippet = str_replace(array("\n", "\r"), array(" ", " "), $textSnippet);
356		$textSnippet = trim($textSnippet);
357		if (strlen($textSnippet) > 15) {
358			$textSnippet = substr($textSnippet, 0, 15) . '...';
359		}
360		$textSnippet = htmlspecialchars($textSnippet);
361		$sectionElements[_('Static text') . ': ' . $textSnippet] = $key;
362		$sectionHeadlineOutput = new htmlOutputText(_('Static text'));
363		$structureContent->addLabel($sectionHeadlineOutput);
364		$actionGroup = new htmlGroup();
365		if ($key != 0) {
366			$actionGroup->addElement($linkUp);
367		}
368		else {
369			$actionGroup->addElement($emptyBox);
370		}
371		if ($key != sizeof($sections) - 1) {
372			$actionGroup->addElement($linkDown);
373		}
374		else {
375			$actionGroup->addElement($emptyBox);
376		}
377		$actionGroup->addElement($linkRemove, true);
378		$structureContent->addField($actionGroup);
379		$structureContent->addVerticalSpacer('1rem');
380		$staticTextOutput = new htmlOutputText($section->getText());
381		$staticTextOutput->setPreformatted();
382		$structureContent->add($staticTextOutput, 12);
383	}
384}
385$sectionElements[_('End')] = sizeof($structure->getSections());
386$mainContent->add($structureContent, 12);
387$container->add($mainContent, 12);
388$container->addVerticalSpacer('2rem');
389
390// new field
391if (!empty($nonTextSectionElements)) {
392	$newFieldContainer = new htmlResponsiveRow();
393	$newFieldContainer->add(new htmlSubTitle(_('New field')), 12);
394	$newFieldFieldSelect = new htmlResponsiveSelect('new_field', $newFieldFieldElements, array(), _('Field'));
395	$newFieldFieldSelect->setHasDescriptiveElements(true);
396	$newFieldFieldSelect->setContainsOptgroups(true);
397	$newFieldContainer->add($newFieldFieldSelect, 12);
398	$newFieldSectionSelect = new htmlResponsiveSelect('add_field_position', $nonTextSectionElements, array(), _('Position'));
399	$newFieldSectionSelect->setHasDescriptiveElements(true);
400	$newFieldContainer->add($newFieldSectionSelect, 12);
401	$newFieldContainer->addLabel(new htmlOutputText('&nbsp;', false));
402	$newFieldContainer->addField(new htmlButton('add_new_field', _('Add')));
403	$container->add($newFieldContainer, 12);
404}
405
406// new section
407$container->addVerticalSpacer('1rem');
408$newSectionContent = new htmlResponsiveRow();
409$newSectionContent->add(new htmlSubTitle(_('New section')), 12);
410// add new section with text title
411$newSectionContent->add(new htmlResponsiveInputField(_("Headline"), 'new_section_text'), 12);
412$newSectionPositionSelect1 = new htmlResponsiveSelect('add_sectionText_position', $sectionElements, array(), _('Position'));
413$newSectionPositionSelect1->setHasDescriptiveElements(true);
414$newSectionPositionSelect1->setSortElements(false);
415$newSectionContent->add($newSectionPositionSelect1, 12);
416$newSectionContent->addLabel(new htmlOutputText('&nbsp;', false));
417$newSectionContent->addField(new htmlButton('add_sectionText', _('Add')));
418$newSectionContent->addVerticalSpacer('2rem');
419// add new section with field title
420$newSectionFieldSelect = new htmlResponsiveSelect('new_section_item', $newFieldFieldElements, array(), _("Headline"));
421$newSectionFieldSelect->setHasDescriptiveElements(true);
422$newSectionFieldSelect->setContainsOptgroups(true);
423$newSectionContent->add($newSectionFieldSelect, 12);
424$newSectionPositionSelect2 = new htmlResponsiveSelect('add_section_position', $sectionElements, array(), _('Position'));
425$newSectionPositionSelect2->setHasDescriptiveElements(true);
426$newSectionPositionSelect2->setSortElements(false);
427$newSectionContent->add($newSectionPositionSelect2, 12);
428$newSectionContent->addLabel(new htmlOutputText('&nbsp;', false));
429$newSectionContent->addField(new htmlButton('add_section', _('Add')));
430
431// new text area
432$container->add($newSectionContent, 12);
433$container->addVerticalSpacer('1rem');
434$newTextFieldContent = new htmlResponsiveRow();
435$newTextFieldContent->add(new htmlSubTitle(_('New text area')), 12);
436$newTextFieldContent->add(new htmlResponsiveInputTextarea('text_text', '', 40, 3, _('Static text')), 12);
437$newTextFieldPositionSelect = new htmlResponsiveSelect('add_text_position', $sectionElements, array(), _('Position'));
438$newTextFieldPositionSelect->setHasDescriptiveElements(true);
439$newTextFieldPositionSelect->setSortElements(false);
440$newTextFieldContent->add($newTextFieldPositionSelect, 12);
441$newTextFieldContent->addLabel(new htmlOutputText('&nbsp;', false));
442$newTextFieldContent->addField(new htmlButton('add_text', _('Add')));
443$newTextFieldContent->addVerticalSpacer('2rem');
444$container->add($newTextFieldContent, 12);
445
446// buttons
447$buttonContainer = new htmlResponsiveRow();
448$saveButton = new htmlButton('submit', _("Save"));
449$saveButton->setIconClass('saveButton');
450$cancelButton = new htmlButton('abort', _("Cancel"));
451$cancelButton->setIconClass('cancelButton');
452$buttonGroup = new htmlGroup();
453$buttonGroup->addElement($saveButton);
454$buttonGroup->addElement($cancelButton);
455$buttonContainer->add($buttonGroup, 12);
456$buttonContainer->add(new htmlHiddenInput('modules', $modules), 4);
457$buttonContainer->add(new htmlHiddenInput('type', $type->getId()), 4);
458$buttonContainer->add(new htmlHiddenInput('form_submit', 'true'), 4);
459
460$container->add($buttonContainer, 12);
461addSecurityTokenToMetaHTML($container);
462
463$tabindex = 1;
464parseHtml(null, $container, array(), false, $tabindex, $type->getScope());
465
466if ((sizeof($saveErrors) == 0) && isset($_POST['scrollPositionTop']) && isset($_POST['scrollPositionLeft'])) {
467	// scroll to last position
468	echo '<script type="text/javascript">
469		jQuery(document).ready(function() {
470			jQuery(window).scrollTop(' . $_POST['scrollPositionTop'] . ');
471			jQuery(window).scrollLeft('. $_POST['scrollPositionLeft'] . ');
472	});
473	</script>';
474}
475
476echo '</form></div>';
477include __DIR__ . '/../../lib/adminFooter.inc';
478
479
480/**
481 * Translates a given field ID (e.g. inetOrgPerson_givenName) to its descriptive name.
482 *
483 * @param String $id field ID
484 * @param String $scope account type
485 * @param array $availablePDFFields available PDF fields
486 * @return string|null field label or null if no matching module found
487 */
488function translateFieldIDToName($id, $scope, $availablePDFFields) {
489	foreach ($availablePDFFields as $module => $fields) {
490		if (!(strpos($id, $module . '_') === 0)) {
491			continue;
492		}
493		foreach ($fields as $name => $label) {
494			if ($id == $module . '_' . $name) {
495				if ($module == 'main') {
496					return _('Main') . ': ' . $label;
497				}
498				else  {
499					return getModuleAlias($module, $scope) . ': ' . $label;
500				}
501			}
502		}
503	}
504	return null;
505}
506
507/**
508 * Updates basic settings such as logo and head line.
509 *
510 * @param PDFStructure $structure
511 */
512function updateBasicSettings(PDFStructure &$structure) {
513	// set headline
514	if (isset($_POST['headline'])) {
515		$structure->setTitle(str_replace('<', '', str_replace('>', '', $_POST['headline'])));
516	}
517	// set logo
518	if (isset($_POST['logoFile'])) {
519		$structure->setLogo($_POST['logoFile']);
520	}
521	// set folding marks
522	if (isset($_POST['foldingmarks'])) {
523		$structure->setFoldingMarks($_POST['foldingmarks']);
524	}
525}
526
527/**
528 * Updates section titles.
529 *
530 * @param PDFStructure $structure
531 */
532function updateSectionTitles(PDFStructure &$structure) {
533	$sections = $structure->getSections();
534	foreach ($_POST as $key => $value) {
535		if (strpos($key, 'section_') === 0) {
536			$pos = substr($key, strlen('section_'));
537			$sections[$pos]->setTitle($value);
538		}
539	}
540}
541
542/**
543 * Adds a new section if requested.
544 *
545 * @param PDFStructure $structure
546 */
547function addSection(PDFStructure &$structure) {
548	$sections = $structure->getSections();
549	// add a new text field
550	if(isset($_POST['add_text'])) {
551		// Check if text for static text field is specified
552		if(empty($_POST['text_text'])) {
553			StatusMessage('ERROR',_('No static text specified'),_('The static text must contain at least one character.'));
554		}
555		else {
556			$section = new PDFTextSection(str_replace("\r", "", $_POST['text_text']));
557			array_splice($sections, $_POST['add_text_position'], 0, array($section));
558			$structure->setSections($sections);
559		}
560	}
561	// add a new section with text headline
562	elseif(isset($_POST['add_sectionText'])) {
563		// Check if name for new section is specified when needed
564		if(empty($_POST['new_section_text'])) {
565			StatusMessage('ERROR',_('No section text specified'),_('The headline for a new section must contain at least one character.'));
566		}
567		else {
568			$section = new PDFEntrySection($_POST['new_section_text']);
569			array_splice($sections, $_POST['add_sectionText_position'], 0, array($section));
570			$structure->setSections($sections);
571		}
572	}
573	// Add a new section with item as headline
574	elseif(isset($_POST['add_section'])) {
575		$section = new PDFEntrySection('_' . $_POST['new_section_item']);
576		array_splice($sections, $_POST['add_section_position'], 0, array($section));
577		$structure->setSections($sections);
578	}
579}
580
581/**
582 * Adds a new entry to a section if requested.
583 *
584 * @param PDFStructure $structure
585 */
586function addSectionEntry(PDFStructure &$structure) {
587	if(isset($_POST['add_new_field'])) {
588		$field = new PDFSectionEntry($_POST['new_field']);
589		$sections = $structure->getSections();
590		$pos = $_POST['add_field_position'];
591		$entries = $sections[$pos]->getEntries();
592		$entries[] = $field;
593		$sections[$pos]->setEntries($entries);
594		$structure->setSections($sections);
595	}
596}
597
598/**
599 * Removes a section or entry if requested.
600 *
601 * @param PDFStructure $structure
602 */
603function removeItem(PDFStructure &$structure) {
604	$sections = $structure->getSections();
605	foreach ($_POST as $key => $value) {
606		// remove section
607		if (strpos($key, 'remove_section_') === 0) {
608			$pos = substr($key, strlen('remove_section_'));
609			unset($sections[$pos]);
610			$sections = array_values($sections);
611			$structure->setSections($sections);
612		}
613		// remove section entry
614		if (strpos($key, 'remove_entry_') === 0) {
615			$parts = substr($key, strlen('remove_entry_'));
616			$parts = explode('_', $parts);
617			$sectionPos = $parts[0];
618			$entryPos = $parts[1];
619			$entries = $sections[$sectionPos]->getEntries();
620			unset($entries[$entryPos]);
621			$entries = array_values($entries);
622			$sections[$sectionPos]->setEntries($entries);
623			$structure->setSections($sections);
624		}
625	}
626}
627
628/**
629 * Moves up a section or entry if requested.
630 *
631 * @param PDFStructure $structure
632 */
633function moveUp(PDFStructure &$structure) {
634	$sections = $structure->getSections();
635	foreach ($_POST as $key => $value) {
636		// move section
637		if (strpos($key, 'up_section_') === 0) {
638			$pos = substr($key, strlen('up_section_'));
639			$sectionTmp = $sections[$pos - 1];
640			$sections[$pos - 1] = $sections[$pos];
641			$sections[$pos] = $sectionTmp;
642			$structure->setSections($sections);
643		}
644		// move section entry
645		if (strpos($key, 'up_entry_') === 0) {
646			$parts = substr($key, strlen('up_entry_'));
647			$parts = explode('_', $parts);
648			$sectionPos = $parts[0];
649			$entryPos = $parts[1];
650			$entries = $sections[$sectionPos]->getEntries();
651			$entryTmp = $entries[$entryPos - 1];
652			$entries[$entryPos - 1] = $entries[$entryPos];
653			$entries[$entryPos] = $entryTmp;
654			$sections[$sectionPos]->setEntries($entries);
655			$structure->setSections($sections);
656		}
657	}
658}
659
660/**
661 * Moves down a section or entry if requested.
662 *
663 * @param PDFStructure $structure
664 */
665function moveDown(PDFStructure &$structure) {
666	$sections = $structure->getSections();
667	foreach ($_POST as $key => $value) {
668		// move section
669		if (strpos($key, 'down_section_') === 0) {
670			$pos = substr($key, strlen('down_section_'));
671			$sectionTmp = $sections[$pos + 1];
672			$sections[$pos + 1] = $sections[$pos];
673			$sections[$pos] = $sectionTmp;
674			$structure->setSections($sections);
675		}
676		// move section entry
677		if (strpos($key, 'down_entry_') === 0) {
678			$parts = substr($key, strlen('down_entry_'));
679			$parts = explode('_', $parts);
680			$sectionPos = $parts[0];
681			$entryPos = $parts[1];
682			$entries = $sections[$sectionPos]->getEntries();
683			$entryTmp = $entries[$entryPos + 1];
684			$entries[$entryPos + 1] = $entries[$entryPos];
685			$entries[$entryPos] = $entryTmp;
686			$sections[$sectionPos]->setEntries($entries);
687			$structure->setSections($sections);
688		}
689	}
690}
691
692?>
693