Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate;
|
||||
|
||||
use Xentral\Core\DependencyInjection\ContainerInterface;
|
||||
use Xentral\Modules\ImportTemplate\Service\ImportTemplateGateway;
|
||||
use Xentral\Modules\ImportTemplate\Service\ImportTemplateJsonService;
|
||||
use Xentral\Modules\ImportTemplate\Service\ImportTemplateService;
|
||||
|
||||
final class Bootstrap
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function registerServices()
|
||||
{
|
||||
return [
|
||||
'ImportTemplateJsonService' => 'onInitImportTemplateJsonService',
|
||||
'ImportTemplateService' => 'onInitImportTemplateService',
|
||||
'ImportTemplateGateway' => 'onInitImportTemplateGateway',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return ImportTemplateJsonService
|
||||
*/
|
||||
public static function onInitImportTemplateJsonService(ContainerInterface $container)
|
||||
{
|
||||
return new ImportTemplateJsonService(
|
||||
$container->get('ImportTemplateService'),
|
||||
$container->get('ImportTemplateGateway')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return ImportTemplateService
|
||||
*/
|
||||
public function onInitImportTemplateService(ContainerInterface $container)
|
||||
{
|
||||
return new ImportTemplateService(
|
||||
$container->get('Database')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return ImportTemplateGateway
|
||||
*/
|
||||
public function onInitImportTemplateGateway(ContainerInterface $container)
|
||||
{
|
||||
return new ImportTemplateGateway(
|
||||
$container->get('Database')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
use Xentral\Modules\ImportTemplate\Exception\InvalidTemplateDataException;
|
||||
|
||||
class ImportTemplate implements JsonSerializable
|
||||
{
|
||||
/**@var string $id */
|
||||
private $id;
|
||||
|
||||
/**@var string $label */
|
||||
private $label = '';
|
||||
|
||||
/**@var string $delimiter */
|
||||
private $delimiter='';
|
||||
|
||||
/**@var int $lineNumber */
|
||||
private $lineNumber=0;
|
||||
|
||||
/**@var string $masking */
|
||||
private $masking='';
|
||||
|
||||
/**@var string $importCharSet */
|
||||
private $importCharSet='';
|
||||
|
||||
/**@var string $fields */
|
||||
private $fields='';
|
||||
|
||||
/**@var string $target */
|
||||
private $target='';
|
||||
|
||||
/**@var string $internalNote */
|
||||
private $internalNote='';
|
||||
|
||||
/**@var int $utf8decode */
|
||||
private $utf8decode=1;
|
||||
|
||||
/**@var string $charset */
|
||||
private $charset='';
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws InvalidTemplateDataException
|
||||
*
|
||||
* @return ImportTemplate
|
||||
*/
|
||||
public static function fromArray($data)
|
||||
{
|
||||
$isValid = self::validate($data);
|
||||
if (!$isValid) {
|
||||
throw new InvalidTemplateDataException();
|
||||
}
|
||||
|
||||
return self::fromDbState($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function validate($data)
|
||||
{
|
||||
$isValid = true;
|
||||
|
||||
if (isset($data['fields'])) {
|
||||
$regex = '/[\r\n0-9]+:/';
|
||||
preg_match_all($regex, $data['fields'], $matches);
|
||||
if (count($matches[0]) == 0) {
|
||||
$isValid = false;
|
||||
}
|
||||
} else {
|
||||
$isValid = false;
|
||||
}
|
||||
|
||||
if (isset($data['label'])) {
|
||||
if (strlen($data['label']) == 0) {
|
||||
$isValid = false;
|
||||
}
|
||||
} else {
|
||||
$isValid = false;
|
||||
}
|
||||
|
||||
return $isValid;
|
||||
}
|
||||
|
||||
public static function fromDbState($data)
|
||||
{
|
||||
$instance = new self();
|
||||
|
||||
if (isset($data['id'])) {
|
||||
$instance->id = $data['id'];
|
||||
}
|
||||
|
||||
if (isset($data['label'])) {
|
||||
$instance->label = $data['label'];
|
||||
}
|
||||
|
||||
if (isset($data['delimiter'])) {
|
||||
$instance->delimiter = $data['delimiter'];
|
||||
}
|
||||
|
||||
if (isset($data['lineNumber'])) {
|
||||
$instance->lineNumber = $data['lineNumber'];
|
||||
}
|
||||
|
||||
if (isset($data['masking'])) {
|
||||
$instance->masking = $data['masking'];
|
||||
}
|
||||
|
||||
if (isset($data['importCharSet'])) {
|
||||
$instance->importCharSet = $data['importCharSet'];
|
||||
}
|
||||
|
||||
if (isset($data['fields'])) {
|
||||
$instance->fields = $data['fields'];
|
||||
}
|
||||
|
||||
if (isset($data['target'])) {
|
||||
$instance->target = $data['target'];
|
||||
}
|
||||
|
||||
if (isset($data['internalNote'])) {
|
||||
$instance->internalNote = $data['internalNote'];
|
||||
}
|
||||
|
||||
if (isset($data['utf8decode'])) {
|
||||
$instance->utf8decode = $data['utf8decode'] == 1;
|
||||
}
|
||||
|
||||
if (isset($data['charset'])) {
|
||||
$instance->charset = $data['charset'];
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
public function getDelimiter()
|
||||
{
|
||||
return $this->delimiter;
|
||||
}
|
||||
|
||||
public function getLineNumber()
|
||||
{
|
||||
return $this->lineNumber;
|
||||
}
|
||||
|
||||
public function getMasking()
|
||||
{
|
||||
return $this->masking;
|
||||
}
|
||||
|
||||
public function getImportCharSet()
|
||||
{
|
||||
return $this->importCharSet;
|
||||
}
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
return $this->fields;
|
||||
}
|
||||
|
||||
public function getTarget()
|
||||
{
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
public function getInternalNote()
|
||||
{
|
||||
return $this->internalNote;
|
||||
}
|
||||
|
||||
public function getUtf8decode()
|
||||
{
|
||||
return $this->utf8decode;
|
||||
}
|
||||
|
||||
public function getCharset()
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
public function updateLabel($label)
|
||||
{
|
||||
$this->label = $label;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'label' => $this->label,
|
||||
'delimiter' => $this->delimiter,
|
||||
'lineNumber' => $this->lineNumber,
|
||||
'masking' => $this->masking,
|
||||
'importCharSet' => $this->importCharSet,
|
||||
'fields' => $this->fields,
|
||||
'target' => $this->target,
|
||||
'internalNote' => $this->internalNote,
|
||||
'utf8decode' => $this->utf8decode,
|
||||
'charset' => $this->charset,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Exception;
|
||||
|
||||
use Xentral\Core\Exception\ModuleExceptionInterface;
|
||||
|
||||
interface ImportTemplateExceptionInterface extends ModuleExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Exception;
|
||||
|
||||
class ImportTemplateNotFoundException extends \RuntimeException implements ImportTemplateExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class InvalidTemplateDataException extends Exception implements ImportTemplateExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Service;
|
||||
|
||||
use Xentral\Components\Database\Database;
|
||||
use Xentral\Modules\ImportTemplate\Exception\ImportTemplateNotFoundException;
|
||||
|
||||
class ImportTemplateGateway
|
||||
{
|
||||
/** @var Database $db */
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* @param Database $db
|
||||
*/
|
||||
public function __construct(Database $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $importTemplateId
|
||||
*
|
||||
* @throws ImportTemplateNotFoundException
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getImportTemplateById($importTemplateId)
|
||||
{
|
||||
$importTemplateData = $this->db->fetchRow(
|
||||
'SELECT
|
||||
i.id,
|
||||
i.bezeichnung AS `label`,
|
||||
i.ziel AS `target`,
|
||||
i.internebemerkung AS `internalnote`,
|
||||
i.fields AS `fields`,
|
||||
i.importtrennzeichen AS `delimiter`,
|
||||
i.importerstezeilenummer AS `linenumber`,
|
||||
i.importdatenmaskierung AS `masking`,
|
||||
i.importzeichensatz AS `importcharset`,
|
||||
i.utf8decode AS `utf8decode`,
|
||||
i.charset AS `charset`
|
||||
FROM `importvorlage` AS `i`
|
||||
WHERE i.id = :id',
|
||||
['id' => $importTemplateId]
|
||||
);
|
||||
|
||||
if (empty($importTemplateData)) {
|
||||
throw new ImportTemplateNotFoundException('ImportTemplateId not found: ID ' . $importTemplateId);
|
||||
}
|
||||
|
||||
return $importTemplateData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Service;
|
||||
|
||||
use RuntimeException;
|
||||
use Xentral\Modules\ImportTemplate\Data\ImportTemplate;
|
||||
use Xentral\Modules\ImportTemplate\Exception\ImportTemplateNotFoundException;
|
||||
use Xentral\Modules\ImportTemplate\Exception\InvalidTemplateDataException;
|
||||
|
||||
final class ImportTemplateJsonService
|
||||
{
|
||||
/** @var ImportTemplateService $importTemplateService */
|
||||
private $importTemplateService;
|
||||
|
||||
/** @var ImportTemplateGateway $importTemplateGateway */
|
||||
private $importTemplateGateway;
|
||||
|
||||
/**
|
||||
* @param ImportTemplateService $importTemplateService
|
||||
* @param ImportTemplateGateway $importTemplateGateway
|
||||
*/
|
||||
public function __construct(
|
||||
ImportTemplateService $importTemplateService,
|
||||
ImportTemplateGateway $importTemplateGateway
|
||||
) {
|
||||
$this->importTemplateService = $importTemplateService;
|
||||
$this->importTemplateGateway = $importTemplateGateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $templateData
|
||||
*
|
||||
* @throws InvalidTemplateDataException
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function insertAndValidateImportTemplate($templateData)
|
||||
{
|
||||
$validData = ImportTemplate::fromArray($templateData);
|
||||
|
||||
return $this->insertImportTemplate($validData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportTemplate $importTemplate
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function insertImportTemplate(ImportTemplate $importTemplate)
|
||||
{
|
||||
return $this->importTemplateService->insertImportTemplate($importTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $importTemplateId
|
||||
*
|
||||
* @throws ImportTemplateNotFoundException
|
||||
*
|
||||
* @return ImportTemplate
|
||||
*/
|
||||
public function getImportTemplate($importTemplateId)
|
||||
{
|
||||
$importTemplateData = $this->importTemplateGateway->getImportTemplateById($importTemplateId);
|
||||
$importTemplate = ImportTemplate::fromDbState($importTemplateData);
|
||||
|
||||
return $importTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\ImportTemplate\Service;
|
||||
|
||||
use RuntimeException;
|
||||
use Xentral\Modules\ImportTemplate\Data\ImportTemplate;
|
||||
use Xentral\Components\Database\Database;
|
||||
|
||||
class ImportTemplateService
|
||||
{
|
||||
/** @var Database $db */
|
||||
private $db;
|
||||
|
||||
public function __construct(Database $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportTemplate $importTemplate
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function insertImportTemplate(ImportTemplate $importTemplate)
|
||||
{
|
||||
$sql = 'INSERT INTO `importvorlage`
|
||||
(
|
||||
`bezeichnung`,
|
||||
`ziel`,
|
||||
`internebemerkung`,
|
||||
`fields`,
|
||||
`importtrennzeichen`,
|
||||
`importerstezeilenummer`,
|
||||
`importdatenmaskierung`,
|
||||
`importzeichensatz`,
|
||||
`utf8decode`,
|
||||
`charset`
|
||||
|
||||
)
|
||||
VALUES (
|
||||
:bezeichnung,
|
||||
:ziel,
|
||||
:internebemerkung,
|
||||
:fields,
|
||||
:importtrennzeichen,
|
||||
:importerstezeilenummer,
|
||||
:importdatenmaskierung,
|
||||
:importzeichensatz,
|
||||
:utf8decode,
|
||||
:charset
|
||||
)';
|
||||
|
||||
$values = [
|
||||
'bezeichnung' => $importTemplate->getLabel(),
|
||||
'ziel' => $importTemplate->getTarget(),
|
||||
'internebemerkung' => $importTemplate->getInternalNote(),
|
||||
'fields' => $importTemplate->getFields(),
|
||||
'importtrennzeichen' => $importTemplate->getDelimiter(),
|
||||
'importerstezeilenummer' => $importTemplate->getLineNumber(),
|
||||
'importdatenmaskierung' => $importTemplate->getMasking(),
|
||||
'importzeichensatz' => $importTemplate->getImportCharSet(),
|
||||
'utf8decode' => $importTemplate->getUtf8decode(),
|
||||
'charset' => $importTemplate->getCharset(),
|
||||
];
|
||||
$this->db->perform($sql, $values);
|
||||
$insertId = $this->db->lastInsertId();
|
||||
if ($insertId === 0) {
|
||||
throw new RuntimeException('Import template could not be created.');
|
||||
}
|
||||
|
||||
return $insertId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
var ImportTemplateJsonFileUpload = (function ($) {
|
||||
'use strict';
|
||||
|
||||
var me = {
|
||||
isInitialized: false,
|
||||
|
||||
selector: {
|
||||
uploadDialog: '#jsonUploadDialog',
|
||||
editUploadDialog: '#jsonEditUploadDialog',
|
||||
jsonfile: '#jsonfile'
|
||||
},
|
||||
|
||||
storage: {
|
||||
$dialog: null
|
||||
},
|
||||
|
||||
init: function () {
|
||||
|
||||
if (me.isInitialized === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
me.storage.$dialog = $(me.selector.editUploadDialog);
|
||||
|
||||
me.dialogInit();
|
||||
me.registerEvents();
|
||||
me.isInitialized = true;
|
||||
},
|
||||
|
||||
registerEvents: function () {
|
||||
|
||||
$(me.selector.uploadDialog).on('click', function (event) {
|
||||
event.preventDefault();
|
||||
me.dialogOpen();
|
||||
});
|
||||
},
|
||||
|
||||
dialogInit: function () {
|
||||
me.storage.$dialog.dialog({
|
||||
modal: true,
|
||||
bgiframe: true,
|
||||
closeOnEscape: false,
|
||||
minWidth: 500,
|
||||
minHeight: 110,
|
||||
maxHeight: 200,
|
||||
autoOpen: false,
|
||||
|
||||
open: function () {
|
||||
$(me.selector.inputKey).trigger('focus');
|
||||
},
|
||||
close: function () {
|
||||
me.dialogReset();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
dialogOpen: function () {
|
||||
me.dialogReset();
|
||||
me.storage.$dialog.dialog('open');
|
||||
},
|
||||
|
||||
dialogClose: function () {
|
||||
me.storage.$dialog.dialog('close');
|
||||
},
|
||||
|
||||
dialogReset: function () {
|
||||
me.storage.$dialog.find(me.selector.jsonfile).val(null);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
init: me.init
|
||||
};
|
||||
|
||||
})(jQuery);
|
||||
|
||||
$(document).ready(function () {
|
||||
ImportTemplateJsonFileUpload.init();
|
||||
});
|
||||
Reference in New Issue
Block a user