2011-02-19 12:52:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
|
|
|
use Symfony\Component\Form\ValueTransformer\BooleanToStringTransformer;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\ChoiceList\DefaultChoiceList;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\ChoiceList\PaddedChoiceList;
|
|
|
|
use Symfony\Component\Form\ChoiceList\MonthChoiceList;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\DataProcessor\RadioToArrayConverter;
|
2011-02-19 12:52:37 +00:00
|
|
|
use Symfony\Component\Form\Renderer\DefaultRenderer;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
|
2011-02-19 12:52:37 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\IdPlugin;
|
|
|
|
use Symfony\Component\Form\Renderer\Plugin\NamePlugin;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\ParameterPlugin;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\ChoicePlugin;
|
|
|
|
use Symfony\Component\Form\Renderer\Plugin\ParentNamePlugin;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\DatePatternPlugin;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\ValueTransformer\ScalarToChoicesTransformer;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
|
2011-02-19 12:52:37 +00:00
|
|
|
|
|
|
|
class FormFactory
|
|
|
|
{
|
|
|
|
private $theme;
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
public function __construct(ThemeInterface $theme)
|
|
|
|
{
|
|
|
|
$this->setTheme($theme);
|
|
|
|
}
|
|
|
|
|
2011-02-19 12:52:37 +00:00
|
|
|
public function setTheme(ThemeInterface $theme)
|
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTheme()
|
|
|
|
{
|
|
|
|
return $this->theme;
|
|
|
|
}
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
protected function getField($key, $template)
|
|
|
|
{
|
|
|
|
$field = new Field($key);
|
|
|
|
|
|
|
|
return $field
|
|
|
|
->setRenderer(new DefaultRenderer($this->theme, $template))
|
|
|
|
->addRendererPlugin(new IdPlugin($field))
|
|
|
|
->addRendererPlugin(new NamePlugin($field));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getForm($key, $template)
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-02-19 14:26:07 +00:00
|
|
|
$field = new Form($key);
|
2011-02-19 12:52:37 +00:00
|
|
|
|
|
|
|
return $field
|
|
|
|
->setRenderer(new DefaultRenderer($this->theme, $template))
|
|
|
|
->addRendererPlugin(new IdPlugin($field))
|
|
|
|
->addRendererPlugin(new NamePlugin($field));
|
|
|
|
}
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
public function getCheckboxField($key, array $options = array())
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-02-19 16:05:14 +00:00
|
|
|
$options = array_merge(array(
|
2011-02-19 14:26:07 +00:00
|
|
|
'value' => '1',
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getField($key, 'checkbox')
|
2011-02-19 12:52:37 +00:00
|
|
|
->setValueTransformer(new BooleanToStringTransformer())
|
2011-02-19 16:05:14 +00:00
|
|
|
->addRendererPlugin(new ParameterPlugin('value', $options['value']));
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
public function getRadioField($key, array $options = array())
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-02-19 16:05:14 +00:00
|
|
|
$options = array_merge(array(
|
2011-02-19 14:26:07 +00:00
|
|
|
'value' => null,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$field = $this->getField($key, 'radio');
|
2011-02-19 12:52:37 +00:00
|
|
|
|
|
|
|
return $field
|
|
|
|
->setValueTransformer(new BooleanToStringTransformer())
|
2011-02-19 14:26:07 +00:00
|
|
|
->addRendererPlugin(new ParentNamePlugin($field))
|
2011-02-19 16:05:14 +00:00
|
|
|
->addRendererPlugin(new ParameterPlugin('value', $options['value']));
|
2011-02-19 14:26:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
protected function getChoiceFieldForList($key, ChoiceListInterface $choiceList, array $options = array())
|
2011-02-19 14:26:07 +00:00
|
|
|
{
|
2011-02-19 16:05:14 +00:00
|
|
|
$options = array_merge(array(
|
2011-02-19 14:26:07 +00:00
|
|
|
'multiple' => false,
|
|
|
|
'expanded' => false,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if (!$options['expanded']) {
|
|
|
|
$field = $this->getField($key, 'choice');
|
|
|
|
} else {
|
2011-02-19 16:05:14 +00:00
|
|
|
$field = $this->getForm($key, 'choice');
|
2011-02-19 14:26:07 +00:00
|
|
|
$choices = array_merge($choiceList->getPreferredChoices(), $choiceList->getOtherChoices());
|
|
|
|
|
|
|
|
foreach ($choices as $choice => $value) {
|
|
|
|
if ($options['multiple']) {
|
|
|
|
$field->add($this->getCheckboxField($choice, array(
|
|
|
|
'value' => $choice,
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
$field->add($this->getRadioField($choice, array(
|
|
|
|
'value' => $choice,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
$field->addRendererPlugin(new ChoicePlugin($choiceList))
|
|
|
|
->addRendererPlugin(new ParameterPlugin('multiple', $options['multiple']))
|
|
|
|
->addRendererPlugin(new ParameterPlugin('expanded', $options['expanded']));
|
2011-02-19 14:26:07 +00:00
|
|
|
|
|
|
|
if ($options['multiple'] && $options['expanded']) {
|
|
|
|
$field->setValueTransformer(new ArrayToChoicesTransformer($choiceList));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$options['multiple'] && $options['expanded']) {
|
|
|
|
$field->setValueTransformer(new ScalarToChoicesTransformer($choiceList));
|
|
|
|
$field->setDataPreprocessor(new RadioToArrayConverter());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['multiple'] && !$options['expanded']) {
|
|
|
|
$field->addRendererPlugin(new SelectMultipleNamePlugin($field));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|
2011-02-19 16:05:14 +00:00
|
|
|
|
|
|
|
public function getChoiceField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'choices' => array(),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new DefaultChoiceList(
|
|
|
|
$options['choices'],
|
|
|
|
$options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDayField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'days' => range(1, 31),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['days'], 2, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMonthField($key, \IntlDateFormatter $formatter, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'months' => range(1, 12),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new MonthChoiceList(
|
|
|
|
$formatter, $options['months'], $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getYearField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'years' => range(date('Y') - 5, date('Y') + 5),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['years'], 4, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDateField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'widget' => 'choice',
|
|
|
|
'type' => 'datetime',
|
|
|
|
'pattern' => null,
|
|
|
|
'format' => \IntlDateFormatter::MEDIUM,
|
|
|
|
'data_timezone' => date_default_timezone_get(),
|
|
|
|
'user_timezone' => date_default_timezone_get(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$formatter = new \IntlDateFormatter(
|
|
|
|
\Locale::getDefault(),
|
|
|
|
$options['format'],
|
|
|
|
\IntlDateFormatter::NONE
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($options['widget'] === 'text') {
|
|
|
|
$field = $this->getField($key, 'date')
|
|
|
|
->setValueTransformer(new DateTimeToLocalizedStringTransformer(array(
|
|
|
|
'date_format' => $options['format'],
|
|
|
|
'time_format' => DateTimeToLocalizedStringTransformer::NONE,
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['user_timezone'],
|
|
|
|
)))
|
|
|
|
->addRendererPlugin(new ParameterPlugin('widget', 'text'));
|
|
|
|
} else {
|
|
|
|
$field = $this->getForm($key, 'date')
|
|
|
|
->add($this->getYearField('year', $options))
|
|
|
|
->add($this->getMonthField('month', $formatter, $options))
|
|
|
|
->add($this->getDayField('day', $options))
|
|
|
|
->setValueTransformer(new DateTimeToArrayTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['user_timezone'],
|
|
|
|
)))
|
|
|
|
->addRendererPlugin(new ParameterPlugin('widget', 'choice'))
|
|
|
|
->addRendererPlugin(new DatePatternPlugin($formatter))
|
|
|
|
// Don't modify \DateTime classes by reference, we treat
|
|
|
|
// them like immutable value objects
|
|
|
|
->setModifyByReference(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['type'] === 'string') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToStringTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'format' => 'Y-m-d',
|
|
|
|
))
|
|
|
|
));
|
|
|
|
} else if ($options['type'] === 'timestamp') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToTimestampTransformer(array(
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
))
|
|
|
|
));
|
|
|
|
} else if ($options['type'] === 'raw') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToArrayTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'fields' => array('year', 'month', 'day'),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|