1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-21 14:02:59 +02:00

Refactor FieldtypeDatetime module so that it no longer extends FieldtypeText

This commit is contained in:
Ryan Cramer
2019-10-18 06:18:45 -04:00
parent 22fe5bf9ef
commit 7be5fb4a68

View File

@@ -8,23 +8,27 @@
* For documentation about the fields used in this class, please see: * For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php * /wire/core/Fieldtype.php
* *
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer * ProcessWire 3.x, Copyright 2019 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
*/ */
class FieldtypeDatetime extends FieldtypeText { class FieldtypeDatetime extends Fieldtype {
const defaultDateOutputFormat = 'Y-m-d';
public static function getModuleInfo() { public static function getModuleInfo() {
return array( return array(
'title' => 'Datetime', 'title' => 'Datetime',
'version' => 104, 'version' => 105,
'summary' => 'Field that stores a date and optionally time', 'summary' => 'Field that stores a date and optionally time',
); );
} }
/**
* Default date output format
*
*/
const defaultDateOutputFormat = 'Y-m-d';
/** /**
* Return all predefined PHP date() formats for use as dates * Return all predefined PHP date() formats for use as dates
* *
@@ -102,15 +106,6 @@ class FieldtypeDatetime extends FieldtypeText {
/********************************************************************************************************************************* /*********************************************************************************************************************************
*********************************************************************************************************************************/ *********************************************************************************************************************************/
/**
* Initlialize this module
*
*/
public function init() {
parent::init();
$this->allowTextFormatters(false);
}
/** /**
* Return the Inputfield used for date/time (InputfieldDatetime) * Return the Inputfield used for date/time (InputfieldDatetime)
* *
@@ -121,7 +116,7 @@ class FieldtypeDatetime extends FieldtypeText {
*/ */
public function getInputfield(Page $page, Field $field) { public function getInputfield(Page $page, Field $field) {
/** @var InputfieldDatetime $inputfield */ /** @var InputfieldDatetime $inputfield */
$inputfield = $this->modules->get('InputfieldDatetime'); $inputfield = $this->wire('modules')->get('InputfieldDatetime');
$inputfield->class = $this->className(); $inputfield->class = $this->className();
return $inputfield; return $inputfield;
} }
@@ -182,10 +177,33 @@ class FieldtypeDatetime extends FieldtypeText {
return $this->wire('datetime')->formatDate($value, $format); return $this->wire('datetime')->formatDate($value, $format);
} }
/**
* Export value
*
* @param Page $page
* @param Field $field
* @param int $value
* @param array $options
*
* @return array|false|float|int|string
*
*/
public function ___exportValue(Page $page, Field $field, $value, array $options = array()) { public function ___exportValue(Page $page, Field $field, $value, array $options = array()) {
if(!$value) return ''; if(!$value) return '';
return date('Y-m-d H:i:s', $value); return date('Y-m-d H:i:s', $value);
} }
/**
* Return whether the given value is considered empty or not
*
* @param Field $field
* @param mixed $value
* @return bool
*
*/
public function isEmptyValue(Field $field, $value) {
return !strlen($value);
}
/** /**
* Match a date/time value in the database, as used by PageFinder * Match a date/time value in the database, as used by PageFinder
@@ -251,10 +269,17 @@ class FieldtypeDatetime extends FieldtypeText {
return $query; return $query;
} }
/**
* Get selector info
*
* @param Field $field
* @param array $data
* @return array
*
*/
public function ___getSelectorInfo(Field $field, array $data = array()) { public function ___getSelectorInfo(Field $field, array $data = array()) {
$a = parent::___getSelectorInfo($field, $data); $a = parent::___getSelectorInfo($field, $data);
$a['operators'][] = '%='; $a['operators'] = array('=', '!=', '>', '>=', '<', '<=', '%=', '^=');
$a['operators'][] = '^=';
return $a; return $a;
} }
@@ -268,8 +293,7 @@ class FieldtypeDatetime extends FieldtypeText {
public function getDatabaseSchema(Field $field) { public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field); $schema = parent::getDatabaseSchema($field);
$schema['data'] = 'datetime NOT NULL'; $schema['data'] = 'datetime NOT NULL';
unset($schema['keys']['data_exact']); $schema['keys']['data'] = 'KEY data (data)';
$schema['keys']['data'] = 'KEY data (data)'; // wipe out keys from parent
return $schema; return $schema;
} }
@@ -303,6 +327,22 @@ class FieldtypeDatetime extends FieldtypeText {
return $value; return $value;
} }
/**
* Get compatible Fieldtypes
*
* @param Field $field
* @return Fieldtypes
* @throws WireException
*
*/
public function ___getCompatibleFieldtypes(Field $field) {
$fieldtypes = $this->wire(new Fieldtypes());
foreach($this->wire('fieldtypes') as $fieldtype) {
if($fieldtype instanceof FieldtypeDatetime) $fieldtypes->add($fieldtype);
}
return $fieldtypes;
}
/** /**
* Field configuration screen * Field configuration screen
* *