continue with language export/import impl.

This commit is contained in:
Dennis Eichhorn 2021-02-08 18:47:42 +01:00
parent b0a3bb4123
commit e81a0b8ffe
12 changed files with 302 additions and 35 deletions

View File

@ -23,6 +23,7 @@ use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Model\Message\FormValidation;
use phpOMS\System\File\Local\Directory;
use phpOMS\System\MimeType;
/**
* Exchange controller class.
@ -160,20 +161,38 @@ final class ApiController extends Controller
*/
public function apiExchangeExport(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{
$export = $this->exportDataFromRequest($request);
$status = NotificationLevel::ERROR;
$message = 'Export failed.';
$export = $this->exportDataFromRequest($request);
if ($export['type'] === 'file') {
$file = \explode('.', $export['name']);
if ($export) {
$status = NotificationLevel::OK;
$message = 'Export succeeded.';
$response->header->setDownloadable($file[0], $file[1]);
switch ($file[1]) {
case 'csv':
$response->header->set(
'Content-disposition', 'attachment; filename="'
. $export['name']
. '"'
, true);
//$response->header->set('Content-Type', MimeType::M_CONF, true);
break;
}
$response->set('export', $export['content']);
} else {
$status = NotificationLevel::ERROR;
$message = 'Export failed.';
if ($export['status']) {
$status = NotificationLevel::OK;
$message = 'Export succeeded.';
}
$response->set($request->uri->__toString(), [
'status' => $status,
'title' => 'Exchange',
'message' => $message,
]);
}
$response->set($request->uri->__toString(), [
'status' => $status,
'title' => 'Exchange',
'message' => $message,
]);
}
/**
@ -181,11 +200,11 @@ final class ApiController extends Controller
*
* @param RequestAbstract $request Request
*
* @return bool
* @return array
*
* @since 1.0.0
*/
private function exportDataFromRequest(RequestAbstract $request) : bool
private function exportDataFromRequest(RequestAbstract $request) : array
{
/** @var \Modules\Exchange\Models\InterfaceManager[] $interfaces */
$interfaces = InterfaceManagerMapper::getAll();
@ -200,7 +219,7 @@ final class ApiController extends Controller
Directory::delete(__DIR__ . '/../tmp/');
return false;
return [];
}
/**

View File

@ -135,6 +135,14 @@ final class BackendController extends Controller
$view->setTemplate('/Modules/Exchange/Theme/Backend/exchange-export');
$view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1007001001, $request, $response));
/** @var \Modules\Exchange\Models\InterfaceManager $interface */
$interface = InterfaceManagerMapper::get((int) $request->getData('id'));
$view->addData('interface', $interface);
$lang = include __DIR__ . '/../Interfaces/' . $interface->getInterfacePath() . '/Lang/' . $response->getLanguage() . '.lang.php';
$view->addData('lang', $lang);
return $view;
}

View File

@ -23,6 +23,8 @@ use phpOMS\Localization\ISO639x1Enum;
use phpOMS\Message\RequestAbstract;
use phpOMS\System\File\Local\Directory;
use phpOMS\Utils\IO\Zip\Zip;
use Modules\Exchange\Models\ExporterAbstract;
use phpOMS\Utils\StringUtils;
/**
* OMS export class
@ -70,33 +72,128 @@ final class Exporter extends ExporterAbstract
*
* @param RequestAbstract $request Request
*
* @return bool
* @return array
*
* @since 1.0.0
*/
public function exportFromRequest(RequestAbstract $request) : bool
public function exportFromRequest(RequestAbstract $request) : array
{
$start = new \DateTime($request->getData('start') ?? 'now');
$end = new \DateTime($request->getData('end') ?? 'now');
$this->account = $request->header->account;
if (((bool) ($request->getData('language') ?? false))) {
$this->exportLanguage();
if ($request->getData('type') === 'language') {
return $this->exportLanguage();
}
return true;
return [];
}
/**
* Export language
*
* @return void
* @return array
*
* @since 1.0.0
*/
public function exportLanguage() : void
public function exportLanguage() : array
{
$languageArray = [];
$supportedLanguages = [];
$basePath = __DIR__ . '/../../../';
$modules = \scandir($basePath);
foreach ($modules as $module) {
$themePath = $basePath . $module . '/Theme/';
if (!\is_dir($basePath . $module) || $module === '.' || $module === '..'
|| !\is_dir($themePath)
) {
continue;
}
$themes = \scandir($themePath);
foreach ($themes as $theme) {
$langPath = $themePath . $theme . '/Lang/';
if (!\is_dir($themePath . $theme) || $theme === '.' || $theme === '..'
|| !\is_dir($langPath)
) {
continue;
}
$languages = \scandir($themePath . $theme . '/Lang/');
foreach ($languages as $language) {
if (\stripos($language, '.lang.') === false || $language === '.' || $language === '..') {
continue;
}
$components = \explode('.', $language);
if (\strlen($components[0]) === 2) {
// normal language file
$supportedLanguages[] = $components[0];
$array = include $themePath . $theme . '/Lang/' . $language;
$array = \reset($array);
if ($array === false) {
continue;
}
foreach ($array as $key => $value) {
$languageArray[\trim($module, '/')][\trim($theme, '/')][$key][$components[0]] = $value;
}
}
}
}
// search for translations in tpl files which are not included in the language fieles
foreach ($themes as $theme) {
if (!\is_dir($themePath . $theme) || $theme === '.' || $theme === '..') {
continue;
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($themePath . $theme . '/', \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
if ($item->isDir() || !StringUtils::endsWith($item->getFilename(), '.tpl.php')) {
continue;
}
$template = \file_get_contents($item->getPathname());
$keys = [];
\preg_match_all('/(\$this\->getHtml\(\')([a-zA-Z:]+)(\'\))/', $template, $keys, \PREG_PATTERN_ORDER);
foreach ($keys[2] ?? [] as $key) {
if (!isset($languageArray[\trim($module, '/')][\trim($theme, '/')][$key])) {
$languageArray[\trim($module, '/')][\trim($theme, '/')][$key]['en'] = '';
}
}
}
}
}
$supportedLanguages = \array_unique($supportedLanguages);
$content = '"Module";"Theme";"ID";"' . \implode('";"', $supportedLanguages) . '"';
foreach ($languageArray as $module => $themes) {
foreach ($themes as $theme => $keys) {
foreach ($keys as $key => $value) {
$content .= "\n\"" . $module . '";"' . $theme . '";"' . $key . '"';
foreach ($supportedLanguages as $language) {
$content .= ';"' . ($value[$language] ?? '') . '"';
}
}
}
}
return [
'type' => 'file',
'name' => 'languages.csv',
'content' => $content
];
}
}

View File

@ -24,6 +24,7 @@ use phpOMS\Message\RequestAbstract;
use phpOMS\System\File\Local\Directory;
use phpOMS\Utils\IO\Zip\Zip;
use Modules\Media\Controller\ApiController;
use Modules\Exchange\Models\ImporterAbstract;
/**
* OMS import class
@ -100,7 +101,7 @@ final class Importer extends ImporterAbstract
$this->account = $request->header->account;
if (((bool) ($request->getData('language') ?? false))) {
if ($request->getData('type') === 'language') {
$this->importLanguage($request);
}
@ -117,5 +118,64 @@ final class Importer extends ImporterAbstract
public function importLanguage(RequestAbstract $request) : void
{
$upload = ApiController::uploadFilesToDestination($request->getFiles());
$fp = \fopen($upload['file0']['path'] . '/' . $upload['file0']['filename'], 'r');
$header = \fgetcsv($fp, ';', '"');
$languageArray = [];
$supportedLanguages = \array_slice($header, 3);
while(($line = \fgetcsv($fp, ';', '"')) !== false) {
$translations = \array_slice($header, 3);
foreach ($languageArray as $index => $language) {
if (empty(\trim($language))) {
continue;
}
$languageArray[\trim($line[0])][\trim($line[1])][\trim($line[2])][\trim($language)] = $translations[$index];
}
}
\fclose($fp);
\unlink($upload['file0']['path'] . '/' . $upload['file0']['filename']);
foreach ($languageArray as $module => $themes) {
foreach ($themes as $theme => $keys) {
foreach ($supportedLanguages as $language) {
$langFile = __DIR__ . '/../../../' . \trim($module) . '/Theme/' . $theme . '/Lang/' . \trim($language) . '.lang.php';
if (\is_file($langFile)) {
\unlink($langFile);
}
$fp = \fopen($langFile, 'w+');
\fwrite($fp,
"<?php\n"
. "/**\n"
. " * Orange Management\n"
. " *\n"
. " * PHP Version 8.0\n"
. " *\n"
. " * @copyright Dennis Eichhorn\n"
. " * @license OMS License 1.0\n"
. " * @version 1.0.0\n"
. " * @link https://orange-management.org\n"
. " */\n"
. "declare(strict_types=1);\n\n"
. "return [\'' . '\'] => [\n"
);
foreach ($keys as $key => $values) {
\fwrite($fp,
" '" . $key . "' => '" . ($values[$language] ?? '') . "',\n"
);
}
\fwrite($fp, "]];\n");
\fclose($fp);
}
}
}
}
}

