From 34fb1cc683bb1f8bfb8989ce40944795d20c9ebe Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 19 Oct 2009 21:58:37 -0400 Subject: [PATCH] Refactoring out common code blocks into a separate method. --- cake/libs/view/helpers/form.php | 92 ++++++++++++--------------------- 1 file changed, 32 insertions(+), 60 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index b064833cc..51736d580 100755 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -1313,20 +1313,7 @@ class FormHelper extends AppHelper { */ function day($fieldName, $selected = null, $attributes = array()) { $attributes += array('empty' => true); - if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { - if (is_array($value)) { - extract($value); - $selected = $day; - } else { - if (empty($value)) { - if (!$attributes['empty']) { - $selected = 'now'; - } - } else { - $selected = $value; - } - } - } + $selected = $this->__dateTimeSelected('day', $fieldName, $selected, $attributes); if (strlen($selected) > 2) { $selected = date('d', strtotime($selected)); @@ -1399,20 +1386,7 @@ class FormHelper extends AppHelper { */ function month($fieldName, $selected = null, $attributes = array()) { $attributes += array('empty' => true); - if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { - if (is_array($value)) { - extract($value); - $selected = $month; - } else { - if (empty($value)) { - if (!$attributes['empty']) { - $selected = 'now'; - } - } else { - $selected = $value; - } - } - } + $selected = $this->__dateTimeSelected('month', $fieldName, $selected, $attributes); if (strlen($selected) > 2) { $selected = date('m', strtotime($selected)); @@ -1447,28 +1421,13 @@ class FormHelper extends AppHelper { */ function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array()) { $attributes += array('empty' => 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; - } - } - } else { - $value = $selected; - } + $selected = $this->__dateTimeSelected('hour', $fieldName, $selected, $attributes); if (strlen($selected) > 2) { if ($format24Hours) { - $selected = date('H', strtotime($value)); + $selected = date('H', strtotime($selected)); } else { - $selected = date('g', strtotime($value)); + $selected = date('g', strtotime($selected)); } } elseif ($selected === false) { $selected = null; @@ -1495,20 +1454,7 @@ class FormHelper extends AppHelper { */ function minute($fieldName, $selected = null, $attributes = array()) { $attributes += array('empty' => true); - if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { - if (is_array($value)) { - extract($value); - $selected = $min; - } else { - if (empty($value)) { - if (!$attributes['empty']) { - $selected = 'now'; - } - } else { - $selected = $value; - } - } - } + $selected = $this->__dateTimeSelected('minute', $fieldName, $selected, $attributes); if (strlen($selected) > 2) { $selected = date('i', strtotime($selected)); @@ -1527,6 +1473,32 @@ class FormHelper extends AppHelper { ); } +/** + * 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. *