cakephp2-php8/cake/libs/view/helpers/form.php
phpnut d6f73252ae Moved all deprecated methods to end of file.
Added notices for all methods that are deprecated.
Added fix for Ticket #2185.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4583 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-03-04 15:28:08 +00:00

1307 lines
No EOL
43 KiB
PHP

<?php
/* SVN FILE: $Id$ */
/**
* Automatic generation of HTML FORMs from given data.
*
* Used for scaffolding.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.view.helpers
* @since CakePHP(tm) v 0.10.0.1076
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/* Deprecated */
/**
* Tag template for a div with a class attribute.
* @deprecated
*/
define('TAG_DIV', '<div class="%s">%s</div>');
/**
* Tag template for a paragraph with a class attribute.
*/
define('TAG_P_CLASS', '<p class="%s">%s</p>');
/**
* Tag template for a label with a for attribute.
*/
define('TAG_LABEL', '<label for="%s">%s</label>');
/**
* Tag template for a fieldset with a legend tag inside.
*/
define('TAG_FIELDSET', '<fieldset><legend>%s</legend>%s</label>');
/**
* Form helper library.
*
* Automatic generation of HTML FORMs from given data.
*
* @package cake
* @subpackage cake.cake.libs.view.helpers
*/
class FormHelper extends AppHelper {
var $helpers = array('Html');
var $Html = null;
var $__options = array(
'day' => array(), 'minute' => array(), 'hour' => array(),
'month' => array(), 'year' => array(), 'meridian' => array()
);
/**
* Returns an HTML FORM element.
*
* @access public
* @param string $model The model object which the form is being defined for
* @param array $options
* @return string An formatted opening FORM tag.
*/
function create($model = null, $options = array()) {
$data = array('fields' => '','key' => '', 'validates' => '');
if (is_array($model) && empty($options)) {
$options = $model;
$model = null;
}
if (empty($model) && $model !== false) {
$model = $this->params['models'][0];
}
if (ClassRegistry::isKeySet($model)) {
$object =& ClassRegistry::getObject($model);
}
$this->setFormTag($model . '/');
$append = '';
$created = false;
if(isset($object)) {
$fields = $object->loadInfo();
$data = array(
'fields' => array_combine($fields->extract('{n}.name'), $fields->extract('{n}.type')),
'key' => $object->primaryKey,
'validates' => (ife(empty($object->validate), array(), array_keys($object->validate)))
);
}
if (isset($this->data[$model]) && isset($this->data[$model][$data['key']]) && !empty($this->data[$model][$data['key']])) {
$created = true;
}
$options = am(array(
'type' => ($created && empty($options['action'])) ? 'put' : 'post',
'id' => $model . ife($created, 'Edit', 'Add') . 'Form',
'action' => array(),
'default' => true),
$options);
if (empty($options['action']) || is_array($options['action'])) {
$options = (array)$options;
$actionDefaults = array(
'controller' => Inflector::underscore($this->params['controller']),
'action' => $created ? 'edit' : 'add',
'id' => $created ? $this->data[$model][$data['key']] : null
);
$options['action'] = am($actionDefaults, $options['action']);
}
switch (low($options['type'])) {
case 'get':
$htmlAttributes['method'] = 'get';
break;
case 'file':
$htmlAttributes['enctype'] = 'multipart/form-data';
$options['type'] = ife($created, 'put', 'post');
case 'post':
case 'put':
case 'delete':
//$append .= $this->hidden('_method', array('value' => up($options['type']), 'id' => $options['id'] . 'Method'));
default:
$htmlAttributes['method'] = 'post';
break;
}
$htmlAttributes['action'] = $this->url($options['action']);
unset($options['type'], $options['action']);
if ($options['default'] == false) {
if (isset($htmlAttributes['onSubmit'])) {
$htmlAttributes['onSubmit'] .= ' return false;';
} else {
$htmlAttributes['onSubmit'] = 'return false;';
}
}
unset($options['default']);
$htmlAttributes = am($options, $htmlAttributes);
if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {
$append .= $this->hidden('_Token/key', array('value' => $this->params['_Token']['key'], 'id' => $options['id'] . 'Token'));
}
$this->setFormTag($model . '/');
return $this->output(sprintf($this->Html->tags['form'], $this->Html->parseHtmlOptions($htmlAttributes, null, ''))) . $append;
}
/**
* Closes an HTML form.
*
* @access public
* @return string A closing FORM tag.
*/
function end($model = null) {
if (empty($model) && !empty($this->params['models'])) {
$models = $this->params['models'][0];
}
return $this->output($this->Html->tags['formend']);
}
/**
* Returns true if there is an error for the given field, otherwise false
*
* @access public
* @param string $field This should be "Modelname/fieldname"
* @return bool If there are errors this method returns true, else false.
*/
function isFieldError($field) {
$this->setFormTag($field);
return (bool)$this->tagIsInvalid();
}
/**
* Returns a formatted error message for given FORM field, NULL if no errors.
*
* @param string $field A field name, like "Modelname/fieldname"
* @param string $text Error message
* @param array $options Rendering options for <div /> wrapper tag
* @return string If there are errors this method returns an error message, otherwise null.
*/
function error($field, $text = null, $options = array()) {
$this->setFormTag($field);
if ($error = $this->tagIsInvalid()) {
if ($text != null) {
$error = $text;
} elseif (is_numeric($error)) {
$error = 'Error in field ' . Inflector::humanize($this->field());
}
return $this->Html->div('error-message', $error);
} else {
return null;
}
}
/**
* Returns a formatted LABEL element for HTML FORMs.
*
* @param string $tagName This should be "Modelname/fieldname"
* @param string $text Text that will appear in the label field.
* @return string The formatted LABEL element
*/
function label($tagName = null, $text = null, $attributes = array()) {
if (empty($tagName)) {
$tagName = implode('/', array_filter(array($this->model(), $this->field())));
}
if ($text == null) {
if (strpos($tagName, '/') !== false) {
list( , $text) = explode('/', $tagName);
} else {
$text = $tagName;
}
if (substr($text, -3) == '_id') {
$text = substr($text, 0, strlen($text) - 3);
}
$text = Inflector::humanize($text);
}
if (strpos($tagName, '/') !== false) {
$tagName = Inflector::camelize(r('/', '_', $tagName));
}
return $this->output(sprintf($this->Html->tags['label'], $tagName, $this->_parseAttributes($attributes), $text));
}
/**
* Will display all the fields passed in an array expects tagName as an array key
* replaces generateFields
*
* @access public
* @param array $fields works well with Controller::generateFieldNames();
* @return output
*/
function inputs($fields) {
$out = null;
foreach($fields as $name => $options) {
if (is_numeric($name) && !is_array($options)) {
$name = $options;
$options = array();
}
if(is_array($options) && isset($options['tagName'])) {
$name = $options['tagName'];
unset($options['tagName']);
}
$out .= $this->input($name, $options);
}
return $out;
}
/**
* Generates a form input element complete with label and wrapper div
*
* @param string $tagName This should be "Modelname/fieldname"
* @param array $options
* @return string
*/
function input($tagName, $options = array()) {
$this->setFormTag($tagName);
$options = am(
array(
'before' => null,
'between' => null,
'after' => null
),
$options);
if ((!isset($options['type']) || $options['type'] == 'select') && !isset($options['options'])) {
$view =& ClassRegistry::getObject('view');
$varName = Inflector::variable(Inflector::pluralize(preg_replace('/_id$/', '', $this->field())));
$varOptions = $view->getVar($varName);
if (is_array($varOptions)) {
$options['type'] = 'select';
$options['options'] = $varOptions;
}
}
if (!isset($options['type'])) {
$options['type'] = 'text';
if (isset($options['options'])) {
$options['type'] = 'select';
} elseif (in_array($this->field(), array('passwd', 'password'))) {
$options['type'] = 'password';
} elseif (ClassRegistry::isKeySet($this->model())) {
$model =& ClassRegistry::getObject($this->model());
$type = $model->getColumnType($this->field());
$map = array(
'string' => 'text', 'datetime' => 'datetime',
'boolean' => 'checkbox', 'timestamp' => 'datetime',
'text' => 'textarea', 'time' => 'time',
'date' => 'date'
);
if (isset($map[$type])) {
$options['type'] = $map[$type];
}
if($this->field() == $model->primaryKey) {
$options['type'] = 'hidden';
}
}
}
$out = '';
$div = true;
if (isset($options['div'])) {
$div = $options['div'];
unset($options['div']);
}
$divOptions = array();
if ($div === true) {
$divOptions['class'] = 'input';
} elseif ($div === false) {
unset($divOptions);
} elseif (is_string($div)) {
$divOptions['class'] = $div;
} elseif (is_array($div)) {
$divOptions = am(array('class' => 'input'), $div);
}
$label = null;
if (isset($options['label'])) {
$label = $options['label'];
unset($options['label']);
}
if (is_array($label)) {
$labelText = null;
if (isset($label['text'])) {
$labelText = $label['text'];
unset($label['text']);
}
$out = $this->label(null, $labelText, $label);
$label = $labelText;
} elseif ($label !== false) {
$out = $this->label(null, $label);
}
$error = null;
if (isset($options['error'])) {
$error = $options['error'];
unset($options['error']);
}
$selected = null;
if (array_key_exists('selected', $options)) {
$selected = $options['selected'];
unset($options['selected']);
}
if(isset($options['rows']) || isset($options['cols'])) {
$options['type'] = 'textarea';
}
$empty = false;
if(isset($options['empty'])) {
$empty = $options['empty'];
unset($options['empty']);
}
$type = $options['type'];
$before = $options['before'];
$between = $options['between'];
$after = $options['after'];
unset($options['type'], $options['before'], $options['between'], $options['after']);
switch ($type) {
case 'hidden':
$out = $this->hidden($tagName, $options);
unset($divOptions);
break;
case 'checkbox':
$out = $before . $this->Html->checkbox($tagName, null, $options) . $between . $out;
break;
case 'text':
case 'password':
$out = $before . $out . $between . $this->{$type}($tagName, $options);
break;
case 'file':
$out = $before . $out . $between . $this->file($tagName);
break;
case 'select':
$options = am(array('options' => array()), $options);
$list = $options['options'];
unset($options['options']);
$out = $before . $out . $between . $this->select($tagName, $list, $selected, $options, $empty);
break;
case 'time':
$out = $before . $out . $between . $this->dateTime($tagName, null, '12', $selected, $options, null, $empty);
break;
case 'date':
$out = $before . $out . $between . $this->dateTime($tagName, 'MDY', null, $selected, $options, null, $empty);
break;
case 'datetime':
$out = $before . $out . $between . $this->dateTime($tagName, 'MDY', '12', $selected, $options, null, $empty);
break;
case 'textarea':
default:
$out = $before . $out . $between . $this->textarea($tagName, am(array('cols' => '30', 'rows' => '10'), $options));
break;
}
if ($type != 'hidden' && $error !== false) {
$out .= $this->error($tagName, $error);
$out .= $after;
}
if (isset($divOptions)) {
$out = $this->Html->div($divOptions['class'], $out, $divOptions);
}
return $out;
}
/**
* Creates a text input widget.
*
* @param string $fieldNamem Name of a field, like this "Modelname/fieldname"
* @param array $htmlAttributes Array of HTML attributes.
* @return string An HTML text input element
*/
function text($fieldName, $htmlAttributes = array()) {
$htmlAttributes = am(array('type' => 'text'), $htmlAttributes);
$htmlAttributes = $this->__value($htmlAttributes, $fieldName);
$htmlAttributes = $this->domId($htmlAttributes);
if ($this->tagIsInvalid()) {
$htmlAttributes = $this->addClass($htmlAttributes, 'form-error');
}
return $this->output(sprintf($this->Html->tags['input'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, null, ' ')));
}
/**
* Creates a password input widget.
*
* @param string $fieldName Name of a field, like this "Modelname/fieldname"
* @param array $htmlAttributes Array of HTML attributes.
* @return string
*/
function password($fieldName, $htmlAttributes = array()) {
$htmlAttributes = $this->__value($htmlAttributes, $fieldName);
$htmlAttributes = $this->domId($htmlAttributes);
if ($this->tagIsInvalid()) {
$htmlAttributes = $this->addClass($htmlAttributes, 'form-error');
}
return $this->output(sprintf($this->Html->tags['password'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, null, ' ')));
}
/**
* Creates a textarea widget.
*
* @param string $fieldNamem Name of a field, like this "Modelname/fieldname"
* @param array $htmlAttributes Array of HTML attributes.
* @return string An HTML text input element
*/
function textarea($fieldName, $htmlAttributes = array()) {
$htmlAttributes = $this->__value($htmlAttributes, $fieldName);
$htmlAttributes = $this->domId($htmlAttributes);
if (isset($htmlAttributes['type'])) {
unset($htmlAttributes['type']);
}
$value = null;
if (array_key_exists('value', $htmlAttributes)) {
$value = $htmlAttributes['value'];
unset($htmlAttributes['value']);
}
if ($this->tagIsInvalid()) {
$htmlAttributes = $this->addClass($htmlAttributes, 'form-error');
}
return $this->output(sprintf($this->Html->tags['textarea'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' '), $value));
}
/**
* Creates a hidden input field.
*
* @param string $fieldName Name of a field, like this "Modelname/fieldname"
* @param array $htmlAttributes Array of HTML attributes.
* @return string
* @access public
*/
function hidden($fieldName, $htmlAttributes = array()) {
$htmlAttributes = $this->domId($this->__value($htmlAttributes, $fieldName));
$model = $this->model();
if (in_array($fieldName, array('_method'))) {
$model = null;
}
return $this->output(sprintf($this->Html->tags['hidden'], $model, $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' ')));
}
/**
* Creates file input widget.
*
* @param string $fieldName Name of a field, like this "Modelname/fieldname"
* @param array $htmlAttributes Array of HTML attributes.
* @return string
* @access public
*/
function file($fieldName, $htmlAttributes = array()) {
$htmlAttributes = $this->domId($this->__value($htmlAttributes, $fieldName));
return $this->output(sprintf($this->Html->tags['file'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, '', ' ')));
}
/**
* Creates a button tag.
*
* @param mixed $params Array of params [content, type, options] or the
* content of the button.
* @param string $type Type of the button (button, submit or reset).
* @param array $options Array of options.
* @return string A HTML button tag.
* @access public
*/
function button($params, $type = 'button', $options = array()) {
trigger_error(__("Don't use me yet"), E_USER_ERROR);
if (isset($options['name'])) {
if (strpos($options['name'], "/") !== false) {
if ($this->fieldValue($options['name'])) {
$options['checked'] = 'checked';
}
$this->setFieldName($options['name']);
$options['name'] = 'data[' . $this->model() . '][' . $this->field() . ']';
}
}
$options['type'] = $type;
$values = array(
'options' => $this->_parseOptions($options),
'tagValue' => $content
);
return $this->_assign('button', $values);
}
/**
* Creates a submit button element.
*
* @param string $caption The label appearing on the button
* @param array $options
* @return string A HTML submit button
*/
function submit($caption = 'Submit', $options = array()) {
$options['value'] = $caption;
$div = true;
if (isset($options['div'])) {
$div = $options['div'];
unset($options['div']);
}
$divOptions = array();
if ($div === true) {
$divOptions['class'] = 'submit';
} elseif ($div === false) {
unset($divOptions);
} elseif (is_string($div)) {
$divOptions['class'] = $div;
} elseif (is_array($div)) {
$divOptions = am(array('class' => 'submit'), $div);
}
$out = $this->output(sprintf($this->Html->tags['submit'], $this->_parseAttributes($options, null, '', ' ')));
if (isset($divOptions)) {
$out = $this->Html->div($divOptions['class'], $out, $divOptions);
}
return $out;
}
/**
* Creates an image input widget.
*
* @param string $path Path to the image file, relative to the webroot/img/ directory.
* @param array $htmlAttributes Array of HTML attributes.
* @return string HTML submit image element
*/
function submitImage($path, $htmlAttributes = array()) {
if (strpos($path, '://')) {
$url = $path;
} else {
$url = $this->webroot(IMAGES_URL . $path);
}
return $this->output(sprintf($this->Html->tags['submitimage'], $url, $this->_parseAttributes($htmlAttributes, null, '', ' ')));
}
/**
* Returns a formatted SELECT element.
*
* @param string $fieldName Name attribute of the SELECT
* @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the SELECT element
* @param mixed $selected The option selected by default. If null, the default value
* from POST data will be used when available.
* @param array $attributes The HTML attributes of the select element. If
* 'showParents' is included in the array and set to true,
* an additional option element will be added for the parent
* of each option group.
* @param mixed $showEmpty If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
* @return string Formatted SELECT element
*/
function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') {
$showParents = false;
$this->setFormTag($fieldName);
$attributes = $this->domId($attributes);
if ($this->tagIsInvalid()) {
$attributes = $this->addClass($attributes, 'form-error');
}
if (is_string($options) && isset($this->__options[$options])) {
$options = $this->__generateOptions($options);
} elseif(!is_array($options)) {
$options = array();
}
if (isset($attributes['type'])) {
unset($attributes['type']);
}
if (in_array('showParents', $attributes)) {
$showParents = true;
unset($attributes['showParents']);
}
if (!isset($selected)) {
$selected = $this->__value($fieldName);
}
if (isset($attributes) && array_key_exists('multiple', $attributes)) {
$tag = $this->Html->tags['selectmultiplestart'];
} else {
$tag = $this->Html->tags['selectstart'];
}
$select[] = sprintf($tag, $this->model(), $this->field(), $this->Html->parseHtmlOptions($attributes));
if ($showEmpty !== null && $showEmpty !== false) {
if($showEmpty === true) {
$showEmpty = '';
}
$options = array_reverse($options, true);
$options[''] = $showEmpty;
$options = array_reverse($options, true);
}
$select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents));
$select[] = sprintf($this->Html->tags['selectend']);
return $this->output(implode("\n", $select));
}
/**
* Returns a SELECT element for days.
*
* @param string $tagName Prefix name for the SELECT element
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @param boolean $show_empty Show/hide the empty select option
* @return string
*/
function day($tagName, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
if (empty($selected) && $value = $this->__value($tagName)) {
$selected = date('d', strtotime($value));
}
$dayValue = empty($selected) ? ($showEmpty == true ? NULL : date('d')) : $selected;
$days = array('01' => '1', '02' => '2', '03' => '3', '04' => '4', '05' => '5', '06' => '6', '07' => '7', '08' => '8', '09' => '9', '10' => '10', '11' => '11', '12' => '12', '13' => '13', '14' => '14', '15' => '15', '16' => '16', '17' => '17', '18' => '18', '19' => '19', '20' => '20', '21' => '21', '22' => '22', '23' => '23', '24' => '24', '25' => '25', '26' => '26', '27' => '27', '28' => '28', '29' => '29', '30' => '30', '31' => '31');
$option = $this->select($tagName . "_day", $days, $dayValue, $selectAttr, $optionAttr, $showEmpty);
return $option;
}
/**
* Returns a SELECT element for years
*
* @param string $tagName Prefix name for the SELECT element
* @param integer $minYear First year in sequence
* @param integer $maxYear Last year in sequence
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @param boolean $showEmpty Show/hide the empty select option
* @return string
*/
function year($tagName, $minYear = null, $maxYear = null, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
if (empty($selected) && $value = $this->__value($tagName)) {
$selected = date('Y', strtotime($value));
}
$yearValue = empty($selected) ? ($showEmpty ? NULL : date('Y')) : $selected;
$currentYear = date('Y');
$maxYear = is_null($maxYear) ? $currentYear + 11 : $maxYear + 1;
$minYear = is_null($minYear) ? $currentYear - 60 : $minYear;
if ($minYear > $maxYear) {
$tmpYear = $minYear;
$minYear = $maxYear;
$maxYear = $tmpYear;
}
$minYear = $currentYear < $minYear ? $currentYear : $minYear;
$maxYear = $currentYear > $maxYear ? $currentYear : $maxYear;
for($yearCounter = $minYear; $yearCounter < $maxYear; $yearCounter++) {
$years[$yearCounter] = $yearCounter;
}
$option = $this->select($tagName . "_year", $years, $yearValue, $selectAttr, $optionAttr, $showEmpty);
return $option;
}
/**
* Returns a SELECT element for months.
*
* @param string $tagName Prefix name for the SELECT element
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @param boolean $showEmpty Show/hide the empty select option
* @return string
*/
function month($tagName, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
if (empty($selected) && $value = $this->__value($tagName)) {
$selected = date('m', strtotime($value));
}
$monthValue = empty($selected) ? ($showEmpty ? NULL : date('m')) : $selected;
$months = array('01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December');
$option = $this->select($tagName . "_month", $months, $monthValue, $selectAttr, $optionAttr, $showEmpty);
return $option;
}
/**
* Returns a SELECT element for hours.
*
* @param string $tagName Prefix name for the SELECT element
* @param boolean $format24Hours True for 24 hours format
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @return string
*/
function hour($tagName, $format24Hours = false, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
if (empty($selected) && $value = $this->__value($tagName)) {
if ($format24Hours) {
$selected = date('H', strtotime($value));
} else {
$selected = date('g', strtotime($value));
}
}
if ($format24Hours) {
$hourValue = empty($selected) ? ($showEmpty ? NULL : date('H')) : $selected;
} else {
$hourValue = empty($selected) ? ($showEmpty ? NULL : date('g')) : $selected;
if (isset($selected) && intval($hourValue) == 0 && !$showEmpty) {
$hourValue = 12;
}
}
if ($format24Hours) {
$hours = array('00' => '00', '01' => '01', '02' => '02', '03' => '03', '04' => '04', '05' => '05', '06' => '06', '07' => '07', '08' => '08', '09' => '09', '10' => '10', '11' => '11', '12' => '12', '13' => '13', '14' => '14', '15' => '15', '16' => '16', '17' => '17', '18' => '18', '19' => '19', '20' => '20', '21' => '21', '22' => '22', '23' => '23');
} else {
$hours = array('01' => '1', '02' => '2', '03' => '3', '04' => '4', '05' => '5', '06' => '6', '07' => '7', '08' => '8', '09' => '9', '10' => '10', '11' => '11', '12' => '12');
}
$option = $this->select($tagName . "_hour", $hours, $hourValue, $selectAttr, $optionAttr, $showEmpty);
return $option;
}
/**
* Returns a SELECT element for minutes.
*
* @param string $tagName Prefix name for the SELECT element
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @return string
*/
function minute($tagName, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
if (empty($selected) && $value = $this->__value($tagName)) {
$selected = date('i', strtotime($value));
}
$minValue = empty($selected) ? ($showEmpty ? NULL : date('i')) : $selected;
for($minCount = 0; $minCount < 60; $minCount++) {
$mins[$minCount] = sprintf('%02d', $minCount);
}
$option = $this->select($tagName . "_min", $mins, $minValue, $selectAttr, $optionAttr, $showEmpty);
return $option;
}
/**
* Returns a SELECT element for AM or PM.
*
* @param string $tagName Prefix name for the SELECT element
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @return string
*/
function meridian($tagName, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
if (empty($selected) && $value = $this->__value($tagName)) {
$selected = date('a', strtotime($value));
}
$merValue = empty($selected) ? ($showEmpty ? NULL : date('a')) : $selected;
$meridians = array('am' => 'am', 'pm' => 'pm');
$option = $this->select($tagName . "_meridian", $meridians, $merValue, $selectAttr, $optionAttr, $showEmpty);
return $option;
}
/**
* Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time.
*
* @param string $tagName Prefix name for the SELECT element
* @param string $dateFormat DMY, MDY, YMD or NONE.
* @param string $timeFormat 12, 24, NONE
* @param string $selected Option which is selected.
* @param array $optionAttr Attribute array for the option elements.
* @return string The HTML formatted OPTION element
*/
function dateTime($tagName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) {
$day = null;
$month = null;
$year = null;
$hour = null;
$min = null;
$meridian = null;
if (empty($selected)) {
$selected = $this->__value($tagName);
}
if (!empty($selected)) {
if (is_int($selected)) {
$selected = strftime('%Y-%m-%d %H:%M:%S', $selected);
}
$meridian = 'am';
$pos = strpos($selected, '-');
if($pos !== false){
$date = explode('-', $selected);
$days = explode(' ', $date[2]);
$day = $days[0];
$month = $date[1];
$year = $date[0];
} else {
$days[1] = $selected;
}
if ($timeFormat != 'NONE' && !empty($timeFormat)) {
$time = explode(':', $days[1]);
$check = str_replace(':', '', $days[1]);
if (($check > 115959) && $timeFormat == '12') {
$time[0] = $time[0] - 12;
$meridian = 'pm';
} elseif($time[0] > 12) {
$meridian = 'pm';
}
$hour = $time[0];
$min = $time[1];
}
}
$elements = array('Day','Month','Year','Hour','Minute','Meridian');
if (isset($selectAttr['id'])) {
if (is_string($selectAttr['id'])) {
// build out an array version
foreach ($elements as $element) {
$selectAttrName = 'select' . $element . 'Attr';
${$selectAttrName} = $selectAttr;
${$selectAttrName}['id'] = $selectAttr['id'] . $element;
}
} elseif (is_array($selectAttr['id'])) {
// check for missing ones and build selectAttr for each element
foreach ($elements as $element) {
$selectAttrName = 'select' . $element . 'Attr';
${$selectAttrName} = $selectAttr;
${$selectAttrName}['id'] = $selectAttr['id'][strtolower($element)];
}
}
} else {
// build the selectAttrName with empty id's to pass
foreach ($elements as $element) {
$selectAttrName = 'select' . $element . 'Attr';
${$selectAttrName} = $selectAttr;
}
}
switch($dateFormat) {
case 'DMY': // so uses the new selex
$opt = $this->day($tagName, $day, $selectDayAttr, $optionAttr, $showEmpty) . '-' .
$this->month($tagName, $month, $selectMonthAttr, $optionAttr, $showEmpty) . '-' . $this->year($tagName, null, null, $year, $selectYearAttr, $optionAttr, $showEmpty);
break;
case 'MDY':
$opt = $this->month($tagName, $month, $selectMonthAttr, $optionAttr, $showEmpty) . '-' .
$this->day($tagName, $day, $selectDayAttr, $optionAttr, $showEmpty) . '-' . $this->year($tagName, null, null, $year, $selectYearAttr, $optionAttr, $showEmpty);
break;
case 'YMD':
$opt = $this->year($tagName, null, null, $year, $selectYearAttr, $optionAttr, $showEmpty) . '-' .
$this->month($tagName, $month, $selectMonthAttr, $optionAttr, $showEmpty) . '-' .
$this->day($tagName, $day, $selectDayAttr, $optionAttr, $showEmpty);
break;
case 'Y':
$opt = $this->year($tagName, null, null, $selected, $selectYearAttr, $optionAttr, $showEmpty);
break;
case 'NONE':
default:
$opt = '';
break;
}
switch($timeFormat) {
case '24':
$opt .= $this->hour($tagName, true, $hour, $selectHourAttr, $optionAttr, $showEmpty) . ':' .
$this->minute($tagName, $min, $selectMinuteAttr, $optionAttr, $showEmpty);
break;
case '12':
$opt .= $this->hour($tagName, false, $hour, $selectHourAttr, $optionAttr, $showEmpty) . ':' .
$this->minute($tagName, $min, $selectMinuteAttr, $optionAttr, $showEmpty) . ' ' .
$this->meridian($tagName, $meridian, $selectMeridianAttr, $optionAttr, $showEmpty);
break;
case 'NONE':
default:
$opt .= '';
break;
}
return $opt;
}
/**
* Returns an array of formatted OPTION/OPTGROUP elements
*
* @return array
*/
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null) {
$select = array();
foreach($elements as $name => $title) {
$htmlOptions = array();
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
if (!empty($name)) {
$select[] = $this->Html->tags['optiongroupend'];
$parents[] = $name;
}
$select = am($select, $this->__selectOptions($title, $selected, $parents, $showParents));
if (!empty($name)) {
$select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
}
$name = null;
} elseif (is_array($title)) {
$htmlOptions = $title;
$name = $title['value'];
$title = $title['name'];
unset($htmlOptions['name'], $htmlOptions['value']);
}
if ($name !== null) {
if (($selected !== null) && ($selected == $name)) {
$htmlOptions['selected'] = 'selected';
} else if(is_array($selected) && in_array($name, $selected)) {
$htmlOptions['selected'] = 'selected';
}
if($showParents || (!in_array($title, $parents))) {
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->parseHtmlOptions($htmlOptions), h($title));
}
}
}
return array_reverse($select, true);
}
/**
* Generates option lists for common <select /> menus
*
* @return void
*/
function __generateOptions($name) {
if (!empty($this->options[$name])) {
return $this->options[$name];
}
$data = array();
switch ($name) {
case 'minute':
for($i = 0; $i < 60; $i++) {
$data[$i] = sprintf('%02d', $i);
}
break;
case 'hour':
for($i = 0; $i < 31; $i++) {
$data[sprintf('%02d', $i)] = $i;
}
break;
case 'meridian':
$data = array('am' => 'am', 'pm' => 'pm');
break;
case 'day':
for($i = 0; $i < 31; $i++) {
$data[sprintf('%02d', $i)] = $i;
}
break;
case 'month':
break;
case 'year':
$current = intval(date('Y'));
for ($i = ($current - 20); $i < ($current + 20); $i++) {
$data[$i] = $i;
}
break;
}
$this->__options[$name] = $data;
return $this->__options[$name];
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateInputDiv($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null) {
trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::text() instead'), E_USER_WARNING);
$htmlOptions['id'] = strtolower(str_replace('/', '_', $tagName));
$htmlAttributes = $htmlOptions;
$htmlAttributes['size'] = $size;
$str = $this->Html->input($tagName, $htmlAttributes);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
return $this->divTag($divClass, $divTagInside);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateCheckboxDiv($tagName, $prompt, $required = false, $errorMsg = null, $htmlOptions = null) {
trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::checkbox() instead'), E_USER_WARNING);
$htmlOptions['class'] = "inputCheckbox";
$htmlOptions['id'] = strtolower(str_replace('/', '_', $tagName));
$str = $this->Html->checkbox($tagName, null, $htmlOptions);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
return $this->divTag($divClass, $divTagInside);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateDate($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) {
trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING);
$htmlOptions['id']=strtolower(str_replace('/', '_', $tagName));
$str = $this->dateTime($tagName, 'MDY', 'NONE', $selected, $htmlOptions);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
$requiredDiv = $this->divTag($divClass, $divTagInside);
return $this->divTag("date", $requiredDiv);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) {
trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING);
$str = $this->dateTime($tagName, 'NONE', '24', $selected, $htmlOptions);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
$requiredDiv = $this->divTag($divClass, $divTagInside);
return $this->divTag("time", $requiredDiv);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateDateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) {
trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING);
$htmlOptions['id']=strtolower(str_replace('/', '_', $tagName));
$str = $this->dateTime($tagName, 'MDY', '12', $selected, $htmlOptions, null, false);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
$requiredDiv = $this->divTag($divClass, $divTagInside);
return $this->divTag("date", $requiredDiv);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateAreaDiv($tagName, $prompt, $required = false, $errorMsg = null, $cols = 60, $rows = 10, $htmlOptions = null) {
trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING);
$htmlOptions['id'] = strtolower(str_replace('/', '_', $tagName));
$htmlAttributes = $htmlOptions;
$htmlAttributes['cols'] = $cols;
$htmlAttributes['rows'] = $rows;
$str = $this->Html->textarea($tagName, $htmlAttributes);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass="required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
return $this->divTag($divClass, $divTagInside);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateSelectDiv($tagName, $prompt, $options, $selected = null, $selectAttr = null, $optionAttr = null, $required = false, $errorMsg = null) {
trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::select() instead'), E_USER_WARNING);
$selectAttr['id'] = strtolower(str_replace('/', '_', $tagName));
$str = $this->Html->selectTag($tagName, $options, $selected, $selectAttr, $optionAttr);
$strLabel = $this->label($tagName, $prompt);
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
if ($this->isFieldError($tagName)) {
$strError = $this->Html->para('error', $errorMsg);
$divClass = sprintf("%s error", $divClass);
}
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
return $this->divTag($divClass, $divTagInside);
}
/**
* @deprecated
* @see FormHelper::input()
*/
function generateSubmitDiv($displayText, $htmlOptions = null) {
trigger_error(__('Deprecated: Use FormHelper::submit() instead'), E_USER_WARNING);
return $this->divTag('submit', $this->Html->submit($displayText, $htmlOptions));
}
/**
* @deprecated
* @see FormHelper::inputs()
*/
function generateFields($fields, $readOnly = false) {
trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING);
$strFormFields = '';
foreach($fields as $field) {
if (isset($field['type'])) {
if (!isset($field['required'])) {
$field['required'] = false;
}
if (!isset($field['errorMsg'])) {
$field['errorMsg'] = null;
}
if (!isset($field['htmlOptions'])) {
$field['htmlOptions'] = array();
}
if ($readOnly) {
$field['htmlOptions']['READONLY'] = "readonly";
}
switch($field['type']) {
case "input":
if (!isset($field['size'])) {
$field['size'] = 40;
}
$strFormFields = $strFormFields . $this->generateInputDiv($field['tagName'], $field['prompt'],
$field['required'], $field['errorMsg'], $field['size'], $field['htmlOptions']);
break;
case "checkbox":
$strFormFields = $strFormFields . $this->generateCheckboxDiv($field['tagName'], $field['prompt'],
$field['required'], $field['errorMsg'], $field['htmlOptions']);
break;
case "select":
case "selectMultiple":
if ("selectMultiple" == $field['type']) {
$field['selectAttr']['multiple'] = 'multiple';
$field['selectAttr']['class'] = 'selectMultiple';
}
if (!isset($field['selected'])) {
$field['selected'] = null;
}
if (!isset($field['selectAttr'])) {
$field['selectAttr'] = null;
}
if (!isset($field['optionsAttr'])) {
$field['optionsAttr'] = null;
}
if ($readOnly) {
$field['selectAttr']['DISABLED'] = true;
}
if (!isset($field['options'])) {
$field['options'] = null;
}
$strFormFields = $strFormFields . $this->generateSelectDiv($field['tagName'], $field['prompt'], $field['options'],
$field['selected'], $field['selectAttr'], $field['optionsAttr'], $field['required'], $field['errorMsg']);
break;
case "area":
if (!isset($field['rows'])) {
$field['rows'] = 10;
}
if (!isset($field['cols'])) {
$field['cols'] = 60;
}
$strFormFields = $strFormFields . $this->generateAreaDiv($field['tagName'], $field['prompt'],
$field['required'], $field['errorMsg'], $field['cols'], $field['rows'], $field['htmlOptions']);
break;
case "fieldset":
$strFieldsetFields = $this->generateFields($field['fields']);
$strFieldSet = sprintf(' <fieldset><legend>%s</legend><div class="notes"><h4>%s</h4><p class="last">%s</p></div>%s</fieldset>',
$field['legend'], $field['noteHeading'], $field['note'], $strFieldsetFields);
$strFormFields = $strFormFields . $strFieldSet;
break;
case "hidden":
if(!isset($field['value'])){
$field['value'] = null;
}
$strFormFields = $strFormFields . $this->Html->hidden($field['tagName'], $field['value']);
break;
case "date":
if (!isset($field['selected'])) {
$field['selected'] = null;
}
$strFormFields = $strFormFields . $this->generateDate($field['tagName'], $field['prompt'], null,
null, null, null, $field['selected']);
break;
case "datetime":
if (!isset($field['selected'])) {
$field['selected'] = null;
}
$strFormFields = $strFormFields . $this->generateDateTime($field['tagName'], $field['prompt'], '', '', '', '', $field['selected']);
break;
case "time":
if (!isset($field['selected'])) {
$field['selected'] = null;
}
$strFormFields = $strFormFields . $this->generateTime($field['tagName'], $field['prompt'], '', '', '', '', $field['selected']);
break;
default:
break;
}
}
}
return $strFormFields;
}
/**
* @deprecated will not be available after 1.1.x.x
* @see FormHelper::label()
*/
function labelTag($tagName, $text) {
trigger_error(__('Deprecated: Use FormHelper::label() instead'), E_USER_WARNING);
return sprintf($this->Html->tags['label'], Inflector::camelize(r('/', '_', $tagName)), $text);
}
/**
* @deprecated
* @see HtmlHelper::div
*/
function divTag($class, $text) {
//trigger_error(__('(FormHelper::divTag) Deprecated: Use HtmlHelper::div instead'), E_USER_WARNING);
return sprintf(TAG_DIV, $class, $text);
}
/**
* @deprecated
* @see HtmlHelper::para
*/
function pTag($class, $text) {
//trigger_error(__('(FormHelper::pTag) Deprecated: Use HtmlHelper::para instead'), E_USER_WARNING);
return sprintf(TAG_P_CLASS, $class, $text);
}
}
?>