From 6fc259d718d330e7f9dcb02451e56204ecc5ad68 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Tue, 12 May 2020 20:58:56 +0200 Subject: [PATCH] config import and export --- lam/lib/pdfstruct.inc | 47 +++++++++++++++++++++++- lam/lib/persistence.inc | 36 ++++++++++++++++++- lam/tests/lib/pdfstructTest.php | 59 +++++++++++++++++++++++++++++++ lam/tests/lib/persistenceTest.php | 14 +++++++- 4 files changed, 153 insertions(+), 3 deletions(-) diff --git a/lam/lib/pdfstruct.inc b/lam/lib/pdfstruct.inc index c9bff0e8..1df4e334 100644 --- a/lam/lib/pdfstruct.inc +++ b/lam/lib/pdfstruct.inc @@ -93,7 +93,7 @@ function deletePDFStructure($typeId, $name, $serverProfileName) { * This function returns an array with all aviliable logo images. * * @param string $serverProfileName server profile name - * @return array list of logo files + * @return array list of logo files (array('filename' => PATH, 'infos' => array(width, height))) */ function getAvailableLogos($serverProfileName) { $return = array(); @@ -564,6 +564,35 @@ class PDFStructure { return $data; } + /** + * Imports an array representation of the structure. + * + * @param array $data import data + */ + public function import($data) { + if (isset($data['title'])) { + $this->title = $data['title']; + } + if (isset($data['foldingMarks'])) { + $this->foldingMarks = $data['foldingMarks']; + } + if (isset($data['logo'])) { + $this->logo = $data['logo']; + } + if (isset($data['sections'])) { + foreach($data['sections'] as $section) { + if ($section['type'] === 'text') { + $this->sections[] = new PDFTextSection($section['data']); + } + else { + $entrySection = new PDFEntrySection(null); + $entrySection->import($section['data']); + $this->sections[] = $entrySection; + } + } + } + } + /** * Returns the logo file path. * @@ -710,6 +739,22 @@ class PDFEntrySection { return $data; } + /** + * Imports the section. + * + * @param array $data import data + */ + public function import($data) { + if (isset($data['title'])) { + $this->title = $data['title']; + } + if ($data['entries']) { + foreach($data['entries'] as $entry) { + $this->entries[] = new PDFSectionEntry($entry); + } + } + } + /** * Returns if the title is an attribute value. * diff --git a/lam/lib/persistence.inc b/lam/lib/persistence.inc index e1a0f933..edb400ab 100644 --- a/lam/lib/persistence.inc +++ b/lam/lib/persistence.inc @@ -1,8 +1,11 @@ _getServerProfiles($serverProfiles); $jsonData['accountProfiles'] = $this->_getAccountProfiles($serverProfiles); + $jsonData['pdfProfiles'] = $this->_getPdfProfiles($serverProfiles); /** * TODO * - * account profiles * PDF profiles * self service profiles * webauthn @@ -132,6 +135,36 @@ class ConfigDataExporter { return $data; } + /** + * Returns the content of the PDF profiles. + * + * @param array $serverProfiles list of server profiles (name => object) + * @return array $data + */ + public function _getPdfProfiles($serverProfiles) { + $data = array(); + foreach ($serverProfiles as $profileName => $serverProfile) { + foreach ($serverProfile->get_ActiveTypes() as $typeId) { + $pdfProfileNames = getPDFStructures($typeId, $profileName); + $reader = new PDFStructureReader($profileName); + foreach ($pdfProfileNames as $pdfProfileName) { + $pdfStructure = $reader->read($typeId, $pdfProfileName); + $data[$profileName]['structures'][$typeId][$pdfProfileName] = $pdfStructure->export(); + } + } + $logoData = getAvailableLogos($profileName); + foreach ($logoData as $logo) { + $logoFileName = $logo['filename']; + $logoPath = __DIR__ . '/../config/pdf/' . $profileName . '/logos/' . $logoFileName; + $handle = fopen($logoPath, 'r'); + $logoBinary = fread($handle, 100000000); + fclose($handle); + $data[$profileName]['logos'][$logoFileName] = base64_encode($logoBinary); + } + } + return $data; + } + } /** @@ -174,6 +207,7 @@ class ConfigDataImporter { } $steps[] = $mainStep; break; + // TODO PDF structures default: logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); } diff --git a/lam/tests/lib/pdfstructTest.php b/lam/tests/lib/pdfstructTest.php index f74d480b..58933af4 100644 --- a/lam/tests/lib/pdfstructTest.php +++ b/lam/tests/lib/pdfstructTest.php @@ -181,6 +181,65 @@ class PdfStructTest extends TestCase { $this->assertEquals($expected, $data); } + /** + * Tests import in PDFEntrySection. + */ + public function testImportPDFEntrySection() { + $data = array( + 'title' => 'mytitle', + 'entries' => array('e1', 'e2') + ); + + $section = new PDFEntrySection(null); + $section->import($data); + + $this->assertEquals('mytitle', $section->getTitle()); + $entries = $section->getEntries(); + $this->assertEquals(2, sizeof($entries)); + $this->assertEquals('e1', ($entries[0]->getKey())); + $this->assertEquals('e2', ($entries[1]->getKey())); + } + + /** + * Tests the import in PDFStructure. + */ + public function testImportPDFStructure() { + $data = array( + 'title' => 'mytitle', + 'foldingMarks' => PDFStructure::FOLDING_STANDARD, + 'logo' => 'logo', + 'sections' => array( + array( + 'type' => 'text', + 'data' => 'textvalue' + ), + array( + 'type' => 'entry', + 'data' => array( + 'title' => 'etitle', + 'entries' => array('e1', 'e2') + ) + ), + ) + ); + + $structure = new PDFStructure(); + $structure->import($data); + + $this->assertEquals('mytitle', $structure->getTitle()); + $this->assertEquals(PDFStructure::FOLDING_STANDARD, $structure->getFoldingMarks()); + $this->assertEquals('logo', $structure->getLogo()); + $sections = $structure->getSections(); + $this->assertEquals(2, sizeof($sections)); + $this->assertTrue($sections[0] instanceof PDFTextSection); + $this->assertEquals('textvalue', $sections[0]->getText()); + $this->assertTrue($sections[1] instanceof PDFEntrySection); + $entries = $sections[1]->getEntries(); + $this->assertEquals(2, sizeof($entries)); + $this->assertEquals('e1', $entries[0]->getKey()); + $this->assertEquals('e2', $entries[1]->getKey()); + } + } ?> \ No newline at end of file diff --git a/lam/tests/lib/persistenceTest.php b/lam/tests/lib/persistenceTest.php index 27a5d37a..36a9ddf6 100644 --- a/lam/tests/lib/persistenceTest.php +++ b/lam/tests/lib/persistenceTest.php @@ -48,21 +48,33 @@ class ConfigDataExporterTest extends TestCase { 'group' => array('default' => array('key' => 'value')), ), ); + $pdfData = array( + 'profile1' => array('structures' => array( + 'user' => array( + 'default' => array('key' => 'value')) + )), + 'profile1' => array('structures' => array( + 'user' => array('default' => array('key' => 'value')), + 'group' => array('default' => array('key' => 'value')), + )), + ); $expectedJson = json_encode(array( 'mainConfig' => $mainData, 'certificates' => 'certs', 'serverProfiles' => $profileData, 'accountProfiles' => $accountProfileData, + 'pdfProfiles' => $pdfData, )); $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') ->setMethods(array('_getMainConfigData', '_getCertificates', '_getServerProfiles', - '_getAccountProfiles')) + '_getAccountProfiles', '_getPdfProfiles')) ->getMock(); $exporter->method('_getMainConfigData')->willReturn($mainData); $exporter->method('_getCertificates')->willReturn('certs'); $exporter->method('_getServerProfiles')->willReturn($profileData); $exporter->method('_getAccountProfiles')->willReturn($accountProfileData); + $exporter->method('_getPdfProfiles')->willReturn($pdfData); $json = $exporter->exportAsJson();