/** * Encapsulates {@link CJuiButton} and {@link CHtml::radioButtonList} for an easy way to create input for flag fields. * There are two uses for this method: static and active fields. * Static fields make use of CHtml methods to create the radio buttons, and you'll only need the three first arguments. * For use with {@link CActiveForm} you can skip the $value argument and supply $form and $model too, or use * {@link activeJuiFlag}, that's a shorthand method to simplify this use. * * @param string $name the field name * @param array $options a list of options, where the keys are the field values and * the values are: a string with the label or an array where the first element * is the label and the second is the icon class. For adding icons, you need to * include CSS rules that add an image as background to that class, like: * <code>.ui-icon-custom-yes { background-image: url(/images/icons/tick.png); }</code> * @param mixed $value [optional] The current value for the field. * @param CActiveForm [optional] $form the form widget being used * @param CModel [optional] $model the model */ public function juiFlag($name, array $options, $value = null, CActiveForm $form = null, CModel $model = null) { $radio_options = $icons = array(); $button_number = 0; foreach ($options as $value => $data) { $radio_options[$value] = ($is_array = is_array($data)) ? $data[0] : $data; if ($is_array && isset($data[1])) { $icons[$button_number] = $data[1]; } ++$button_number; } $this->owner->beginWidget('zii.widgets.jui.CJuiButton', array('buttonType' => 'buttonset', 'name' => $name)); if ($form) { echo $form->radioButtonList($model, $name, $radio_options, array('separator' => '')); $radio_id_prefix = get_class($model) . '_' . $name; } else { echo CHtml::radioButtonList($name, $value, $radio_options, array('separator' => '')); $radio_id_prefix = $name; } $this->owner->endWidget(); $js = function () use($icons, $radio_id_prefix) { $js = ''; foreach ($icons as $i => $icon) { $js .= "\$('#{$radio_id_prefix}_{$i}').button('option', 'icons', {primary: '{$icon}'})\n"; } return $js; }; Yii::app()->clientScript->registerScript("{$radio_id_prefix}_juiFlagIcons", $js()); }
/** * Explodes radioButtonList into array * enabling to render buttons separately ($radio[0], $radio[1]...) * @param CActiveForm $form the form widgets * @param CModel $model the data model * @param string $attribute the attribute * @param array $data value-label pairs used to generate the radio button list. * @return array of radio buttons */ public static function explodeRadioButtonList($form, $model, $attribute, $data) { return explode('|', $form->radioButtonList($model, $attribute, $data, array('template' => '{input}{label}', 'separator' => '|'))); }
/** * @inheritDoc */ public function radioButtonList($model, $attribute, $data, $htmlOptions = array()) { if (!$this->qualifyNames && !isset($htmlOptions['name'])) { $htmlOptions['name'] = $attribute; } if (!isset($htmlOptions['itemprop'])) { $htmlOptions['itemprop'] = $this->getItemPropName($attribute); } return parent::radioButtonList($model, $attribute, $data, $htmlOptions); }
/** * Renders a radio button list for a model attribute. * @param CModel $parentModel the parent data model * @param string $attributedPath the attribute or path to related model attribute * @param array $data value-label pairs used to generate the radio button list. * @param array $htmlOptions additional HTML options. * @return string the generated radio button list */ public function radioButtonList($parentModel, $attributedPath, $data, $htmlOptions = array()) { list($model, $attribute, $htmlOptions) = self::resolveArgs($parentModel, $attributedPath, $htmlOptions); return parent::radioButtonList($model, $attribute, $data, $htmlOptions); }