server()) {
metaRefresh("../login.php");
exit;
}
// Write $_POST variables to $_GET when form was submitted via post
if (isset($_POST['type'])) {
$_GET = $_POST;
if ($_POST['pdfname'] == '') {
unset($_GET['pdfname']);
}
}
$typeManager = new \LAM\TYPES\TypeManager();
$type = $typeManager->getConfiguredType($_GET['type']);
if ($type->isHidden() || !checkIfWriteAccessIsAllowed($type->getId())) {
logNewMessage(LOG_ERR, 'User tried to access hidden PDF structure: ' . $type->getId());
die();
}
// Abort and go back to main pdf structure page
if(isset($_GET['abort'])) {
metarefresh('pdfmain.php');
exit;
}
// Check if pdfname is valid, then save current structure to file and go to
// main pdf structure page
$saveErrors = array();
if(isset($_GET['submit'])) {
if(!isset($_GET['pdfname']) || !preg_match('/[a-zA-Z0-9\-\_]+/',$_GET['pdfname'])) {
$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\savePDFStructure($type->getId(), $_GET['pdfname']);
if($return == 'ok') {
metaRefresh('pdfmain.php?savedSuccessfully=' . $_GET['pdfname']);
exit;
}
elseif($return == 'no perms'){
$saveErrors[] = array('ERROR', _("Could not save PDF structure, access denied."), $_GET['pdfname']);
}
}
}
// Add a new value field
elseif(isset($_GET['add_new_field'])) {
$field = array('tag' => 'ENTRY','type' => 'complete','level' => '3','attributes' => array('NAME' => $_GET['new_field']));
$pos = 0;
// Find begin section to insert into
while($pos < $_GET['add_field_position']) {
next($_SESSION['currentPDFStructure']);
$pos++;
}
$current = next($_SESSION['currentPDFStructure']);
$pos++;
// End of section to insert into
while($current && $current['tag'] != 'SECTION' && $current['type'] != 'close') {
$current = next($_SESSION['currentPDFStructure']);
$pos++;
}
// Insert new entry before closing section tag
array_splice($_SESSION['currentPDFStructure'],$pos,0,array($field));
}
foreach ($_GET as $key => $value) {
// remove entry or section
if (strpos($key, 'remove_') === 0) {
$pos = substr($key, strlen('remove_'));
$remove = $_SESSION['currentPDFStructure'][$pos];
// We have a section to remove
if($remove['tag'] == "SECTION") {
$end = $pos + 1;
// Find end of section to remove
while (isset($_SESSION['currentPDFStructure'][$end]) && ($_SESSION['currentPDFStructure'][$end]['tag'] != 'SECTION')
&& ($_SESSION['currentPDFStructure'][$end]['type'] != 'close')) {
$end++;
}
// Remove complete section with all value entries in it from structure
array_splice($_SESSION['currentPDFStructure'], $pos, $end - $pos + 1);
}
// We have a value entry to remove
elseif($remove['tag'] == "ENTRY") {
array_splice($_SESSION['currentPDFStructure'], $pos, 1);
}
// We hava a static text to remove
elseif($remove['tag'] == "TEXT") {
array_splice($_SESSION['currentPDFStructure'], $pos, 1);
}
}
// Move a section, static text field or value entry downwards
elseif (strpos($key, 'down_') === 0) {
$index = substr($key, strlen('down_'));
$tmp = $_SESSION['currentPDFStructure'][$index];
$next = $_SESSION['currentPDFStructure'][$index + 1];
// We have a section or static text to move
if($tmp['tag'] == 'SECTION' || $tmp['tag'] == 'TEXT') {
$pos = 0;
$current = current($_SESSION['currentPDFStructure']);
// Find section or static text entry to move
while($pos < $index) {
$current = next($_SESSION['currentPDFStructure']);
$pos++;
}
$borders = array();
// We have a section to move
if($current['tag'] == 'SECTION'){
$borders[$current['type']][] = $pos;
$current = next($_SESSION['currentPDFStructure']);
$pos++;
// Find end of section to move
while($current && $current['tag'] != 'SECTION' && $current['type'] != 'close') {
$current = next($_SESSION['currentPDFStructure']);
$pos++;
}
$borders['close'][] = $pos;
}
// We have a static text entry to move
elseif($current['tag'] == 'TEXT') {
$borders['open'][] = $pos;
$borders['close'][] = $pos;
}
$current = next($_SESSION['currentPDFStructure']);
$pos++;
// Find next section or static text entry in structure
if($current) {
// Next is a section
if($current['tag'] == 'SECTION') {
$borders[$current['type']][] = $pos;
$current = next($_SESSION['currentPDFStructure']);
$pos++;
// Find end of this section
while($current && $current['tag'] != 'SECTION' && $current['type'] != 'close') {
if($current['tag'] == 'SECTION') {
$borders[$current['type']][] = $pos;
}
$current = next($_SESSION['currentPDFStructure']);
$pos++;
}
}
// Next is static text entry
elseif($current['tag'] == 'TEXT') {
$borders['open'][] = $pos;
}
$borders['close'][] = $pos;
}
// Move only downwars if not bottenmost element of this structure
if(count($borders['open']) > 1) {
// Calculate entries to move and move them
$cut_start = $borders['open'][count($borders['open']) - 1];
$cut_count = $borders['close'][count($borders['close']) - 1] - $borders['open'][count($borders['open']) - 1] + 1;
$insert_pos = $borders['open'][count($borders['open']) - 2];
$tomove = array_splice($_SESSION['currentPDFStructure'],$cut_start,$cut_count);
array_splice($_SESSION['currentPDFStructure'],$insert_pos,0,$tomove);
}
}
// We have a value entry to move; move it only if it is not the bottmmost
// element of this section.
elseif($tmp['tag'] == 'ENTRY' && $next['tag'] == 'ENTRY') {
$_SESSION['currentPDFStructure'][$index] = $_SESSION['currentPDFStructure'][$index + 1];
$_SESSION['currentPDFStructure'][$index + 1] = $tmp;
}
}
// Move a section, static text or value entry upwards
elseif (strpos($key, 'up_') === 0) {
$index = substr($key, strlen('up_'));
$tmp = $_SESSION['currentPDFStructure'][$index];
$prev = $_SESSION['currentPDFStructure'][$index - 1];
// We have a section or static text to move
if($tmp['tag'] == 'SECTION' || $tmp['tag'] == 'TEXT') {
$pos = 0;
$borders = array();
$current = current($_SESSION['currentPDFStructure']);
// Add borders of sections and static text entry to array
if($current['tag'] == 'SECTION') {
$borders[$current['type']][] = $pos;
}
elseif($current['tag'] == 'TEXT') {
$borders['open'][] = $pos;
$borders['close'][] = $pos;
}
// Find all sections and statci text fields before the section or static
// text entry to move upwards
while($pos < $index) {
$current = next($_SESSION['currentPDFStructure']);
$pos++;
if($current['tag'] == 'SECTION') {
$borders[$current['type']][] = $pos;
}
elseif($current['tag'] == 'TEXT') {
$borders['open'][] = $pos;
$borders['close'][] = $pos;
}
}
// Move only when not topmost element
if(count($borders['close']) > 0) {
// We have a section to move up
if($current['tag'] == 'SECTION') {
$current = next($_SESSION['currentPDFStructure']);
$pos++;
// Find end of section to move
while($current && $current['tag'] != 'SECTION' && $current['type'] != 'close') {
$current = next($_SESSION['currentPDFStructure']);
$pos++;
}
$borders['close'][] = $pos;
}
// Calculate the entries to move and move them
$cut_start = $borders['open'][count($borders['open']) - 1];
$cut_count = $borders['close'][count($borders['close']) - 1] - $borders['open'][count($borders['open']) - 1] + 1;
$insert_pos = $borders['open'][count($borders['open']) - 2];
$tomove = array_splice($_SESSION['currentPDFStructure'],$cut_start,$cut_count);
array_splice($_SESSION['currentPDFStructure'],$insert_pos,0,$tomove);
}
}
// We have a value entry to move; move it only if its not the topmost
// entry in this section
elseif($tmp['tag'] == 'ENTRY' && $prev['tag'] == 'ENTRY') {
$_SESSION['currentPDFStructure'][$index] = $prev;
$_SESSION['currentPDFStructure'][$index - 1] = $tmp;
}
}
}
// Load PDF structure from file if it is not defined in session
if(!isset($_SESSION['currentPDFStructure'])) {
// Load structure file to be edit
$reader = new PDFStructureReader();
if(isset($_GET['edit'])) {
$_SESSION['currentPDFStructure'] = $reader->read($type->getId(), $_GET['edit']);
$_GET['pdfname'] = $_GET['edit'];
}
// Load default structure file when creating a new one
else {
$_SESSION['currentPDFStructure'] = $reader->read($type->getId(), 'default');
}
}
if (!empty($_POST['form_submit'])) {
updateBasicSettings($_SESSION['currentPDFStructure']);
updateSectionTitles($_SESSION['currentPDFStructure']);
addSection($_SESSION['currentPDFStructure']);
}
$availablePDFFields = getAvailablePDFFields($type->getId());
// Create the values for the dropdown boxes for section headline defined by
// value entries and fetch all available modules
$modules = array();
$section_items_array = array();
$section_items = '';
$sortedModules = array();
foreach($availablePDFFields as $module => $fields) {
if ($module != 'main') {
$title = getModuleAlias($module, $type->getScope());
}
else {
$title = _('Main');
}
$sortedModules[$module] = $title;
}
natcasesort($sortedModules);
foreach($sortedModules as $module => $title) {
$values = $availablePDFFields[$module];
if (!is_array($values) || (sizeof($values) < 1)) {
continue;
}
$modules[] = $module;
$section_items .= "\n";
}
$modules = join(',',$modules);
// print header
include '../main_header.php';
// print error messages if any
if (sizeof($saveErrors) > 0) {
for ($i = 0; $i < sizeof($saveErrors); $i++) {
call_user_func_array('StatusMessage', $saveErrors[$i]);
}
echo "
\n";
}
$newFieldFieldElements = array();
foreach($sortedModules as $module => $title) {
$fields = $availablePDFFields[$module];
if (isset($fields) && is_array($fields) && (sizeof($fields) > 0)) {
$moduleFields = array();
foreach ($fields as $field => $fieldLabel) {
$moduleFields[$fieldLabel] = $module . "_" . $field;
}
$newFieldFieldElements[$title] = $moduleFields;
}
}
// structure name
$structureName = '';
if (isset($_GET['edit'])) {
$structureName = $_GET['edit'];
}
elseif (isset($_GET['pdfname'])) {
$structureName = $_GET['pdfname'];
}
else if (isset($_POST['pdfname'])) {
$structureName = $_POST['pdfname'];
}
// headline
$headline = $_SESSION['currentPDFStructure']->getTitle();
// logo
$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'];
}
$selectedLogo = array('printLogo.jpg');
if (isset($_SESSION['currentPDFStructure'])) {
$selectedLogo = array($_SESSION['currentPDFStructure']->getLogo());
}
?>