Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 8061ee0

Browse files
committed
Made form values remain filled on error after wizard submission.
1 parent c11f508 commit 8061ee0

File tree

3 files changed

+235
-19
lines changed

3 files changed

+235
-19
lines changed

copy_this/modules/oxps/modulegenerator/controllers/admin/admin_oxpsmodulegenerator.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Class Admin_oxpsModuleGenerator.
2929
* Module Generator GUI controller.
30+
*
31+
* @todo (nice2have) Class got too long -> move some methods to validation helper or some other class.
3032
*/
3133
class Admin_oxpsModuleGenerator extends oxAdminView
3234
{
@@ -79,6 +81,9 @@ public function render()
7981
$this->_setMessage('OXPS_MODULEGENERATOR_ADMIN_MODULE_ERROR_NO_VENDOR');
8082
}
8183

84+
// Add clean module generation options and form values to view data
85+
$this->_addViewData(array('oValues' => (object) $this->_getFormValues()));
86+
8287
// Parent render call
8388
return $this->_Admin_oxpsModuleGenerator_render_parent();
8489
}
@@ -209,6 +214,109 @@ protected function _getGenerationOptions()
209214
return $this->_filterListModels($aGenerationOptions);
210215
}
211216

217+
/**
218+
* Get initial form values for a case when the form was submitted.
219+
*
220+
* @return array
221+
*/
222+
protected function _getFormValues()
223+
{
224+
/** @var oxpsModuleGeneratorValidator $oValidator */
225+
$oValidator = oxRegistry::get('oxpsModuleGeneratorValidator');
226+
227+
$aOptions = (array) $this->_getGenerationOptions();
228+
229+
return array(
230+
'name' => $this->_getTextParam('modulegenerator_module_name'),
231+
'extend' => $this->_toString(array_keys($oValidator->getArrayValue($aOptions, 'aExtendClasses', 'array'))),
232+
'controllers' => $this->_toString($oValidator->getArrayValue($aOptions, 'aNewControllers', 'array')),
233+
'models' => $this->_toString($oValidator->getArrayValue($aOptions, 'aNewModels', 'array')),
234+
'lists' => $this->_getListModelsFieldValue($oValidator->getArrayValue($aOptions, 'aNewLists', 'array')),
235+
'widgets' => $this->_toString($oValidator->getArrayValue($aOptions, 'aNewWidgets', 'array')),
236+
'blocks' => $this->_getBlocksFieldValue($oValidator->getArrayValue($aOptions, 'aNewBlocks', 'array')),
237+
'settings' => $oValidator->getArrayValue($aOptions, 'aModuleSettings', 'array'),
238+
'version' => $this->_getFormVersionFieldValue($oValidator->getArrayValue($aOptions, 'sInitialVersion')),
239+
'tests' => $oValidator->getArrayValue($aOptions, 'blFetchUnitTests', 'boolean'),
240+
'tasks' => $oValidator->getArrayValue($aOptions, 'blRenderTasks', 'boolean'),
241+
'samples' => $oValidator->getArrayValue($aOptions, 'blRenderSamples', 'boolean'),
242+
);
243+
}
244+
245+
/**
246+
* Convert array to a multi-line string.
247+
*
248+
* @param array $aData
249+
*
250+
* @return string
251+
*/
252+
protected function _toString(array $aData)
253+
{
254+
return trim((string) implode(PHP_EOL, $aData));
255+
}
256+
257+
/**
258+
* Get initial value for list models field.
259+
* Removes "List" suffixes from names.
260+
*
261+
* @param array $aRequestListModels
262+
*
263+
* @return string
264+
*/
265+
protected function _getListModelsFieldValue(array $aRequestListModels)
266+
{
267+
/** @var oxStrMb|oxStrRegular $oStr */
268+
$oStr = oxStr::getStr();
269+
270+
$aLists = array();
271+
272+
foreach ($aRequestListModels as $sListClassName) {
273+
$aLists[] = $oStr->substr($sListClassName, 0, ($oStr->strlen($sListClassName) - 4));
274+
}
275+
276+
return $this->_toString($aLists);
277+
}
278+
279+
/**
280+
* Get initial value for module blocks field.
281+
*
282+
* @param array $aRequestBlocks
283+
*
284+
* @return string
285+
*/
286+
protected function _getBlocksFieldValue(array $aRequestBlocks)
287+
{
288+
/** @var oxpsModuleGeneratorValidator $oValidator */
289+
$oValidator = oxRegistry::get('oxpsModuleGeneratorValidator');
290+
291+
$aBlocks = array();
292+
293+
foreach ($aRequestBlocks as $oBlock) {
294+
$aBlock = (array) $oBlock;
295+
$aBlocks[] = $oValidator->getArrayValue($aBlock, 'block') . '@' .
296+
$oValidator->getArrayValue($aBlock, 'template');
297+
}
298+
299+
return $this->_toString($aBlocks);
300+
}
301+
302+
/**
303+
* Get initial value for module version field.
304+
*
305+
* @param string $sRequestVersion
306+
*
307+
* @return string
308+
*/
309+
protected function _getFormVersionFieldValue($sRequestVersion)
310+
{
311+
$sModuleVersion = trim((string) $sRequestVersion);
312+
313+
if (empty($sModuleVersion)) {
314+
$sModuleVersion = '1.0.0';
315+
}
316+
317+
return $sModuleVersion;
318+
}
319+
212320
/**
213321
* Get request parameter as trimmed string.
214322
*

copy_this/modules/oxps/modulegenerator/tests/unit/modules/controllers/admin/Admin_oxpsModuleGeneratorTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,93 @@ public function testRender_noVendorDataConfigured_setError()
166166
$this->assertTrue($aViewData['blError']);
167167
}
168168

169+
public function testRender_formWasSubmitted_collectProperFormInitialValuesFromParsedRequest()
170+
{
171+
// Config mock (request data)
172+
modConfig::setRequestParameter('modulegenerator_module_name', 'badModuleName ');
173+
modConfig::setRequestParameter(
174+
'modulegenerator_extend_classes',
175+
'oxarticle' . PHP_EOL . 'oxarticle' . PHP_EOL . 'oxlist' . PHP_EOL . 'oxarticle' . PHP_EOL . 'asdasd'
176+
);
177+
modConfig::setRequestParameter(
178+
'modulegenerator_controllers',
179+
' Page' . PHP_EOL . PHP_EOL . ' ' . PHP_EOL . 'view'
180+
);
181+
modConfig::setRequestParameter('modulegenerator_models', 'Item' . PHP_EOL . 'Thing_Two');
182+
modConfig::setRequestParameter('modulegenerator_lists', 'Item' . PHP_EOL . 'ThingTwo' . PHP_EOL . 'ItemList');
183+
modConfig::setRequestParameter('modulegenerator_widgets', 'Bar' . PHP_EOL . '1Trash');
184+
modConfig::setRequestParameter(
185+
'modulegenerator_blocks',
186+
'block@' . PHP_EOL . '@page.tpl' . PHP_EOL . 'block@page.tpl'
187+
);
188+
modConfig::setRequestParameter(
189+
'modulegenerator_settings',
190+
array(
191+
array(
192+
'name' => 'MyString',
193+
'type' => 'str',
194+
'value' => 'Hello, word!',
195+
),
196+
array(
197+
'name' => '',
198+
'type' => 'str',
199+
'value' => '',
200+
),
201+
array(
202+
'name' => 'MyNumber',
203+
'type' => 'num',
204+
'value' => '888.8',
205+
)
206+
)
207+
);
208+
modConfig::setRequestParameter('modulegenerator_init_version', '0.0.1 beta');
209+
modConfig::setRequestParameter('modulegenerator_render_tasks', '1');
210+
modConfig::setRequestParameter('modulegenerator_render_samples', '1');
211+
212+
$this->SUT->expects($this->once())->method('_Admin_oxpsModuleGenerator_init_parent');
213+
$this->SUT->expects($this->once())->method('_Admin_oxpsModuleGenerator_render_parent')->will(
214+
$this->returnValue('admin_oxpsmodulegenerator.tpl')
215+
);
216+
$this->SUT->init();
217+
$this->SUT->render();
218+
219+
$aViewData = $this->SUT->getViewData();
220+
$this->assertArrayHasKey('oValues', $aViewData);
221+
$this->assertEquals(
222+
(object) array(
223+
'name' => 'badModuleName',
224+
'extend' => 'oxarticle' . PHP_EOL . 'oxlist',
225+
'controllers' => 'Page',
226+
'models' => 'Item',
227+
'lists' => 'Item',
228+
'widgets' => 'Bar',
229+
'blocks' => 'block@page.tpl',
230+
'settings' => array(
231+
array(
232+
'name' => 'MyString',
233+
'type' => 'str',
234+
'value' => 'Hello, word!'
235+
),
236+
array(
237+
'name' => '',
238+
'type' => 'str',
239+
'value' => ''
240+
),
241+
array(
242+
'name' => 'MyNumber',
243+
'type' => 'num',
244+
'value' => '888.8'
245+
),
246+
),
247+
'version' => '0.0.1 beta',
248+
'tests' => false,
249+
'tasks' => true,
250+
'samples' => true,
251+
),
252+
$aViewData['oValues']
253+
);
254+
}
255+
169256

170257
public function testGetModule()
171258
{

copy_this/modules/oxps/modulegenerator/views/admin/admin_oxpsmodulegenerator.tpl

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
<span class="req"> *</span>
3030
</td>
3131
<td class="edittext">
32-
<input type="text" name="modulegenerator_module_name" value="" maxlength="20"/>
32+
<input type="text" name="modulegenerator_module_name" value="[{$oValues->name}]"
33+
maxlength="20"/>
3334
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_MODULE_NAME_HINT"}]
3435
</td>
3536
</tr>
@@ -41,7 +42,8 @@
4142
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_OVERRIDE_CLASSES"}]
4243
</td>
4344
<td class="edittext">
44-
<textarea name="modulegenerator_extend_classes" cols="20" rows="3"></textarea>
45+
<textarea name="modulegenerator_extend_classes" cols="20"
46+
rows="3">[{$oValues->extend}]</textarea>
4547
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_OVERRIDE_CLASSES_HINT"}]
4648
</td>
4749
</tr>
@@ -50,7 +52,8 @@
5052
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_CONTROLLERS"}]
5153
</td>
5254
<td class="edittext">
53-
<textarea name="modulegenerator_controllers" cols="20" rows="1"></textarea>
55+
<textarea name="modulegenerator_controllers" cols="20"
56+
rows="1">[{$oValues->controllers}]</textarea>
5457
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_CONTROLLERS_HINT"}]
5558
</td>
5659
</tr>
@@ -59,7 +62,8 @@
5962
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_MODELS"}]
6063
</td>
6164
<td class="edittext">
62-
<textarea name="modulegenerator_models" cols="20" rows="1"></textarea>
65+
<textarea name="modulegenerator_models" cols="20"
66+
rows="1">[{$oValues->models}]</textarea>
6367
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_MODELS_HINT"}]
6468
</td>
6569
</tr>
@@ -68,7 +72,8 @@
6872
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_LISTS"}]
6973
</td>
7074
<td class="edittext">
71-
<textarea name="modulegenerator_lists" cols="20" rows="1"></textarea>
75+
<textarea name="modulegenerator_lists" cols="20"
76+
rows="1">[{$oValues->lists}]</textarea>
7277
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_LISTS_HINT"}]
7378
</td>
7479
</tr>
@@ -77,7 +82,8 @@
7782
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_WIDGETS"}]
7883
</td>
7984
<td class="edittext">
80-
<textarea name="modulegenerator_widgets" cols="20" rows="1"></textarea>
85+
<textarea name="modulegenerator_widgets" cols="20"
86+
rows="1">[{$oValues->widgets}]</textarea>
8187
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_WIDGETS_HINT"}]
8288
</td>
8389
</tr>
@@ -87,7 +93,7 @@
8793
</td>
8894
<td class="edittext">
8995
<textarea class="wider" name="modulegenerator_blocks" cols="20"
90-
rows="2"></textarea>
96+
rows="2">[{$oValues->blocks}]</textarea>
9197
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CREATE_BLOCKS_HINT"}]
9298
</td>
9399
</tr>
@@ -113,24 +119,36 @@
113119
<tbody>
114120
[{section name=settings start=0 loop=3}]
115121
[{assign var='i' value=$smarty.section.settings.index}]
122+
[{assign var='aSetting' value=$oValues->settings.$i}]
123+
[{assign var='sType' value=$aSetting.type}]
124+
[{if not $sType}]
125+
[{assign var='sType' value='str'}]
126+
[{/if}]
116127
<tr>
117128
<td>
118129
<input type="text" name="modulegenerator_settings[[{$i}]][name]"
119-
value="" maxlength="12"/>
130+
value="[{$aSetting.name}]" maxlength="12"/>
120131
</td>
121132
<td>
122133
<select name="modulegenerator_settings[[{$i}]][type]">
123-
<option value="bool">Checkbox</option>
124-
<option value="str" selected="selected">String</option>
125-
<option value="num">Number</option>
126-
<option value="arr">Array</option>
127-
<option value="aarr">Assoc Array</option>
128-
<option value="select">Dropdown</option>
134+
[{* todo (nice2have) get possible options as array from view *}]
135+
<option value="bool"
136+
[{if $sType eq 'bool'}]selected[{/if}]>Checkbox</option>
137+
<option value="str"
138+
[{if $sType eq 'str'}]selected[{/if}]>String</option>
139+
<option value="num"
140+
[{if $sType eq 'num'}]selected[{/if}]>Number</option>
141+
<option value="arr"
142+
[{if $sType eq 'arr'}]selected[{/if}]>Array</option>
143+
<option value="aarr"
144+
[{if $sType eq 'aarr'}]selected[{/if}]>Assoc Array</option>
145+
<option value="select"
146+
[{if $sType eq 'select'}]selected[{/if}]>Dropdown</option>
129147
</select>
130148
</td>
131149
<td>
132150
<textarea name="modulegenerator_settings[[{$i}]][value]" cols="10"
133-
rows="1"></textarea>
151+
rows="1">[{$aSetting.value}]</textarea>
134152
</td>
135153
</tr>
136154
[{/section}]
@@ -147,7 +165,7 @@
147165
<span class="req"> *</span>
148166
</td>
149167
<td class="edittext">
150-
<input type="text" name="modulegenerator_init_version" value="1.0.0"
168+
<input type="text" name="modulegenerator_init_version" value="[{$oValues->version}]"
151169
maxlength="12"/>
152170
</td>
153171
</tr>
@@ -170,7 +188,8 @@
170188
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_CHECKOUT_UNIT_TESTS"}]
171189
</td>
172190
<td class="edittext">
173-
<input type="checkbox" name="modulegenerator_fetch_unit_tests" value="1"/>
191+
<input type="checkbox" name="modulegenerator_fetch_unit_tests" value="1"
192+
[{if $oValues->tests}]checked="checked"[{/if}]/>
174193
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_CHECKOUT_UNIT_TESTS_HINT"}]
175194
</td>
176195
</tr>
@@ -182,7 +201,8 @@
182201
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_TASKS"}]
183202
</td>
184203
<td class="edittext">
185-
<input type="checkbox" name="modulegenerator_render_tasks" value="1"/>
204+
<input type="checkbox" name="modulegenerator_render_tasks" value="1"
205+
[{if $oValues->tasks}]checked="checked"[{/if}]/>
186206
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_TASKS_HINT"}]
187207
</td>
188208
</tr>
@@ -191,7 +211,8 @@
191211
[{oxmultilang ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_SAMPLES"}]
192212
</td>
193213
<td class="edittext">
194-
<input type="checkbox" name="modulegenerator_render_samples" value="1"/>
214+
<input type="checkbox" name="modulegenerator_render_samples" value="1"
215+
[{if $oValues->samples}]checked="checked"[{/if}]/>
195216
[{oxinputhelp ident="OXPS_MODULEGENERATOR_ADMIN_RENDER_SAMPLES_HINT"}]
196217
</td>
197218
</tr>

0 commit comments

Comments
 (0)