Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatelocaleoperator.php
Go to the documentation of this file.
1 <?php
29 class eZTemplateLocaleOperator
30 {
36 public function __construct()
37 {
38 $this->Operators = array( 'l10n', 'locale', 'datetime', 'currentdate', 'maketime', 'makedate', 'gettime' );
39 $this->LocaleName = 'l10n';
40 $this->LocaleFetchName = 'locale';
41 $this->DateTimeName = 'datetime';
42 $this->CurrentDateName = 'currentdate';
43 $this->MakeTimeName = 'maketime';
44 $this->MakeDateName = 'makedate';
45 $this->GetTimeName = 'gettime';
46 }
47
51 function operatorList()
52 {
53 return $this->Operators;
54 }
55
59 function operatorTemplateHints()
60 {
61 $hints = array(
62 $this->LocaleName => array( 'input' => true, 'output' => true, 'parameters' => true,
63 'transform-parameters' => true, 'input-as-parameter' => 'always',
64 'element-transformation' => true,
65 'element-transformation-func' => 'l10nTransformation' ),
66 $this->LocaleFetchName => array( 'input' => true, 'output' => true, 'parameters' => true,
67 'transform-parameters' => true, 'input-as-parameter' => 'always',
68 'element-transformation' => false ),
69 $this->DateTimeName => array( 'input' => true, 'output' => true, 'parameters' => true,
70 'transform-parameters' => true, 'input-as-parameter' => 'always',
71 'element-transformation' => true,
72 'element-transformation-func' => 'dateTimeTransformation' ),
73 $this->CurrentDateName => array( 'input' => false, 'output' => true, 'parameters' => false,
74 'transform-parameters' => true, 'input-as-parameter' => false,
75 'element-transformation' => true,
76 'element-transformation-func' => 'currentDateTransformation' ),
77 $this->MakeTimeName => array( 'input' => true, 'output' => true, 'parameters' => true,
78 'transform-parameters' => true, 'input-as-parameter' => false,
79 'element-transformation' => true,
80 'element-transformation-func' => 'makeDateTimeTransformation' ),
81 $this->MakeDateName => array( 'input' => true, 'output' => true, 'parameters' => true,
82 'transform-parameters' => true, 'input-as-parameter' => false,
83 'element-transformation' => true,
84 'element-transformation-func' => 'makeDateTimeTransformation' ),
85 $this->GetTimeName => array( 'input' => true, 'output' => true, 'parameters' => 1,
86 'transform-parameters' => true, 'input-as-parameter' => false,
87 'element-transformation' => true,
88 'element-transformation-func' => 'getTimeTransformation' )
89 );
90 return $hints;
91 }
92
96 function namedParameterPerOperator()
97 {
98 return true;
99 }
100
104 function namedParameterList()
105 {
106 return array( 'l10n' => array( 'type' => array( 'type' => 'string', 'required' => true, 'default' => false ),
107 'locale' => array( 'type' => 'string', 'required' => false, 'default' => false ),
108 'param' => array( 'type' => 'string', 'required' => false, 'default' => false ) ),
109 'datetime' => array( 'class' => array( 'type' => 'string', 'required' => true, 'default' => false ),
110 'data' => array( 'type' => 'mixed', 'required' => false, 'default' => false ) ),
111 'gettime' => array( 'timestamp' => array( 'type' => 'integer', 'required' => false, 'default' => false ) ),
112 'maketime' => array( 'hour' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
113 'minute' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
114 'second' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
115 'month' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
116 'day' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
117 'year' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
118 'dst' => array( 'type' => 'integer', 'required' => false, 'default' => false ) ),
119 'makedate' => array( 'month' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
120 'day' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
121 'year' => array( 'type' => 'integer', 'required' => false, 'default' => false ),
122 'dst' => array( 'type' => 'integer', 'required' => false, 'default' => false ) ) );
123 }
124
128 function l10nTransformation( $operatorName, &$node, $tpl, &$resourceData,
129 $element, $lastElement, $elementList, $elementTree, &$parameters )
130 {
131 $values = array();
132 $newElements = array();
133
134 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation begin' . "\n" );
135 $values[] = $parameters[0];
136
137 if ( count( $parameters ) > 2 )
138 {
139 $values[] = $parameters[2];
140 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "\$locale = eZLocale::instance( %2% );\n", $values );
141 }
142 else
143 {
144 $values[] = false;
145 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "\$locale = eZLocale::instance();\n" );
146 }
147
148 if ( !eZTemplateNodeTool::isConstantElement( $parameters[1] ) )
149 {
150 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation: not static' . "\n" );
151 $values[] = $parameters[1];
152
153 $code = "%tmp1% = \$locale->getFormattingFunction( %3% );\n";
154 $code .= "if ( %tmp1% )\n";
155 $code .= "{\n";
156 $code .= " if ( %3% === 'currency' )\n";
157 if ( count( $parameters ) > 3 )
158 {
159 $values[] = $parameters[3];
160 $code .= " if( %4% === false )\n";
161 $code .= " %output% = \$locale->%tmp1%( %1%, \$locale->attribute( 'currency_symbol' ) );\n";
162 $code .= " else\n";
163 $code .= " %output% = \$locale->%tmp1%( %1%, %4% );\n";
164
165 }
166 else
167 {
168 $code .= " %output% = \$locale->%tmp1%( %1%, \$locale->attribute( 'currency_symbol' ) );\n";
169 }
170 $code .= " else\n";
171 $code .= " %output% = \$locale->%tmp1%( %1% );\n";
172 $code .= "}\n";
173 $code .= "else\n";
174 $code .= " %output% = %1%;\n";
175
176 $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, false, 1 );
177
178
179 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation end' . "\n" );
180 return $newElements;
181 }
182 else
183 {
184 $values[] = false;
185 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation: static' . "\n" );
186 if ( ( $function = eZTemplateNodeTool::elementConstantValue( $parameters[1] ) ) !== false )
187 {
188 $locale = eZLocale::instance();
189 $method = $locale->getFormattingFunction( $function );
190
191 if ( $method )
192 {
193 switch( $function )
194 {
195 case 'currency':
196 {
197 if ( count( $parameters ) > 3 )
198 {
199 $values[] = $parameters[3];
200 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "if( %4% === false)\n%output% = \$locale->$method( %1%, \$locale->attribute( 'currency_symbol' ) );\nelse\n%output% = \$locale->$method( %1%, %4% );\n", $values );
201 }
202 else
203 {
204 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = \$locale->$method( %1%, \$locale->attribute( 'currency_symbol' ) );\n", $values );
205 }
206
207 } break;
208 default:
209 {
210 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "\n%output% = \$locale->$method( %1% );\n", $values );
211 } break;
212 }
213 return $newElements;
214 }
215 }
216 }
217 }
218
219 function dateTimeTransformation( $operatorName, &$node, $tpl, &$resourceData,
220 $element, $lastElement, $elementList, $elementTree, &$parameters )
221 {
222 $values = array();
223 $newElements = array();
224 $paramCount = count( $parameters );
225 if ( $paramCount < 2 )
226 {
227 return false;
228 }
229 if ( !eZTemplateNodeTool::isConstantElement( $parameters[1] ) )
230 {
231 return false;
232 }
233 else
234 {
235 $class = eZTemplateNodeTool::elementConstantValue( $parameters[1] );
236 }
237 if ( ( $class == 'custom' ) && ( $paramCount != 3 ) )
238 {
239 return false;
240 }
241
242 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '$locale = eZLocale::instance();' . "\n" );
243
244 if ( $class == 'custom' )
245 {
246 $values[] = $parameters[0];
247 $values[] = $parameters[2];
248 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = \$locale->formatDateTimeType( %2%, %1% );\n", $values );
249 return $newElements;
250
251 }
252 else
253 {
254 $dtINI = eZINI::instance( 'datetime.ini' );
255 $formats = $dtINI->variable( 'ClassSettings', 'Formats' );
256 if ( array_key_exists( $class, $formats ) )
257 {
258 $classFormat = addcslashes( $formats[$class], "'" );
259 $values[] = $parameters[0];
260 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = \$locale->formatDateTimeType( '$classFormat', %1% );\n", $values );
261 return $newElements;
262 }
263 }
264 return false;
265 }
266
267 function currentDateTransformation( $operatorName, &$node, $tpl, &$resourceData,
268 $element, $lastElement, $elementList, $elementTree, &$parameters )
269 {
270 $newElements = array();
271 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = time();\n" );
272 return $newElements;
273 }
274
275 function makeDateTimeTransformation( $operatorName, &$node, $tpl, &$resourceData,
276 $element, $lastElement, $elementList, $elementTree, &$parameters )
277 {
278 $values = array();
279 $arguments = array();
280 $newElements = array();
281 $paramCount = count( $parameters );
282
283 if ( $operatorName == 'makedate' )
284 {
285 $arguments = array ( 0, 0, 0 );
286 }
287 for ( $i = 0; $i < $paramCount; ++$i )
288 {
289 if ( $parameters[$i] === null )
290 {
291 break;
292 }
293 $values[] = $parameters[$i];
294 $arguments[] = '%' . ($i + 1) . '%';
295 }
296
297 $code = count( $arguments ) == 0 ? "%output% = time();\n" : "%output% = mktime( " . implode( ', ', $arguments ) . " );\n";
298 $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
299 return $newElements;
300 }
301
302 function getTimeTransformation( $operatorName, &$node, $tpl, &$resourceData,
303 $element, $lastElement, $elementList, $elementTree, &$parameters )
304 {
305 $newElements = array();
306 $values = array();
307 $paramCount = count( $parameters );
308
309 if ( $paramCount == 1 )
310 {
311 $values[] = $parameters[0];
312 $code = "%tmp1% = %1%;\n";
313 }
314 else if ( $paramCount == 0 )
315 {
316 $code = "%tmp1% = time();\n";
317 }
318 else
319 {
320 return false;
321 }
322 $newElements[] = eZTemplateNodeTool::createCodePieceElement(
323 $code .
324 "%tmp2% = getdate( %tmp1% );\n".
325 "%tmp3% = date( 'W', %tmp1% );\n".
326 "if ( %tmp2%['wday'] == 0 )\n{\n\t++%tmp3%;\n}\n".
327 "%output% = array( 'seconds' => %tmp2%['seconds'],
328 'minutes' => %tmp2%['minutes'],
329 'hours' => %tmp2%['hours'],
330 'day' => %tmp2%['mday'],
331 'month' => %tmp2%['mon'],
332 'year' => %tmp2%['year'],
333 'weeknumber' => %tmp3%,
334 'weekday' => %tmp2%['wday'],
335 'yearday' => %tmp2%['yday'],
336 'epoch' => %tmp2%[0] );\n", $values, false, 3);
337 return $newElements;
338 }
339
351 function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
352 $placement )
353 {
354 if ( $operatorName == $this->LocaleFetchName )
355 {
356 if ( $operatorValue !== null )
357 {
358 $localeString = $operatorValue;
359 }
360 else
361 {
362 if ( count( $operatorParameters ) < 1 )
363 {
364 $tpl->missingParameter( $operatorName, 'localestring' );
365 return;
366 }
367 $localeString = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement, true );
368 }
369 $locale = eZLocale::instance( $localeString );
370 $operatorValue = $locale;
371 return;
372 }
373 $locale = eZLocale::instance();
374 if ( $operatorName == $this->GetTimeName )
375 {
376 $timestamp = $operatorValue;
377 if ( $timestamp === null )
378 $timestamp = $namedParameters['timestamp'];
379
380 if ( !$timestamp )
381 $timestamp = time();
382
383 $info = getdate( $timestamp );
384 $week = date( 'W', $timestamp );
385 if ( $info['wday'] == 0 )
386 ++$week;
387 $operatorValue = array( 'seconds' => $info['seconds'],
388 'minutes' => $info['minutes'],
389 'hours' => $info['hours'],
390 'day' => $info['mday'],
391 'month' => $info['mon'],
392 'year' => $info['year'],
393 'weeknumber' => $week,
394 'weekday' => $info['wday'],
395 'yearday' => $info['yday'],
396 'epoch' => $info[0] );
397 }
398 else if ( $operatorName == $this->MakeTimeName )
399 {
400 $parameters = array();
401 if ( $namedParameters['hour'] !== false )
402 $parameters[] = $namedParameters['hour'];
403 if ( $namedParameters['minute'] !== false )
404 $parameters[] = $namedParameters['minute'];
405 if ( $namedParameters['second'] !== false )
406 $parameters[] = $namedParameters['second'];
407 if ( $namedParameters['month'] !== false )
408 $parameters[] = $namedParameters['month'];
409 {
410 if ( $namedParameters['day'] !== false )
411 $parameters[] = $namedParameters['day'];
412 {
413 if ( $namedParameters['year'] !== false )
414 $parameters[] = $namedParameters['year'];
415 {
416 if ( $namedParameters['dst'] !== false )
417 $parameters[] = $namedParameters['dst'];
418 }
419 }
420 }
421
422 $operatorValue = count( $parameters ) == 0 ? time() : call_user_func_array( 'mktime', $parameters );
423 }
424 else if ( $operatorName == $this->MakeDateName )
425 {
426 $parameters = array( 0, 0, 0 );
427 if ( $namedParameters['month'] !== false )
428 $parameters[] = $namedParameters['month'];
429 {
430 if ( $namedParameters['day'] !== false )
431 $parameters[] = $namedParameters['day'];
432 {
433 if ( $namedParameters['year'] !== false )
434 $parameters[] = $namedParameters['year'];
435 {
436 if ( $namedParameters['dst'] !== false )
437 $parameters[] = $namedParameters['dst'];
438 }
439 }
440 }
441 $operatorValue = call_user_func_array( 'mktime', $parameters );
442 }
443 else if ( $operatorName == $this->CurrentDateName )
444 {
445 $operatorValue = time();
446 }
447 else if ( $operatorName == $this->DateTimeName )
448 {
449 $class = $namedParameters['class'];
450 if ( $class === null )
451 return;
452 if ( $class == 'custom' )
453 {
454 $operatorValue = $locale->formatDateTimeType( $namedParameters['data'], $operatorValue );
455 }
456 else
457 {
458 $dtINI = eZINI::instance( 'datetime.ini' );
459 $formats = $dtINI->variable( 'ClassSettings', 'Formats' );
460 if ( array_key_exists( $class, $formats ) )
461 {
462 $classFormat = $formats[$class];
463 $operatorValue = $locale->formatDateTimeType( $classFormat, $operatorValue );
464 }
465 else
466 $tpl->error( $operatorName, "DateTime class '$class' is not defined", $placement );
467 }
468 }
469 else if ( $operatorName == $this->LocaleName )
470 {
471 $type = $namedParameters['type'];
472 if ( $type === null )
473 return;
474
475 $localeString = $namedParameters['locale'];
476 $param = $namedParameters['param'];
477
478 // change locale if need
479 if ( $localeString )
480 $locale = eZLocale::instance( $localeString );
481
482 $method = $locale->getFormattingFunction( $type );
483 if ( $method )
484 {
485 switch ( $type )
486 {
487 case 'currency':
488 {
489 if ( $param === false )
490 $param = $locale->attribute( 'currency_symbol' );
491
492 $operatorValue = $locale->$method( $operatorValue, $param );
493 } break;
494
495 default:
496 {
497 $operatorValue = $locale->$method( $operatorValue );
498 } break;
499 }
500 }
501 else
502 {
503 $tpl->error( $operatorName, "Unknown locale type: '$type'", $placement );
504 }
505 }
506 }
507
510 public $Operators;
512 public $Locale;
513
514 public $LocaleName;
515 public $DateTimeName;
516 public $CurrentDateName;
517 public $LocaleFetchName;
518 public $MakeTimeName;
519 public $MakeDateName;
520 public $GetTimeName;
521 }
522
523 ?>
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
eZTemplateLocaleOperator
Locale aware conversions and output using operator "l10n".
Definition: eztemplatelocaleoperator.php:30
eZTemplateLocaleOperator\$CurrentDateName
$CurrentDateName
Definition: eztemplatelocaleoperator.php:516
eZTemplateLocaleOperator\$GetTimeName
$GetTimeName
Definition: eztemplatelocaleoperator.php:520
eZTemplateLocaleOperator\currentDateTransformation
currentDateTransformation( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters)
Definition: eztemplatelocaleoperator.php:267
eZTemplateLocaleOperator\getTimeTransformation
getTimeTransformation( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters)
Definition: eztemplatelocaleoperator.php:302
eZTemplateLocaleOperator\$DateTimeName
$DateTimeName
Definition: eztemplatelocaleoperator.php:515
eZTemplateLocaleOperator\dateTimeTransformation
dateTimeTransformation( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters)
Definition: eztemplatelocaleoperator.php:219
eZTemplateLocaleOperator\$MakeTimeName
$MakeTimeName
Definition: eztemplatelocaleoperator.php:518
eZTemplateLocaleOperator\namedParameterList
namedParameterList()
Definition: eztemplatelocaleoperator.php:104
eZTemplateLocaleOperator\operatorTemplateHints
operatorTemplateHints()
Definition: eztemplatelocaleoperator.php:59
eZTemplateLocaleOperator\modify
modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement)
Definition: eztemplatelocaleoperator.php:351
eZTemplateLocaleOperator\__construct
__construct()
Definition: eztemplatelocaleoperator.php:36
eZTemplateLocaleOperator\$LocaleName
$LocaleName
Definition: eztemplatelocaleoperator.php:514
eZTemplateLocaleOperator\$LocaleFetchName
$LocaleFetchName
Definition: eztemplatelocaleoperator.php:517
eZTemplateLocaleOperator\l10nTransformation
l10nTransformation( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters)
Definition: eztemplatelocaleoperator.php:128
eZTemplateLocaleOperator\$MakeDateName
$MakeDateName
Definition: eztemplatelocaleoperator.php:519
eZTemplateLocaleOperator\$Locale
$Locale
A reference to the locale object.
Definition: eztemplatelocaleoperator.php:512
eZTemplateLocaleOperator\makeDateTimeTransformation
makeDateTimeTransformation( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters)
Definition: eztemplatelocaleoperator.php:275
eZTemplateLocaleOperator\$Operators
$Operators
Definition: eztemplatelocaleoperator.php:510
eZTemplateLocaleOperator\namedParameterPerOperator
namedParameterPerOperator()
Definition: eztemplatelocaleoperator.php:96
eZTemplateLocaleOperator\operatorList
operatorList()
Definition: eztemplatelocaleoperator.php:51
eZTemplateNodeTool\elementConstantValue
static elementConstantValue( $elements)
Definition: eztemplatenodetool.php:197
eZTemplateNodeTool\createCodePieceElement
static createCodePieceElement( $codePiece, $values=false, $placement=false, $tmpValues=false, $knownTypes=true)
Definition: eztemplatenodetool.php:437
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.