diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 3403cd6..81f608f 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -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 []; } /** diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 13552ce..9fbdbfd 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -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; } diff --git a/Interfaces/OMS/Exporter.php b/Interfaces/OMS/Exporter.php index 07043b1..667315d 100644 --- a/Interfaces/OMS/Exporter.php +++ b/Interfaces/OMS/Exporter.php @@ -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 + ]; } } diff --git a/Interfaces/OMS/Importer.php b/Interfaces/OMS/Importer.php index ba3a1d5..fd4ffd8 100644 --- a/Interfaces/OMS/Importer.php +++ b/Interfaces/OMS/Importer.php @@ -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, + " [\n" + ); + + foreach ($keys as $key => $values) { + \fwrite($fp, + " '" . $key . "' => '" . ($values[$language] ?? '') . "',\n" + ); + } + + \fwrite($fp, "]];\n"); + \fclose($fp); + } + } + } } } diff --git a/Interfaces/OMS/Lang/en.lang.php b/Interfaces/OMS/Lang/en.lang.php index 384e0ac..8a6302f 100755 --- a/Interfaces/OMS/Lang/en.lang.php +++ b/Interfaces/OMS/Lang/en.lang.php @@ -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', ]; diff --git a/Interfaces/OMS/export.tpl.php b/Interfaces/OMS/export.tpl.php index e69de29..bf74739 100644 --- a/Interfaces/OMS/export.tpl.php +++ b/Interfaces/OMS/export.tpl.php @@ -0,0 +1,18 @@ +getData('lang'); +?> +
+
+
+
+
printHtml($lang['Language']); ?> - OMS
+
+
+
+ +
+
+
+
+
diff --git a/Interfaces/OMS/import.tpl.php b/Interfaces/OMS/import.tpl.php index b8bc366..4361261 100644 --- a/Interfaces/OMS/import.tpl.php +++ b/Interfaces/OMS/import.tpl.php @@ -1,8 +1,12 @@ +getData('lang'); +?>
-
getHtml('Langauge'); ?> - OMS
+
printHtml($lang['Language']); ?> - OMS
diff --git a/Models/ExchangeLog.php b/Models/ExchangeLog.php index 6667b8e..2078022 100644 --- a/Models/ExchangeLog.php +++ b/Models/ExchangeLog.php @@ -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. * diff --git a/Models/ExporterAbstract.php b/Models/ExporterAbstract.php new file mode 100644 index 0000000..bbd0326 --- /dev/null +++ b/Models/ExporterAbstract.php @@ -0,0 +1,60 @@ +local = $local; + } + + /** + * Export data from request + * + * @param RequestAbstract $request Request + * + * @return array + * + * @since 1.0.0 + */ + abstract public function exportFromRequest(RequestAbstract $request) : array; +} diff --git a/Theme/Backend/exchange-export-list.tpl.php b/Theme/Backend/exchange-export-list.tpl.php index bbf7b68..0098fea 100755 --- a/Theme/Backend/exchange-export-list.tpl.php +++ b/Theme/Backend/exchange-export-list.tpl.php @@ -29,7 +29,7 @@ echo $this->getData('nav')->render(); $value) : ++$count; $url = \phpOMS\Uri\UriFactory::build('{/prefix}admin/exchange/export/profile?{?}&id=' . $value->getId()); ?> - -
printHtml($value->name); ?> + printHtml($value->getName()); ?>
getHtml('Empty', '0', '0'); ?> diff --git a/Theme/Backend/exchange-export.tpl.php b/Theme/Backend/exchange-export.tpl.php index d263b86..86f0bad 100755 --- a/Theme/Backend/exchange-export.tpl.php +++ b/Theme/Backend/exchange-export.tpl.php @@ -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'; diff --git a/Theme/Backend/exchange-import-list.tpl.php b/Theme/Backend/exchange-import-list.tpl.php index 0ad269d..2fae6ab 100755 --- a/Theme/Backend/exchange-import-list.tpl.php +++ b/Theme/Backend/exchange-import-list.tpl.php @@ -29,7 +29,7 @@ echo $this->getData('nav')->render(); $value) : ++$count; $url = \phpOMS\Uri\UriFactory::build('{/prefix}admin/exchange/import/profile?{?}&id=' . $value->getId()); ?>
printHtml($value->name); ?> + printHtml($value->getName()); ?>
getHtml('Empty', '0', '0'); ?>