From af9cc1759d74af344af1fefc28019330ad79b692 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 22 Jun 2021 13:26:51 +0200 Subject: [PATCH] implement action runner --- .../Controller/Admin/d3ActionWizard.php | 2 +- .../Controller/Admin/d3ExportWizard.php | 2 +- src/Application/Model/ActionBase.php | 104 ++++-------------- .../Model/Actions/FixArtextendsItems.php | 89 +++++++++++++++ src/Application/Model/Configuration.php | 29 ++++- src/Application/Model/QueryBase.php | 5 - .../views/admin/de/d3DataWizard_lang.php | 10 +- .../views/admin/tpl/d3ActionWizard.tpl | 24 ++-- 8 files changed, 154 insertions(+), 111 deletions(-) create mode 100644 src/Application/Model/Actions/FixArtextendsItems.php diff --git a/src/Application/Controller/Admin/d3ActionWizard.php b/src/Application/Controller/Admin/d3ActionWizard.php index 970700f..caca3aa 100644 --- a/src/Application/Controller/Admin/d3ActionWizard.php +++ b/src/Application/Controller/Admin/d3ActionWizard.php @@ -44,7 +44,7 @@ class d3ActionWizard extends AdminDetailsController public function getGroups() { - return $this->configuration->getGroups(); + return $this->configuration->getActionGroups(); } public function getGroupActions($group) diff --git a/src/Application/Controller/Admin/d3ExportWizard.php b/src/Application/Controller/Admin/d3ExportWizard.php index ef8d555..236c97c 100644 --- a/src/Application/Controller/Admin/d3ExportWizard.php +++ b/src/Application/Controller/Admin/d3ExportWizard.php @@ -44,7 +44,7 @@ class d3ExportWizard extends AdminDetailsController public function getGroups() { - return $this->configuration->getGroups(); + return $this->configuration->getExportGroups(); } public function getGroupExports($group) diff --git a/src/Application/Model/ActionBase.php b/src/Application/Model/ActionBase.php index 79b46aa..20c9e43 100644 --- a/src/Application/Model/ActionBase.php +++ b/src/Application/Model/ActionBase.php @@ -15,15 +15,9 @@ declare(strict_types=1); namespace D3\DataWizard\Application\Model; -use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge; -use D3\ModCfg\Application\Model\d3filesystem; -use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception; -use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException; -use Doctrine\DBAL\DBALException; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; -use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Registry; abstract class ActionBase implements QueryBase @@ -35,85 +29,14 @@ abstract class ActionBase implements QueryBase { return ''; } - + /** - * @param string $format - * - * @throws DBALException * @throws DatabaseConnectionException * @throws DatabaseErrorException - * @throws Exceptions\NoSuitableRendererException - * @throws Exceptions\TaskException - * @throws StandardException - * @throws d3ShopCompatibilityAdapterException - * @throws d3_cfg_mod_exception */ public function run() { - $rowCount = $this->getExportData( $this->getQuery() ); - } - - /** - * @return string - */ - public function getButtonText() : string - { - return "D3_DATAWIZARD_EXPORT_SUBMIT"; - } - - /** - * @param $format - * - * @return ExportRenderer\RendererInterface - * @throws Exceptions\NoSuitableRendererException - */ - public function getRenderer($format): ExportRenderer\RendererInterface - { - return oxNew(RendererBridge::class)->getRenderer($format); - } - - /** - * @param $format - * - * @return string - * @throws Exceptions\NoSuitableRendererException - */ - public function getFileExtension($format): string - { - return $this->getRenderer($format)->getFileExtension(); - } - - /** - * @param $rows - * @param $fieldnames - * @param $format - * - * @return string - * @throws Exceptions\NoSuitableRendererException - */ - public function renderContent($rows, $fieldnames, $format): string - { - $renderer = $this->getRenderer($format); - return $renderer->getContent($rows, $fieldnames); - } - - /** - * @return string - */ - public function getExportFilenameBase() : string - { - return $this->getTitle(); - } - - /** - * @param $format - * - * @return string - * @throws Exceptions\NoSuitableRendererException - */ - public function getExportFileName($format) : string - { - return $this->getExportFilenameBase().'_'.date('Y-m-d_H-i-s').'.'.$this->getFileExtension($format); + $this->executeAction( $this->getQuery() ); } /** @@ -123,7 +46,7 @@ abstract class ActionBase implements QueryBase * @throws DatabaseConnectionException * @throws DatabaseErrorException */ - public function getExportData( array $query ): array + public function executeAction( array $query ): int { [ $queryString, $parameters ] = $query; @@ -137,6 +60,25 @@ abstract class ActionBase implements QueryBase ); } - return DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC )->execute( $queryString, $parameters ); + $affected = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC )->execute( $queryString, $parameters ); + + throw oxNew( + Exceptions\TaskException::class, + $this, + sprintf( + Registry::getLang()->translateString( + $affected === 1 ? 'D3_DATAWIZARD_ERR_ACTIONRESULT' : 'D3_DATAWIZARD_ERR_ACTIONRESULTS' + ), + $affected + ) + ); + } + + /** + * @return string + */ + public function getButtonText() : string + { + return "D3_DATAWIZARD_ACTION_SUBMIT"; } } \ No newline at end of file diff --git a/src/Application/Model/Actions/FixArtextendsItems.php b/src/Application/Model/Actions/FixArtextendsItems.php new file mode 100644 index 0000000..4a7f571 --- /dev/null +++ b/src/Application/Model/Actions/FixArtextendsItems.php @@ -0,0 +1,89 @@ + + * @link http://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\DataWizard\Application\Model\Actions; + +use D3\DataWizard\Application\Model\ActionBase; +use OxidEsales\Eshop\Core\Model\BaseModel; +use OxidEsales\Eshop\Core\Model\MultiLanguageModel; +use OxidEsales\Eshop\Core\Registry; + +class FixArtextendsItems extends ActionBase +{ + /** + * fehlende oxartextends-Einträge nachtragen + */ + + /** + * @return string + */ + public function getTitle() : string + { + return Registry::getLang()->translateString('D3_DATAWIZARD_ACTIONS_FIXARTEXTENDSITEMS'); + } + + /** + * @return array + */ + public function getQuery() : array + { + $aDefaultValueFields = array( + 'oxtimestamp' => "''", + ); + + $aNonArtExtendsFields = array( + 'oxid' => 'oxarticles.oxid', + ); + + $aArtExtendsFields = array_fill_keys($this->getArtExtendsFields(), "''"); + $aMergedFields = array_merge($aNonArtExtendsFields, $aArtExtendsFields); + $aQueryFields = array_diff_key($aMergedFields, $aDefaultValueFields); + + $sArtExtendsFields = implode(', ', array_keys($aQueryFields)); + + $select = "SELECT ".implode(', ', $aQueryFields). + " FROM oxarticles". + " LEFT JOIN oxartextends AS arx ON oxarticles.oxid = arx.oxid". + " WHERE arx.oxid IS NULL"; + + $query = "INSERT INTO oxartextends ($sArtExtendsFields) ". + $select; + + return [$query, []]; + } + + /** + * @return array + */ + public function getArtExtendsFields(): array + { + /** @var $oArtExtends MultiLanguageModel */ + $oArtExtends = oxNew(BaseModel::class); + $oArtExtends->init('oxartextends', false); + + $aFieldNames = $oArtExtends->getFieldNames(); + + if (false == $aFieldNames) { + $oArtExtends->disableLazyLoading(); + $aFieldNames = $oArtExtends->getFieldNames(); + } + + unset($aFieldNames[array_search('oxid', $aFieldNames)]); + + return $aFieldNames; + } +} \ No newline at end of file diff --git a/src/Application/Model/Configuration.php b/src/Application/Model/Configuration.php index 4a44dd4..c565431 100644 --- a/src/Application/Model/Configuration.php +++ b/src/Application/Model/Configuration.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\DataWizard\Application\Model; +use D3\DataWizard\Application\Model\Actions\FixArtextendsItems; use D3\DataWizard\Application\Model\Exports\InactiveCategories; use D3\DataWizard\Application\Model\Exports\KeyFigures; use OxidEsales\Eshop\Core\Registry; @@ -39,6 +40,8 @@ class Configuration public function configure() { if (false === Registry::getConfig()->getConfigParam('d3datawizard_hideexamples', false)) { + $this->registerAction(self::GROUP_ARTICLES, oxNew(FixArtextendsItems::class)); + $this->registerExport(self::GROUP_CATEGORY, oxNew(InactiveCategories::class)); $this->registerExport(self::GROUP_SHOP, oxNew(KeyFigures::class)); } @@ -81,16 +84,34 @@ class Configuration /** * @return array */ - public function getGroups(): array + public function getActionGroups(): array + { + return array_keys($this->actions); + } + + /** + * @return array + */ + public function getExportGroups(): array { return array_keys($this->exports); } + /** + * @param $group + * + * @return mixed + */ public function getActionsByGroup($group) { return $this->actions[$group]; } + /** + * @param $group + * + * @return mixed + */ public function getExportsByGroup($group) { return $this->exports[$group]; @@ -103,7 +124,7 @@ class Configuration { $all = []; - foreach ($this->getGroups() as $group) { + foreach ($this->getActionGroups() as $group) { $all = array_merge($all, $this->getActionsByGroup($group)); } @@ -117,7 +138,7 @@ class Configuration { $all = []; - foreach ($this->getGroups() as $group) { + foreach ($this->getExportGroups() as $group) { $all = array_merge($all, $this->getExportsByGroup($group)); } @@ -131,7 +152,7 @@ class Configuration */ public function getActionById($id) : ActionBase { - return $this->getAllExports()[$id]; + return $this->getAllActions()[$id]; } /** diff --git a/src/Application/Model/QueryBase.php b/src/Application/Model/QueryBase.php index b034dd8..c17b593 100644 --- a/src/Application/Model/QueryBase.php +++ b/src/Application/Model/QueryBase.php @@ -19,11 +19,6 @@ interface QueryBase { public function run(); - /** - * @return string - */ - public function getExportFilenameBase() : string; - /** * @return string */ diff --git a/src/Application/views/admin/de/d3DataWizard_lang.php b/src/Application/views/admin/de/d3DataWizard_lang.php index c7e6dab..e78d7f9 100644 --- a/src/Application/views/admin/de/d3DataWizard_lang.php +++ b/src/Application/views/admin/de/d3DataWizard_lang.php @@ -40,6 +40,8 @@ $aLang = array( 'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV-Format', 'D3_DATAWIZARD_EXPORT_FORMAT_PRETTY' => 'Pretty-Format', + 'D3_DATAWIZARD_ACTION_SUBMIT' => 'Aktion starten', + 'D3_DATAWIZARD_DEBUG' => 'Debug: %1$s', 'D3_DATAWIZARD_ERR_NOEXPORTSELECT' => 'Export kann nicht ausgefĂĽhrt werden. Exporte erfordern SELECT Query.', @@ -57,7 +59,9 @@ $aLang = array( 'D3_DATAWIZARD_EXPORTS_KEYFIGURES_BASKETSIZE' => 'Warenkorbhöhe', 'D3_DATAWIZARD_EXPORTS_KEYFIGURES_MONTH' => 'Monat', -// Abracadata -// Harry Potter -// Magic + 'D3_DATAWIZARD_ERR_ACTIONSELECT' => 'Aktion kann nicht ausgefĂĽhrt werden. Aktionen können keine SELECTs exportieren.', + 'D3_DATAWIZARD_ERR_NOACTION_INSTALLED' => 'Es sind keine Aktionen installiert oder aktiviert.', + 'D3_DATAWIZARD_ERR_ACTIONRESULT' => '%1$s Eintrag verändert', + 'D3_DATAWIZARD_ERR_ACTIONRESULTS' => '%1$s Einträge verändert', + 'D3_DATAWIZARD_ACTIONS_FIXARTEXTENDSITEMS' => 'fehlende oxartextends-Einträge nachtragen', ); diff --git a/src/Application/views/admin/tpl/d3ActionWizard.tpl b/src/Application/views/admin/tpl/d3ActionWizard.tpl index 0fc6046..efe3469 100644 --- a/src/Application/views/admin/tpl/d3ActionWizard.tpl +++ b/src/Application/views/admin/tpl/d3ActionWizard.tpl @@ -26,7 +26,7 @@ [{capture name="d3script"}][{strip}] - function startExport(id, format) { + function startAction(id) { let elements = document.getElementsByClassName('errorbox'); for (var i = 0; i < elements.length; i++){ elements[i].style.display = 'none'; @@ -37,8 +37,7 @@ }, 3000); document.getElementById('mask').className='on'; document.getElementById('popup2').className='d3loader-2 on'; - document.getElementById('exportid').value = id; - document.getElementById('exportformat').value = format; + document.getElementById('actionid').value = id; document.getElementById('myedit').submit(); } [{/strip}][{/capture}] @@ -47,9 +46,8 @@
[{$oViewConf->getHiddenSid()}] - - - + + [{assign var="groups" value=$oView->getGroups()}] [{if $groups|@count}] @@ -67,27 +65,21 @@
- [{foreach from=$oView->getGroupActions($group) key="id" item="export"}] + [{foreach from=$oView->getGroupActions($group) key="id" item="action"}]
- [{$export->getTitle()}] + [{$action->getTitle()}]

- [{$export->getDescription()}] + [{$action->getDescription()}]

-