1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 00:06:55 +02:00

Add @LostKobrakai PR #50 plus some other tweaks to FieldtypeDatetime module

This commit is contained in:
Ryan Cramer
2017-03-30 06:27:52 -04:00
parent 94876a7bde
commit 3f67722294

View File

@@ -114,6 +114,10 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Return the Inputfield used for date/time (InputfieldDatetime) * Return the Inputfield used for date/time (InputfieldDatetime)
* *
* @param Page $page
* @param Field $field
* @return InputfieldDatetime
*
*/ */
public function getInputfield(Page $page, Field $field) { public function getInputfield(Page $page, Field $field) {
$inputfield = $this->modules->get('InputfieldDatetime'); $inputfield = $this->modules->get('InputfieldDatetime');
@@ -124,6 +128,11 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Sanitize value, per Fieldtype interface * Sanitize value, per Fieldtype interface
* *
* @param Page $page
* @param Field $field
* @param string|int|\DateTime $value
* @return int
*
*/ */
public function sanitizeValue(Page $page, Field $field, $value) { public function sanitizeValue(Page $page, Field $field, $value) {
return $this->_sanitizeValue($value); return $this->_sanitizeValue($value);
@@ -132,9 +141,13 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Sanitize a value assumed to be either a timestamp or in strtotime() compatible format * Sanitize a value assumed to be either a timestamp or in strtotime() compatible format
* *
* @param string|int|\DateTime
* @return int
*
*/ */
protected function _sanitizeValue($value) { protected function _sanitizeValue($value) {
if(empty($value)) return ''; if(empty($value)) return '';
if($value instanceof \DateTime) return $value->getTimestamp();
// already a timestamp // already a timestamp
if(ctype_digit(ltrim($value, '-'))) return (int) $value; if(ctype_digit(ltrim($value, '-'))) return (int) $value;
return strtotime($value); return strtotime($value);
@@ -143,12 +156,17 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Format the value for output, according to selected format and language * Format the value for output, according to selected format and language
* *
* @param Page $page
* @param Field $field
* @param int $value
* @return string
*
*/ */
public function ___formatValue(Page $page, Field $field, $value) { public function ___formatValue(Page $page, Field $field, $value) {
$format = ''; $format = '';
if($this->languages && !$this->user->language->isDefault()) $format = $field->get("dateOutputFormat{$this->user->language}"); if($this->languages && !$this->user->language->isDefault()) $format = $field->get("dateOutputFormat{$this->user->language}");
if(!$format) $format = $field->dateOutputFormat; if(!$format) $format = $field->get('dateOutputFormat');
return $this->formatDate($value, $format); return $this->wire('datetime')->formatDate($value, $format);
} }
public function ___exportValue(Page $page, Field $field, $value, array $options = array()) { public function ___exportValue(Page $page, Field $field, $value, array $options = array()) {
@@ -159,6 +177,13 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Match a date/time value in the database, as used by PageFinder * Match a date/time value in the database, as used by PageFinder
* *
* @param DatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param int|string $value
* @return DatabaseQuerySelect
*
*/ */
public function getMatchQuery($query, $table, $subfield, $operator, $value) { public function getMatchQuery($query, $table, $subfield, $operator, $value) {
$value = (int) $this->_sanitizeValue($value); $value = (int) $this->_sanitizeValue($value);
@@ -178,6 +203,9 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Return database schema used by this field * Return database schema used by this field
* *
* @param Field $field
* @return array
*
*/ */
public function getDatabaseSchema(Field $field) { public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field); $schema = parent::getDatabaseSchema($field);
@@ -190,6 +218,11 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Convert value from timestamp to Y-m-d H:i:s date string * Convert value from timestamp to Y-m-d H:i:s date string
* *
* @param Page $page
* @param Field $field
* @param string|int $value
* @return string
*
*/ */
public function ___sleepValue(Page $page, Field $field, $value) { public function ___sleepValue(Page $page, Field $field, $value) {
$value = $this->_sanitizeValue($value); $value = $this->_sanitizeValue($value);
@@ -199,6 +232,11 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Convert value from Y-m-d H:i:s string to timestamp * Convert value from Y-m-d H:i:s string to timestamp
* *
* @param Page $page
* @param Field $field
* @param string $value
* @return int
*
*/ */
public function ___wakeupValue(Page $page, Field $field, $value) { public function ___wakeupValue(Page $page, Field $field, $value) {
if(empty($value)) return ''; if(empty($value)) return '';
@@ -210,12 +248,16 @@ class FieldtypeDatetime extends FieldtypeText {
/** /**
* Field configuration screen * Field configuration screen
* *
* @param Field $field
* @return InputfieldWrapper
*
*/ */
public function ___getConfigInputfields(Field $field) { public function ___getConfigInputfields(Field $field) {
$inputfields = parent::___getConfigInputfields($field); $inputfields = parent::___getConfigInputfields($field);
$wdt = new WireDateTime(); $wdt = $this->wire('datetime');
/** @var InputfieldSelect $f */
$f = $this->modules->get('InputfieldSelect'); $f = $this->modules->get('InputfieldSelect');
$f->attr('name', '_dateOutputFormat'); $f->attr('name', '_dateOutputFormat');
$f->label = $this->_('Date Output Format'); $f->label = $this->_('Date Output Format');
@@ -230,7 +272,7 @@ class FieldtypeDatetime extends FieldtypeText {
$dateFormatted = $wdt->formatDate($date, $format); $dateFormatted = $wdt->formatDate($date, $format);
if($format == 'U') $dateFormatted .= " " . $this->_('(unix timestamp)'); if($format == 'U') $dateFormatted .= " " . $this->_('(unix timestamp)');
$f->addOption($format, $dateFormatted); $f->addOption($format, $dateFormatted);
if(!$found && strpos($field->dateOutputFormat, $format) !== false) { if(!$found && strpos($field->get('dateOutputFormat'), $format) !== false) {
$f->attr('value', $format); $f->attr('value', $format);
$found = true; $found = true;
} }
@@ -250,7 +292,7 @@ class FieldtypeDatetime extends FieldtypeText {
foreach($wdt->getTimeFormats() as $format) { foreach($wdt->getTimeFormats() as $format) {
$timeFormatted = $wdt->formatDate($date, $format); $timeFormatted = $wdt->formatDate($date, $format);
$f->addOption($format, $timeFormatted); $f->addOption($format, $timeFormatted);
if(!$found && strpos($field->dateOutputFormat, $format) !== false) { if(!$found && strpos($field->get('dateOutputFormat'), $format) !== false) {
$f->attr('value', $format); $f->attr('value', $format);
$found = true; $found = true;
} }
@@ -259,9 +301,10 @@ class FieldtypeDatetime extends FieldtypeText {
//$f->collapsed = Inputfield::collapsedBlank; //$f->collapsed = Inputfield::collapsedBlank;
$inputfields->add($f); $inputfields->add($f);
/** @var InputfieldText $f */
$f = $this->modules->get("InputfieldText"); $f = $this->modules->get("InputfieldText");
$f->attr('name', 'dateOutputFormat'); $f->attr('name', 'dateOutputFormat');
$f->attr('value', $field->dateOutputFormat); $f->attr('value', $field->get('dateOutputFormat'));
$f->attr('size', 20); $f->attr('size', 20);
$f->label = $this->_('Date/Time Output Format Code'); $f->label = $this->_('Date/Time Output Format Code');
$f->description = $this->_('The date/time will be output according to the format below. This is automatically built from the date/time selections above, but you may change it as needed to suit your needs.') . ' '; $f->description = $this->_('The date/time will be output according to the format below. This is automatically built from the date/time selections above, but you may change it as needed to suit your needs.') . ' ';