form.php

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

PHP
1,711
字号
			$selected = null;		}		return $this->select($fieldName . ".hour", $this->__generateOptions($format24Hours ? 'hour24' : 'hour'), $selected, $attributes, $showEmpty);	}/** * Returns a SELECT element for minutes. * * @param string $fieldName Prefix name for the SELECT element * @param string $selected Option which is selected. * @param string $attributes Array of Attributes * @param bool $showEmpty True to show an empty element, or a string to provide default empty element text * @return string */	function minute($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {			if (is_array($value)) {				extract($value);				$selected = $min;			} else {				if (empty($value)) {					if (!$showEmpty) {						$selected = 'now';					}				} else {					$selected = $value;				}			}		}		if (strlen($selected) > 2) {			$selected = date('i', strtotime($selected));		} elseif ($selected === false) {			$selected = null;		}		$minuteOptions = array();		if (isset($attributes['interval'])) {			$minuteOptions['interval'] = $attributes['interval'];			unset($attributes['interval']);		}		return $this->select($fieldName . ".min", $this->__generateOptions('minute', $minuteOptions), $selected, $attributes, $showEmpty);	}/** * Returns a SELECT element for AM or PM. * * @param string $fieldName Prefix name for the SELECT element * @param string $selected Option which is selected. * @param string $attributes Array of Attributes * @param bool $showEmpty Show/Hide an empty option * @return string */	function meridian($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {			if (is_array($value)) {				extract($value);				$selected = $meridian;			} else {				if (empty($value)) {					if (!$showEmpty) {						$selected = date('a');					}				} else {					$selected = date('a', strtotime($value));				}			}		}		if ($selected === false) {			$selected = null;		}		return $this->select($fieldName . ".meridian", $this->__generateOptions('meridian'), $selected, $attributes, $showEmpty);	}/** * Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time. * * @param string $fieldName 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 string $attributes array of Attributes *			'monthNames' If set and false numbers will be used for month select instead of text. *			'minYear' The lowest year to use in the year select *			'maxYear' The maximum year to use in the year select *			'interval' The interval for the minutes select. Defaults to 1 *			'separator' The contents of the string between select elements. Defaults to '-' * @param bool $showEmpty Whether or not to show an empty default value. * @return string The HTML formatted OPTION element */	function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array(), $showEmpty = true) {		$year = $month = $day = $hour = $min = $meridian = null;		if (empty($selected)) {			$selected = $this->value($fieldName);		}		if ($selected === null && $showEmpty !== true) {			$selected = time();		}		if (!empty($selected)) {			if (is_array($selected)) {				extract($selected);			} else {				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] == '00' && $timeFormat == '12') {						$time[0] = 12;					} elseif ($time[0] > 12) {						$meridian = 'pm';					}					if ($time[0] == 0 && $timeFormat == '12') {						$time[0] = 12;					}					$hour = $time[0];					$min = $time[1];				}			}		}				$elements = array('Day','Month','Year','Hour','Minute','Meridian');		$defaults = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);		$attributes = array_merge($defaults, (array) $attributes);		if (isset($attributes['minuteInterval'])) {			$attributes['interval'] = $attributes['minuteInterval'];			unset($attributes['minuteInterval']);		}		$minYear = $attributes['minYear'];		$maxYear = $attributes['maxYear'];		$separator = $attributes['separator'];		$interval = $attributes['interval'];		$monthNames = $attributes['monthNames'];		$attributes = array_diff_key($attributes, $defaults);		if (isset($attributes['id'])) {			if (is_string($attributes['id'])) {				// build out an array version				foreach ($elements as $element) {					$selectAttrName = 'select' . $element . 'Attr';					${$selectAttrName}['id'] = $attributes['id'] . $element;				}			} elseif (is_array($attributes['id'])) {				// check for missing ones and build selectAttr for each element				foreach ($elements as $element) {					$selectAttrName = 'select' . $element . 'Attr';					${$selectAttrName} = $attributes;					${$selectAttrName}['id'] = $attributes['id'][strtolower($element)];				}			}		} else {			// build the selectAttrName with empty id's to pass			foreach ($elements as $element) {				$selectAttrName = 'select' . $element . 'Attr';				${$selectAttrName} = $attributes;			}		}		$opt = '';		if ($dateFormat != 'NONE') {			$selects = array();			foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {				switch ($char) {					case 'Y':						$selects[] = $this->year($fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty);					break;					case 'M':						$selectMonthAttr['monthNames'] = $monthNames;						$selects[] = $this->month($fieldName, $month, $selectMonthAttr, $showEmpty);					break;					case 'D':						$selects[] = $this->day($fieldName, $day, $selectDayAttr, $showEmpty);					break;				}			}			$opt = implode($separator, $selects);		}		switch($timeFormat) {			case '24':				$opt .= $this->hour($fieldName, true, $hour, $selectHourAttr, $showEmpty) . ':' .				$this->minute($fieldName, $min, $selectMinuteAttr, $showEmpty);			break;			case '12':				$selectMinuteAttr['interval'] = $interval;				$opt .= $this->hour($fieldName, false, $hour, $selectHourAttr, $showEmpty) . ':' .				$this->minute($fieldName, $min, $selectMinuteAttr, $showEmpty) . ' ' .				$this->meridian($fieldName, $meridian, $selectMeridianAttr, $showEmpty);			break;			case 'NONE':			default:				$opt .= '';			break;		}		return $opt;	}/** * Gets the input field name for the current tag * * @param array $options * @param string $key * @return array */	function __name($options = array(), $field = null, $key = 'name') {		if ($this->requestType == 'get') {			if ($options === null) {				$options = array();			} elseif (is_string($options)) {				$field = $options;				$options = 0;			}			if (!empty($field)) {				$this->setEntity($field);			}			if (is_array($options) && isset($options[$key])) {				return $options;			}			$name = $this->field();			if (is_array($options)) {				$options[$key] = $name;				return $options;			} else {				return $name;			}		}		return parent::__name($options, $field, $key);	}/** * Returns an array of formatted OPTION/OPTGROUP elements * @access private * @return array */	function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {		$select = array();		$attributes = array_merge(array('escape' => true, 'style' => null), $attributes);		$selectedIsEmpty = ($selected === '' || $selected === null);		$selectedIsArray = is_array($selected);		foreach ($elements as $name => $title) {			$htmlOptions = array();			if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {				if (!empty($name)) {					if ($attributes['style'] === 'checkbox') {						$select[] = $this->Html->tags['fieldsetend'];					} else{						$select[] = $this->Html->tags['optiongroupend'];					}					$parents[] = $name;				}				$select = array_merge($select, $this->__selectOptions($title, $selected, $parents, $showParents, $attributes));				if (!empty($name)) {					if ($attributes['style'] === 'checkbox') {						$select[] = sprintf($this->Html->tags['fieldsetstart'], $name);					} else{						$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 ((!$selectedIsEmpty && $selected == $name) || ($selectedIsArray && in_array($name, $selected))) {					if ($attributes['style'] === 'checkbox') {						$htmlOptions['checked'] = true;					} else {						$htmlOptions['selected'] = 'selected';					}				}				if ($showParents || (!in_array($title, $parents))) {					$title = ife($attributes['escape'], h($title), $title);					if ($attributes['style'] === 'checkbox') {						$htmlOptions['value'] = $name;						$tagName = Inflector::camelize($this->model() . '_' . $this->field() . '_'.Inflector::underscore($name));						$htmlOptions['id'] = $tagName;						$label = array('for' => $tagName);						if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {							$label['class'] = 'selected';						}						list($name) = array_values($this->__name());						if (empty($attributes['class'])) {							$attributes['class'] = 'checkbox';						}						$select[] = $this->Html->div($attributes['class'], sprintf($this->Html->tags['checkboxmultiple'], $name, $this->Html->_parseAttributes($htmlOptions)) . $this->label(null, $title, $label));					} else {						$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);					}				}			}		}		return array_reverse($select, true);	}/** * Generates option lists for common <select /> menus * @access private */	function __generateOptions($name, $options = array()) {		if (!empty($this->options[$name])) {			return $this->options[$name];		}		$data = array();		switch ($name) {			case 'minute':				if (isset($options['interval'])) {					$interval = $options['interval'];				} else {					$interval = 1;				}				$i = 0;				while ($i < 60) {					$data[$i] = sprintf('%02d', $i);					$i += $interval;				}			break;			case 'hour':				for ($i = 1; $i <= 12; $i++) {					$data[sprintf('%02d', $i)] = $i;				}			break;			case 'hour24':				for ($i = 0; $i <= 23; $i++) {					$data[sprintf('%02d', $i)] = $i;				}			break;			case 'meridian':				$data = array('am' => 'am', 'pm' => 'pm');			break;			case 'day':				if (!isset($options['min'])) {					$min = 1;				} else {					$min = $options['min'];				}				if (!isset($options['max'])) {					$max = 31;				} else {					$max = $options['max']; 				}				for ($i = $min; $i <= $max; $i++) {					$data[sprintf('%02d', $i)] = $i;				}			break;			case 'month':				if ($options['monthNames']) {					$data['01'] = __('January', true);					$data['02'] = __('February', true);					$data['03'] = __('March', true);					$data['04'] = __('April', true);					$data['05'] = __('May', true);					$data['06'] = __('June', true);					$data['07'] = __('July', true);					$data['08'] = __('August', true);					$data['09'] = __('September', true);					$data['10'] = __('October', true);					$data['11'] = __('November', true);					$data['12'] = __('December', true);				} else {					for ($m = 1; $m <= 12; $m++) {						$data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));					}				}			break;			case 'year':				$current = intval(date('Y'));				if (!isset($options['min'])) {					$min = $current - 20;				} else {					$min = $options['min'];				}				if (!isset($options['max'])) {					$max = $current + 20;				} else {					$max = $options['max'];				}				if ($min > $max) {					list($min, $max) = array($max, $min);				}				for ($i = $min; $i <= $max; $i++) {					$data[$i] = $i;				}				$data = array_reverse($data, true);			break;		}		$this->__options[$name] = $data;		return $this->__options[$name];	}}?>

⌨️ 快捷键说明

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