Skip to content

Commit

Permalink
Refactoring out common code blocks into a separate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 20, 2009
1 parent ae76c44 commit 34fb1cc
Showing 1 changed file with 32 additions and 60 deletions.
92 changes: 32 additions & 60 deletions cake/libs/view/helpers/form.php
Expand Up @@ -1313,20 +1313,7 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
*/ */
function day($fieldName, $selected = null, $attributes = array()) { function day($fieldName, $selected = null, $attributes = array()) {
$attributes += array('empty' => true); $attributes += array('empty' => true);
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { $selected = $this->__dateTimeSelected('day', $fieldName, $selected, $attributes);
if (is_array($value)) {
extract($value);
$selected = $day;
} else {
if (empty($value)) {
if (!$attributes['empty']) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}


if (strlen($selected) > 2) { if (strlen($selected) > 2) {
$selected = date('d', strtotime($selected)); $selected = date('d', strtotime($selected));
Expand Down Expand Up @@ -1399,20 +1386,7 @@ function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $a
*/ */
function month($fieldName, $selected = null, $attributes = array()) { function month($fieldName, $selected = null, $attributes = array()) {
$attributes += array('empty' => true); $attributes += array('empty' => true);
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { $selected = $this->__dateTimeSelected('month', $fieldName, $selected, $attributes);
if (is_array($value)) {
extract($value);
$selected = $month;
} else {
if (empty($value)) {
if (!$attributes['empty']) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}


if (strlen($selected) > 2) { if (strlen($selected) > 2) {
$selected = date('m', strtotime($selected)); $selected = date('m', strtotime($selected));
Expand Down Expand Up @@ -1447,28 +1421,13 @@ function month($fieldName, $selected = null, $attributes = array()) {
*/ */
function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array()) { function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array()) {
$attributes += array('empty' => true); $attributes += array('empty' => true);
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { $selected = $this->__dateTimeSelected('hour', $fieldName, $selected, $attributes);
if (is_array($value)) {
extract($value);
$selected = $hour;
} else {
if (empty($value)) {
if (!$showEmpty) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
} else {
$value = $selected;
}


if (strlen($selected) > 2) { if (strlen($selected) > 2) {
if ($format24Hours) { if ($format24Hours) {
$selected = date('H', strtotime($value)); $selected = date('H', strtotime($selected));
} else { } else {
$selected = date('g', strtotime($value)); $selected = date('g', strtotime($selected));
} }
} elseif ($selected === false) { } elseif ($selected === false) {
$selected = null; $selected = null;
Expand All @@ -1495,20 +1454,7 @@ function hour($fieldName, $format24Hours = false, $selected = null, $attributes
*/ */
function minute($fieldName, $selected = null, $attributes = array()) { function minute($fieldName, $selected = null, $attributes = array()) {
$attributes += array('empty' => true); $attributes += array('empty' => true);
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { $selected = $this->__dateTimeSelected('minute', $fieldName, $selected, $attributes);
if (is_array($value)) {
extract($value);
$selected = $min;
} else {
if (empty($value)) {
if (!$attributes['empty']) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}


if (strlen($selected) > 2) { if (strlen($selected) > 2) {
$selected = date('i', strtotime($selected)); $selected = date('i', strtotime($selected));
Expand All @@ -1527,6 +1473,32 @@ function minute($fieldName, $selected = null, $attributes = array()) {
); );
} }


/**
* Selects values for dateTime selects.
*
* @param string $select Name of element field. ex. 'day'
* @param string $fieldName Name of fieldName being generated ex. Model.created
* @param mixed $selected The current selected value.
* @param array $attributes Array of attributes, must contain 'empty' key.
* @return string Currently selected value.
*/
function __dateTimeSelected($select, $fieldName, $selected, $attributes) {
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
if (is_array($value) && isset($value[$select])) {
$selected = $value[$select];
} else {
if (empty($value)) {
if (!$attributes['empty']) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}
return $selected;
}

/** /**
* Returns a SELECT element for AM or PM. * Returns a SELECT element for AM or PM.
* *
Expand Down

0 comments on commit 34fb1cc

Please sign in to comment.