Development

Changeset 6327

You must first sign up to be able to contribute.

Changeset 6327

Show
Ignore:
Timestamp:
12/05/07 15:37:59 (1 year ago)
Author:
fabien
Message:

refactored date widgets

  • removed the separator option
  • added a format option (replace the separator option - format is much more flexible)
  • added can_be_empty and empty_values options
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/widget/sfWidgetFormDate.class.php

    r5952 r6327  
    2424   * Available options: 
    2525   * 
    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) 
    3032   * 
    3133   * @see sfWidgetForm 
     
    3335  protected function configure($options = array(), $attributes = array()) 
    3436  { 
    35     $this->addOption('separator', '/'); 
     37    $this->addOption('format', '%month%/%day%/%year%'); 
    3638    $this->addOption('days', array_combine(range(1, 31), range(1, 31))); 
    3739    $this->addOption('months', array_combine(range(1, 12), range(1, 12))); 
    3840    $years = range(date('Y') - 5, date('Y') + 5); 
    3941    $this->addOption('years', array_combine($years, $years)); 
     42 
     43    $this->addOption('can_be_empty', true); 
     44    $this->addOption('empty_values', array('year' => '', 'month' => '', 'day' => '')); 
    4045  } 
    4146 
     
    5661 
    5762    $date = array(); 
     63    $emptyValues = $this->getOption('empty_values'); 
    5864 
    5965    // 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) : ''); 
    6268 
    6369    // 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) : ''); 
    6672 
    6773    // 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) : ''); 
    7076 
    71     return implode($this->getOption('separator'), $date); 
     77    return strtr($this->getOption('format'), $date); 
    7278  } 
    7379 
  • trunk/lib/widget/sfWidgetFormDateTime.class.php

    r6196 r6327  
    1919class sfWidgetFormDateTime extends sfWidgetForm 
    2020{ 
     21  protected 
     22    $defaultAttributes = array('date' => array(), 'time' => array()); 
     23 
    2124  /** 
    2225   * Configures the current widget. 
     
    3235   *  * time:      Options for the time widget (see sfWidgetFormTime) 
    3336   *  * 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%
    3538   * 
    3639   * @see sfWidgetForm 
     
    4144    $this->addOption('time', array()); 
    4245    $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    } 
    4459  } 
    4560 
     
    5065  { 
    5166    // 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)); 
    5775 
    5876    // 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')); 
    6578 
    66       $html .= $this->getOption('separator').$time->render($name, $value); 
    67     } 
     79    $dateTime['%time%'] = $time->render($name, $value); 
    6880 
    69     return $html
     81    return strtr($this->getOption('format'), $dateTime)
    7082  } 
    7183 
     
    8193  } 
    8294 
    83   protected function getAttributesFor($options, $attributes
     95  protected function getAttributesFor($type
    8496  { 
    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]; 
    9298  } 
    9399} 
  • trunk/lib/widget/sfWidgetFormTime.class.php

    r5952 r6327  
    2424   * Available options: 
    2525   * 
    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) 
    3134   * 
    3235   * @see sfWidgetForm 
     
    3437  protected function configure($options = array(), $attributes = array()) 
    3538  { 
     39    $this->addOption('format', '%hour%:%minute%:%second%'); 
     40    $this->addOption('format_without_seconds', '%hour%:%minute%'); 
    3641    $this->addOption('with_seconds', false); 
    37     $this->addOption('separator', ':'); 
    3842    $this->addOption('hours', array_combine(range(0, 23), range(0, 23))); 
    3943    $this->addOption('minutes', array_combine(range(0, 59), range(0, 59))); 
    4044    $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' => '')); 
    4148  } 
    4249 
     
    5764 
    5865    $time = array(); 
     66    $emptyValues = $this->getOption('empty_values'); 
    5967 
    6068    // 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) : ''); 
    6371 
    6472    // 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) : ''); 
    6775 
    6876    if ($this->getOption('with_seconds')) 
    6977    { 
    7078      // 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) : ''); 
    7381    } 
    7482 
    75     return implode($this->getOption('separator'), $time); 
     83    return strtr($this->getOption('with_seconds') ? $this->getOption('format') : $this->getOption('format_without_seconds'), $time); 
    7684  } 
    7785 
  • trunk/test/unit/widget/sfWidgetFormDateTest.php

    r6216 r6327  
    5353$t->is(count($css->matchAll('#foo_day option')->getNodes()), 31, '->render() renders a select tag for the 31 days in a month'); 
    5454 
    55 // separator option 
    56 $t->diag('separator option'); 
     55// format option 
     56$t->diag('format option'); 
    5757$t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator'); 
    5858$t->is($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator'); 
    5959 
    60 $w->setOption('separator', '#'); 
     60$w->setOption('format', '%month%#%day%#%year%'); 
    6161$dom->loadHTML($w->render('foo', '2005-10-15')); 
    6262$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'); 
    6570 
    6671// days / months / years options 
  • trunk/test/unit/widget/sfWidgetFormDateTimeTest.php

    r6216 r6327  
    6262$t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute'); 
    6363 
    64 // date and time separator option 
    65 $t->diag('date and time separator option'); 
    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'); 
    7070 
    71 $t->diag('change date and time separator option'); 
    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)); 
    7474$dom->loadHTML($w->render('foo', '2005-10-15 12:30:35')); 
    7575$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'); 
    8080 
    8181// with_time option 
  • trunk/test/unit/widget/sfWidgetFormTimeTest.php

    r6216 r6327  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(20, new lime_output_color()); 
     13$t = new lime_test(23, new lime_output_color()); 
    1414 
    1515$w = new sfWidgetFormTime(array('with_seconds' => true)); 
     
    5252$t->is(count($css->matchAll('#foo_second option')->getNodes()), 60, '->render() renders a select tag for the 60 seconds in a minute'); 
    5353 
    54 // separator option 
    55 $t->diag('separator option'); 
     54// format option 
     55$t->diag('format option'); 
    5656$t->is($css->matchSingle('#foo_hour')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator'); 
    5757$t->is($css->matchSingle('#foo_minute')->getNode()->nextSibling->nodeValue, ':', '->render() renders 3 selects with a default : as a separator'); 
    5858 
    59 $w->setOption('separator', '#'); 
     59$w->setOption('format', '%hour%#%minute%#%second%'); 
    6060$dom->loadHTML($w->render('foo', '12:30:35')); 
    6161$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'); 
    6469 
    6570// hours / minutes / seconds options 
     
    8085$css = new sfDomCssSelector($dom); 
    8186$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');