Skip to content

Commit

Permalink
Fixes timezone issues on editing events
Browse files Browse the repository at this point in the history
This commit changes the implementation of the DateTransformer in such a
way that dates are now handled using a provided dateTime. When no
dateTime is provided UTC is used.

Not using this handler caused dates to change when editing an event.
  • Loading branch information
heiglandreas committed Mar 6, 2015
1 parent 028a075 commit df8c213
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 12 additions & 4 deletions app/src/Event/EventFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ public function getName()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{

list ($continents, $cities) = $this->getListOfTimezoneContinentsAndCities();

$timezone = null;
if (isset($options['data'])) {
$timezone = $options['data']->getFullTimezone();
}

$dateTransformer = new DateTransformer($timezone);
$builder
->add('addr', 'hidden', ['mapped' => false])
->add(
Expand Down Expand Up @@ -107,29 +115,29 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'start_date',
'text',
$this->getOptionsForDateWidget('Start date')
)->addViewTransformer(new DateTransformer())
)->addViewTransformer($dateTransformer)
)
->add(
$builder->create(
'end_date',
'text',
$this->getOptionsForDateWidget('End date')
)->addViewTransformer(new DateTransformer())
)->addViewTransformer($dateTransformer)
)
->add('href', 'url', $this->getOptionsForUrlWidget('Website URL'))
->add(
$builder->create(
'cfp_start_date',
'text',
$this->getOptionsForDateWidget('Opening date', false)
)->addViewTransformer(new DateTransformer())
)->addViewTransformer($dateTransformer)
)
->add(
$builder->create(
'cfp_end_date',
'text',
$this->getOptionsForDateWidget('Closing date', false)
)->addViewTransformer(new DateTransformer())
)->addViewTransformer($dateTransformer)
)
->add('cfp_url', 'url', $this->getOptionsForUrlWidget('Call for papers URL', false))
->add(
Expand Down
16 changes: 14 additions & 2 deletions app/src/Form/DataTransformer/DateTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
namespace Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use \DateTime;
use \DateTimeZone;

class DateTransformer implements DataTransformerInterface
{
protected $timezone;

public function __construct($timezone = 'UTC')
{
if (! in_array($timezone, DateTimeZone::listIdentifiers(DateTimeZone::ALL))) {
$timezone = 'UTC';
}
$this->timezone = new DateTimeZone($timezone);
}

public function transform($value)
{
return $this->convertDate($value, 'j F Y');
Expand All @@ -18,9 +30,9 @@ public function reverseTransform($value)
protected function convertDate($value, $format)
{
if ($value) {
$d = strtotime($value);
$d = new DateTime($value, $this->timezone);
if ($d) {
$value = date($format, $d);
$value = $d->format($format);
}
}
return $value;
Expand Down

0 comments on commit df8c213

Please sign in to comment.