mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Added FormatFromSettings(). Formats dates as per user preferences.
- Required for silverstripe-cms/issue/965. - Separate implementations for Date and SS_Datetime.
This commit is contained in:
parent
cbbce430e1
commit
26f8341438
@ -152,6 +152,28 @@ class Date extends DBField {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a date formatted as per a CMS user's settings.
|
||||
*
|
||||
* @param Member $member
|
||||
* @return boolean | string A date formatted as per user-defined settings.
|
||||
*/
|
||||
public function FormatFromSettings($member = null) {
|
||||
require_once 'Zend/Date.php';
|
||||
|
||||
if(!$member) {
|
||||
if(!Member::currentUserID()) {
|
||||
return false;
|
||||
}
|
||||
$member = Member::currentUser();
|
||||
}
|
||||
|
||||
$formatD = $member->getDateFormat();
|
||||
$zendDate = new Zend_Date($this->getValue());
|
||||
|
||||
return $zendDate->toString($formatD);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a string in the form "12 - 16 Sept" or "12 Aug - 16 Sept"
|
||||
* @param Date $otherDateObj Another date object specifying the end of the range
|
||||
|
@ -92,6 +92,29 @@ class SS_Datetime extends Date implements TemplateGlobalProvider {
|
||||
public function Time24() {
|
||||
if($this->value) return $this->Format('H:i');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a date and time formatted as per a CMS user's settings.
|
||||
*
|
||||
* @param Member $member
|
||||
* @return boolean | string A time and date pair formatted as per user-defined settings.
|
||||
*/
|
||||
public function FormatFromSettings($member = null) {
|
||||
require_once 'Zend/Date.php';
|
||||
|
||||
if(!$member) {
|
||||
if(!Member::currentUserID()) {
|
||||
return false;
|
||||
}
|
||||
$member = Member::currentUser();
|
||||
}
|
||||
|
||||
$formatD = $member->getDateFormat();
|
||||
$formatT = $member->getTimeFormat();
|
||||
|
||||
$zendDate = new Zend_Date($this->getValue());
|
||||
return $zendDate->toString($formatD).' '.$zendDate->toString($formatT);
|
||||
}
|
||||
|
||||
public function requireField() {
|
||||
$parts=Array('datatype'=>'datetime', 'arrayValue'=>$this->arrayValue);
|
||||
|
@ -201,5 +201,27 @@ class DateTest extends SapphireTest {
|
||||
|
||||
SS_Datetime::clear_mock_now();
|
||||
}
|
||||
|
||||
public function testFormatFromSettings() {
|
||||
|
||||
$memberID = $this->logInWithPermission();
|
||||
$member = DataObject::get_by_id('Member', $memberID);
|
||||
$member->DateFormat = 'dd/MM/YYYY';
|
||||
$member->write();
|
||||
|
||||
$fixtures = array(
|
||||
'2000-12-31' => '31/12/2000',
|
||||
'31-12-2000' => '31/12/2000',
|
||||
'31/12/2000' => '31/12/2000'
|
||||
);
|
||||
|
||||
foreach($fixtures as $from => $to) {
|
||||
$date = DBField::create_field('Date', $from);
|
||||
// With member
|
||||
$this->assertEquals($to, $date->FormatFromSettings($member));
|
||||
// Without member
|
||||
$this->assertEquals($to, $date->FormatFromSettings());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -148,5 +148,30 @@ class SS_DatetimeTest extends SapphireTest {
|
||||
|
||||
SS_Datetime::clear_mock_now();
|
||||
}
|
||||
|
||||
public function testFormatFromSettings() {
|
||||
|
||||
$memberID = $this->logInWithPermission();
|
||||
$member = DataObject::get_by_id('Member', $memberID);
|
||||
$member->DateFormat = 'dd/MM/YYYY';
|
||||
$member->TimeFormat = 'hh:mm:ss';
|
||||
$member->write();
|
||||
|
||||
$fixtures = array(
|
||||
'2000-12-31 10:11:01' => '31/12/2000 10:11:01',
|
||||
'2000-12-31 1:11:01' => '31/12/2000 01:11:01',
|
||||
'12/12/2000 1:11:01' => '12/12/2000 01:11:01',
|
||||
'2000-12-31' => '31/12/2000 12:00:00',
|
||||
'10:11:01' => date('d/m/Y').' 10:11:01'
|
||||
);
|
||||
|
||||
foreach($fixtures as $from => $to) {
|
||||
$date = DBField::create_field('Datetime', $from);
|
||||
// With member
|
||||
$this->assertEquals($to, $date->FormatFromSettings($member));
|
||||
// Without member
|
||||
$this->assertEquals($to, $date->FormatFromSettings());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user