Debian Codegraph @

templates/pdfedit/pdfpage.php

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