View File

@ -13,12 +13,5 @@ declare(strict_types=1);
* @link https://orange-management.org
*/
return [
'Account' => 'Account',
'Article' => 'Articls',
'CostCenter' => 'Cost Center',
'CostObject' => 'Cost Object',
'Customer' => 'Customer',
'Invoice' => 'Invoice',
'Options' => 'Options',
'Supplier' => 'Supplier',
'Language' => 'Language',
];

View File

@ -0,0 +1,18 @@
<?php
$lang = $this->getData('lang');
?>
<div class="row">
<div class="col-xs-12 col-md-6">
<section class="portlet">
<form id="fImport" method="POST" action="<?= \phpOMS\Uri\UriFactory::build('{/api}admin/exchange/export/profile?{?}&exchange=OMS&type=language&csrf={$CSRF}'); ?>">
<div class="portlet-head"><?= $this->printHtml($lang['Language']); ?> - OMS</div>
<div class="portlet-body">
</div>
<div class="portlet-foot">
<input type="submit" value="<?= $this->getHtml('Export'); ?>">
</div>
</form>
</section>
</div>
</div>

View File

@ -1,8 +1,12 @@
<?php
$lang = $this->getData('lang');
?>
<div class="row">
<div class="col-xs-12 col-md-6">
<section class="portlet">
<form id="fImport" method="POST" action="<?= \phpOMS\Uri\UriFactory::build('{/api}admin/exchange/import/profile?{?}&exchange=OMS&type=language&csrf={$CSRF}'); ?>">
<div class="portlet-head"><?= $this->getHtml('Langauge'); ?> - OMS</div>
<div class="portlet-head"><?= $this->printHtml($lang['Language']); ?> - OMS</div>
<div class="portlet-body">
<table class="layout wf-100" style="table-layout: fixed">
<tbody>

