Difference between #2 and #1 of Radio Button List with enum values for column of type ENUM >+> incorporate into giix

unchanged
Title
Radio Button List with enum values for column of type ENUM >+> incorporate into giix
unchanged
Category
How-tos
unchanged
Tags
Radiobuttonlist, input, forms, views, enum, giix, activeRadioButtonList
changed
Content
Let's say our table 'mug' has a column named 'color' of the type
**ENUM(**'red','green','blue'**)**.
<br>We want to replace the textfield for the attribute color in the create
and update forms of a 'mug' with a **Radio Button List**, which has the enum
values as options.
<br>This is a rewrite of [c@cba](http://www.yiiframework.com/user/54420/)
in the wiki article of
[enumDropdownList](http://www.yiiframework.com/wiki/303/drop-down-list-with-enum-values-for-column-of-type-enum-incorporate-into-giix/
"enumDropdownList")<br>The main code was contributed by
[zaccaria ](http://www.yiiframework.com/user/2584/ "zaccaria")in the
forum (see [this
post](http://www.yiiframework.com/forum/index.php?/topic/10079-enum-db-type-in-yii/
"")).

I merely repeat that useful peace of code here, so it is available in the wikis,
and describe how you can incorporate it into giix, so the Radio button  list is
added automatically during code generation.

##The enumRadioButtonList activeRadioButtonList

Create the file `..\components\RBLHtml.php` with the following code:
~~~
[php]
class RBLHtml extends CHtml
{
	public static function enumRadioButtonList($model, $attribute,
$htmlOptions=array())
	{
	  return CHtml::activeRadioButtonList( $model, $attribute,
self::enumItem($model,  $attribute), $htmlOptions);
	}
	
	public static function enumItem($model,$attribute) {
		$attr=$attribute;
		self::resolveName($model,$attr);
		preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);
		foreach(explode(',', $matches[1]) as $value) {
				$value=str_replace("'",null,$value);
				$values[$value]=Yii::t('enumItem',$value);
		}
		return $values;
	} 
}
~~~
Then use it in the view files with:
~~~
[php]
<?php echo
ZHtml::enumRadioButtonList(RBLHtml::enumRadioButtonList(
$model,'color' ); ?>
~~~


## Adding it to giix' CrudCode

If you use [giix](http://www.yiiframework.com/extension/giix/ "") for
code generation, you can put the code above into the file
`..\extensions\giix-components\GxHtml.php` (only the function definitions, not
the 'class' part).

Then add the following code into the file
`..\extensions\giix-components\GxActiveForm.php`:
~~~
[php]
	public function enumRadioButtonList($model, $attribute, $htmlOptions =
array()){
		return GxHtml::enumRadioButtonList($model, $attribute, $htmlOptions);
	}
~~~

Now you can use it in your view files with:
~~~
[php]
<?php echo $form->enumRadioButtonList($model, 'color')); ?>
~~~

To edit giix so it adds this line into the view files during code generation: 
in the file `..\extensions\giix-core\giixCrud\GiixCrudCode.php` find the
function `generateActiveField`. Add the condition for the enum type (enclosed
between `/*NEW_BEGIN*` and `/*NEW_END*/`) into the code, so it looks like:
~~~
[php]
...
if (strtoupper($column->dbType) == 'TINYINT(1)'
		|| strtoupper($column->dbType) == 'BIT'
		|| strtoupper($column->dbType) == 'BOOL'
		|| strtoupper($column->dbType) == 'BOOLEAN') {
	return "echo \$form->checkBox(\$model, '{$column->name}')";
} /*NEW-BEGIN*/
else if (stripos($column->dbType, 'enum') !== false) {
	return "echo \$form->enumRadioButtonList(\$model,
'{$column->name}')";
} /*NEW_END*/
else if (strtoupper($column->dbType) == 'DATE') {
...
~~~

That's it. If something is unclear/wrong/incomplete or you have other
suggestions please let me know.