gyro/core/model/base/fields/dbfield.datetime.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * A date and time field in DB 00004 * 00005 * @author Gerd Riesselmann 00006 * @ingroup Model 00007 */ 00008 class DBFieldDateTime extends DBField { 00009 const NOW = 'now'; 00010 const TIMESTAMP = 32; 00011 00012 /** 00013 * Returns the default value for this field 00014 * 00015 * @return mixed 00016 */ 00017 public function get_field_default() { 00018 if ($this->has_policy(self::TIMESTAMP)) { 00019 return null; // Timestamps are managed entirely by DB 00020 } 00021 $ret = parent::get_field_default(); 00022 if ($ret == self::NOW) { 00023 $ret = time(); 00024 } 00025 return $ret; 00026 } 00027 00028 /** 00029 * Reformat passed value to DB format 00030 * 00031 * @param mixed $value 00032 * @return string 00033 */ 00034 public function format($value) { 00035 if ($this->has_policy(self::TIMESTAMP)) { 00036 return 'DEFAULT'; 00037 } 00038 00039 // Treat '', false and 0 as NULL in updates and inserts 00040 if (empty($value)) { 00041 return $this->do_format_null(null); 00042 } 00043 else { 00044 return $this->format_where($value); 00045 } 00046 } 00047 00048 /** 00049 * Format for use in WHERE clause 00050 * 00051 * @param mixed $value 00052 * @return string 00053 */ 00054 public function format_where($value) { 00055 if ($this->is_null($value)) { 00056 return parent::format($value); 00057 } 00058 else if ($value == self::NOW) { 00059 return $this->get_db_now_constant(); 00060 } 00061 else { 00062 return $this->format_date_value(GyroDate::datetime($value));; 00063 } 00064 } 00065 00066 /** 00067 * Returns true, if the value passed fits the fields restrictions 00068 * 00069 * @param mixed $value 00070 * @return Status 00071 */ 00072 public function validate($value) { 00073 $ret = parent::validate($value); 00074 if ($ret->is_ok()) { 00075 $test = GyroDate::datetime($value); 00076 if ($test === false) { 00077 $ret->append(tr( 00078 '%field must be a date and time value', 00079 'core', 00080 array( 00081 '%field' => $this->get_field_name_translation() 00082 ) 00083 )); 00084 } 00085 } 00086 return $ret; 00087 } 00088 00089 /** 00090 * Returns fucntion or constant to set current time in DB (like NOW() or CURRENT_TIMESTAMP) 00091 * 00092 * @return string 00093 */ 00094 protected function get_db_now_constant() { 00095 return 'CURRENT_TIMESTAMP'; 00096 } 00097 00098 /** 00099 * Formats a value for use in DB 00100 * 00101 * @param datetime $value 00102 * @return string 00103 */ 00104 protected function format_date_value($value) { 00105 return $this->quote(GyroDate::mysql_date($value)); 00106 } 00107 00108 /** 00109 * Allow replacements for field in select from clause 00110 */ 00111 public function format_select() { 00112 return 'UNIX_TIMESTAMP(' . parent::format_select() . ')'; 00113 } 00114 00115 /** 00116 * Transform result from SELECT to native 00117 * 00118 * @param mixed $value 00119 * @return mixed 00120 */ 00121 public function convert_result($value) { 00122 return is_null($value) ? null : GyroDate::datetime($value); 00123 } 00124 }