Skip to content

Commit

Permalink
Made form values remain filled on error after wizard submission.
Browse files Browse the repository at this point in the history
  • Loading branch information
druteika committed Dec 16, 2014
1 parent c11f508 commit 8061ee0
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<span class="req"> *</span>
</td>
<td class="edittext">
<input type="text" name="modulegenerator_module_name" value="" maxlength="20"/>
<input type="text" name="modulegenerator_module_name" value="[{$oValues->name}]"
maxlength="20"/>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_MODULE_NAME_HINT"}]
</td>
</tr>
Expand All @@ -41,7 +42,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_OVERRIDE_CLASSES"}]
</td>
<td class="edittext">
<textarea name="modulegenerator_extend_classes" cols="20" rows="3"></textarea>
<textarea name="modulegenerator_extend_classes" cols="20"
rows="3">[{$oValues->extend}]</textarea>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_OVERRIDE_CLASSES_HINT"}]
</td>
</tr>
Expand All @@ -50,7 +52,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_CONTROLLERS"}]
</td>
<td class="edittext">
<textarea name="modulegenerator_controllers" cols="20" rows="1"></textarea>
<textarea name="modulegenerator_controllers" cols="20"
rows="1">[{$oValues->controllers}]</textarea>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_CONTROLLERS_HINT"}]
</td>
</tr>
Expand All @@ -59,7 +62,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_MODELS"}]
</td>
<td class="edittext">
<textarea name="modulegenerator_models" cols="20" rows="1"></textarea>
<textarea name="modulegenerator_models" cols="20"
rows="1">[{$oValues->models}]</textarea>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_MODELS_HINT"}]
</td>
</tr>
Expand All @@ -68,7 +72,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_LISTS"}]
</td>
<td class="edittext">
<textarea name="modulegenerator_lists" cols="20" rows="1"></textarea>
<textarea name="modulegenerator_lists" cols="20"
rows="1">[{$oValues->lists}]</textarea>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_LISTS_HINT"}]
</td>
</tr>
Expand All @@ -77,7 +82,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_WIDGETS"}]
</td>
<td class="edittext">
<textarea name="modulegenerator_widgets" cols="20" rows="1"></textarea>
<textarea name="modulegenerator_widgets" cols="20"
rows="1">[{$oValues->widgets}]</textarea>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_WIDGETS_HINT"}]
</td>
</tr>
Expand All @@ -87,7 +93,7 @@
</td>
<td class="edittext">
<textarea class="wider" name="modulegenerator_blocks" cols="20"
rows="2"></textarea>
rows="2">[{$oValues->blocks}]</textarea>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_BLOCKS_HINT"}]
</td>
</tr>
Expand All @@ -113,24 +119,36 @@
<tbody>
[{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}]
<tr>
<td>
<input type="text" name="modulegenerator_settings[[{$i}]][name]"
value="" maxlength="12"/>
value="[{$aSetting.name}]" maxlength="12"/>
</td>
<td>
<select name="modulegenerator_settings[[{$i}]][type]">
<option value="bool">Checkbox</option>
<option value="str" selected="selected">String</option>
<option value="num">Number</option>
<option value="arr">Array</option>
<option value="aarr">Assoc Array</option>
<option value="select">Dropdown</option>
[{* todo (nice2have) get possible options as array from view *}]
<option value="bool"
[{if $sType eq 'bool'}]selected[{/if}]>Checkbox</option>
<option value="str"
[{if $sType eq 'str'}]selected[{/if}]>String</option>
<option value="num"
[{if $sType eq 'num'}]selected[{/if}]>Number</option>
<option value="arr"
[{if $sType eq 'arr'}]selected[{/if}]>Array</option>
<option value="aarr"
[{if $sType eq 'aarr'}]selected[{/if}]>Assoc Array</option>
<option value="select"
[{if $sType eq 'select'}]selected[{/if}]>Dropdown</option>
</select>
</td>
<td>
<textarea name="modulegenerator_settings[[{$i}]][value]" cols="10"
rows="1"></textarea>
rows="1">[{$aSetting.value}]</textarea>
</td>
</tr>
[{/section}]
Expand All @@ -147,7 +165,7 @@
<span class="req"> *</span>
</td>
<td class="edittext">
<input type="text" name="modulegenerator_init_version" value="1.0.0"
<input type="text" name="modulegenerator_init_version" value="[{$oValues->version}]"
maxlength="12"/>
</td>
</tr>
Expand All @@ -170,7 +188,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CHECKOUT_UNIT_TESTS"}]
</td>
<td class="edittext">
<input type="checkbox" name="modulegenerator_fetch_unit_tests" value="1"/>
<input type="checkbox" name="modulegenerator_fetch_unit_tests" value="1"
[{if $oValues->tests}]checked="checked"[{/if}]/>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CHECKOUT_UNIT_TESTS_HINT"}]
</td>
</tr>
Expand All @@ -182,7 +201,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_TASKS"}]
</td>
<td class="edittext">
<input type="checkbox" name="modulegenerator_render_tasks" value="1"/>
<input type="checkbox" name="modulegenerator_render_tasks" value="1"
[{if $oValues->tasks}]checked="checked"[{/if}]/>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_TASKS_HINT"}]
</td>
</tr>
Expand All @@ -191,7 +211,8 @@
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_SAMPLES"}]
</td>
<td class="edittext">
<input type="checkbox" name="modulegenerator_render_samples" value="1"/>
<input type="checkbox" name="modulegenerator_render_samples" value="1"
[{if $oValues->samples}]checked="checked"[{/if}]/>
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_SAMPLES_HINT"}]
</td>
</tr>
Expand Down

0 comments on commit 8061ee0

Please sign in to comment.