Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1704 from njam/formfield-date-timezone
Browse files Browse the repository at this point in the history
Make sure date formfield renders and validates in the environment's timezone
  • Loading branch information
njam committed Mar 23, 2015
2 parents 9c894eb + 26193a2 commit e65b10c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
6 changes: 3 additions & 3 deletions layout/default/FormField/Date/default.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{select name="{$name}[year]" optionList=$years placeholder={translate 'Year'} selectedValue=$yy}
{select name="{$name}[month]" optionList=$months placeholder={translate 'Month'} selectedValue=$mm translate=true translatePrefix='.date.month.'}
{select name="{$name}[day]" optionList=$days placeholder={translate 'Day'} selectedValue=$dd}
{select class="year" name="{$name}[year]" optionList=$years placeholder={translate 'Year'} selectedValue=$yy}
{select class="month" name="{$name}[month]" optionList=$months placeholder={translate 'Month'} selectedValue=$mm translate=true translatePrefix='.date.month.'}
{select class="day" name="{$name}[day]" optionList=$days placeholder={translate 'Day'} selectedValue=$dd}
17 changes: 13 additions & 4 deletions library/CM/FormField/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function validate(CM_Frontend_Environment $environment, $userInput) {
$mm = (int) trim($userInput['month']);
$yy = (int) trim($userInput['year']);

return new DateTime($yy . '-' . $mm . '-' . $dd);
return new DateTime($yy . '-' . $mm . '-' . $dd, $environment->getTimeZone());
}

public function prepare(CM_Params $renderParams, CM_Frontend_Environment $environment, CM_Frontend_ViewResponse $viewResponse) {
Expand All @@ -33,10 +33,19 @@ public function prepare(CM_Params $renderParams, CM_Frontend_Environment $enviro
$viewResponse->set('months', array_combine($months, $months));
$viewResponse->set('days', array_combine($days, $days));

/** @var DateTime|null $value */
$value = $this->getValue();
$viewResponse->set('yy', $value ? $value->format('Y') : null);
$viewResponse->set('mm', $value ? $value->format('n') : null);
$viewResponse->set('dd', $value ? $value->format('j') : null);
$year = $month = $day = null;
if (null !== $value) {
$value->setTimezone($environment->getTimeZone());
$year = $value->format('Y');
$month = $value->format('n');
$day = $value->format('j');
}

$viewResponse->set('yy', $year);
$viewResponse->set('mm', $month);
$viewResponse->set('dd', $day);
}

public function isEmpty($userInput) {
Expand Down
25 changes: 25 additions & 0 deletions tests/library/CM/FormField/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class CM_FormField_DateTest extends CMTest_TestCase {

public function testRenderWithEnvironmentTimezone() {
$field = new CM_FormField_Date(['name' => 'foo']);
$field->setValue(new DateTime('2015-03-02 01:00:00', new DateTimeZone('UTC')));

$environment = new CM_Frontend_Environment(null, null, null, new DateTimeZone('America/New_York'));
$render = new CM_Frontend_Render($environment);
$doc = $this->_renderFormField($field, null, $render);

$this->assertSame('selected', $doc->find('select.year option[value="2015"]')->getAttribute('selected'));
$this->assertSame('selected', $doc->find('select.month option[value="3"]')->getAttribute('selected'));
$this->assertSame('selected', $doc->find('select.day option[value="1"]')->getAttribute('selected'));
}

public function testValidateWithEnvironmentTimezone() {
$formField = new CM_FormField_Date();

$environment = new CM_Frontend_Environment(null, null, null, new DateTimeZone('Asia/Tokyo'));
$value = $formField->validate($environment, ['year' => 2015, 'month' => 03, 'day' => 02]);
$this->assertEquals(new DateTime('2015-03-02 00:00:00', new DateTimeZone('Asia/Tokyo')), $value);
}
}

0 comments on commit e65b10c

Please sign in to comment.