Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdatetype.php
Go to the documentation of this file.
1 <?php
18 class eZDateType extends eZDataType
19 {
20 const DATA_TYPE_STRING = "ezdate";
21
22 const DEFAULT_FIELD = 'data_int1';
23
24 const DEFAULT_EMTPY = 0;
25
26 const DEFAULT_CURRENT_DATE = 1;
27
28 public function __construct()
29 {
30 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Date", 'Datatype name' ),
31 array( 'serialize_supported' => true ) );
32 }
33
34
35 function validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute )
36 {
37 $state = eZDateTimeValidator::validateDate( $day, $month, $year );
38 if ( $state == eZInputValidator::STATE_INVALID )
39 {
40 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
41 'Date is not valid.' ) );
42 return eZInputValidator::STATE_INVALID;
43 }
44 return $state;
45 }
50 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
51 {
52 $classAttribute = $contentObjectAttribute->contentClassAttribute();
53
54 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
55 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
56 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
57 {
58 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
59 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
60 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
61
62 if ( $year == '' or $month == '' or $day == '' )
63 {
64 if ( !( $year == '' and $month == '' and $day == '' ) or
65 ( !$classAttribute->attribute( 'is_information_collector' ) and
66 $contentObjectAttribute->validateIsRequired() ) )
67 {
68 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
69 'Missing date input.' ) );
70 return eZInputValidator::STATE_INVALID;
71 }
72 else
73 return eZInputValidator::STATE_ACCEPTED;
74 }
75 else
76 {
77 return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
78 }
79 }
80 else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
81 {
82 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Missing date input.' ) );
83 return eZInputValidator::STATE_INVALID;
84 }
85
86 return eZInputValidator::STATE_ACCEPTED;
87 }
88
92 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
93 {
94 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
95 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
96 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
97 {
98
99 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
100 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
101 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
102 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
103
104 if ( ( $year == '' and $month == '' and $day == '' ) or
105 !checkdate( $month, $day, $year ) )
106 {
107 $stamp = null;
108 }
109 else
110 {
111 $date = new eZDate();
112 $date->setMDY( $month, $day, $year );
113 $stamp = eZTimestamp::getUtcTimestampFromLocalTimestamp( $date->timeStamp() );
114 }
115
116 $contentObjectAttribute->setAttribute( 'data_int', $stamp );
117 return true;
118 }
119 return false;
120 }
121
122 function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
123 {
124 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
125 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
126 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
127 {
128 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
129 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
130 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
131 $classAttribute = $contentObjectAttribute->contentClassAttribute();
132
133 if ( $year == '' or $month == '' or $day == '' )
134 {
135 if ( !( $year == '' and $month == '' and $day == '' ) or
136 $contentObjectAttribute->validateIsRequired() )
137 {
138 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
139 'Missing date input.' ) );
140 return eZInputValidator::STATE_INVALID;
141 }
142 else
143 return eZInputValidator::STATE_ACCEPTED;
144 }
145 else
146 {
147 return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
148 }
149 }
150 else
151 return eZInputValidator::STATE_INVALID;
152 }
153
157 function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
158 {
159 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
160 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
161 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
162 {
163
164 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
165 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
166 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
167 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
168
169 if ( ( $year == '' and $month == '' and $day == '' ) or
170 !checkdate( $month, $day, $year ) )
171 {
172 $stamp = null;
173 }
174 else
175 {
176 $date = new eZDate();
177 $date->setMDY( $month, $day, $year );
178 $stamp = eZTimestamp::getUtcTimestampFromLocalTimestamp( $date->timeStamp() );
179 }
180
181 $collectionAttribute->setAttribute( 'data_int', $stamp );
182 return true;
183 }
184 return false;
185 }
186
190 function objectAttributeContent( $contentObjectAttribute )
191 {
192 $date = new eZDate( );
193 $stamp = $contentObjectAttribute->attribute( 'data_int' );
194 $date->setTimeStamp(
195 eZTimestamp::getLocalTimestampFromUtcTimestamp( $stamp )
196 );
197 return $date;
198 }
199
203 function initializeClassAttribute( $classAttribute )
204 {
205 if ( $classAttribute->attribute( self::DEFAULT_FIELD ) == null )
206 $classAttribute->setAttribute( self::DEFAULT_FIELD, 0 );
207 $classAttribute->store();
208 }
209
213 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
214 {
215 if ( $currentVersion != false )
216 {
217 $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
218 $contentObjectAttribute->setAttribute( "data_int", $dataInt );
219 }
220 else
221 {
222 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
223 $defaultType = $contentClassAttribute->attribute( self::DEFAULT_FIELD );
224 if ( $defaultType == 1 )
225 $contentObjectAttribute->setAttribute( "data_int", time() );
226 }
227 }
228
229 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
230 {
231 $default = $base . "_ezdate_default_" . $classAttribute->attribute( 'id' );
232 if ( $http->hasPostVariable( $default ) )
233 {
234 $defaultValue = $http->postVariable( $default );
235 $classAttribute->setAttribute( self::DEFAULT_FIELD, $defaultValue );
236 }
237 return true;
238 }
239
240 function isIndexable()
241 {
242 return true;
243 }
244
245 function isInformationCollector()
246 {
247 return true;
248 }
249
253 function metaData( $contentObjectAttribute )
254 {
255 return (int)$contentObjectAttribute->attribute( 'data_int' );
256 }
257
262 function toString( $contentObjectAttribute )
263 {
264 $stamp = $contentObjectAttribute->attribute( 'data_int' );
265 return $stamp === null ? '' : $stamp;
266 }
267
268 function fromString( $contentObjectAttribute, $string )
269 {
270 if ( empty( $string ) )
271 {
272 $string = null;
273 }
274
275 return $contentObjectAttribute->setAttribute( 'data_int', $string );
276 }
277
281 function title( $contentObjectAttribute, $name = null )
282 {
283 $locale = eZLocale::instance();
284 $retVal = $contentObjectAttribute->attribute( "data_int" ) === null ? '' : $locale->formatDate( $contentObjectAttribute->attribute( "data_int" ) );
285 return $retVal;
286 }
287
288 function hasObjectAttributeContent( $contentObjectAttribute )
289 {
290 return $contentObjectAttribute->attribute( "data_int" ) !== null;
291 }
292
293 function sortKey( $contentObjectAttribute )
294 {
295 return (int)$contentObjectAttribute->attribute( 'data_int' );
296 }
297
298 function sortKeyType()
299 {
300 return 'int';
301 }
302
303 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
304 {
305 $dom = $attributeParametersNode->ownerDocument;
306
307 $defaultValue = $classAttribute->attribute( self::DEFAULT_FIELD );
308 $defaultValueNode = $dom->createElement( 'default-value' );
309 switch ( $defaultValue )
310 {
311 case self::DEFAULT_EMTPY:
312 $defaultValueNode->setAttribute( 'type', 'empty' );
313 break;
314
315 case self::DEFAULT_CURRENT_DATE:
316 $defaultValueNode->setAttribute( 'type', 'current-date' );
317 break;
318 }
319 $attributeParametersNode->appendChild( $defaultValueNode );
320 }
321
322 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
323 {
324 $defaultNode = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 );
325 $defaultValue = strtolower( $defaultNode->getAttribute( 'type' ) );
326 switch ( $defaultValue )
327 {
328 case 'empty':
329 {
330 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_EMTPY );
331 } break;
332 case 'current-date':
333 {
334 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_CURRENT_DATE );
335 } break;
336 }
337 }
338
339 function serializeContentObjectAttribute( $package, $objectAttribute )
340 {
341 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
342
343 $stamp = $objectAttribute->attribute( 'data_int' );
344
345 if ( $stamp !== null )
346 {
347 $dom = $node->ownerDocument;
348 $dateNode = $dom->createElement( 'date' );
349 $dateNode->appendChild(
350 $dom->createTextNode(
351 eZDateUtils::rfc1123Date(
352 eZTimestamp::getLocalTimestampFromUtcTimestamp( $stamp )
353 )
354 )
355 );
356 $node->appendChild( $dateNode );
357 }
358 return $node;
359 }
360
361 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
362 {
363 $dateNode = $attributeNode->getElementsByTagName( 'date' )->item( 0 );
364 if ( is_object( $dateNode ) )
365 {
366 $timestamp = eZTimestamp::getUtcTimestampFromLocalTimestamp(
367 eZDateUtils::textToDate( $dateNode->textContent )
368 );
369 $objectAttribute->setAttribute( 'data_int', $timestamp );
370 }
371 }
372
373 function supportsBatchInitializeObjectAttribute()
374 {
375 return true;
376 }
377
378 function batchInitializeObjectAttributeData( $classAttribute )
379 {
380 $defaultType = $classAttribute->attribute( self::DEFAULT_FIELD );
381 if ( $defaultType == 1 )
382 {
383 $default = time();
384 return array( 'data_int' => $default,
385 'sort_key_int' => $default );
386 }
387 else
388 {
389 return array();
390 }
391 }
392 }
393
394 eZDataType::register( eZDateType::DATA_TYPE_STRING, "eZDateType" );
395
396 ?>
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZDataType\createContentObjectAttributeDOMNode
createContentObjectAttributeDOMNode( $objectAttribute)
Definition: ezdatatype.php:1371
eZDateTimeValidator\validateDate
static validateDate( $day, $month, $year)
Definition: ezdatetimevalidator.php:19
eZDateType
Stores a date value.
Definition: ezdatetype.php:19
eZDateType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezdatetype.php:229
eZDateType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezdatetype.php:20
eZDateType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezdatetype.php:373
eZDateType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezdatetype.php:268
eZDateType\validateCollectionAttributeHTTPInput
validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezdatetype.php:122
eZDateType\DEFAULT_CURRENT_DATE
const DEFAULT_CURRENT_DATE
Definition: ezdatetype.php:26
eZDateType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezdatetype.php:92
eZDateType\sortKey
sortKey( $contentObjectAttribute)
Definition: ezdatetype.php:293
eZDateType\validateDateTimeHTTPInput
validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute)
Definition: ezdatetype.php:35
eZDateType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezdatetype.php:322
eZDateType\initializeClassAttribute
initializeClassAttribute( $classAttribute)
Definition: ezdatetype.php:203
eZDateType\batchInitializeObjectAttributeData
batchInitializeObjectAttributeData( $classAttribute)
Definition: ezdatetype.php:378
eZDateType\unserializeContentObjectAttribute
unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode)
Definition: ezdatetype.php:361
eZDateType\DEFAULT_FIELD
const DEFAULT_FIELD
Definition: ezdatetype.php:22
eZDateType\fetchCollectionAttributeHTTPInput
fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute)
Definition: ezdatetype.php:157
eZDateType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezdatetype.php:288
eZDateType\title
title( $contentObjectAttribute, $name=null)
Definition: ezdatetype.php:281
eZDateType\isInformationCollector
isInformationCollector()
Definition: ezdatetype.php:245
eZDateType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezdatetype.php:303
eZDateType\toString
toString( $contentObjectAttribute)
Definition: ezdatetype.php:262
eZDateType\__construct
__construct()
Definition: ezdatetype.php:28
eZDateType\sortKeyType
sortKeyType()
Definition: ezdatetype.php:298
eZDateType\metaData
metaData( $contentObjectAttribute)
Definition: ezdatetype.php:253
eZDateType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezdatetype.php:50
eZDateType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezdatetype.php:190
eZDateType\DEFAULT_EMTPY
const DEFAULT_EMTPY
Definition: ezdatetype.php:24
eZDateType\isIndexable
isIndexable()
Definition: ezdatetype.php:240
eZDateType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezdatetype.php:213
eZDateType\serializeContentObjectAttribute
serializeContentObjectAttribute( $package, $objectAttribute)
Definition: ezdatetype.php:339
eZDateUtils\textToDate
static textToDate( $dateText)
Definition: ezdateutils.php:88
eZDateUtils\rfc1123Date
static rfc1123Date( $timestamp=false)
Definition: ezdateutils.php:34
eZDate
Locale aware date handler.
Definition: ezdate.php:58
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
eZTimestamp\getUtcTimestampFromLocalTimestamp
static getUtcTimestampFromLocalTimestamp( $localTimestamp)
Definition: eztimestamp.php:16
eZTimestamp\getLocalTimestampFromUtcTimestamp
static getLocalTimestampFromUtcTimestamp( $utcTimestamp)
Definition: eztimestamp.php:36
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.