Changeset 6327
- Timestamp:
- 12/05/07 15:37:59 (2 years ago)
- Files:
-
- trunk/lib/widget/sfWidgetFormDate.class.php (modified) (3 diffs)
- trunk/lib/widget/sfWidgetFormDateTime.class.php (modified) (5 diffs)
- trunk/lib/widget/sfWidgetFormTime.class.php (modified) (3 diffs)
- trunk/test/unit/widget/sfWidgetFormDateTest.php (modified) (1 diff)
- trunk/test/unit/widget/sfWidgetFormDateTimeTest.php (modified) (1 diff)
- trunk/test/unit/widget/sfWidgetFormTimeTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/widget/sfWidgetFormDate.class.php
r5952 r6327 24 24 * Available options: 25 25 * 26 * * separator: Date separator (/ by default) 27 * * years: An array of years for the year select tag (optional) 28 * * months: An array of months for the month select tag (optional) 29 * * days: An array of days for the day select tag (optional) 26 * * format: The date format string (%month%/%day%/%year% by default) 27 * * years: An array of years for the year select tag (optional) 28 * * months: An array of months for the month select tag (optional) 29 * * days: An array of days for the day select tag (optional) 30 * * can_be_empty: Whether the widget accept an empty value (true by default) 31 * * empty_values: An array of values to use for the empty value (empty string for year, month, and date by default) 30 32 * 31 33 * @see sfWidgetForm … … 33 35 protected function configure($options = array(), $attributes = array()) 34 36 { 35 $this->addOption(' separator', '/');37 $this->addOption('format', '%month%/%day%/%year%'); 36 38 $this->addOption('days', array_combine(range(1, 31), range(1, 31))); 37 39 $this->addOption('months', array_combine(range(1, 12), range(1, 12))); 38 40 $years = range(date('Y') - 5, date('Y') + 5); 39 41 $this->addOption('years', array_combine($years, $years)); 42 43 $this->addOption('can_be_empty', true); 44 $this->addOption('empty_values', array('year' => '', 'month' => '', 'day' => '')); 40 45 } 41 46 … … 56 61 57 62 $date = array(); 63 $emptyValues = $this->getOption('empty_values'); 58 64 59 65 // days 60 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption(' days')));61 $date[ ] = $widget->render($name.'[day]', $value ? date('j', $value) : '');66 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['day']) + $this->getOption('days') : $this->getOption('days'))); 67 $date['%day%'] = $widget->render($name.'[day]', $value ? date('j', $value) : ''); 62 68 63 69 // months 64 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption(' months')));65 $date[ ] = $widget->render($name.'[month]', $value ? date('n', $value) : '');70 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['month']) + $this->getOption('months') : $this->getOption('months'))); 71 $date['%month%'] = $widget->render($name.'[month]', $value ? date('n', $value) : ''); 66 72 67 73 // years 68 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption(' years')));69 $date[ ] = $widget->render($name.'[year]', $value ? date('Y', $value) : '');74 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['year']) + $this->getOption('years') : $this->getOption('years'))); 75 $date['%year%'] = $widget->render($name.'[year]', $value ? date('Y', $value) : ''); 70 76 71 return implode($this->getOption('separator'), $date);77 return strtr($this->getOption('format'), $date); 72 78 } 73 79 trunk/lib/widget/sfWidgetFormDateTime.class.php
r6196 r6327 19 19 class sfWidgetFormDateTime extends sfWidgetForm 20 20 { 21 protected 22 $defaultAttributes = array('date' => array(), 'time' => array()); 23 21 24 /** 22 25 * Configures the current widget. … … 32 35 * * time: Options for the time widget (see sfWidgetFormTime) 33 36 * * with_time: Whether to include time (true by default) 34 * * separator: Separator between the date and the time widget (a space by default)37 * * format: The format string for the date and the time widget (default to %date% %time%) 35 38 * 36 39 * @see sfWidgetForm … … 41 44 $this->addOption('time', array()); 42 45 $this->addOption('with_time', true); 43 $this->addOption('separator', ' '); 46 $this->addOption('format', '%date% %time%'); 47 48 if (isset($attributes['date'])) 49 { 50 $defaultAttributes['time'] = $attributes['date']; 51 unset($attributes['date']); 52 } 53 54 if (isset($attributes['time'])) 55 { 56 $defaultAttributes['time'] = $attributes['time']; 57 unset($attributes['time']); 58 } 44 59 } 45 60 … … 50 65 { 51 66 // date 52 $options = $this->getOptionsFor('date'); 53 $attributes = $this->getAttributesFor($options, $attributes); 54 unset($options['attributes']); 55 $date = new sfWidgetFormDate($options, $attributes); 56 $html = $date->render($name, $value); 67 $date = new sfWidgetFormDate($this->getOptionsFor('date'), $this->getAttributesFor('date')); 68 69 if (!$this->getOption('with_time')) 70 { 71 return $date->render($name, $value); 72 } 73 74 $dateTime = array('%date%' => $date->render($name, $value)); 57 75 58 76 // time 59 if ($this->getOption('with_time')) 60 { 61 $options = $this->getOptionsFor('time'); 62 $attributes = $this->getAttributesFor($options, $attributes); 63 unset($options['attributes']); 64 $time = new sfWidgetFormTime($options, $attributes); 77 $time = new sfWidgetFormTime($this->getOptionsFor('time'), $this->getAttributesFor('time')); 65 78 66 $html .= $this->getOption('separator').$time->render($name, $value); 67 } 79 $dateTime['%time%'] = $time->render($name, $value); 68 80 69 return $html;81 return strtr($this->getOption('format'), $dateTime); 70 82 } 71 83 … … 81 93 } 82 94 83 protected function getAttributesFor($ options, $attributes)95 protected function getAttributesFor($type) 84 96 { 85 $attributes = array_merge($this->attributes, $attributes); 86 if (isset($options['attributes'])) 87 { 88 $attributes = array_merge($attributes, $options['attributes']); 89 } 90 91 return $attributes; 97 return isset($attributes[$type]) ? array_merge($this->defaultAttributes[$type], $attributes[$type]) : $this->defaultAttributes[$type]; 92 98 } 93 99 } trunk/lib/widget/sfWidgetFormTime.class.php
r5952 r6327 24 24 * Available options: 25 25 * 26 * * with_second: Whether to include a select for seconds (false by default) 27 * * separator: Time separator (: by default) 28 * * hours: An array of hours for the hour select tag (optional) 29 * * minutes: An array of minutes for the minute select tag (optional) 30 * * seconds: An array of seconds for the second select tag (optional) 26 * * format: The time format string (%hour%:%minute%:%second%) 27 * * format_without_seconds: The time format string without seconds (%hour%:%minute%) 28 * * with_second: Whether to include a select for seconds (false by default) 29 * * hours: An array of hours for the hour select tag (optional) 30 * * minutes: An array of minutes for the minute select tag (optional) 31 * * seconds: An array of seconds for the second select tag (optional) 32 * * can_be_empty: Whether the widget accept an empty value (true by default) 33 * * empty_values: An array of values to use for the empty value (empty string for year, month, and date by default) 31 34 * 32 35 * @see sfWidgetForm … … 34 37 protected function configure($options = array(), $attributes = array()) 35 38 { 39 $this->addOption('format', '%hour%:%minute%:%second%'); 40 $this->addOption('format_without_seconds', '%hour%:%minute%'); 36 41 $this->addOption('with_seconds', false); 37 $this->addOption('separator', ':');38 42 $this->addOption('hours', array_combine(range(0, 23), range(0, 23))); 39 43 $this->addOption('minutes', array_combine(range(0, 59), range(0, 59))); 40 44 $this->addOption('seconds', array_combine(range(0, 59), range(0, 59))); 45 46 $this->addOption('can_be_empty', true); 47 $this->addOption('empty_values', array('hour' => '', 'minute' => '', 'second' => '')); 41 48 } 42 49 … … 57 64 58 65 $time = array(); 66 $emptyValues = $this->getOption('empty_values'); 59 67 60 68 // hours 61 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption(' hours')));62 $time[ ] = $widget->render($name.'[hour]', $value ? date('G', $value) : '');69 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['hour']) + $this->getOption('hours') : $this->getOption('hours'))); 70 $time['%hour%'] = $widget->render($name.'[hour]', $value ? date('G', $value) : ''); 63 71 64 72 // minutes 65 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption(' minutes')));66 $time[ ] = $widget->render($name.'[minute]', $value ? date('i', $value) : '');73 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['minute']) + $this->getOption('minutes') : $this->getOption('minutes'))); 74 $time['%minute%'] = $widget->render($name.'[minute]', $value ? date('i', $value) : ''); 67 75 68 76 if ($this->getOption('with_seconds')) 69 77 { 70 78 // seconds 71 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption(' seconds')));72 $time[ ] = $widget->render($name.'[second]', $value ? date('s', $value) : '');79 $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['second']) + $this->getOption('seconds') : $this->getOption('seconds'))); 80 $time['%second%'] = $widget->render($name.'[second]', $value ? date('s', $value) : ''); 73 81 } 74 82 75 return implode($this->getOption('separator'), $time);83 return strtr($this->getOption('with_seconds') ? $this->getOption('format') : $this->getOption('format_without_seconds'), $time); 76 84 } 77 85 trunk/test/unit/widget/sfWidgetFormDateTest.php
r6216 r6327 53 53 $t->is(count($css->matchAll('#foo_day option')->getNodes()), 31, '->render() renders a select tag for the 31 days in a month'); 54 54 55 // separatoroption56 $t->diag(' separatoroption');55 // format option 56 $t->diag('format option'); 57 57 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator'); 58 58 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator'); 59 59 60 $w->setOption(' separator', '#');60 $w->setOption('format', '%month%#%day%#%year%'); 61 61 $dom->loadHTML($w->render('foo', '2005-10-15')); 62 62 $css = new sfDomCssSelector($dom); 63 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default separator'); 64 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default separator'); 63 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default date format'); 64 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default date format'); 65 66 $w->setOption('format', '%day%/%month%/%year%'); 67 $dom->loadHTML($w->render('foo', '2005-10-15')); 68 $css = new sfDomCssSelector($dom); 69 $t->is($css->matchSingle('select')->getNode()->getAttribute('name'), 'foo[day]', '__construct() can change the default date format'); 65 70 66 71 // days / months / years options trunk/test/unit/widget/sfWidgetFormDateTimeTest.php
r6216 r6327 62 62 $t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute'); 63 63 64 // date and time separatoroption65 $t->diag('date and time separatoroption');66 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator');67 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator');68 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator');69 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator');64 // date and time format option 65 $t->diag('date and time format option'); 66 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a format'); 67 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a format'); 68 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a format'); 69 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a format'); 70 70 71 $t->diag('change date and time separatoroption');72 $w->setOption('date', array(' separator' => '-'));73 $w->setOption('time', array(' separator' => '!', 'with_seconds' => true));71 $t->diag('change date and time format option'); 72 $w->setOption('date', array('format' => '%month%-%day%-%year%')); 73 $w->setOption('time', array('format' => '%hour%!%minute%!%second%', 'with_seconds' => true)); 74 74 $dom->loadHTML($w->render('foo', '2005-10-15 12:30:35')); 75 75 $css = new sfDomCssSelector($dom); 76 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '-', '__construct() can change the default separator');77 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '-', '__construct() can change the default separator');78 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '!', '__construct() can change the default separator');79 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, '!', '__construct() can change the default separator');76 $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '-', '__construct() can change the default format'); 77 $t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '-', '__construct() can change the default format'); 78 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '!', '__construct() can change the default format'); 79 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, '!', '__construct() can change the default format'); 80 80 81 81 // with_time option trunk/test/unit/widget/sfWidgetFormTimeTest.php
r6216 r6327 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(2 0, new lime_output_color());13 $t = new lime_test(23, new lime_output_color()); 14 14 15 15 $w = new sfWidgetFormTime(array('with_seconds' => true)); … … 52 52 $t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute'); 53 53 54 // separatoroption55 $t->diag(' separatoroption');54 // format option 55 $t->diag('format option'); 56 56 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator'); 57 57 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator'); 58 58 59 $w->setOption(' separator', '#');59 $w->setOption('format', '%hour%#%minute%#%second%'); 60 60 $dom->loadHTML($w->render('foo', '12:30:35')); 61 61 $css = new sfDomCssSelector($dom); 62 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default separator'); 63 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default separator'); 62 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default format'); 63 $t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default format'); 64 65 $w->setOption('format', '%minute%#%hour%#%second%'); 66 $dom->loadHTML($w->render('foo', '12:30:35')); 67 $css = new sfDomCssSelector($dom); 68 $t->is($css->matchSingle('select')->getNode()->getAttribute('name'), 'foo[minute]', '__construct() can change the default time format'); 64 69 65 70 // hours / minutes / seconds options … … 80 85 $css = new sfDomCssSelector($dom); 81 86 $t->is(count($css->matchAll('#foo_second option')->getNodes()), 0, '__construct() can enable or disable the seconds select box with the with_seconds option'); 87 88 $w->setOption('format_without_seconds', '%hour%#%minute%'); 89 $dom->loadHTML($w->render('foo', '12:30:35')); 90 $css = new sfDomCssSelector($dom); 91 $t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default format'); 92 $t->ok(!count($css->matchSingle('#foo_second')->getNodes()), '__construct() can change the default format');

