Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztimetype.php
Go to the documentation of this file.
1 <?php
18 class eZTimeType extends eZDataType
19 {
20 const DATA_TYPE_STRING = "eztime";
21 const DEFAULT_FIELD = 'data_int1';
22 const USE_SECONDS_FIELD = 'data_int2';
23 const DEFAULT_EMTPY = 0;
24 const DEFAULT_CURRENT_DATE = 1;
25
26 public function __construct()
27 {
28 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Time", 'Datatype name' ),
29 array( 'serialize_supported' => true ) );
30 }
31
35 function validateTimeHTTPInput( $hours, $minute, $second, $contentObjectAttribute )
36 {
37 $state = eZDateTimeValidator::validateTime( $hours, $minute, $second );
38 if ( $state == eZInputValidator::STATE_INVALID )
39 {
40 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
41 'Invalid time.' ) );
42 return eZInputValidator::STATE_INVALID;
43 }
44 return $state;
45 }
46
47 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
48 {
49 $classAttribute = $contentObjectAttribute->contentClassAttribute();
50 $useSeconds = ( $classAttribute->attribute( self::USE_SECONDS_FIELD ) == 1 );
51
52 if ( $http->hasPostVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
53 $http->hasPostVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) ) and
54 ( !$useSeconds or $http->hasPostVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) ) )
55 {
56 $hours = $http->postVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) );
57 $minute = $http->postVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) );
58 $second = $useSeconds ? $http->postVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) : 0;
59
60 if ( $hours == '' or $minute == '' or ( $useSeconds and $second == '' ) )
61 {
62 if ( !( $hours == '' and
63 $minute == '' and
64 ( !$useSeconds or $second == '' ) ) or
65 ( !$classAttribute->attribute( 'is_information_collector' ) and
66 $contentObjectAttribute->validateIsRequired() ) )
67 {
68 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
69 'Time input required.' ) );
70 return eZInputValidator::STATE_INVALID;
71 }
72 else
73 return eZInputValidator::STATE_ACCEPTED;
74 }
75 else
76 {
77 return $this->validateTimeHTTPInput( $hours, $minute, $second, $contentObjectAttribute );
78 }
79 }
80 else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
81 {
82 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Time input required.' ) );
83 return eZInputValidator::STATE_INVALID;
84 }
85 else
86 return eZInputValidator::STATE_ACCEPTED;
87 }
88
89 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
90 {
91 $classAttribute = $contentObjectAttribute->contentClassAttribute();
92 $useSeconds = ( $classAttribute->attribute( self::USE_SECONDS_FIELD ) == 1 );
93
94 if ( $http->hasPostVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
95 $http->hasPostVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) ) and
96 ( !$useSeconds or $http->hasPostVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) ) )
97 {
98 $hours = $http->postVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) );
99 $minute = $http->postVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) );
100 $second = $useSeconds ? $http->postVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) : 0;
101
102 if ( $hours != '' or $minute != '' or ( $useSeconds and $second != '' ) )
103 {
104 $time = new eZTime();
105 $time->setHMS( $hours, $minute, $second );
106 $contentObjectAttribute->setAttribute( 'data_int', $time->timeOfDay() );
107 }
108 else
109 {
110 $contentObjectAttribute->setAttribute( 'data_int', null );
111 }
112 return true;
113 }
114 return false;
115 }
116
117 function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
118 {
119 $classAttribute = $contentObjectAttribute->contentClassAttribute();
120 $useSeconds = ( $classAttribute->attribute( self::USE_SECONDS_FIELD ) == 1 );
121
122 if ( $http->hasPostVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
123 $http->hasPostVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) ) and
124 ( !$useSeconds or $http->hasPostVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) ) )
125 {
126 $hours = $http->postVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) );
127 $minute = $http->postVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) );
128 $second = $useSeconds ? $http->postVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) : 0;
129
130 if ( $hours == '' or $minute == '' or ( $useSeconds and $second == '' ) )
131 {
132 if ( !( $hours == '' and $minute == '' and ( !$useSeconds or $second == '' ) ) or
133 $contentObjectAttribute->validateIsRequired() )
134 {
135 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
136 'Time input required.' ) );
137 return eZInputValidator::STATE_INVALID;
138 }
139 else
140 return eZInputValidator::STATE_ACCEPTED;
141 }
142 else
143 {
144 return $this->validateTimeHTTPInput( $hours, $minute, $second, $contentObjectAttribute );
145 }
146 }
147 else
148 return eZInputValidator::STATE_INVALID;
149 }
150
154 function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
155 {
156 $classAttribute = $contentObjectAttribute->contentClassAttribute();
157 $useSeconds = ( $classAttribute->attribute( self::USE_SECONDS_FIELD ) == 1 );
158
159 if ( $http->hasPostVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
160 $http->hasPostVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) ) and
161 ( !$useSeconds or $http->hasPostVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) ) )
162 {
163 $hours = $http->postVariable( $base . '_time_hour_' . $contentObjectAttribute->attribute( 'id' ) );
164 $minute = $http->postVariable( $base . '_time_minute_' . $contentObjectAttribute->attribute( 'id' ) );
165 $second = $useSeconds ? $http->postVariable( $base . '_time_second_' . $contentObjectAttribute->attribute( 'id' ) ) : 0;
166
167 if ( $hours != '' or $minute != '' or ( $useSeconds and $second != '' ) )
168 {
169 $time = new eZTime();
170 $time->setHMS( $hours, $minute, $second );
171 $collectionAttribute->setAttribute( 'data_int', $time->timeOfDay() );
172 }
173 else
174 $collectionAttribute->setAttribute( 'data_int', null );
175 return true;
176 }
177 return false;
178 }
179
183 function objectAttributeContent( $contentObjectAttribute )
184 {
185 $stamp = $contentObjectAttribute->attribute( 'data_int' );
186
187 if ( $stamp !== null )
188 {
189 $time = new eZTime( $stamp );
190
191 }
192 else
193 $time = array( 'timestamp' => '',
194 'time_of_day' => '',
195 'hour' => '',
196 'minute' => '',
197 'second' => '',
198 'is_valid' => false );
199 return $time;
200 }
201
202 function sortKey( $contentObjectAttribute )
203 {
204 $timestamp = $contentObjectAttribute->attribute( 'data_int' );
205 if ( $timestamp !== null )
206 {
207 $time = new eZTime( $timestamp );
208 return $time->timeOfDay();
209 }
210 else
211 return 0;
212 }
213
214 function sortKeyType()
215 {
216 return 'int';
217 }
218
223 function toString( $contentObjectAttribute )
224 {
225 $time = $contentObjectAttribute->attribute( 'content' );
226 if ( is_object( $time ) )
227 {
228 return $time->attribute( 'hour' ) . ':' . $time->attribute( 'minute' ) . ':' . $time->attribute( 'second' );
229 }
230 else
231 return '';
232 }
233
234 function fromString( $contentObjectAttribute, $string )
235 {
236 if ( $string != '' )
237 {
238 [ $hour, $minute, $second ] = explode( ':', $string ) + [ '', '', '' ];
239 if ( $hour == '' || $minute == '' )
240 return false;
241 if ( $second == '' )
242 {
243 $second = 0;
244 }
245 $time = new eZTime();
246 $time->setHMS( $hour, $minute, $second );
247 $contentObjectAttribute->setAttribute( 'data_int', $time->timeOfDay() );
248 }
249
250 return true;
251 }
252
253 function isInformationCollector()
254 {
255 return true;
256 }
257
261 function initializeClassAttribute( $classAttribute )
262 {
263 if ( $classAttribute->attribute( self::DEFAULT_FIELD ) == null )
264 $classAttribute->setAttribute( self::DEFAULT_FIELD, 0 );
265 $classAttribute->store();
266 }
267
271 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
272 {
273 if ( $currentVersion != false )
274 {
275 $dataInt = $originalContentObjectAttribute->attribute( 'data_int' );
276 $contentObjectAttribute->setAttribute( 'data_int', $dataInt );
277 }
278 else
279 {
280 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
281 $defaultType = $contentClassAttribute->attribute( self::DEFAULT_FIELD );
282
283 if ( $defaultType == 1 )
284 {
285 $time = new eZTime();
286 $contentObjectAttribute->setAttribute( 'data_int', $time->timeOfDay() );
287 }
288 }
289 }
290
291 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
292 {
293 $default = $base . "_eztime_default_" . $classAttribute->attribute( 'id' );
294 if ( $http->hasPostVariable( $default ) )
295 {
296 $defaultValue = $http->postVariable( $default );
297 $classAttribute->setAttribute( self::DEFAULT_FIELD, $defaultValue );
298
299 $useSeconds = $base . "_eztime_use_seconds_" . $classAttribute->attribute( 'id' );
300 $classAttribute->setAttribute( self::USE_SECONDS_FIELD, $http->hasPostVariable( $useSeconds ) ? 1 : 0 );
301
302 return true;
303 }
304 return false;
305 }
306
310 function metaData( $contentObjectAttribute )
311 {
312 return (int)$contentObjectAttribute->attribute( 'data_int' );
313 }
314
318 function title( $contentObjectAttribute, $name = null )
319 {
320 $timestamp = $contentObjectAttribute->attribute( 'data_int' );
321 $locale = eZLocale::instance();
322
323 if ( $timestamp !== null )
324 {
325 $time = new eZTime( $timestamp );
326 return $locale->formatTime( $time->timeStamp() );
327 }
328 return '';
329 }
330
331 function hasObjectAttributeContent( $contentObjectAttribute )
332 {
333 return $contentObjectAttribute->attribute( 'data_int' ) !== null;
334 }
335
336 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
337 {
338 $dom = $attributeParametersNode->ownerDocument;
339
340 $defaultValue = $classAttribute->attribute( self::DEFAULT_FIELD );
341 $defaultValueNode = $dom->createElement( 'default-value' );
342 switch ( $defaultValue )
343 {
344 case self::DEFAULT_EMTPY:
345 $defaultValueNode->setAttribute( 'type', 'empty' );
346 break;
347
348 case self::DEFAULT_CURRENT_DATE:
349 $defaultValueNode->setAttribute( 'type', 'current-date' );
350 break;
351 }
352 $attributeParametersNode->appendChild( $defaultValueNode );
353
354 $useSeconds = $classAttribute->attribute( self::USE_SECONDS_FIELD );
355 $useSecondsNode = $dom->createElement( 'use-seconds' );
356 $useSecondsNode->appendChild( $dom->createTextNode( $useSeconds ) );
357 $attributeParametersNode->appendChild( $useSecondsNode );
358 }
359
360 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
361 {
362 $defaultNode = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 );
363 $defaultValue = strtolower( $defaultNode->getAttribute( 'type' ) );
364 switch ( $defaultValue )
365 {
366 case 'empty':
367 {
368 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_EMTPY );
369 } break;
370 case 'current-date':
371 {
372 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_CURRENT_DATE );
373 } break;
374 }
375
376 $useSecondsNode = $attributeParametersNode->getElementsByTagName( 'use-seconds' )->item( 0 );
377 if ( $useSecondsNode && $useSecondsNode->textContent === '1' )
378 {
379 $classAttribute->setAttribute( self::USE_SECONDS_FIELD, 1 );
380 }
381 }
382
389 function serializeContentObjectAttribute( $package, $objectAttribute )
390 {
391 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
392
393 $stamp = $objectAttribute->attribute( 'data_int' );
394
395 if ( $stamp !== null )
396 {
397 $dom = $node->ownerDocument;
398 $dateNode = $dom->createElement( 'time' );
399 $dateNode->appendChild( $dom->createTextNode( eZDateUtils::rfc1123Date( $stamp ) ) );
400 $node->appendChild( $dateNode );
401 }
402 return $node;
403 }
404
410 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
411 {
412 $timeNode = $attributeNode->getElementsByTagName( 'time' )->item( 0 );
413 if ( is_object( $timeNode ) )
414 {
415 $timestamp = eZDateUtils::textToDate( $timeNode->textContent );
416 $timeOfDay = null;
417 if ( $timestamp >= 0 )
418 {
419 $time = new eZTime( $timestamp );
420 $timeOfDay = $time->timeOfDay();
421 }
422 $objectAttribute->setAttribute( 'data_int', $timeOfDay );
423 }
424 }
425
426 function supportsBatchInitializeObjectAttribute()
427 {
428 return true;
429 }
430
431 function batchInitializeObjectAttributeData( $classAttribute )
432 {
433 $defaultType = $classAttribute->attribute( self::DEFAULT_FIELD );
434
435 if ( $defaultType == 1 )
436 {
437 $time = new eZTime();
438 $default = $time->timeOfDay();
439 return array( 'data_int' => $default, 'sort_key_int' => $default );
440 }
441
442 return array();
443 }
444 }
445
446 eZDataType::register( eZTimeType::DATA_TYPE_STRING, "eZTimeType" );
447
448 ?>
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\validateTime
static validateTime( $hour, $minute, $second=0)
Definition: ezdatetimevalidator.php:31
eZDateUtils\textToDate
static textToDate( $dateText)
Definition: ezdateutils.php:88
eZDateUtils\rfc1123Date
static rfc1123Date( $timestamp=false)
Definition: ezdateutils.php:34
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
eZTimeType
Stores a time value.
Definition: eztimetype.php:19
eZTimeType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: eztimetype.php:89
eZTimeType\fetchCollectionAttributeHTTPInput
fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute)
Definition: eztimetype.php:154
eZTimeType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: eztimetype.php:20
eZTimeType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: eztimetype.php:426
eZTimeType\batchInitializeObjectAttributeData
batchInitializeObjectAttributeData( $classAttribute)
Definition: eztimetype.php:431
eZTimeType\metaData
metaData( $contentObjectAttribute)
Definition: eztimetype.php:310
eZTimeType\fromString
fromString( $contentObjectAttribute, $string)
Definition: eztimetype.php:234
eZTimeType\DEFAULT_EMTPY
const DEFAULT_EMTPY
Definition: eztimetype.php:23
eZTimeType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: eztimetype.php:271
eZTimeType\__construct
__construct()
Definition: eztimetype.php:26
eZTimeType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: eztimetype.php:360
eZTimeType\USE_SECONDS_FIELD
const USE_SECONDS_FIELD
Definition: eztimetype.php:22
eZTimeType\isInformationCollector
isInformationCollector()
Definition: eztimetype.php:253
eZTimeType\title
title( $contentObjectAttribute, $name=null)
Definition: eztimetype.php:318
eZTimeType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: eztimetype.php:336
eZTimeType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: eztimetype.php:331
eZTimeType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: eztimetype.php:47
eZTimeType\DEFAULT_FIELD
const DEFAULT_FIELD
Definition: eztimetype.php:21
eZTimeType\DEFAULT_CURRENT_DATE
const DEFAULT_CURRENT_DATE
Definition: eztimetype.php:24
eZTimeType\toString
toString( $contentObjectAttribute)
Definition: eztimetype.php:223
eZTimeType\serializeContentObjectAttribute
serializeContentObjectAttribute( $package, $objectAttribute)
Definition: eztimetype.php:389
eZTimeType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: eztimetype.php:291
eZTimeType\validateTimeHTTPInput
validateTimeHTTPInput( $hours, $minute, $second, $contentObjectAttribute)
Definition: eztimetype.php:35
eZTimeType\initializeClassAttribute
initializeClassAttribute( $classAttribute)
Definition: eztimetype.php:261
eZTimeType\validateCollectionAttributeHTTPInput
validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: eztimetype.php:117
eZTimeType\sortKey
sortKey( $contentObjectAttribute)
Definition: eztimetype.php:202
eZTimeType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: eztimetype.php:183
eZTimeType\sortKeyType
sortKeyType()
Definition: eztimetype.php:214
eZTimeType\unserializeContentObjectAttribute
unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode)
Definition: eztimetype.php:410
eZTime
Locale aware time handler.
Definition: eztime.php:59
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+.