formHelper::getRadios PHP Method

getRadios() public method

array( 'name' => '', 'value' => '', 'id' => '', 'legend' => '', 'options' => array('value1' => 'text1', 'value2' => 'text2', 'value3' => 'text3'), 'options' => array('text1', 'text2', 'text3'), //also acceptable (cannot do half this, half above syntax) )
public getRadios ( array $args ) : str
$args array
return str
    public function getRadios(array $args)
    {
        $id = isset($args['id']) ? $args['id'] : $args['name'];
        $properties = $this->_getProperties($args);
        if (isset($properties['value'])) {
            $checked = $properties['value'];
            unset($properties['value']);
        }
        $properties['type'] = isset($args['type']) ? $args['type'] : 'radio';
        $useValues = key($args['options']) !== 0 || isset($args['useValue']) && $args['useValue'];
        foreach ($args['options'] as $value => $text) {
            if (!$useValues) {
                $value = $text;
            }
            $properties['id'] = $id . '_' . preg_replace('/\\W/', '', $value);
            $properties['value'] = $value;
            if (isset($checked) && (($properties['type'] == 'radio' || !is_array($checked)) && $value == $checked || $properties['type'] == 'checkbox' && is_array($checked) && in_array($value, $checked))) {
                $properties['checked'] = 'checked';
                $rowClass = !isset($properties['class']) ? 'checked' : $properties['class'] . ' checked';
            }
            $labelFirst = isset($args['labelFirst']) ? $args['labelFirst'] : false;
            $labelArgs = array('label' => $text, 'id' => $properties['id'], 'labelFirst' => $labelFirst);
            $input = '<input ' . htmlHelper::formatProperties($properties) . ' />';
            $row = $this->_getLabel($labelArgs, $input);
            if (isset($rowClass)) {
                $row = '<span class="' . $rowClass . '">' . $row . '</span>';
            }
            $radios[] = $row;
            unset($properties['checked'], $rowClass);
        }
        $this->{$properties['type'] == 'radio' ? 'radios' : 'checkboxes'} = $radios;
        $break = !isset($args['optionBreak']) ? '<br />' : $args['optionBreak'];
        $addFieldset = isset($args['addFieldset']) ? $args['addFieldset'] : isset($args['label']) && $args['label'] || count($args['options']) > 1;
        if ($addFieldset) {
            $return = '<fieldset id="' . $id . '">';
            if (isset($args['label'])) {
                $return .= '<legend>' . get::htmlentities($args['label']) . '</legend>';
            }
            $return .= implode($break, $radios) . '</fieldset>';
        } else {
            $return = implode($break, $radios);
        }
        if (isset($_POST['errors']) && isset($_POST['errors'][$id])) {
            $return = $this->getErrorMessageContainer($id, $_POST['errors'][$id]) . $return;
        }
        return $return;
    }

Usage Example

    $url = $baseUrl . '?' . http_build_query($_GET);
    if (isset($collection)) {
        $url .= '&collection=' . urlencode($collection);
    }
    echo $form->getFormOpen(array('action' => $url, 'style' => 'width: 80px; height: 20px;')) . $form->getInput(array('name' => 'limit', 'value' => $_SESSION['limit'], 'label' => '', 'addBreak' => false, 'style' => 'width: 40px;')) . $form->getInput(array('type' => 'submit', 'value' => 'limit', 'class' => 'ui-state-hover')) . $form->getFormClose();
    echo '</div>';
}
echo '</div>';
//end of dbcollnav
$dbcollnavJs = '$("#dbcollnav").after(\'<a id="dbcollnavlink" href="javascript: $(\\\'#dbcollnav\\\').show();' . ' $(\\\'#dbcollnavlink\\\').hide(); void(0);">[Show Database &amp; Collection selection]</a>\').hide();';
if (isset($mo->mongo['listRows'])) {
    echo $html->h1($collection);
    if (isset($mo->mongo['listIndexes'])) {
        echo '<ol id="indexes" style="display: none; margin-bottom: 10px;">';
        echo $form->getFormOpen(array('method' => 'get'));
        echo '<div id="indexInput">' . $form->getInput(array('name' => 'index[]', 'label' => '', 'addBreak' => false)) . $form->getCheckboxes(array('name' => 'isdescending[]', 'options' => array('Descending'))) . '</div>' . '<a id="addindexcolumn" style="margin-left: 160px;" href="javascript: ' . "\$('#addindexcolumn').before('<div>' + \$('#indexInput').html().replace(/isdescending_Descending/g, " . "'isdescending_Descending' + mo.indexCount++) + '</div>'); void(0);" . '">[Add another index field]</a>' . $form->getRadios(array('name' => 'unique', 'options' => array('Index', 'Unique'), 'value' => 'Index')) . $form->getInput(array('type' => 'submit', 'value' => 'Add new index', 'class' => 'ui-state-hover')) . $form->getInput(array('name' => 'action', 'type' => 'hidden', 'value' => 'ensureIndex')) . $form->getInput(array('name' => 'db', 'value' => get::htmlentities($db), 'type' => 'hidden')) . $form->getInput(array('name' => 'collection', 'value' => $collection, 'type' => 'hidden')) . $form->getFormClose();
        foreach ($mo->mongo['listIndexes'] as $indexArray) {
            $index = '';
            foreach ($indexArray['key'] as $key => $direction) {
                $index .= !$index ? $key : ', ' . $key;
                if (!is_object($direction)) {
                    $index .= ' [' . ($direction == -1 ? 'desc' : 'asc') . ']';
                }
            }
            if (isset($indexArray['unique']) && $indexArray['unique']) {
                $index .= ' [unique]';
            }
            if (key($indexArray['key']) != '_id' || count($indexArray['key']) !== 1) {
                $index = '[' . $html->link($baseUrl . '?db=' . $dbUrl . '&collection=' . urlencode($collection) . '&action=deleteIndex&index=' . serialize($indexArray['key']), 'X', array('title' => 'Drop Index', 'onclick' => "mo.confirm.href=this.href; " . "mo.confirm('Are you sure that you want to drop this index?', " . "function() {window.location.replace(mo.confirm.href);}); return false;")) . '] ' . $index;
            }
            echo '<li>' . $index . '</li>';