Skip to content

Instantly share code, notes, and snippets.

@gajus
Created January 13, 2014 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gajus/8392582 to your computer and use it in GitHub Desktop.
Save gajus/8392582 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Gajus Kuizinas <g.kuizinas@anuary.com>
* @version 1.7.2 (2013 07 28)
* @todo Consider adding layout attribute [label][input][name][/label][comment]
*/
function input ($name, $label = null, array $input_options = null, array $row_options = []) {
if (!isset($row_options['order'])) {
$row_options['order'] = 'label-input';
}
if (empty($input_options['type'])) {
$input_options['type'] = isset($input_options['options']) ? 'select' : 'text';
}
// Input generated using \ay\input() is sent through $_POST['ay'] array.
$name = strpos($name, '[') === false ? 'ay[' . $name . ']' : 'ay[' . strstr($name, '[', true) . ']' . strstr($name, '[');
$original_name_path = explode('][', mb_substr($name, 3, -1));
$input_options['name'] = $name;
if ($input_options['type'] != 'password') {
$original_name_path = array_filter($original_name_path); // ay[name][]
$find_value = function ($source) use ($original_name_path) {
foreach ($original_name_path as $key) {
if (!is_array($source) || !array_key_exists($key, $source)) {
return false;
}
$source = $source[$key];
}
return $source;
};
$value = false;
if (isset($_SESSION['ay']['flash']['input'])) {
$value = $find_value($_SESSION['ay']['flash']['input']);
}
if ($value === false) {
if (isset($input_options['value'])) {
$value = $input_options['value'];
} else if (isset($GLOBALS['ay']['input'])) {
$value = $find_value($GLOBALS['ay']['input']);
} else if (isset($_GET['ay'])) {
$value = $find_value($_GET['ay']);
}
}
}
$input_attr_str = '';
if ($input_options['type'] === 'text' && !isset($input_options['maxlength'])) {
$input_options['maxlength'] = 50;
}
foreach ($input_options as $k => $v) {
if ($k === 'options') {
continue;
}
$input_attr_str .= ' ' . $k . '="' . $v . '"';
}
$str = [
'append' => '',
'class' => 'ay-input-' . implode('-', $original_name_path) . '-input ay-input-order-' . $row_options['order']
];
// generate input string
switch ($input_options['type']) {
case 'select':
if (!array_key_exists('options', $input_options)) {
throw new \ay\flow\Helper_Exception('Select input is missing options array.');
}
$option_str = '';
foreach ($input_options['options'] as $v => $l) {
$selected = '';
if ($value && (is_array($value) && in_array($v, $value) || $value == $v)) {
$selected = ' selected="selected"';
}
$option_str .= '<option value="' . $v . '"' . $selected . '>' . $l . '</option>';
}
$str['input'] = '<select ' . $input_attr_str . '>' . $option_str . '</select>';
break;
case 'checkbox':
if (!array_key_exists('value', $input_options)) {
$input_options['value'] = 1;
} else {
$input_options['value'] = $input_options['value'];
}
$str['input'] = '<input type="checkbox" value="' . $input_options['value'] . '"' . $input_attr_str . '' . ($input_options['value'] == $value ? ' checked="checked"' : '') . ' />';
break;
case 'radio':
if (!array_key_exists('value', $input_options)) {
throw new \ay\flow\Helper_Exception('Radio input is missing value parameter.');
}
$input_options['value'] = (int) $input_options['value'];
$str['input'] = '<input type="radio" value="' . $input_options['value'] . '"' . $input_attr_str . '' . ($input_options['value'] == $value ? ' checked="checked"' : '') . ' />';
break;
case 'textarea':
$str['input'] = '<textarea' . $input_attr_str . '>' . filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS) . '</textarea>';
break;
case 'password':
$str['input'] = '<input type="password" ' . $input_attr_str . ' />';
break;
default:
$str['input'] = '<input type="' . $input_options['type'] . '" value="' . filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS) . '"' . $input_attr_str . ' />';
break;
}
if ($label === null) {
return $str['input'];
}
if (!empty($row_options['comment'])) {
$str['append'] .= '<div class="ay-input-comment">' . $row_options['comment'] . '</div>';
}
if (!empty($row_options['class'])) {
$str['class'] .= ' ' . $row_options['class'];
}
if ($label === null) {
$str['body'] = $str['input'];
} else {
$input_label = $row_options['order'] === 'input-label' ? $str['input'] . '<span class="ay-input-label">' . $label . '</span>' : '<span class="ay-input-label">' . $label . '</span>' . $str['input'];
$str['body'] = '<label class="ay-input-row ' . $str['class'] . ' ay-input-' . $input_options['type'] . '">' . $input_label . '</label>';
}
return $str['body'] . ' ' . $str['append'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment