config import and export

This commit is contained in:
Roland Gruber 2020-05-12 20:58:56 +02:00
parent 40fd19e3bf
commit 6fc259d718
4 changed files with 153 additions and 3 deletions

View File

@ -93,7 +93,7 @@ function deletePDFStructure($typeId, $name, $serverProfileName) {
* This function returns an array with all aviliable logo images. * This function returns an array with all aviliable logo images.
* *
* @param string $serverProfileName server profile name * @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) { function getAvailableLogos($serverProfileName) {
$return = array(); $return = array();
@ -564,6 +564,35 @@ class PDFStructure {
return $data; 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. * Returns the logo file path.
* *
@ -710,6 +739,22 @@ class PDFEntrySection {
return $data; 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. * Returns if the title is an attribute value.
* *

View File

@ -1,8 +1,11 @@
<?php <?php
namespace LAM\PERSISTENCE; namespace LAM\PERSISTENCE;
use LAM\PDF\PDFStructureReader;
use LAMCfgMain; use LAMCfgMain;
use LAMConfig; use LAMConfig;
use LAMException; use LAMException;
use function LAM\PDF\getAvailableLogos;
use function LAM\PDF\getPDFStructures;
use function LAM\PROFILES\getAccountProfiles; use function LAM\PROFILES\getAccountProfiles;
use function LAM\PROFILES\loadAccountProfile; use function LAM\PROFILES\loadAccountProfile;
use function LAM\PROFILES\saveAccountProfile; use function LAM\PROFILES\saveAccountProfile;
@ -58,10 +61,10 @@ class ConfigDataExporter {
} }
$jsonData['serverProfiles'] = $this->_getServerProfiles($serverProfiles); $jsonData['serverProfiles'] = $this->_getServerProfiles($serverProfiles);
$jsonData['accountProfiles'] = $this->_getAccountProfiles($serverProfiles); $jsonData['accountProfiles'] = $this->_getAccountProfiles($serverProfiles);
$jsonData['pdfProfiles'] = $this->_getPdfProfiles($serverProfiles);
/** /**
* TODO * TODO
* *
* account profiles
* PDF profiles * PDF profiles
* self service profiles * self service profiles
* webauthn * webauthn
@ -132,6 +135,36 @@ class ConfigDataExporter {
return $data; 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; $steps[] = $mainStep;
break; break;
// TODO PDF structures
default: default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
} }

View File

@ -181,6 +181,65 @@ class PdfStructTest extends TestCase {
$this->assertEquals($expected, $data); $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());
}
} }
?> ?>

View File

@ -48,21 +48,33 @@ class ConfigDataExporterTest extends TestCase {
'group' => array('default' => array('key' => 'value')), '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( $expectedJson = json_encode(array(
'mainConfig' => $mainData, 'mainConfig' => $mainData,
'certificates' => 'certs', 'certificates' => 'certs',
'serverProfiles' => $profileData, 'serverProfiles' => $profileData,
'accountProfiles' => $accountProfileData, 'accountProfiles' => $accountProfileData,
'pdfProfiles' => $pdfData,
)); ));
$exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter')
->setMethods(array('_getMainConfigData', '_getCertificates', '_getServerProfiles', ->setMethods(array('_getMainConfigData', '_getCertificates', '_getServerProfiles',
'_getAccountProfiles')) '_getAccountProfiles', '_getPdfProfiles'))
->getMock(); ->getMock();
$exporter->method('_getMainConfigData')->willReturn($mainData); $exporter->method('_getMainConfigData')->willReturn($mainData);
$exporter->method('_getCertificates')->willReturn('certs'); $exporter->method('_getCertificates')->willReturn('certs');
$exporter->method('_getServerProfiles')->willReturn($profileData); $exporter->method('_getServerProfiles')->willReturn($profileData);
$exporter->method('_getAccountProfiles')->willReturn($accountProfileData); $exporter->method('_getAccountProfiles')->willReturn($accountProfileData);
$exporter->method('_getPdfProfiles')->willReturn($pdfData);
$json = $exporter->exportAsJson(); $json = $exporter->exportAsJson();