function bb_option_form_element($name = 'name', $args = null)
{
    global $bb_hardcoded;
    $defaults = array('title' => 'title', 'type' => 'text', 'value' => false, 'options' => false, 'message' => false, 'class' => false, 'default' => false, 'before' => '', 'after' => '', 'note' => false, 'attributes' => false, 'disabled' => false);
    $args = nxt_parse_args($args, $defaults);
    $id = str_replace(array('_', '[', ']'), array('-', '-', ''), $name);
    if (false !== strpos($name, '[')) {
        list($option_name, $option_key) = preg_split('/[\\[\\]]/', $name, -1, PREG_SPLIT_NO_EMPTY);
        $option = bb_get_option($option_name);
        $value = false === $args['value'] ? esc_attr($option[$option_key]) : esc_attr($args['value']);
        $hardcoded = isset($bb_hardcoded[$option_name][$option_key]);
    } else {
        $value = false === $args['value'] ? bb_get_form_option($name) : esc_attr($args['value']);
        $hardcoded = isset($bb_hardcoded[$name]);
    }
    $class = $args['class'] ? (array) $args['class'] : array();
    array_unshift($class, $args['type']);
    if ($hardcoded || $args['disabled']) {
        $disabled = ' disabled="disabled"';
    } else {
        $disabled = false;
    }
    if ($args['attributes']) {
        $attributes = array();
        foreach ($args['attributes'] as $k => $v) {
            $attributes[] = "{$k}='{$v}'";
        }
        $attributes = ' ' . join(' ', $attributes);
    } else {
        $attributes = '';
    }
    ?>

		<div id="option-<?php 
    echo $id;
    ?>
"<?php 
    if (!empty($disabled)) {
        echo ' class="disabled"';
    }
    ?>
>
<?php 
    switch ($args['type']) {
        case 'radio':
        case 'checkbox':
        case 'message':
            ?>
			<div class="label">
				<?php 
            echo $args['title'];
            ?>
			</div>

<?php 
            break;
        case 'select':
        default:
            ?>
			<label for="<?php 
            echo $id;
            ?>
">
				<?php 
            echo $args['title'];
            ?>
			</label>

<?php 
            break;
    }
    ?>
			<div class="inputs">

<?php 
    if ($args['before']) {
        echo '<span class="before">' . $args['before'] . '</span>';
    }
    switch ($args['type']) {
        case 'select':
            echo "<select{$disabled} class='" . join(' ', $class) . "' name='{$name}' id='{$id}'{$attributes}>\n";
            if (is_array($args['options'])) {
                foreach ($args['options'] as $option => $label) {
                    echo "\t<option value='{$option}'" . ($value == $option ? " selected='selected'" : '') . ">{$label}</option>\n";
                }
            } elseif (is_string($args['options'])) {
                echo $args['options'] . "\n";
            }
            echo "</select>\n";
            break;
        case 'radio':
        case 'checkbox':
            if (is_array($args['options'])) {
                $_id = 0;
                if ('radio' === $args['type'] && !in_array($value, array_keys($args['options'])) && empty($value)) {
                    $use_first_value = true;
                }
                $type = $args['type'];
                foreach ($args['options'] as $option => $label) {
                    if ($use_first_value) {
                        $use_first_value = false;
                        $value = $option;
                    }
                    if (is_array($label)) {
                        if (isset($label['attributes'])) {
                            $attributes = array();
                            foreach ($label['attributes'] as $k => $v) {
                                $attributes[] = "{$k}='{$v}'";
                            }
                            $attributes = ' ' . join(' ', $attributes);
                        } else {
                            $attributes = '';
                        }
                        if (isset($label['name'])) {
                            $name = $label['name'];
                            $id = str_replace(array('_', '[', ']'), array('-', '-', ''), $name);
                            $hardcoded = isset($bb_hardcoded[$name]);
                        }
                        if (isset($label['value'])) {
                            $_value = $label['value'];
                        } else {
                            $_value = $args['value'];
                        }
                        $value = false === $_value ? bb_get_form_option($name) : esc_attr($_value);
                        $label = $label['label'];
                    }
                    echo "<label class=\"{$type}s\"><input{$disabled} type='{$type}' class='" . join(' ', $class) . "' name='{$name}' id='{$id}-{$_id}' value='{$option}'" . ($value == $option ? " checked='checked'" : '') . "{$attributes} /> {$label}</label>\n";
                    $_id++;
                }
            } elseif (is_string($args['options'])) {
                echo $args['options'] . "\n";
            }
            break;
        case 'message':
            if ($args['message']) {
                echo $args['message'];
            }
            break;
        default:
            echo "<input{$disabled} type='{$args['type']}' class='" . join(' ', $class) . "' name='{$name}' id='{$id}' value='{$value}'{$attributes} />\n";
            break;
    }
    if ($args['after']) {
        echo '<span class="after">' . $args['after'] . '</span>';
    }
    if ($args['note']) {
        foreach ((array) $args['note'] as $note) {
            ?>

				<p><?php 
            echo $note;
            ?>
</p>

<?php 
        }
    }
    ?>

			</div>
		</div>

<?php 
}
Ejemplo n.º 2
0
function bb_form_option($option)
{
    echo bb_get_form_option($option);
}