View File

@ -40,7 +40,7 @@ class ExchangeLog implements \JsonSerializable, ArrayableInterface
* @var string
* @since 1.0.0
*/
private string $message = '';
public string $message = '';
/**
* Fields.
@ -58,6 +58,8 @@ class ExchangeLog implements \JsonSerializable, ArrayableInterface
*/
private int $type = ExchangeType::IMPORT;
public string $subtype = '';
/**
* Date type.
*
@ -66,6 +68,8 @@ class ExchangeLog implements \JsonSerializable, ArrayableInterface
*/
public \DateTimeImmutable $createdAt;
public int $createdBy = 0;
/**
* Constructor.
*

View File

@ -0,0 +1,60 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Interfaces
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Exchange\Models;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\Message\RequestAbstract;
/**
* Export abstract
*
* @package Interfaces
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
abstract class ExporterAbstract
{
/**
* Database connection.
*
* @var ConnectionAbstract
* @since 1.0.0
*/
protected ConnectionAbstract $local;
/**
* Constructor
*
* @param ConnectionAbstract $local Database connection
*
* @since 1.0.0
*/
public function __construct(ConnectionAbstract $local)
{
$this->local = $local;
}
/**
* Export data from request
*
* @param RequestAbstract $request Request
*
* @return array
*
* @since 1.0.0
*/
abstract public function exportFromRequest(RequestAbstract $request) : array;
}

View File

@ -29,7 +29,7 @@ echo $this->getData('nav')->render();
<?php $count = 0; foreach ($interfaces as $key => $value) : ++$count;
$url = \phpOMS\Uri\UriFactory::build('{/prefix}admin/exchange/export/profile?{?}&id=' . $value->getId()); ?>
<tr tabindex="0" data-href="<?= $url; ?>">
<td data-label="<?= $this->getHtml('Title'); ?>"><a href="<?= $url; ?>"><?= $this->printHtml($value->name); ?></a>
<td data-label="<?= $this->getHtml('Title'); ?>"><a href="<?= $url; ?>"><?= $this->printHtml($value->getName()); ?></a>
<?php endforeach; ?>
<?php if ($count === 0) : ?>
<tr><td colspan="2" class="empty"><?= $this->getHtml('Empty', '0', '0'); ?>

View File

@ -12,5 +12,9 @@
*/
declare(strict_types=1);
$lang = $this->getData('lang');
/** @var \phpOMS\Views\View $this */
echo $this->getData('nav')->render();
include __DIR__ . '/../../Interfaces/' . $this->getData('interface')->getInterfacePath() . '/export.tpl.php';

View File

@ -29,7 +29,7 @@ echo $this->getData('nav')->render();
<?php $count = 0; foreach ($interfaces as $key => $value) : ++$count;
$url = \phpOMS\Uri\UriFactory::build('{/prefix}admin/exchange/import/profile?{?}&id=' . $value->getId()); ?>
<tr tabindex="0" data-href="<?= $url; ?>">
<td data-label="<?= $this->getHtml('Title'); ?>"><a href="<?= $url; ?>"><?= $this->printHtml($value->name); ?></a>
<td data-label="<?= $this->getHtml('Title'); ?>"><a href="<?= $url; ?>"><?= $this->printHtml($value->getName()); ?></a>
<?php endforeach; ?>
<?php if ($count === 0) : ?>
<tr><td colspan="2" class="empty"><?= $this->getHtml('Empty', '0', '0'); ?>