/mvc/helpers/form
[return to app]1
<?php
/**
* Form-related helper tools
*/
class formHelper {
/**
* Internal flag to keep track if a form tag has been opened and not yet closed
* @var boolean
*/
private $_formOpen = false;
/**
* Internal form element counter
* @var int
*/
private $_inputCounter = array();
/**
* Default value for addBreak
* @var Boolean Defaults to true
*/
public $addBreak = true;
/**
* Converts dynamically-assigned array indecies to use an explicitely defined index
*
* @param string $name
* @return string
*/
protected function _indexDynamicArray($name) {
$dynamicArrayStart = strpos($name, '[]');
if ($dynamicArrayStart) {
$prefix = substr($name, 0, $dynamicArrayStart);
if (!isset($this->_inputCounter[$prefix])) {
$this->_inputCounter[$prefix] = -1;
}
$name = $prefix . '[' . ++$this->_inputCounter[$prefix] . substr($name, ($dynamicArrayStart + 1));
}
return $name;
}
/**
* Form types that do not change value with user input
* @var array
*/
protected $_staticTypes = array('hidden', 'submit', 'button', 'image');
/**
* Sets the standard properties available to all input elements in addition to user-defined properties
* Standard properties are: name, value, class, style, id
*
* @param array $args
* @param array $propertyNames Optional, an array of user-defined properties
* @return array
*/
protected function _getProperties(array $args, array $propertyNames = array()) {
$method = (isset($this->_formOpen['method']) && $this->_formOpen['method'] == 'get' ? $_GET : $_POST);
if (isset($args['name']) && (!isset($args['type']) || !in_array($args['type'], $this->_staticTypes))) {
$arrayStart = strpos($args['name'], '[');
if (!$arrayStart) {
if (isset($method[$args['name']])) {
$args['value'] = $method[$args['name']];
}
} else {
$name = $this->_indexDynamicArray($args['name']);
if (preg_match_all('/\[(.*)\]/', $name, $arrayIndex)) {
array_shift($arrayIndex); //dump the 0 index element containing full match string
}
$name = substr($name, 0, $arrayStart);
if (isset($method[$name])) {
$args['value'] = $method[$name];
if (!isset($args['type']) || $args['type'] != 'checkbox') {
foreach ($arrayIndex as $idx) {
if (isset($args['value'][current($idx)])) {
$args['value'] = $args['value'][current($idx)];
} else {
unset($args['value']);
break;
}
}
}
}
}
}
$return = array();
$validProperties = array_merge($propertyNames, array('name', 'value', 'class', 'style', 'id'));
foreach ($validProperties as $propertyName) {
if (isset($args[$propertyName])) {
$return[$propertyName] = $args[$propertyName];
}
}
return $return;
}
/**
* Begins a form
* Includes a safety mechanism to prevent re-opening an already-open form
*
* @param array $args
* @return string
*/
public function open(array $args = array()) {
if (!$this->_formOpen) {
if (!isset($args['method'])) {
$args['method'] = 'post';
}
$this->_formOpen = array('id' => (isset($args['id']) ? $args['id'] : true),
'method' => $args['method']);
if (!isset($args['action'])) {
$args['action'] = $_SERVER['REQUEST_URI'];
}
if (isset($args['upload']) && $args['upload'] && !isset($args['enctype'])) {
$args['enctype'] = 'multipart/form-data';
}
if (isset($args['legend'])) {
$legend = $args['legend'];
unset($args['legend']);
if (!isset($args['title'])) {
$args['title'] = $legend;
}
} else if (isset($args['title'])) {
$legend = $args['title'];
}
if (isset($args['alert'])) {
if ($args['alert']) {
$alert = (is_array($args['alert']) ? implode('<br />', $args['alert']) : $args['alert']);
}
unset($args['alert']);
}
$return = '<form ' . htmlHelper::formatProperties($args) . '>' . PHP_EOL . '<fieldset>' . PHP_EOL;
if (isset($legend)) {
$return .= '<legend>' . $legend . '</legend>' . PHP_EOL;
}
if (isset($alert)) {
$return .= $this->getErrorMessageContainer((isset($args['id']) ? $args['id'] : 'form'), $alert);
}
return $return;
} else if (DEBUG_MODE) {
$errorMsg = 'Invalid usage of ' . __METHOD__ . '() - a form is already open';
trigger_error($errorMsg, E_USER_NOTICE);
}
}
/**
* Closes a form if one is open
*
* @return string
*/
public function close() {
if ($this->_formOpen) {
$this->_formOpen = false;
return '</fieldset>' . PHP_EOL . '</form>' . PHP_EOL;
} else if (DEBUG_MODE) {
$errorMsg = 'Invalid usage of ' . __METHOD__ . '() - there is no open form to close';
trigger_error($errorMsg, E_USER_NOTICE);
}
}
/**
* Adds label tags to a form element
*
* @param array $args
* @param str $formElement
* @return str
*/
protected function _getLabel(array $args, $formElement) {
if (!isset($args['label']) && isset($args['name'])
&& (!isset($args['type']) || !in_array($args['type'], $this->_staticTypes))) {
$args['label'] = ucfirst($args['name']);
}
if (isset($args['label'])) {
$label = (!isset($args['htmlok']) || !$args['htmlok'] ? get::xhtmlentities($args['label']) :
$args['label']);
if (isset($_POST['errors']) && isset($args['name']) && isset($_POST['errors'][$args['name']])) {
$label .= ' ' . $this->getErrorMessageContainer($args['name'], $_POST['errors'][$args['name']]);
}
$labelFirst = (!isset($args['labelFirst']) || $args['labelFirst']);
if (isset($args['id'])) {
$label = '<label for="' . $args['id'] . '" id="' . $args['id'] . 'label">' . $label . '</label>';
}
if (isset($args['addBreak']) && $args['addBreak']) {
$label = ($labelFirst ? $label . '<br />' . PHP_EOL : '<br />' . PHP_EOL . $label);
}
$formElement = ($labelFirst ? $label . $formElement : $formElement . $label);
if (!isset($args['id'])) {
$formElement = '<label>' . $formElement . '</label>';
}
}
return $formElement;
}
/**
* Returns a standardized container to wrap an error message
*
* @param string $id
* @param string $errorMessage Optional, you may want to leave this blank and populate dynamically via
JavaScript
* @return string
*/
public function getErrorMessageContainer($id, $errorMessage = '') {
return '<span class="errormessage" id="' . $id . 'errorwrapper">'
. get::xhtmlentities($errorMessage) . '</span>';
}
/**
* Internal counters used by the wysiwygTextarea system
* @var int
*/
protected $_wysiwygTextareaCount = 0, $_wysiwygTextareaDefaultInitSent = 0;
/**
* Initializes WYSIWYG system
* docs at: http://wiki.moxiecode.com/index.php/TinyMCE:Configuration
*
* @param array $args Optional
* @return string
*/
public function wysiwygTextareaInit(array $args = array()) {
$init = array(
'theme' => 'advanced',
'plugins' => 'spellchecker,xhtmlxtras,searchreplace,advlink,advimage,media,fullscreen',
'mode' => 'specific_textareas',
'editor_selector' => 'wysiwyg',
'theme_advanced_buttons1' => 'bold,italic,|,formatselect,|,'
. 'justifyleft,justifycenter,justifyright,justifyfull,|,'
. 'bullist,numlist,sub,sup,blockquote,cite,abbr,acronym,|,'
. 'indent,outdent,|,link,unlink,|,image,media,hr,charmap,|,code,cleanup,|,'
. 'search,replace,|,spellchecker,|,fullscreen',
'theme_advanced_buttons2' => '',
'theme_advanced_buttons3' => '',
'theme_advanced_blockformats' => 'p,h1,h2,h3,h4',
'theme_advanced_toolbar_location' => 'top',
'skin' => 'o2k7',
'extended_valid_elements' => 'a[name|href|target|title|onclick]',
'relative_urls' => false
);
$html = get::helper('html');
$initJsObj = $return[] = $html->jsInline('var vorkInitTinyMce = ' . json_encode($init));
$init = array_merge($args, $init);
if (!isset($args['nocache'])) {
$return[] = $html->js('/js/tinymce/tiny_mce_gzip.js');
$return[] = $html->jsInline('tinyMCE_GZ.init({theme: "' . $init['theme']
. '", plugins: "' . $init['plugins']
. '", disk_cache: ' . (isset($init['disk_cache'])
? $init['disk_cache'] : 'true') . '});');
}
$return[] = $html->js('/js/tinymce/tiny_mce.js');
return implode(PHP_EOL, $return);
}
/**
* Adds WYSIWYG capabilities to a textarea
* This is a simplified interface to $this->createWysiwygTextareaParts() and $this->wysiwygTextareaParts
*
* @param array $args Optional
* @return string
*/
public function wysiwygTextarea(array $args = array()) {
$return = (!$this->_wysiwygTextareaCount++ ? $this->wysiwygTextareaInit($args) : '');
unset($args['disk_cache']); //only used in the init gzip
if ($args) {
$jsObj = 'vorkTinyMce' . $this->_wysiwygTextareaCount;
$js = 'var ' . $jsObj . ' = vorkInitTinyMce;';
foreach ($args as $key => $val) {
$js .= PHP_EOL . $jsObj . '.' . $key . ' = '
. (!in_array($val, array('true', 'false')) ? '"' . $val . '"' : $val) . ';';
}
$js .= PHP_EOL . 'tinyMCE.init(' . $jsObj . ');';
$return .= get::helper('html')->jsInline($js);
} else if (!$this->_wysiwygTextareaDefaultInitSent++) {
$return .= get::helper('html')->jsInline('tinyMCE.init(vorkInitTinyMce);');
}
return $return;
}
/**
* Sanitizes a string for use as an ID by swapping brackets with underscores
*
* @param string $str
* @return string
*/
protected function _getValidId($str) {
return str_replace(array('[', ']'), '_', $str);
}
/**
* Used for text, textarea (standard and WYSIWYG,) hidden, password, file, button, image and submit
*
* Valid args are any properties valid within an HTML input as well as label
*
* @param array $args
* @return string
*/
public function input(array $args) {
$args['type'] = (isset($args['type']) ? $args['type'] : 'text');
switch ($args['type']) {
case 'select':
return $this->select($args);
break;
case 'checkbox':
return $this->checkboxes($args);
break;
case 'radio':
return $this->radios($args);
break;
}
if ($args['type'] == 'textarea' && isset($args['maxlength'])) {
if (!isset($args['id']) && isset($args['name'])) {
$args['id'] = $this->_getValidId($args['name']);
}
if (isset($args['id'])) {
$maxlength = $args['maxlength'];
}
unset($args['maxlength']);
}
if ($args['type'] == 'submit' && !isset($args['class'])) {
$args['class'] = $args['type'];
}
$takeFocus = (isset($args['focus']) && $args['focus'] && $args['type'] != 'hidden');
if ($takeFocus && !isset($args['id'])) {
if (isset($args['name'])) {
$args['id'] = $this->_getValidId($args['name']);
} else {
$takeFocus = false;
if (DEBUG_MODE) {
$errorMsg = 'Either name or id is required to use the focus option on a form input';
trigger_error($errorMsg, E_USER_NOTICE);
}
}
}
if (isset($args['wysiwyg']) && $args['wysiwyg']) {
$args['class'] = (!isset($args['class']) ? 'wysiwyg' : $args['class'] . ' wysiwyg');
}
$properties = $this->_getProperties($args, array('type', 'maxlength'));
if ($args['type'] == 'image') {
$properties['src'] = $args['src'];
$properties['alt'] = (isset($args['alt']) ? $args['alt'] : '');
$optionalProperties = array('height', 'width');
foreach ($optionalProperties as $optionalProperty) {
if (isset($args[$optionalProperty])) {
$properties[$optionalProperty] = $args[$optionalProperty];
}
}
}
if (!class_exists('htmlHelper')) {
get::helper('html');
}
if ($args['type'] != 'textarea') {
$return[] = '<input ' . htmlHelper::formatProperties($properties) . ' />' . PHP_EOL;
} else {
unset($properties['type']);
if (isset($properties['value'])) {
$value = $properties['value'];
unset($properties['value']);
}
if (isset($args['preview']) && $args['preview'] && !isset($properties['id'])) {
$properties['id'] = 'textarea_' . rand(100, 999);
}
$properties['rows'] = (isset($args['rows']) ? $args['rows'] : 13);
$properties['cols'] = (isset($args['cols']) ? $args['cols'] : 55);
$return[] = '<textarea ' . htmlHelper::formatProperties($properties);
if (isset($maxlength)) {
$return[] = ' onkeyup="document.getElementById(\''
. $properties['id'] . 'errorwrapper\').innerHTML = (this.value.length > '
. $maxlength . ' ? \'Form content exceeds maximum length of '
. $maxlength . ' characters\' : \'Length: \' + this.value.length + \' (maximum: '
. $maxlength . ' characters)\')"';
}
$return[] = '>';
if (isset($value)) {
$return[] = get::htmlentities($value, null, null, true); //double-encode allowed
}
$return[] = '</textarea>' . PHP_EOL;
if (isset($args['wysiwyg']) && $args['wysiwyg']) {
if (!is_array($args['wysiwyg'])) {
$args['wysiwyg'] = array();
}
$return[] = $this->wysiwygTextarea($args['wysiwyg']);
}
if (isset($maxlength) && (!isset($args['validatedInput']) || !$args['validatedInput'])) {
$return[] = $this->getErrorMessageContainer($properties['id']);
}
}
if (!isset($args['addBreak'])) {
$args['addBreak'] = $this->addBreak;
}
if ($takeFocus) {
$html = get::helper('html');
$return[] = $html->jsInline($html->jsTools(true) . 'dom("' . $args['id'] . '").focus();');
}
if (isset($args['preview']) && $args['preview']) {
$js = 'document.writeln(\'<div class="htmlpreviewlabel">'
. '<label for="livepreview_' . $properties['id'] . '">Preview:</label></div>'
. '<div id="livepreview_' . $properties['id'] . '" class="htmlpreview"></div>\');' . PHP_EOL
. 'if (dom("livepreview_' . $properties['id'] . '")) {' . PHP_EOL
. ' var updateLivePreview_' . $properties['id'] . ' = '
. 'function() {dom("livepreview_' . $properties['id'] . '").innerHTML = '
. 'dom("' . $properties['id'] . '").value};' . PHP_EOL
. ' dom("' . $properties['id'] . '").onkeyup = updateLivePreview_' . $properties['id'] . ';'
. ' updateLivePreview_' . $properties['id'] . '();' . PHP_EOL
. '}';
if (!isset($html)) {
$html = get::helper('html');
}
$return[] = $html->jsInline($html->jsTools(true) . $js);
}
return $this->_getLabel($args, implode($return));
}
/**
* Returns an input that is validated using the Validator package
*
* @param array $args
* @return string
*/
public function validatedInput(array $args) {
$args['validatedInput'] = true;
$errorMessage = '';
if (isset(Validator::$errors[$this->_formOpen['id']][$args['id']])) {
$errorMessage = Validator::$errors[$this->_formOpen['id']][$args['id']];
}
return $this->input($args) . $this->getErrorMessageContainer($args['id'], $errorMessage);
}
/**
* Returns a form select element
*
* $args = array(
* 'name' => '',
* 'multiple' => true,
* 'leadingOptions' => array(),
* 'optgroups' => array('group 1' => array('label' => 'g1o1', 'value' => 'grp 1 opt 1'),
* 'group 2' => array(),),
* 'options' => array('value1' => 'text1', 'value2' => 'text2', 'value3' => 'text3'),
* 'value' => array('value2', 'value3') //if (multiple==false) 'value' => (str) 'value3'
* );
*
* @param array $args
* @return str
*/
public function select(array $args) {
if (!isset($args['id'])) {
$args['id'] = $this->_getValidId($args['name']);
}
if (isset($args['multiple']) && $args['multiple']) {
$args['multiple'] = 'multiple';
if (substr($args['name'], -2) != '[]') {
$args['name'] .= '[]';
}
}
$properties = $this->_getProperties($args, array('multiple'));
$values = (isset($properties['value']) ? $properties['value'] : null);
unset($properties['value']);
if (!is_array($values)) {
$values = ($values != '' ? array($values) : array());
}
$return = '<select ' . htmlHelper::formatProperties($properties) . '>' . PHP_EOL;
if (isset($args['prependBlank']) && $args['prependBlank']) {
$return .= '<option value=""></option>' . PHP_EOL;
}
if (isset($args['leadingOptions'])) {
$useValues = (key($args['leadingOptions']) !== 0
|| (isset($args['useValue']) && $args['useValue']));
foreach ($args['leadingOptions'] as $value => $text) {
if (!$useValues) {
$value = $text;
}
$return .= '<option value="' . get::htmlentities($value) . '">'
. get::htmlentities($text) . '</option>' . PHP_EOL;
}
}
if (isset($args['optgroups'])) {
foreach ($args['optgroups'] as $groupLabel => $optgroup) {
$return .= '<optgroup label="' . get::htmlentities($groupLabel) . '">' . PHP_EOL;
foreach ($optgroup as $value => $label) {
$return .= '<option value="' . get::htmlentities($value) . '"';
if (isset($label)) {
$return .= ' label="' . get::htmlentities($label) . '"';
}
if (in_array($value, $values)) {
$return .= ' selected="selected"';
}
$return .= '>' . get::htmlentities($label) . '</option>' . PHP_EOL;
}
$return .= '</optgroup>' . PHP_EOL;
}
}
if (isset($args['options'])) {
$useValues = (key($args['options']) !== 0 || (isset($args['useValue']) && $args['useValue']));
foreach ($args['options'] as $value => $text) {
if (!$useValues) {
$value = $text;
}
$return .= '<option value="' . get::htmlentities($value) . '"';
if (in_array($value, $values)) {
$return .= ' selected="selected"';
}
$return .= '>' . get::htmlentities($text) . '</option>' . PHP_EOL;
}
}
$return .= '</select>' . PHP_EOL;
if (!isset($args['addBreak'])) {
$args['addBreak'] = $this->addBreak;
}
$return = $this->_getLabel($args, $return);
if (isset($args['error'])) {
$return .= $this->getErrorMessageContainer($args['id'], '<br />' . $args['error']);
}
return $return;
}
/**
* Cache containing individual radio, checkbox and dateSelect-selector elements in an array
* @var array
*/
public $radios = array(), $checkboxes = array(), $dateSelect = array();
/**
* Returns a set of radio form elements
*
* array(
* 'name' => '',
* 'value' => '',
* 'id' => '',
* 'legend' => '',
* 'options' => array('value1' => 'text1', 'value2' => 'text2', 'value3' => 'text3'), // if just one option
exists this can be a string
* 'options' => array('text1', 'text2', 'text3'), //also acceptable (cannot do half this, half above syntax)
* )
*
* @param array $args
* @return str
*/
public function radios(array $args) {
$id = (isset($args['id']) ? $args['id'] : $this->_getValidId($args['name']));
$properties = $this->_getProperties($args);
if (isset($properties['value'])) {
$checked = $properties['value'];
unset($properties['value']);
}
$properties['type'] = (isset($args['type']) ? $args['type'] : 'radio');
if (!empty($args['options']) && !is_array($args['options'])) {
$args['options'] = (isset($args['label']) ? array($args['options'] => $args['label']) :
array($args['options']));
$args['addFieldset'] = false;
}
$useValues = (key($args['options']) !== 0 || (isset($args['useValue']) && $args['useValue']));
foreach ($args['options'] as $value => $text) {
if (!$useValues) {
$value = $text;
}
$properties['id'] = $id . '_' . $this->_getValidId(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);
if (isset($args['htmlok'])) {
$labelArgs['htmlok'] = $args['htmlok'];
}
$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['addBreak']) ? '<br />' . PHP_EOL : $args['addBreak']);
$addFieldset = (isset($args['addFieldset']) ? $args['addFieldset'] : ((isset($args['label']) &&
$args['label']) || count($args['options']) > 1));
if ($addFieldset) {
$return = '<fieldset id="' . $id . '">' . PHP_EOL;
if (isset($args['label'])) {
$return .= '<legend>' . get::xhtmlentities($args['label']) . '</legend>' . PHP_EOL;
}
$return .= implode($break, $radios) . '</fieldset>' . PHP_EOL;
} else {
$return = implode($break, $radios);
}
if (isset($_POST['errors']) && isset($_POST['errors'][$id])) {
$return = $this->getErrorMessageContainer($id, $_POST['errors'][$id]) . $return;
}
return $return;
}
/**
* Returns a set of checkbox form elements
*
* This method essentially extends the getRadios method and uses an identical signature except
* that $args['value'] can also accept an array of values to be checked.
*
* @param array $args
* @return str
*/
public function checkboxes(array $args) {
$args['type'] = 'checkbox';
if (isset($args['value']) && !is_array($args['value'])) {
$args['value'] = array($args['value']);
}
$nameParts = explode('[', $args['name']);
if (!isset($args['id'])) {
$args['id'] = $this->_getValidId($nameParts[0]);
}
if (!isset($nameParts[1]) && count($args['options']) > 1) {
$args['name'] .= '[]';
}
return $this->radios($args);
}
/**
* Months of the year in the format commonly used when entering credit cards
* @var array
*/
public $months = array(1 => '01 - January', '02 - February', '03 - March', '04 - April',
'05 - May', '06 - June', '07 - July', '08 - August',
'09 - September', '10 - October', '11 - November', '12 - December');
/**
* Creates a Date selection box
*
* @param array $args
* @return string
*/
public function dateSelect(array $args) {
$includeTime = ((isset($args['time']) && $args['time'])
|| isset($args['hour']) || isset($args['minute']) || isset($args['mil']));
$includeDate = (!isset($args['date']) || $args['date'] || !$includeTime /* must show something */);
if (!isset($args['addBreak'])) {
$args['addBreak'] = false;
}
$dateParts = ($includeDate ? array('month' => 'm', 'day' => 'j', 'year' => 'Y') : array());
if ($includeTime) {
$dateParts['hour'] = 'h';
$dateParts['minute'] = 'i';
$isMil = (isset($args['mil']) && $args['mil']);
if (!isset($args['ampm']) && isset($args['pmam'])) {
$args['ampm'] = $args['pmam'];
}
if (!$isMil) {
$dateParts['ampm'] = 'p';
}
if (!isset($args['hour']['options'])) {
$args['hour']['options'] = (!$isMil ? range(1, 12) : range(0, 23));
}
if (!isset($args['minute']['options'])) {
if (isset($args['minute']['increment'])) {
switch ($args['minute']['increment']) {
case 5:
$minutes = range(10, 55, 5);
array_unshift($minutes, '00', '05');
break;
case 10:
$minutes = range(10, 50, 10);
array_unshift($minutes, '00');
break;
case 15:
$minutes = array(00, 15, 30, 45);
break;
case 20:
$minutes = array(00, 20, 40);
break;
case 30:
$minutes = array(00, 30);
break;
}
} else {
$minutes = range(0, 59);
}
foreach ($minutes as $x) {
$args['minute']['options'][] = str_pad($x, 2, '0', STR_PAD_LEFT);
}
}
if (!isset($args['hour']['label'])) {
$args['hour']['label'] = 'Time';
}
if (!isset($args['minute']['label'])) {
$args['minute']['label'] = ':';
}
if (!$isMil) {
if (!isset($args['ampm']['options'])) {
$args['ampm']['options'] = array('AM', 'PM');
}
if (!isset($args['ampm']['label'])) {
$args['ampm']['label'] = '';
}
}
if (isset($args['ampm']['value'])) {
$args['ampm']['value'] = strtoupper($args['ampm']['value']);
}
}
if ($includeDate) {
if (!isset($args['month']['options'])) {
$selectArgs['month']['options'] = $this->months;
}
$selectArgs['day']['options'] = range(1, 31);
$datePart = 'year';
if (!isset($args[$datePart]['start'])) {
$args[$datePart]['start'] = date($dateParts[$datePart]);
}
if (!isset($args['year']['end'])) {
$args[$datePart]['end'] = ($args[$datePart]['start'] - 100);
}
$selectArgs['year']['options'] = range(current($args[$datePart]), next($args[$datePart]));
}
$method = (isset($this->_formOpen['method']) && $this->_formOpen['method'] == 'get' ? $_GET : $_POST);
foreach ($dateParts as $dateType => $unixCode) {
$selectArgs[$dateType]['name'] = $args['name'] . '[' . $dateType . ']';
$selectArgs[$dateType]['id'] = $this->_getValidId($args['name']) . '_' . $dateType;
if (isset($args[$dateType])) {
$selectArgs[$dateType] = array_merge($args[$dateType], $selectArgs[$dateType]);
}
if (!isset($selectArgs[$dateType]['label'])) {
$selectArgs[$dateType]['label'] = ucfirst($dateType);
}
if (isset($method[$args['name']][$dateType])) {
$selectArgs[$dateType]['value'] = $method[$args['name']][$dateType];
} else if (isset($args['value'][$dateType])) {
$selectArgs[$dateType]['value'] = $args['value'][$dateType];
}
$this->dateSelect[] = $this->select(array_merge($args, $selectArgs[$dateType]));
}
$return = '<fieldset class="dateselect';
if (isset($args['class'])) {
$return .= ' ' . $args['class'];
}
$return .= '"';
if (isset($args['id'])) {
$return .= ' id="' . $args['id'] . '"';
}
$return .= '>';
if (isset($args['label'])) {
$return .= '<legend>' . get::htmlentities($args['label']) . '</legend>' . PHP_EOL;
}
$return .= implode($this->dateSelect) . '</fieldset>' . PHP_EOL;
return $return;
}
/**
* Opens up shorthand usage of form elements like $form->file() and $form->submit()
* The $legacyNames section keeps legacy naming convention working. New applications can safely remove this
section.
*
* @param string $name
* @param array $args
* @return mixed
*/
public function __call($name, array $args) {
$inputShorthand = array('text', 'textarea', 'password', 'file', 'hidden', 'submit', 'button', 'image');
if (in_array($name, $inputShorthand)) {
if (!is_array($args[0]) && $args[0]) { //shortcut strings
$defaultKey = 'name';
if ($name == 'submit' && !isset($args[1])) {
$defaultKey = 'value';
}
$args[0] = array($defaultKey => $args[0]);
if (isset($args[1]) && !is_array($args[1]) && $args[1]) {
$args[0][$name != 'image' ? 'value' : 'src'] = $args[1];
}
}
$args[0]['type'] = $name;
return $this->input($args[0]);
}
$legacyNames = array('getFormOpen' => 'open', 'getFormClose' => 'close',
'getInput' => 'input', 'getValidatedInput' => 'validatedInput', 'getRadios' =>
'radios',
'getSelect' => 'select', 'getDateSelect' => 'dateSelect', 'getCheckboxes' =>
'checkboxes');
if (isset($legacyNames[$name])) {
return call_user_func_array(array($this, $legacyNames[$name]), $args);
} else {
trigger_error('Call to undefined method ' . __CLASS__ . '::' . $name . '()', E_USER_ERROR);
}
}
}

