From 8061ee0abc7f7d71b1983337b81fb0745b1c20dd Mon Sep 17 00:00:00 2001 From: Dmitrijus Druteika Date: Tue, 16 Dec 2014 17:18:55 +0200 Subject: [PATCH] Made form values remain filled on error after wizard submission. --- .../admin/admin_oxpsmodulegenerator.php | 108 ++++++++++++++++++ .../admin/Admin_oxpsModuleGeneratorTest.php | 87 ++++++++++++++ .../views/admin/admin_oxpsmodulegenerator.tpl | 59 +++++++--- 3 files changed, 235 insertions(+), 19 deletions(-) diff --git a/copy_this/modules/oxps/modulegenerator/controllers/admin/admin_oxpsmodulegenerator.php b/copy_this/modules/oxps/modulegenerator/controllers/admin/admin_oxpsmodulegenerator.php index 1e426ed..6f45e7e 100644 --- a/copy_this/modules/oxps/modulegenerator/controllers/admin/admin_oxpsmodulegenerator.php +++ b/copy_this/modules/oxps/modulegenerator/controllers/admin/admin_oxpsmodulegenerator.php @@ -27,6 +27,8 @@ /** * Class Admin_oxpsModuleGenerator. * Module Generator GUI controller. + * + * @todo (nice2have) Class got too long -> move some methods to validation helper or some other class. */ class Admin_oxpsModuleGenerator extends oxAdminView { @@ -79,6 +81,9 @@ public function render() $this->_setMessage('OXPS_MODULEGENERATOR_ADMIN_MODULE_ERROR_NO_VENDOR'); } + // Add clean module generation options and form values to view data + $this->_addViewData(array('oValues' => (object) $this->_getFormValues())); + // Parent render call return $this->_Admin_oxpsModuleGenerator_render_parent(); } @@ -209,6 +214,109 @@ protected function _getGenerationOptions() return $this->_filterListModels($aGenerationOptions); } + /** + * Get initial form values for a case when the form was submitted. + * + * @return array + */ + protected function _getFormValues() + { + /** @var oxpsModuleGeneratorValidator $oValidator */ + $oValidator = oxRegistry::get('oxpsModuleGeneratorValidator'); + + $aOptions = (array) $this->_getGenerationOptions(); + + return array( + 'name' => $this->_getTextParam('modulegenerator_module_name'), + 'extend' => $this->_toString(array_keys($oValidator->getArrayValue($aOptions, 'aExtendClasses', 'array'))), + 'controllers' => $this->_toString($oValidator->getArrayValue($aOptions, 'aNewControllers', 'array')), + 'models' => $this->_toString($oValidator->getArrayValue($aOptions, 'aNewModels', 'array')), + 'lists' => $this->_getListModelsFieldValue($oValidator->getArrayValue($aOptions, 'aNewLists', 'array')), + 'widgets' => $this->_toString($oValidator->getArrayValue($aOptions, 'aNewWidgets', 'array')), + 'blocks' => $this->_getBlocksFieldValue($oValidator->getArrayValue($aOptions, 'aNewBlocks', 'array')), + 'settings' => $oValidator->getArrayValue($aOptions, 'aModuleSettings', 'array'), + 'version' => $this->_getFormVersionFieldValue($oValidator->getArrayValue($aOptions, 'sInitialVersion')), + 'tests' => $oValidator->getArrayValue($aOptions, 'blFetchUnitTests', 'boolean'), + 'tasks' => $oValidator->getArrayValue($aOptions, 'blRenderTasks', 'boolean'), + 'samples' => $oValidator->getArrayValue($aOptions, 'blRenderSamples', 'boolean'), + ); + } + + /** + * Convert array to a multi-line string. + * + * @param array $aData + * + * @return string + */ + protected function _toString(array $aData) + { + return trim((string) implode(PHP_EOL, $aData)); + } + + /** + * Get initial value for list models field. + * Removes "List" suffixes from names. + * + * @param array $aRequestListModels + * + * @return string + */ + protected function _getListModelsFieldValue(array $aRequestListModels) + { + /** @var oxStrMb|oxStrRegular $oStr */ + $oStr = oxStr::getStr(); + + $aLists = array(); + + foreach ($aRequestListModels as $sListClassName) { + $aLists[] = $oStr->substr($sListClassName, 0, ($oStr->strlen($sListClassName) - 4)); + } + + return $this->_toString($aLists); + } + + /** + * Get initial value for module blocks field. + * + * @param array $aRequestBlocks + * + * @return string + */ + protected function _getBlocksFieldValue(array $aRequestBlocks) + { + /** @var oxpsModuleGeneratorValidator $oValidator */ + $oValidator = oxRegistry::get('oxpsModuleGeneratorValidator'); + + $aBlocks = array(); + + foreach ($aRequestBlocks as $oBlock) { + $aBlock = (array) $oBlock; + $aBlocks[] = $oValidator->getArrayValue($aBlock, 'block') . '@' . + $oValidator->getArrayValue($aBlock, 'template'); + } + + return $this->_toString($aBlocks); + } + + /** + * Get initial value for module version field. + * + * @param string $sRequestVersion + * + * @return string + */ + protected function _getFormVersionFieldValue($sRequestVersion) + { + $sModuleVersion = trim((string) $sRequestVersion); + + if (empty($sModuleVersion)) { + $sModuleVersion = '1.0.0'; + } + + return $sModuleVersion; + } + /** * Get request parameter as trimmed string. * diff --git a/copy_this/modules/oxps/modulegenerator/tests/unit/modules/controllers/admin/Admin_oxpsModuleGeneratorTest.php b/copy_this/modules/oxps/modulegenerator/tests/unit/modules/controllers/admin/Admin_oxpsModuleGeneratorTest.php index a0921a5..6707657 100644 --- a/copy_this/modules/oxps/modulegenerator/tests/unit/modules/controllers/admin/Admin_oxpsModuleGeneratorTest.php +++ b/copy_this/modules/oxps/modulegenerator/tests/unit/modules/controllers/admin/Admin_oxpsModuleGeneratorTest.php @@ -166,6 +166,93 @@ public function testRender_noVendorDataConfigured_setError() $this->assertTrue($aViewData['blError']); } + public function testRender_formWasSubmitted_collectProperFormInitialValuesFromParsedRequest() + { + // Config mock (request data) + modConfig::setRequestParameter('modulegenerator_module_name', 'badModuleName '); + modConfig::setRequestParameter( + 'modulegenerator_extend_classes', + 'oxarticle' . PHP_EOL . 'oxarticle' . PHP_EOL . 'oxlist' . PHP_EOL . 'oxarticle' . PHP_EOL . 'asdasd' + ); + modConfig::setRequestParameter( + 'modulegenerator_controllers', + ' Page' . PHP_EOL . PHP_EOL . ' ' . PHP_EOL . 'view' + ); + modConfig::setRequestParameter('modulegenerator_models', 'Item' . PHP_EOL . 'Thing_Two'); + modConfig::setRequestParameter('modulegenerator_lists', 'Item' . PHP_EOL . 'ThingTwo' . PHP_EOL . 'ItemList'); + modConfig::setRequestParameter('modulegenerator_widgets', 'Bar' . PHP_EOL . '1Trash'); + modConfig::setRequestParameter( + 'modulegenerator_blocks', + 'block@' . PHP_EOL . '@page.tpl' . PHP_EOL . 'block@page.tpl' + ); + modConfig::setRequestParameter( + 'modulegenerator_settings', + array( + array( + 'name' => 'MyString', + 'type' => 'str', + 'value' => 'Hello, word!', + ), + array( + 'name' => '', + 'type' => 'str', + 'value' => '', + ), + array( + 'name' => 'MyNumber', + 'type' => 'num', + 'value' => '888.8', + ) + ) + ); + modConfig::setRequestParameter('modulegenerator_init_version', '0.0.1 beta'); + modConfig::setRequestParameter('modulegenerator_render_tasks', '1'); + modConfig::setRequestParameter('modulegenerator_render_samples', '1'); + + $this->SUT->expects($this->once())->method('_Admin_oxpsModuleGenerator_init_parent'); + $this->SUT->expects($this->once())->method('_Admin_oxpsModuleGenerator_render_parent')->will( + $this->returnValue('admin_oxpsmodulegenerator.tpl') + ); + $this->SUT->init(); + $this->SUT->render(); + + $aViewData = $this->SUT->getViewData(); + $this->assertArrayHasKey('oValues', $aViewData); + $this->assertEquals( + (object) array( + 'name' => 'badModuleName', + 'extend' => 'oxarticle' . PHP_EOL . 'oxlist', + 'controllers' => 'Page', + 'models' => 'Item', + 'lists' => 'Item', + 'widgets' => 'Bar', + 'blocks' => 'block@page.tpl', + 'settings' => array( + array( + 'name' => 'MyString', + 'type' => 'str', + 'value' => 'Hello, word!' + ), + array( + 'name' => '', + 'type' => 'str', + 'value' => '' + ), + array( + 'name' => 'MyNumber', + 'type' => 'num', + 'value' => '888.8' + ), + ), + 'version' => '0.0.1 beta', + 'tests' => false, + 'tasks' => true, + 'samples' => true, + ), + $aViewData['oValues'] + ); + } + public function testGetModule() { diff --git a/copy_this/modules/oxps/modulegenerator/views/admin/admin_oxpsmodulegenerator.tpl b/copy_this/modules/oxps/modulegenerator/views/admin/admin_oxpsmodulegenerator.tpl index 15b1934..4f4dee7 100644 --- a/copy_this/modules/oxps/modulegenerator/views/admin/admin_oxpsmodulegenerator.tpl +++ b/copy_this/modules/oxps/modulegenerator/views/admin/admin_oxpsmodulegenerator.tpl @@ -29,7 +29,8 @@ * - + [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_MODULE_NAME_HINT"}] @@ -41,7 +42,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_OVERRIDE_CLASSES"}] - + [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_OVERRIDE_CLASSES_HINT"}] @@ -50,7 +52,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_CONTROLLERS"}] - + [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_CONTROLLERS_HINT"}] @@ -59,7 +62,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_MODELS"}] - + [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_MODELS_HINT"}] @@ -68,7 +72,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_LISTS"}] - + [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_LISTS_HINT"}] @@ -77,7 +82,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_WIDGETS"}] - + [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_WIDGETS_HINT"}] @@ -87,7 +93,7 @@ + rows="2">[{$oValues->blocks}] [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_BLOCKS_HINT"}] @@ -113,24 +119,36 @@ [{section name=settings start=0 loop=3}] [{assign var='i' value=$smarty.section.settings.index}] + [{assign var='aSetting' value=$oValues->settings.$i}] + [{assign var='sType' value=$aSetting.type}] + [{if not $sType}] + [{assign var='sType' value='str'}] + [{/if}] + value="[{$aSetting.name}]" maxlength="12"/> + rows="1">[{$aSetting.value}] [{/section}] @@ -147,7 +165,7 @@ * - @@ -170,7 +188,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CHECKOUT_UNIT_TESTS"}] - + tests}]checked="checked"[{/if}]/> [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CHECKOUT_UNIT_TESTS_HINT"}] @@ -182,7 +201,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_TASKS"}] - + tasks}]checked="checked"[{/if}]/> [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_TASKS_HINT"}] @@ -191,7 +211,8 @@ [{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_SAMPLES"}] - + samples}]checked="checked"[{/if}]/> [{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_SAMPLES_HINT"}]