This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/DateTimeField.php
2010-10-02 12:38:14 +02:00

135 lines
4.3 KiB
PHP

<?php
namespace Symfony\Component\Form;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Symfony\Component\Form\ValueTransformer\StringToDateTimeTransformer;
use Symfony\Component\Form\ValueTransformer\TimestampToDateTimeTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\ValueTransformer\ValueTransformerChain;
/**
* A field for editing a date and a time simultaneously
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class DateTimeField extends FieldGroup
{
const DATETIME = 'datetime';
const STRING = 'string';
const TIMESTAMP = 'timestamp';
protected static $types = array(
self::DATETIME,
self::STRING,
self::TIMESTAMP,
);
protected static $dateWidgets = array(
DateField::CHOICE,
DateField::INPUT,
);
protected static $timeWidgets = array(
TimeField::CHOICE,
TimeField::INPUT,
);
/**
* {@inheritDoc}
*/
public function configure()
{
$this->addOption('years', range(date('Y') - 5, date('Y') + 5));
$this->addOption('months', range(1, 12));
$this->addOption('days', range(1, 31));
$this->addOption('hours', range(0, 23));
$this->addOption('minutes', range(0, 59));
$this->addOption('seconds', range(0, 59));
$this->addOption('data_timezone', 'UTC');
$this->addOption('user_timezone', 'UTC');
$this->addOption('date_widget', DateField::INPUT, self::$dateWidgets);
$this->addOption('time_widget', TimeField::CHOICE, self::$timeWidgets);
$this->addOption('type', self::DATETIME, self::$types);
$this->addOption('with_seconds', false);
$this->add(new DateField('date', array(
'type' => DateField::RAW,
'widget' => $this->getOption('date_widget'),
'data_timezone' => $this->getOption('user_timezone'),
'user_timezone' => $this->getOption('user_timezone'),
'years' => $this->getOption('years'),
'months' => $this->getOption('months'),
'days' => $this->getOption('days'),
)));
$this->add(new TimeField('time', array(
'type' => TimeField::RAW,
'widget' => $this->getOption('time_widget'),
'data_timezone' => $this->getOption('user_timezone'),
'user_timezone' => $this->getOption('user_timezone'),
'with_seconds' => $this->getOption('with_seconds'),
'hours' => $this->getOption('hours'),
'minutes' => $this->getOption('minutes'),
'seconds' => $this->getOption('seconds'),
)));
$transformers = array();
if ($this->getOption('type') == self::STRING) {
$transformers[] = new StringToDateTimeTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
));
} else if ($this->getOption('type') == self::TIMESTAMP) {
$transformers[] = new TimestampToDateTimeTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
));
}
$transformers[] = new DateTimeToArrayTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('user_timezone'),
));
$this->setValueTransformer(new ValueTransformerChain($transformers));
}
/**
* {@inheritDoc}
*/
protected function transform($value)
{
$value = parent::transform($value);
return array('date' => $value, 'time' => $value);
}
/**
* {@inheritDoc}
*/
protected function reverseTransform($value)
{
return parent::reverseTransform(array_merge($value['date'], $value['time']));
}
/**
* {@inheritDoc}
*/
public function render(array $attributes = array())
{
$html = $this->get('date')->render($attributes)."\n";
$html .= $this->get('time')->render($attributes);
return $html;
}
}