Created
February 20, 2017 18:48
-
-
Save jpitoniak/79d8ae3f6654ba14684cd85dcb43c9dd to your computer and use it in GitHub Desktop.
Smarty plugin for generating form fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* formfield plugin | |
* Create dynamic HTML form elements programmatically | |
* | |
* Parameters: | |
* type - the type of element, can be any standard HTML <input> type or one of the options below (default = text) | |
* options - for select, checkboxes, and radios types, this should be an associative array of key => label pairs | |
* value - the default value of the element; for select or checkboxes types this can be an array selected/checked values, if an array is set for other types, only the first value in the array is used | |
* wrapper - the HTML wrapper to be applied to individual elements when using checkboxes or radios, use %s to specify where default makeup should be inserted | |
* content - the content between <button> and </button> tags in an htmlbutton field | |
* emptyoption - a valueless label for a select list to serve as a default selection | |
* all other parameters will be set as attributes to the elements | |
* | |
* Element Types: | |
* | |
* checkboxes - creates a set of checkboxes from an array of options (expects options param, value param may be an array or single value) | |
* radios - creates a set of radio buttons from an array of options (expects options param, value param should be a single value) | |
* <select> - creates a select box from an array of options (expects options param, value param may be an array or a single value) | |
* <textarea> - creates a textarea (value param will be inserted as the inner content to the <textarea> and </textarea> tags) | |
* <button> - use type="htmlbutton" to create a <button> element (content param will be inserted as inner content between the <button> and </button> tags) | |
* <input> - any other type param will be converted to a standard <input> element with the specified type and all other values added as attributes | |
*/ | |
function smarty_function_formfield($params, Smarty_Internal_Template $template) | |
{ | |
//extract the control variables from the parameters | |
$type = _smarty_function_formfield_extractParam($params, 'type', 'text'); | |
$options = _smarty_function_formfield_extractParam($params, 'options', array()); | |
$value = _smarty_function_formfield_extractParam($params, 'value', ''); | |
$wrapper = _smarty_function_formfield_extractParam($params, 'wrapper'); | |
$content = _smarty_function_formfield_extractParam($params, 'content'); | |
//normalize the disabled attribute | |
if(isset($params['disabled'])) { | |
if($params['disabled'] == false) { | |
unset($params['disabled']); | |
} | |
else { | |
$params['disabled'] = 'disabled'; | |
} | |
} | |
//a string to hold the output | |
$output = ''; | |
if($type == 'checkboxes') { | |
//a set of checkboxes | |
//make sure value is an array | |
$value = (is_array($value)) ? $value : array($value); | |
//name needs square brackets for PHP to parse it correctly | |
$name = _smarty_function_formfield_extractParam($params, 'name', ''); | |
$name = (!empty($name)) ? 'name="' . $name . '[]"' : ''; | |
//get the id, a number will be appended so that each element has a unique id | |
$id = _smarty_function_formfield_extractParam($params,'id'); | |
//store each element in an output array | |
$output = array(); | |
$count = 0; | |
foreach($options as $key => $label) { | |
$chk = (in_array($key, $value)) ? 'checked="checked"' : ''; | |
$myid = (!empty($id)) ? 'id="' . $id . '-' . $count . '"': ''; | |
$html = sprintf('<label><input type="checkbox" %s %s %s value="%s" %s />%s</label>', | |
$name, //field name | |
$myid, //unique id | |
_smarty_function_formfield_getAttribs($params), //parsed attributes | |
$key, //value | |
$chk, //checked status | |
htmlspecialchars($label, ENT_QUOTES) //label | |
); | |
if(!empty($wrapper)) { | |
//wrap the HTML in the wrapper, if specified | |
$html = sprintf($wrapper, $html); | |
} | |
//add element to the output array | |
$output[] = $html; | |
$count++; | |
} | |
//collapse output array into a string | |
$output = implode("\n", $output); | |
} | |
else if($type == 'radios') { | |
//a set of radio buttons | |
//value should not be an array | |
$value = (is_array($value)) ? array_shift($value) : $value; | |
//get the id; a sequential number will be appended on each element so that each has a uniqu id | |
$id = _smarty_function_formfield_extractParam($params, 'id'); | |
//store each element in an output array | |
$output = array(); | |
$count = 0; | |
foreach($options as $key => $label) { | |
//empty() evals to true for empty string, null, and zero | |
//isset() evals to true for empty string | |
//strlen() evals to > 0 for anything other than empty string, null | |
$chk = (strlen($value) > 0 && $key == $value) ? 'checked="checked"' : ''; | |
//$chk = ($key == $value) ? 'checked="checked"' : ''; | |
$myid = (!empty($id)) ? 'id="' . $id . '-' . $count . '"': ''; | |
$html = sprintf('<label><input type="radio" %s %s value="%s" %s />%s</label>', | |
$myid, //unique id | |
_smarty_function_formfield_getAttribs($params), //parsed attributes | |
$key, //value | |
$chk, //checked status | |
htmlspecialchars($label, ENT_QUOTES) //label | |
); | |
if(!empty($wrapper)) { | |
//wrap the HTML in the wrapper, if specified | |
$html = sprintf($wrapper, $html); | |
} | |
//add element to the output array | |
$output[] = $html; | |
$count++; | |
} | |
//collapse output array into a string | |
$output = implode("\n", $output); | |
} | |
else if($type == 'select') { | |
//select box | |
//make sure value is an array | |
$value = (is_array($value)) ? $value : array($value); | |
unset($params['value']); | |
//standardize multiple | |
$multiple = _smarty_function_formfield_extractParam($params, 'multiple'); | |
$multiple = (!empty($multiple)) ? 'multiple="multiple"' : ''; | |
//opening tag | |
$output = sprintf('<select %s %s>', | |
_smarty_function_formfield_getAttribs($params), //parsed attributes | |
$multiple //multiple option | |
) . "\n"; | |
//empty option | |
$emptyoption = (isset($params['emptyoption'])) ? $params['emptyoption'] : null; | |
unset($params['emptyoption']); | |
if($emptyoption) { | |
$output .= sprintf(' <option value="">%s</option>', htmlspecialchars($emptyoption, ENT_QUOTES)) . "\n"; | |
} | |
//actual options | |
foreach($options as $key => $label) { | |
$sel = (in_array($key, $value)) ? 'selected="selected"' : ''; | |
$output .= sprintf(' <option value="%s" %s>%s</option>', | |
$key, //option value | |
$sel, //selection status | |
htmlspecialchars($label, ENT_QUOTES) //label | |
) . "\n"; | |
} | |
//closing tag | |
$output .= '</select>'; | |
} | |
else if($type == 'textarea') { | |
//textarea | |
//value should not be an array | |
$value = (is_array($value)) ? array_shift($value) : $value; | |
$output = sprintf('<textarea %s>%s</textarea>', | |
_smarty_function_formfield_getAttribs($params), //parsed attributes | |
htmlspecialchars($value, ENT_QUOTES) //value | |
); | |
} | |
else if($type == 'htmlbutton') { | |
//value should not be an array | |
$value = (is_array($value)) ? array_shift($value) : $value; | |
$value = (!empty($value)) ? 'value="' . htmlspecialchars($values, ENT_QUOTES) . '"' : ''; | |
$output = sprintf('<button %s %s>%s</button>', | |
_smarty_function_formfield_getAttribs($params), //parsed attributes | |
$value, //element value | |
htmlspecialchars($content, ENT_QUOTES) //the tags inner content | |
); | |
} | |
else { | |
//standard <input> element | |
//works with all other elements including HTML5 form elements as well as single checkboxes and radio buttons | |
//value should not be an array | |
$value = (is_array($value)) ? array_shift($value) : $value; | |
$value = ($value != '') ? 'value="' . htmlspecialchars($value, ENT_QUOTES) . '"' : ''; | |
$output = sprintf('<input type="%s" %s %s />', | |
$type, //field type | |
_smarty_function_formfield_getAttribs($params), //parsed attributes | |
$value //field value | |
); | |
} | |
return $output; | |
} | |
/* | |
* find a value in an associative array by key, remove the element from the array, and return the value | |
* optionally, a default value can be specified if the requested key does not exist, otherwise null will be returned | |
*/ | |
function _smarty_function_formfield_extractParam(&$params, $paramName, $default=null) | |
{ | |
$paramVal = $default; | |
if(isset($params[$paramName])) { | |
$paramVal = $params[$paramName]; | |
} | |
unset($params[$paramName]); | |
return $paramVal; | |
} | |
/* | |
* convert an associative array into a set of HTML attributes | |
*/ | |
function _smarty_function_formfield_getAttribs($params) | |
{ | |
//output array | |
$output = array(); | |
foreach($params as $key => $value) { | |
$output[] = sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES)); | |
} | |
//return output as a string | |
return implode(' ', $output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment