form.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 1,711 行 · 第 1/4 页

PHP
1,711
字号
		$out = array();		foreach ($options as $optValue => $optTitle) {			$optionsHere = array('value' => $optValue);			if (isset($value) && $optValue == $value) {				$optionsHere['checked'] = 'checked';			}			$parsedOptions = $this->_parseAttributes(array_merge($attributes, $optionsHere), array('name', 'type', 'id'), '', ' ');			$tagName = Inflector::camelize($this->model() . '_' . $this->field() . '_'.Inflector::underscore($optValue));			if ($label) {				$optTitle =  sprintf($this->Html->tags['label'], $tagName, null, $optTitle);			}			$out[] =  sprintf($this->Html->tags['radio'], $attributes['name'], $tagName, $parsedOptions, $optTitle);		}		$hidden = null;		if (!isset($value) || $value === '') {			$hidden = $this->hidden($fieldName, array('value' => '', 'id' => $attributes['id'] . '_'), true);		}		$out = $hidden . join($inbetween, $out);		if ($legend) {			$out = sprintf($this->Html->tags['fieldset'], '', sprintf($this->Html->tags['legend'], $legend) . $out);		}		return $this->output($out);	}/** * Creates a text input widget. * * @param string $fieldNamem Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param array  $options Array of HTML attributes. * @return string An HTML text input element */	function text($fieldName, $options = array()) {		$options = $this->__initInputField($fieldName, array_merge(array('type' => 'text'), $options));		$this->__secure();		return $this->output(sprintf($this->Html->tags['input'], $options['name'], $this->_parseAttributes($options, array('name'), null, ' ')));	}/** * Creates a password input widget. * * @param  string  $fieldName Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param  array	$options Array of HTML attributes. * @return string */	function password($fieldName, $options = array()) {		$options = $this->__initInputField($fieldName, $options);		$this->__secure();		return $this->output(sprintf($this->Html->tags['password'], $options['name'], $this->_parseAttributes($options, array('name'), null, ' ')));	}/** * Creates a textarea widget. * * @param string $fieldNamem Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param array $options Array of HTML attributes. * @return string An HTML text input element */	function textarea($fieldName, $options = array()) {		$options = $this->__initInputField($fieldName, $options);		$this->__secure();		$value = null;		if (array_key_exists('value', $options)) {			$value = $options['value'];			if (!array_key_exists('escape', $options) || $options['escape'] !== false) {				$value = h($value);			}			unset($options['value']);		}		return $this->output(sprintf($this->Html->tags['textarea'], $options['name'], $this->_parseAttributes($options, array('type', 'name'), null, ' '), $value));	}/** * Creates a hidden input field. * * @param  string  $fieldName Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param  array	$options Array of HTML attributes. * @return string * @access public */	function hidden($fieldName, $options = array()) {		$options = $this->__initInputField($fieldName, $options);		$model = $this->model();		$value = '';		$key = '_' . $model;		if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {			$options['name'] = preg_replace("/$model/", $key, $options['name'], 1);		}		if (!empty($options['value']) || $options['value'] === '0') {			$value = $options['value'];		}		if (!in_array($fieldName, array('_method'))) {			$this->__secure($key, $value);		}		return $this->output(sprintf($this->Html->tags['hidden'], $options['name'], $this->_parseAttributes($options, array('name', 'class'), '', ' ')));	}/** * Creates file input widget. * * @param string $fieldName Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param array $options Array of HTML attributes. * @return string * @access public */	function file($fieldName, $options = array()) {		$options = $this->__initInputField($fieldName, $options);		$this->__secure();		return $this->output(sprintf($this->Html->tags['file'], $options['name'], $this->_parseAttributes($options, array('name'), '', ' ')));	}/** * Creates a button tag. * * @param  string  $title  The button's caption * @param  array  $options Array of options. * @return string A HTML button tag. * @access public */	function button($title, $options = array()) {		$options = array_merge(array('type' => 'button', 'value' => $title), $options);		if (isset($options['name']) && (strpos($options['name'], "/") !== false || strpos($options['name'], ".") !== false)) {			if ($this->value($options['name'])) {				$options['checked'] = 'checked';			}			$name = $options['name'];			unset($options['name']);			$options = $this->__initInputField($name, $options);		}		return $this->output(sprintf($this->Html->tags['button'], $options['type'], $this->_parseAttributes($options, array('type'), '', ' ')));	}/** * Creates a submit button element. * * @param  string  $caption	 The label appearing on the button OR * 					if string contains :// or the extension .jpg, .jpe, .jpeg, .gif, .png use an image *						if the extension exists, AND the first character is /, image is relative to webroot, *						OR if the first character is not /, image is relative to webroot/img, * @param  array   $options * @return string A HTML submit button */	function submit($caption = null, $options = array()) {		if (!$caption) {			$caption = __('Submit', true);		}		$secured = null;		if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {			$secured = $this->secure($this->fields);			$this->fields = array();		}		$div = true;		if (isset($options['div'])) {			$div = $options['div'];			unset($options['div']);		}		$divOptions = array('tag' => 'div');		if ($div === true) {			$divOptions['class'] = 'submit';		} elseif ($div === false) {			unset($divOptions);		} elseif (is_string($div)) {			$divOptions['class'] = $div;		} elseif (is_array($div)) {			$divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div);		}		$out = $secured;		if (strpos($caption, '://') !== false) {			$out .= $this->output(sprintf($this->Html->tags['submitimage'], $caption, $this->_parseAttributes($options, null, '', ' ')));		} elseif (preg_match('/\.(jpg|jpe|jpeg|gif|png)$/', $caption)) {			if ($caption{0} !== '/') {				$url = $this->webroot(IMAGES_URL . $caption);			} else {				$caption = trim($caption, '/');				$url = $this->webroot($caption);			}			$out .= $this->output(sprintf($this->Html->tags['submitimage'], $url, $this->_parseAttributes($options, null, '', ' ')));		} else {			$options['value'] = $caption;			$out .= $this->output(sprintf($this->Html->tags['submit'], $this->_parseAttributes($options, null, '', ' ')));		}		if (isset($divOptions)) {		    $tag = $divOptions['tag'];		    unset($divOptions['tag']);			$out = $this->Html->tag($tag, $out, $divOptions);		}		return $out;	}/** * @deprecated */	function submitImage($path, $options = array()) {		trigger_error("FormHelper::submitImage() is deprecated. Use \$form->submit('path/to/img.gif')", E_USER_WARNING);		if (strpos($path, '://')) {			$url = $path;		} else {			$url = $this->webroot(IMAGES_URL . $path);		}		return $this->output(sprintf($this->Html->tags['submitimage'], $url, $this->_parseAttributes($options, 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 = '') {		$select = array();		$showParents = false;		$escapeOptions = true;		$style = null;		$tag = null;		if (isset($attributes['escape'])) {			$escapeOptions = $attributes['escape'];			unset($attributes['escape']);		}		$attributes = $this->__initInputField($fieldName, $attributes);		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 = $attributes['value'];		}		if (isset($attributes) && array_key_exists('multiple', $attributes)) {			if ($attributes['multiple'] === 'checkbox') {				$tag = $this->Html->tags['checkboxmultiplestart'];				$style = 'checkbox';			} else {				$tag = $this->Html->tags['selectmultiplestart'];			}			$select[] = $this->hidden(null, array('value' => '', 'id' => null));		} else {			$tag = $this->Html->tags['selectstart'];		}		if (!empty($tag)) {			$this->__secure();			$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes($attributes, array('name', 'value')));		}		if ($showEmpty !== null && $showEmpty !== false && !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))) {			if ($showEmpty === true) {				$showEmpty = '';			}			$options = array_reverse($options, true);			$options[''] = $showEmpty;			$options = array_reverse($options, true);		}		$select = array_merge($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents, array('escape' => $escapeOptions, 'style' => $style)));		if ($style == 'checkbox') {			$select[] = $this->Html->tags['checkboxmultipleend'];		} else {			$select[] = $this->Html->tags['selectend'];		}		return $this->output(implode("\n", $select));	}/** * Returns a SELECT element for days. * * @param string $fieldName Prefix name for the SELECT element * @param string $selected Option which is selected. * @param array	 $attributes HTML attributes for the select element * @param mixed $showEmpty Show/hide the empty select option * @return string */	function day($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {			if (is_array($value)) {				extract($value);				$selected = $day;			} else {				if (empty($value)) {					if (!$showEmpty) {						$selected = 'now';					}				} else {					$selected = $value;				}			}		}		if (strlen($selected) > 2) {			$selected = date('d', strtotime($selected));		} elseif ($selected === false) {			$selected = null;		}		return $this->select($fieldName . ".day", $this->__generateOptions('day'), $selected, $attributes, $showEmpty);	}/** * Returns a SELECT element for years * * @param string $fieldName 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 $attributes Attribute array for the select elements. * @param boolean $showEmpty Show/hide the empty select option * @return string */	function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array(), $showEmpty = true) {		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {			if (is_array($value)) {				extract($value);				$selected = $year;			} else {				if (empty($value)) {					if (!$showEmpty && !$maxYear) {						$selected = 'now';					} elseif (!$showEmpty && $maxYear && !$selected) {						$selected = $maxYear;					}				} else {					$selected = $value;				}			}		}		if (strlen($selected) > 4 || $selected === 'now') {			$selected = date('Y', strtotime($selected));		} elseif ($selected === false) {			$selected = null;		}		$yearOptions = array('min' => $minYear, 'max' => $maxYear);		return $this->select($fieldName . ".year", $this->__generateOptions('year', $yearOptions), $selected, $attributes, $showEmpty);	}/** * Returns a SELECT element for months. * * @param string $fieldName Prefix name for the SELECT element * @param string $selected Option which is selected. * @param array $attributes Attributes for the select element *							If 'monthNames' is set and false 2 digit numbers will be used instead of text. * * @param boolean $showEmpty Show/hide the empty select option * @return string */	function month($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {			if (is_array($value)) {				extract($value);				$selected = $month;			} else {				if (empty($value)) {					if (!$showEmpty) {						$selected = 'now';					}				} else {					$selected = $value;				}			}		}				if (strlen($selected) > 2) {			$selected = date('m', strtotime($selected));		} elseif ($selected === false) {			$selected = null;		}		$defaults = array('monthNames' => true);		$attributes = array_merge($defaults, (array) $attributes);		$monthNames = $attributes['monthNames'];		unset($attributes['monthNames']);				return $this->select($fieldName . ".month", $this->__generateOptions('month', array('monthNames' => $monthNames)), $selected, $attributes, $showEmpty);	}/** * Returns a SELECT element for hours. * * @param string $fieldName Prefix name for the SELECT element * @param boolean $format24Hours True for 24 hours format * @param string $selected Option which is selected. * @param array $attributes List of HTML attributes * @param mixed $showEmpty True to show an empty element, or a string to provide default empty element text * @return string */	function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array(), $showEmpty = true) {		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {			if (is_array($value)) {				extract($value);				$selected = $hour;			} else {				if (empty($value)) {					if (!$showEmpty) {						$selected = 'now';					}				} else {					$selected = $value;				}			}		}		if (strlen($selected) > 2) {			if ($format24Hours) {				$selected = date('H', strtotime($value));			} else {				$selected = date('g', strtotime($value));			}		} elseif ($selected === false) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?