diff --git a/lam/lib/lamPDF.inc b/lam/lib/lamPDF.inc index cabd0f3f..bcd50665 100644 --- a/lam/lib/lamPDF.inc +++ b/lam/lib/lamPDF.inc @@ -1,4 +1,6 @@ fontName = $fontName; if (!defined('FPDF_FONTPATH')) { define('FPDF_FONTPATH', dirname(__FILE__) . '/font/'); @@ -72,7 +74,7 @@ class lamPDF extends UFPDF { // Call constructor of superclass parent::__construct('P','mm','A4'); - $this->page_definitions = $page_definitions; + $this->structure = $structure; // Open PDF file and write some basic information $this->Open(); @@ -81,7 +83,7 @@ class lamPDF extends UFPDF { $this->AddFont($this->fontName, 'I', $this->fontList[$this->fontName][2]); $this->AddFont($this->fontName, 'BI', $this->fontList[$this->fontName][3]); $this->setFont($this->fontName,"",12); - $this->setTitle($this->page_definitions['headline']); + $this->setTitle($structure->getTitle()); $this->setCreator("LDAP Account Manager"); $this->setMargins('10.0','10.0','10.0'); $this->setAutoPageBreak(true,'20.0'); @@ -91,21 +93,21 @@ class lamPDF extends UFPDF { * Creates the PDF page header. */ function header() { - if($this->page_definitions['filename'] != 'none') { - $imageFile = substr(__FILE__,0,strlen(__FILE__)- 14) . "config/pdf/" . $_SESSION['config']->getName() . "/logos/" . $this->page_definitions['filename']; + if($this->structure->getLogo() != 'none') { + $imageFile = substr(__FILE__,0,strlen(__FILE__)- 14) . "config/pdf/" . $_SESSION['config']->getName() . "/logos/" . $this->structure->getLogo(); $imgProperties = getimagesize($imageFile); $this->Image($imageFile,10,15,($imgProperties[0] / $this->k) / 5, ($imgProperties[1] / $this->k) / 5,0,"JPG"); } $this->SetY(23); $this->SetFont($this->fontName,"B",18); - $this->Cell(170,5,$this->page_definitions['headline'],0,1,"R",0); + $this->Cell(170,5,$this->structure->getTitle(), 0, 1, "R", 0); $this->Ln(3); $this->SetLineWidth(0.4); $this->Line(10,38,200,38); $this->Line(10,40,200,40); $this->SetY(50); //set folding marks - if (isset($this->page_definitions['foldingmarks']) && ($this->page_definitions['foldingmarks'] == 'standard')) { + if ($this->structure->getFoldingMarks() == PDFStructure::FOLDING_STANDARD) { $this->SetLineWidth(0.2); $foldingMarks = array(97, 202); foreach ($foldingMarks as $mark) { diff --git a/lam/lib/pdf.inc b/lam/lib/pdf.inc index 966a1a92..36b1edd7 100644 --- a/lam/lib/pdf.inc +++ b/lam/lib/pdf.inc @@ -1,8 +1,6 @@ get_type(); // Get PDF structure from xml file - $load = \LAM\PDF\loadPDFStructure($account_type->getId(), $pdf_structure); - $structure = $load['structure']; + $reader = new PDFStructureReader(); + $structure = $reader->read($account_type->getId(), $pdf_structure); // get list of PDF keys $pdfKeys = array(); - foreach($structure as $entry) { - if ($entry['tag'] == "SECTION" && $entry['type'] == "open") { - $key = $entry['attributes']['NAME']; - // only values with a starting "_" are keys - if (strpos($key, '_') === 0) { - $pdfKeys[] = substr($key, 1); - } + foreach($structure->getSections() as $section) { + if (!$section instanceof PDFEntrySection) { + continue; } - if ($entry['tag'] == "ENTRY") { - $pdfKeys[] = $entry['attributes']['NAME']; + if ($section->isAttributeTitle()) { + $pdfKeys[] = $section->getPdfKey(); + } + foreach ($section->getEntries() as $entry) { + $pdfKeys[] = $entry->getKey(); } } $pdfKeys = array_unique($pdfKeys); @@ -95,7 +92,7 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString = $fontName = "Dejavu"; // Create a new PDF file acording to the account type - $pdf = new \lamPDF($load['page_definitions'],$fontName); + $pdf = new \lamPDF($structure, $fontName); // Loop over each account and add a new page in the PDF file for it foreach($accounts as $account) { @@ -107,28 +104,10 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString = $entries = $account->get_pdfEntries($pdfKeys, $account_type->getId()); // Now create the PDF file acording to the structure with the submitted values - foreach($structure as $entry) { - // We have a new section to start - if($entry['tag'] == "SECTION" && $entry['type'] == "open") { - $name = $entry['attributes']['NAME']; - if(preg_match("/^_[a-zA-Z_]+/",$name)) { - $section_headline = getSectionHeadline($entries[substr($name,1)][0]); - } - else { - $section_headline = $name; - } - $pdf->setFont($fontName, "B", LAMPDF_FONT_SIZE_BIG); - $pdf->Write(0,$section_headline . ":"); - $pdf->Ln(LAMPDF_LINEHEIGHT); - } - // We have a section to end - elseif($entry['tag'] == "SECTION" && $entry['type'] == "close") { - $pdf->Ln(LAMPDF_LINEHEIGHT * 2); - } - // We have to include a static text. - elseif($entry['tag'] == "TEXT") { + foreach ($structure->getSections() as $section) { + if ($section instanceof PDFTextSection) { $pdf->setFont($fontName, "", LAMPDF_FONT_SIZE); - $info_string = str_replace("\r", "", $entry['value']); + $info_string = str_replace("\r", "", $section->getText()); $info_string = explode("\n", $info_string); foreach ($info_string as $text) { $pdf->MultiCell(0, LAMPDF_LINEHEIGHT, trim($text), 0, "L", 0); @@ -136,32 +115,42 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString = } $pdf->Ln(LAMPDF_LINEHEIGHT * 2); } - // We have to include an entry from the account - elseif($entry['tag'] == "ENTRY") { - // Get name of current entry - $name = $entry['attributes']['NAME']; - // skip non-existent entries - if (isset($entries[$name])) { - // Get current entry - $valueEntries = $entries[$name]; - - // Print entry only when module sumitted values for it - if(is_array($valueEntries)) { - // Loop over all rows of this entry (most of the time this will be just one) - foreach($valueEntries as $valueEntry) { - if ($valueEntry instanceof PDFLabelValue) { - printLabelValue($pdf, $valueEntry, $fontName); - } - else if ($valueEntry instanceof PDFTable) { - printTable($pdf, $valueEntry, $fontName); + elseif ($section instanceof PDFEntrySection) { + // section headline + if($section->isAttributeTitle()) { + $section_headline = getSectionHeadline($entries[$section->getPdfKey()][0]); + } + else { + $section_headline = $section->getTitle(); + } + $pdf->setFont($fontName, "B", LAMPDF_FONT_SIZE_BIG); + $pdf->Write(0,$section_headline . ":"); + $pdf->Ln(LAMPDF_LINEHEIGHT); + // entries + foreach ($section->getEntries() as $entry) { + // skip non-existent entries + if (isset($entries[$entry->getKey()])) { + // Get current entry + $valueEntries = $entries[$entry->getKey()]; + // Print entry only when module sumitted values for it + if(is_array($valueEntries)) { + // Loop over all rows of this entry (most of the time this will be just one) + foreach($valueEntries as $valueEntry) { + if ($valueEntry instanceof PDFLabelValue) { + printLabelValue($pdf, $valueEntry, $fontName); + } + else if ($valueEntry instanceof PDFTable) { + printTable($pdf, $valueEntry, $fontName); + } } } } } + // section end + $pdf->Ln(LAMPDF_LINEHEIGHT * 2); } } } - // Close PDF $pdf->Close(); if (!$returnAsString) { diff --git a/lam/lib/pdfstruct.inc b/lam/lib/pdfstruct.inc index 363d9be7..adb58109 100644 --- a/lam/lib/pdfstruct.inc +++ b/lam/lib/pdfstruct.inc @@ -401,10 +401,7 @@ class PDFStructureReader { } $structure->setLogo($xml->getAttribute('filename')); $structure->setTitle($xml->getAttribute('headline')); - $foldingMarks = $xml->getAttribute('foldingmarks'); - if ($foldingMarks === 'yes') { - $structure->setFoldingMarks(true); - } + $structure->setFoldingMarks($xml->getAttribute('foldingmarks')); $sections = array(); while ($xml->read()) { if ($xml->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE) { @@ -474,11 +471,16 @@ class PDFStructureReader { */ class PDFStructure { + /** no folding marks */ + const FOLDING_NONE = 'no'; + /** standard folding marks */ + const FOLDING_STANDARD = 'standard'; + private $logo = null; private $title = null; - private $foldingMarks = false; + private $foldingMarks = 'no'; private $sections = array(); @@ -521,7 +523,7 @@ class PDFStructure { /** * Returns if to print folding marks. * - * @return boolean print folding marks + * @return string print folding marks */ public function getFoldingMarks() { return $this->foldingMarks; @@ -530,7 +532,7 @@ class PDFStructure { /** * Sets if to print folding marks. * - * @param boolean $foldingMarks print folding marks + * @param string $foldingMarks print folding marks */ public function setFoldingMarks($foldingMarks) { $this->foldingMarks = $foldingMarks; diff --git a/lam/tests/lib/pdfstructTest.php b/lam/tests/lib/pdfstructTest.php index 33d94605..ecd850e9 100644 --- a/lam/tests/lib/pdfstructTest.php +++ b/lam/tests/lib/pdfstructTest.php @@ -2,6 +2,7 @@ use LAM\PDF\PDFTextSection; use LAM\PDF\PDFEntrySection; use LAM\PDF\PDFStructureReader; +use LAM\PDF\PDFStructure; /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) @@ -45,7 +46,7 @@ class ReadStructureTest extends PHPUnit_Framework_TestCase { $structure = $reader->read('type', 'name'); $this->assertEquals('printLogo.jpg', $structure->getLogo()); $this->assertEquals('User information', $structure->getTitle()); - $this->assertEquals(true, $structure->getFoldingMarks()); + $this->assertEquals(PDFStructure::FOLDING_STANDARD, $structure->getFoldingMarks()); $sections = $structure->getSections(); $this->assertEquals(3, sizeof($sections)); // check first section diff --git a/lam/tests/resources/pdf/test.xml b/lam/tests/resources/pdf/test.xml index dbeabd7c..ea5621b6 100644 --- a/lam/tests/resources/pdf/test.xml +++ b/lam/tests/resources/pdf/test.xml @@ -1,4 +1,4 @@ - +