Development

Changeset 5950

You must first sign up to be able to contribute.

Changeset 5950

Show
Ignore:
Timestamp:
11/10/07 15:20:39 (2 years ago)
Author:
fabien
Message:

added date as an array as a supported format for date/time widgets

Files:

Legend:

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

    r5937 r5950  
    4646  { 
    4747    // convert value to a timestamp 
    48     $value = ctype_digit($value) ? (integer) $value : strtotime($value); 
     48    if (is_array($value)) 
     49    { 
     50      $value = $this->convertDateArrayToTimestamp($value); 
     51    } 
     52    else 
     53    { 
     54      $value = ctype_digit($value) ? (integer) $value : strtotime($value); 
     55    } 
    4956 
    5057    $date = array(); 
     
    6471    return implode($this->getOption('separator'), $date); 
    6572  } 
     73 
     74  /** 
     75   * Converts an array representing a date to a timestamp. 
     76   * 
     77   * The array can contains the following keys: year, month, day, hour, minute, second 
     78   * 
     79   * @param  array   An array of date elements 
     80   * 
     81   * @return integer A timestamp 
     82   */ 
     83  protected function convertDateArrayToTimestamp($value) 
     84  { 
     85    $clean = mktime(isset($value['hour']) ? $value['hour'] : 0, isset($value['minute']) ? $value['minute'] : 0, isset($value['second']) ? $value['second'] : 0, $value['month'], $value['day'], $value['year']); 
     86 
     87    return false === $clean ? null : $clean; 
     88  } 
    6689} 
  • trunk/lib/widget/sfWidgetFormDateTime.class.php

    r5937 r5950  
    4949  function render($name, $value = null, $attributes = array(), $errors = array()) 
    5050  { 
    51     // convert value to a timestamp 
    52     $value = ctype_digit($value) ? (integer) $value : strtotime($value); 
    53  
    5451    // date 
    5552    $options = $this->getOptionsFor('date'); 
  • trunk/lib/widget/sfWidgetFormTime.class.php

    r5937 r5950  
    4747  { 
    4848    // convert value to a timestamp 
    49     $value = ctype_digit($value) ? (integer) $value : strtotime($value); 
     49    if (is_array($value)) 
     50    { 
     51      $value = $this->convertDateArrayToTimestamp($value); 
     52    } 
     53    else 
     54    { 
     55      $value = ctype_digit($value) ? (integer) $value : strtotime($value); 
     56    } 
    5057 
    5158    $time = array(); 
     
    6875    return implode($this->getOption('separator'), $time); 
    6976  } 
     77 
     78  /** 
     79   * Converts an array representing a date to a timestamp. 
     80   * 
     81   * The array can contains the following keys: hour, minute, second 
     82   * 
     83   * @param  array   An array of date elements 
     84   * 
     85   * @return integer A timestamp 
     86   */ 
     87  protected function convertDateArrayToTimestamp($value) 
     88  { 
     89    $clean = mktime(isset($value['hour']) ? $value['hour'] : 0, isset($value['minute']) ? $value['minute'] : 0, isset($value['second']) ? $value['second'] : 0); 
     90 
     91    return false === $clean ? null : $clean; 
     92  } 
    7093} 
  • trunk/test/unit/widget/sfWidgetFormDateTest.php

    r5937 r5950  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(19, new lime_output_color()); 
     13$t = new lime_test(22, new lime_output_color()); 
    1414 
    1515$w = new sfWidgetFormDate(); 
     
    3535  $t->is($css->matchSingle('#foo_day option[value="'.$values['day'].'"][selected="selected"]')->getValue(), $values['day'], '->render() renders a select tag for the day'); 
    3636} 
     37 
     38$values = array('year' => 2005, 'month' => 10, 'day' => 15); 
     39$dom->loadHTML($w->render('foo', $values)); 
     40$css = new sfDomCssSelector($dom); 
     41 
     42// selected date 
     43$t->is($css->matchSingle('#foo_year option[value="'.$values['year'].'"][selected="selected"]')->getValue(), $values['year'], '->render() renders a select tag for the year'); 
     44$t->is($css->matchSingle('#foo_month option[value="'.$values['month'].'"][selected="selected"]')->getValue(), $values['month'], '->render() renders a select tag for the month'); 
     45$t->is($css->matchSingle('#foo_day option[value="'.$values['day'].'"][selected="selected"]')->getValue(), $values['day'], '->render() renders a select tag for the day'); 
    3746 
    3847$dom->loadHTML($w->render('foo', '2005-10-15')); 
  • trunk/test/unit/widget/sfWidgetFormDateTimeTest.php

    r5938 r5950  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(34, new lime_output_color()); 
     13$t = new lime_test(40, new lime_output_color()); 
    1414 
    1515$dom = new DomDocument('1.0', 'utf-8'); 
     
    3838  $t->is($css->matchSingle('#foo_second option[value="'.$values['second'].'"][selected="selected"]')->getValue(), $values['second'], '->render() renders a select tag for the second'); 
    3939} 
     40 
     41$values = array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 30, 'second' => 35); 
     42$dom->loadHTML($w->render('foo', $values)); 
     43$css = new sfDomCssSelector($dom); 
     44 
     45// selected date / time 
     46$t->is($css->matchSingle('#foo_year option[value="'.$values['year'].'"][selected="selected"]')->getValue(), $values['year'], '->render() renders a select tag for the year'); 
     47$t->is($css->matchSingle('#foo_month option[value="'.$values['month'].'"][selected="selected"]')->getValue(), $values['month'], '->render() renders a select tag for the month'); 
     48$t->is($css->matchSingle('#foo_day option[value="'.$values['day'].'"][selected="selected"]')->getValue(), $values['day'], '->render() renders a select tag for the day'); 
     49$t->is($css->matchSingle('#foo_hour option[value="'.$values['hour'].'"][selected="selected"]')->getValue(), $values['hour'], '->render() renders a select tag for the hour'); 
     50$t->is($css->matchSingle('#foo_minute option[value="'.$values['minute'].'"][selected="selected"]')->getValue(), $values['minute'], '->render() renders a select tag for the minute'); 
     51$t->is($css->matchSingle('#foo_second option[value="'.$values['second'].'"][selected="selected"]')->getValue(), $values['second'], '->render() renders a select tag for the second'); 
    4052 
    4153$dom->loadHTML($w->render('foo', '2005-10-15 12:30:35')); 
  • trunk/test/unit/widget/sfWidgetFormTimeTest.php

    r5938 r5950  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(17, new lime_output_color()); 
     13$t = new lime_test(20, new lime_output_color()); 
    1414 
    1515$w = new sfWidgetFormTime(array('with_seconds' => true)); 
     
    3434  $t->is($css->matchSingle('#foo_second option[value="35"][selected="selected"]')->getValue(), 35, '->render() renders a select tag for the second'); 
    3535} 
     36 
     37$values = array('hour' => 12, 'minute' => '30', 'second' => 35); 
     38$dom->loadHTML($w->render('foo', $values)); 
     39$css = new sfDomCssSelector($dom); 
     40 
     41// selected date 
     42$t->is($css->matchSingle('#foo_hour option[value="12"][selected="selected"]')->getValue(), 12, '->render() renders a select tag for the hour'); 
     43$t->is($css->matchSingle('#foo_minute option[value="30"][selected="selected"]')->getValue(), 30, '->render() renders a select tag for the minute'); 
     44$t->is($css->matchSingle('#foo_second option[value="35"][selected="selected"]')->getValue(), 35, '->render() renders a select tag for the second'); 
    3645 
    3746$dom->loadHTML($w->render('foo', '12:30:35')); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.