Fixed default value for date/datetime attributes

SVN:trunk[551]
This commit is contained in:
Romain Quetiez
2010-07-05 08:06:56 +00:00
parent 300fc8c187
commit 1f3554777d
5 changed files with 80 additions and 179 deletions

View File

@@ -982,158 +982,6 @@ class AttributeEnum extends AttributeString
}
}
/**
* Map a date+time column to an attribute
*
* @package iTopORM
*/
class AttributeDate extends AttributeDBField
{
const MYDATEFORMAT = "Y-m-d";
static public function InitStatics()
{
// Nothing to do...
}
static protected function ListExpectedParams()
{
return parent::ListExpectedParams();
//return array_merge(parent::ListExpectedParams(), array());
}
public function GetType() {return "Date";}
public function GetTypeDesc() {return "Date";}
public function GetEditClass() {return "Date";}
protected function GetSQLCol() {return "DATE";}
// #@# THIS HAS TO REVISED
// Having null not allowed was interpreted by mySQL
// which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
// Then, on each update of the record, the field was modified.
// We will have to specify the default value if we want to restore this option
// In fact, we could also have more verbs dedicated to the DB:
// GetDBDefaultValue()... or GetDBFieldCreationStatement()....
public function IsNullAllowed() {return true;}
public function GetDefaultValue()
{
$default = parent::GetDefaultValue();
if (!parent::IsNullAllowed())
{
if (empty($default))
{
$default = date("Y-m-d");
}
}
return $default;
}
// END OF THE WORKAROUND
///////////////////////////////////////////////////////////////
public function GetValidationPattern()
{
return "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
}
public function GetBasicFilterOperators()
{
return array(
"="=>"equals",
"!="=>"differs from",
"<"=>"before",
"<="=>"before",
">"=>"after (strictly)",
">="=>"after",
"SameMonth"=>"same year/month",
"SameYear"=>"same year",
"Today"=>"today",
">|"=>"after today + N days",
"<|"=>"before today + N days",
"=|"=>"equals today + N days",
);
}
public function GetBasicFilterLooseOperator()
{
// Unless we implement a "same xxx, depending on given precision" !
return "=";
}
public function GetBasicFilterSQLExpr($sOpCode, $value)
{
$sQValue = CMDBSource::Quote($value);
switch ($sOpCode)
{
case '=':
case '!=':
case '<':
case '<=':
case '>':
case '>=':
return $this->GetSQLExpr()." $sOpCode $sQValue";
case 'SameMonth':
return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
case 'SameYear':
return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
case 'Today':
return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
case '>|':
return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
case '<|':
return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
case '=|':
return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
default:
return $this->GetSQLExpr()." = $sQValue";
}
}
public function MakeRealValue($proposedValue)
{
if (!is_numeric($proposedValue))
{
return $proposedValue;
}
else
{
return date("Y-m-d", $proposedValue);
}
throw new CoreException("Invalid type for a date (found ".gettype($proposedValue)." and accepting string/int/DateTime)");
return null;
}
public function ScalarToSQL($value)
{
if (empty($value))
{
// Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
return '0000-00-00';
}
return $value;
}
public function GetAsHTML($value)
{
return Str::pure2html($value);
}
public function GetAsXML($value)
{
return Str::pure2xml($value);
}
public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
{
$sFrom = array("\r\n", $sTextQualifier);
$sTo = array("\n", $sTextQualifier.$sTextQualifier);
$sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
return '"'.$sEscaped.'"';
}
}
// Init static constant once for all (remove when PHP allows real static const)
AttributeDate::InitStatics();
/**
* Map a date+time column to an attribute
*
@@ -1141,7 +989,6 @@ AttributeDate::InitStatics();
*/
class AttributeDateTime extends AttributeDBField
{
const MYDATEFORMAT = "Y-m-d H:i:s";
//const MYDATETIMEZONE = "UTC";
const MYDATETIMEZONE = "Europe/Paris";
static protected $const_TIMEZONE = null; // set once for all upon object construct
@@ -1156,6 +1003,11 @@ class AttributeDateTime extends AttributeDBField
date_default_timezone_set(self::MYDATETIMEZONE);
}
static protected function GetDateFormat()
{
return "Y-m-d H:i:s";
}
static protected function ListExpectedParams()
{
return parent::ListExpectedParams();
@@ -1190,7 +1042,7 @@ class AttributeDateTime extends AttributeDBField
{
if (empty($default))
{
$default = date("Y-m-d H:i");
$default = date(self::GetDateFormat());
}
}
@@ -1262,20 +1114,29 @@ class AttributeDateTime extends AttributeDBField
public function MakeRealValue($proposedValue)
{
if (is_null($proposedValue))
{
return null;
}
if (is_string($proposedValue) && ($proposedValue == "") && $this->IsNullAllowed())
{
return null;
}
if (!is_numeric($proposedValue))
{
return $proposedValue;
}
else
{
return date("Y-m-d H:i:s", $proposedValue);
}
throw new CoreException("Invalid type for a date (found ".gettype($proposedValue)." and accepting string/int/DateTime)");
return null;
return date(self::GetDateFormat(), $proposedValue);
}
public function ScalarToSQL($value)
{
if (empty($value))
if (is_null($value))
{
return null;
}
elseif (empty($value))
{
// Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
return '0000-00-00 00:00:00';
@@ -1301,6 +1162,46 @@ class AttributeDateTime extends AttributeDBField
return '"'.$sEscaped.'"';
}
}
/**
* Map a date+time column to an attribute
*
* @package iTopORM
*/
class AttributeDate extends AttributeDateTime
{
const MYDATEFORMAT = "Y-m-d";
static protected function GetDateFormat()
{
return "Y-m-d";
}
static public function InitStatics()
{
// Nothing to do...
}
static protected function ListExpectedParams()
{
return parent::ListExpectedParams();
//return array_merge(parent::ListExpectedParams(), array());
}
public function GetType() {return "Date";}
public function GetTypeDesc() {return "Date";}
public function GetEditClass() {return "Date";}
protected function GetSQLCol() {return "DATE";}
public function GetValidationPattern()
{
return "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
}
}
// Init static constant once for all (remove when PHP allows real static const)
AttributeDate::InitStatics();
/**
* A dead line stored as a date & time
* The only difference with the DateTime attribute is the display: