| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZTemplateLocaleOperator class 4 // 5 // Created on: <01-Mar-2002 13:49:40 amos> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZTemplateLocaleOperator eztemplatelocaleoperator.php 31 \ingroup eZTemplateOperators 32 \brief Locale aware conversions and output using operator "l10n" 33 34 This class takes care of converting variables and displaying them 35 according to their locale settings. 36 The class has one operator called l10n (short for localization) which 37 takes one parameter which is localization type. 38 Supported types are time, shorttime, date, shortdate, currency and number. 39 40 \code 41 // Example template code 42 {$curdate|l10n(date)} 43 {$cash|l10n(currency)} 44 \endcode 45 */ 46 47 include_once ( "lib/ezlocale/classes/ezlocale.php" ); 48 include_once ( 'lib/ezlocale/classes/ezdatetime.php' ); 49 50 class eZTemplateLocaleOperator 51 { 52 /*! 53 Initializes the object with the default locale. 54 \note Add support for specifying the locale object. 55 */ 56 function eZTemplateLocaleOperator() 57 { 58 $this->Operators = array( 'l10n', 'locale', 'datetime', 'currentdate', 'maketime', 'makedate', 'gettime' ); 59 $this->LocaleName = 'l10n'; 60 $this->LocaleFetchName = 'locale'; 61 $this->DateTimeName = 'datetime'; 62 $this->CurrentDateName = 'currentdate'; 63 $this->MakeTimeName = 'maketime'; 64 $this->MakeDateName = 'makedate'; 65 $this->GetTimeName = 'gettime'; 66 } 67 68 /*! 69 Returns array with l10n. 70 */ 71 function &operatorList() 72 { 73 return $this->Operators; 74 } 75 76 /*! 77 Returns a list with hints for the template compiler. 78 */ 79 function operatorTemplateHints() 80 { 81 $hints = array( 82 $this->LocaleName => array( 'input' => true, 'output' => true, 'parameters' => true, 83 'transform-parameters' => true, 'input-as-parameter' => 'always', 84 'element-transformation' => true, 85 'element-transformation-func' => 'l10nTransformation' ), 86 $this->LocaleFetchName => array( 'input' => true, 'output' => true, 'parameters' => true, 87 'transform-parameters' => true, 'input-as-parameter' => 'always', 88 'element-transformation' => false ), 89 $this->DateTimeName => array( 'input' => true, 'output' => true, 'parameters' => true, 90 'transform-parameters' => true, 'input-as-parameter' => 'always', 91 'element-transformation' => true, 92 'element-transformation-func' => 'dateTimeTransformation' ), 93 $this->CurrentDateName => array( 'input' => false, 'output' => true, 'parameters' => false, 94 'transform-parameters' => true, 'input-as-parameter' => false, 95 'element-transformation' => true, 96 'element-transformation-func' => 'currentDateTransformation' ), 97 $this->MakeTimeName => array( 'input' => true, 'output' => true, 'parameters' => true, 98 'transform-parameters' => true, 'input-as-parameter' => false, 99 'element-transformation' => true, 100 'element-transformation-func' => 'makeDateTimeTransformation' ), 101 $this->MakeDateName => array( 'input' => true, 'output' => true, 'parameters' => true, 102 'transform-parameters' => true, 'input-as-parameter' => false, 103 'element-transformation' => true, 104 'element-transformation-func' => 'makeDateTimeTransformation' ), 105 $this->GetTimeName => array( 'input' => true, 'output' => true, 'parameters' => 1, 106 'transform-parameters' => true, 'input-as-parameter' => false, 107 'element-transformation' => true, 108 'element-transformation-func' => 'getTimeTransformation' ) 109 ); 110 return $hints; 111 } 112 113 /*! 114 \return true to tell the template engine that the parameter list exists per operator type. 115 */ 116 function namedParameterPerOperator() 117 { 118 return true; 119 } 120 121 /*! 122 See eZTemplateOperator::namedParameterList 123 */ 124 function namedParameterList() 125 { 126 return array( 'l10n' => array( 'type' => array( 'type' => 'string', 'required' => true, 'default' => false ), 127 'locale' => array( 'type' => 'string', 'required' => false, 'default' => false ), 128 'param' => array( 'type' => 'string', 'required' => false, 'default' => false ) ), 129 'datetime' => array( 'class' => array( 'type' => 'string', 'required' => true, 'default' => false ), 130 'data' => array( 'type' => 'mixed', 'required' => false, 'default' => false ) ), 131 'gettime' => array( 'timestamp' => array( 'type' => 'integer', 'required' => false, 'default' => false ) ), 132 'maketime' => array( 'hour' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 133 'minute' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 134 'second' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 135 'month' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 136 'day' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 137 'year' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 138 'dst' => array( 'type' => 'integer', 'required' => false, 'default' => false ) ), 139 'makedate' => array( 'month' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 140 'day' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 141 'year' => array( 'type' => 'integer', 'required' => false, 'default' => false ), 142 'dst' => array( 'type' => 'integer', 'required' => false, 'default' => false ) ) ); 143 } 144 145 /*! 146 Transforms 147 */ 148 function l10nTransformation( $operatorName, &$node, &$tpl, &$resourceData, 149 &$element, &$lastElement, &$elementList, &$elementTree, &$parameters ) 150 { 151 $values = array(); 152 $newElements = array(); 153 154 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation begin' . "\n" ); 155 $newElements[] = eZTemplateNodeTool::createCodePieceElement( 'include_once ("lib/ezlocale/classes/ezlocale.php");' . "\n" ); 156 $values[] = $parameters[0]; 157 158 if ( count( $parameters ) > 2 ) 159 { 160 $values[] = $parameters[2]; 161 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "\$locale =& eZLocale::instance( %2% );\n", $values ); 162 } 163 else 164 { 165 $values[] = false; 166 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "\$locale =& eZLocale::instance();\n" ); 167 } 168 169 if ( !eZTemplateNodeTool::isStaticElement( $parameters[1] ) ) 170 { 171 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation: not static' . "\n" ); 172 $values[] = $parameters[1]; 173 174 $code = "%tmp1% = \$locale->getFormattingFunction( %3% );\n"; 175 $code .= "if ( %tmp1% )\n"; 176 $code .= "{\n"; 177 $code .= " if ( %3% === 'currency' )\n"; 178 if ( count( $parameters ) > 3 ) 179 { 180 $values[] = $parameters[3]; 181 $code .= " if( %4% === false )\n"; 182 $code .= " %output% = \$locale->%tmp1%( %1%, \$locale->attribute( 'currency_symbol' ) );\n"; 183 $code .= " else\n"; 184 $code .= " %output% = \$locale->%tmp1%( %1%, %4% );\n"; 185 186 } 187 else 188 { 189 $code .= " %output% = \$locale->%tmp1%( %1%, \$locale->attribute( 'currency_symbol' ) );\n"; 190 } 191 $code .= " else\n"; 192 $code .= " %output% = \$locale->%tmp1%( %1% );\n"; 193 $code .= "}\n"; 194 $code .= "else\n"; 195 $code .= " %output% = %1%;\n"; 196 197 $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, false, 1 ); 198 199 200 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation end' . "\n" ); 201 return $newElements; 202 } 203 else 204 { 205 $values[] = false; 206 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '// l10nTransformation: static' . "\n" ); 207 if ( ( $function = eZTemplateNodeTool::elementStaticValue( $parameters[1] ) ) !== false ) 208 { 209 $locale =& eZLocale::instance(); 210 $method = $locale->getFormattingFunction( $function ); 211 212 if ( $method ) 213 { 214 switch( $function ) 215 { 216 case 'currency': 217 { 218 if ( count( $parameters ) > 3 ) 219 { 220 $values[] = $parameters[3]; 221 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "if( %4% === false)\n%output% = \$locale->$method( %1%, \$locale->attribute( 'currency_symbol' ) );\nelse\n%output% = \$locale->$method( %1%, %4% );\n", $values ); 222 } 223 else 224 { 225 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = \$locale->$method( %1%, \$locale->attribute( 'currency_symbol' ) );\n", $values ); 226 } 227 228 } break; 229 default: 230 { 231 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "\n%output% = \$locale->$method( %1% );\n", $values ); 232 } break; 233 } 234 return $newElements; 235 } 236 } 237 } 238 } 239 240 function dateTimeTransformation( $operatorName, &$node, &$tpl, &$resourceData, 241 &$element, &$lastElement, &$elementList, &$elementTree, &$parameters ) 242 { 243 $values = array(); 244 $newElements = array(); 245 $paramCount = count( $parameters ); 246 if ( $paramCount < 2 ) 247 { 248 return false; 249 } 250 if ( !eZTemplateNodeTool::isStaticElement( $parameters[1] ) ) 251 { 252 return false; 253 } 254 else 255 { 256 $class = eZTemplateNodeTool::elementStaticValue( $parameters[1] ); 257 } 258 if ( ( $class == 'custom' ) && ( $paramCount != 3 ) ) 259 { 260 return false; 261 } 262 263 $newElements[] = eZTemplateNodeTool::createCodePieceElement( 'include_once ("lib/ezlocale/classes/ezlocale.php");' . "\n" ); 264 $newElements[] = eZTemplateNodeTool::createCodePieceElement( '$locale =& eZLocale::instance();' . "\n" ); 265 266 if ( $class == 'custom' ) 267 { 268 $values[] = $parameters[0]; 269 $values[] = $parameters[2]; 270 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = \$locale->formatDateTimeType( %2%, %1% );\n", $values ); 271 return $newElements; 272 273 } 274 else 275 { 276 $dtINI =& eZINI::instance( 'datetime.ini' ); 277 $formats = $dtINI->variable( 'ClassSettings', 'Formats' ); 278 if ( array_key_exists( $class, $formats ) ) 279 { 280 $classFormat = addcslashes( $formats[$class], "'" ); 281 $values[] = $parameters[0]; 282 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = \$locale->formatDateTimeType( '$classFormat', %1% );\n", $values ); 283 return $newElements; 284 } 285 } 286 return false; 287 } 288 289 function currentDateTransformation( $operatorName, &$node, &$tpl, &$resourceData, 290 &$element, &$lastElement, &$elementList, &$elementTree, &$parameters ) 291 { 292 $newElements = array(); 293 $newElements[] = eZTemplateNodeTool::createCodePieceElement( "%output% = time();\n" ); 294 return $newElements; 295 } 296 297 function makeDateTimeTransformation( $operatorName, &$node, &$tpl, &$resourceData, 298 &$element, &$lastElement, &$elementList, &$elementTree, &$parameters ) 299 { 300 $values = array(); 301 $arguments = array(); 302 $newElements = array(); 303 $paramCount = count( $parameters ); 304 305 $code = '%output% = mktime( '; 306 if ( $operatorName == 'makedate' ) 307 { 308 $arguments = array ( 0, 0, 0 ); 309 } 310 for ( $i = 0; $i < $paramCount; ++$i ) 311 { 312 if ( $parameters[$i] === null ) 313 { 314 break; 315 } 316 $values[] = $parameters[$i]; 317 $arguments[] = '%' . ($i + 1) . '%'; 318 } 319 $code .= implode( ', ', $arguments ) . " );\n"; 320 $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values ); 321 return $newElements; 322 } 323 324 function getTimeTransformation( $operatorName, &$node, &$tpl, &$resourceData, 325 &$element, &$lastElement, &$elementList, &$elementTree, &$parameters ) 326 { 327 $newElements = array(); 328 $values = array(); 329 $paramCount = count( $parameters ); 330 331 if ( $paramCount == 1 ) 332 { 333 $values[] = $parameters[0]; 334 $code = "%tmp1% = %1%;\n"; 335 } 336 else if ( $paramCount == 0 ) 337 { 338 $code = "%tmp1% = time();\n"; 339 } 340 else 341 { 342 return false; 343 } 344 $newElements[] = eZTemplateNodeTool::createCodePieceElement( 345 $code . 346 "%tmp2% = getdate( %tmp1% );\n". 347 "%tmp3% = date( 'W', %tmp1% );\n". 348 "if ( %tmp2%['wday'] == 0 )\n{\n\t++%tmp3%;\n}\n". 349 "%output% = array( 'seconds' => %tmp2%['seconds'], 350 'minutes' => %tmp2%['minutes'], 351 'hours' => %tmp2%['hours'], 352 'day' => %tmp2%['mday'], 353 'month' => %tmp2%['mon'], 354 'year' => %tmp2%['year'], 355 'weeknumber' => %tmp3%, 356 'weekday' => %tmp2%['wday'], 357 'yearday' => %tmp2%['yday'], 358 'epoch' => %tmp2%[0] );\n", $values, false, 3); 359 return $newElements; 360 } 361 362 /*! 363 Converts the variable according to the locale type. 364 Allowed types are: 365 - time 366 - shorttime 367 - date 368 - shortdate 369 - currency 370 - clean_currency 371 - number 372 */ 373 function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters, 374 $placement ) 375 { 376 if ( $operatorName == $this->LocaleFetchName ) 377 { 378 if ( $operatorValue !== null ) 379 { 380 $localeString = $operatorValue; 381 } 382 else 383 { 384 if ( count( $operatorParameters ) < 1 ) 385 { 386 $tpl->missingParameter( $operatorName, 'localestring' ); 387 return; 388 } 389 $localeString = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement, true ); 390 } 391 $locale =& eZLocale::instance( $localeString ); 392 $operatorValue = $locale; 393 return; 394 } 395 $locale =& eZLocale::instance(); 396 if ( $operatorName == $this->GetTimeName ) 397 { 398 $timestamp = $operatorValue; 399 if ( $timestamp === null ) 400 $timestamp = $namedParameters['timestamp']; 401 402 if ( !$timestamp ) 403 $timestamp = time(); 404 405 $info = getdate( $timestamp ); 406 $week = date( 'W', $timestamp ); 407 if ( $info['wday'] == 0 ) 408 ++$week; 409 $operatorValue = array( 'seconds' => $info['seconds'], 410 'minutes' => $info['minutes'], 411 'hours' => $info['hours'], 412 'day' => $info['mday'], 413 'month' => $info['mon'], 414 'year' => $info['year'], 415 'weeknumber' => $week, 416 'weekday' => $info['wday'], 417 'yearday' => $info['yday'], 418 'epoch' => $info[0] ); 419 } 420 else if ( $operatorName == $this->MakeTimeName ) 421 { 422 $parameters = array(); 423 if ( $namedParameters['hour'] !== false ) 424 $parameters[] = $namedParameters['hour']; 425 if ( $namedParameters['minute'] !== false ) 426 $parameters[] = $namedParameters['minute']; 427 if ( $namedParameters['second'] !== false ) 428 $parameters[] = $namedParameters['second']; 429 if ( $namedParameters['month'] !== false ) 430 $parameters[] = $namedParameters['month']; 431 { 432 if ( $namedParameters['day'] !== false ) 433 $parameters[] = $namedParameters['day']; 434 { 435 if ( $namedParameters['year'] !== false ) 436 $parameters[] = $namedParameters['year']; 437 { 438 if ( $namedParameters['dst'] !== false ) 439 $parameters[] = $namedParameters['dst']; 440 } 441 } 442 } 443 $operatorValue = call_user_func_array( 'mktime', $parameters ); 444 } 445 else if ( $operatorName == $this->MakeDateName ) 446 { 447 $parameters = array( 0, 0, 0 ); 448 if ( $namedParameters['month'] !== false ) 449 $parameters[] = $namedParameters['month']; 450 { 451 if ( $namedParameters['day'] !== false ) 452 $parameters[] = $namedParameters['day']; 453 { 454 if ( $namedParameters['year'] !== false ) 455 $parameters[] = $namedParameters['year']; 456 { 457 if ( $namedParameters['dst'] !== false ) 458 $parameters[] = $namedParameters['dst']; 459 } 460 } 461 } 462 $operatorValue = call_user_func_array( 'mktime', $parameters ); 463 } 464 else if ( $operatorName == $this->CurrentDateName ) 465 { 466 $operatorValue = time(); 467 } 468 else if ( $operatorName == $this->DateTimeName ) 469 { 470 $class = $namedParameters['class']; 471 if ( $class === null ) 472 return; 473 if ( $class == 'custom' ) 474 { 475 $operatorValue = $locale->formatDateTimeType( $namedParameters['data'], $operatorValue ); 476 } 477 else 478 { 479 $dtINI =& eZINI::instance( 'datetime.ini' ); 480 $formats = $dtINI->variable( 'ClassSettings', 'Formats' ); 481 if ( array_key_exists( $class, $formats ) ) 482 { 483 $classFormat = $formats[$class]; 484 $operatorValue = $locale->formatDateTimeType( $classFormat, $operatorValue ); 485 } 486 else 487 $tpl->error( $operatorName, "DateTime class '$class' is not defined", $placement ); 488 } 489 } 490 else if ( $operatorName == $this->LocaleName ) 491 { 492 $type = $namedParameters['type']; 493 if ( $type === null ) 494 return; 495 496 $localeString = $namedParameters['locale']; 497 $param = $namedParameters['param']; 498 499 // change locale if need 500 if ( $localeString ) 501 $locale =& eZLocale::instance( $localeString ); 502 503 $method = $locale->getFormattingFunction( $type ); 504 if ( $method ) 505 { 506 switch ( $type ) 507 { 508 case 'currency': 509 { 510 if ( $param === false ) 511 $param = $locale->attribute( 'currency_symbol' ); 512 513 $operatorValue = $locale->$method( $operatorValue, $param ); 514 } break; 515 516 default: 517 { 518 $operatorValue = $locale->$method( $operatorValue ); 519 } break; 520 } 521 } 522 else 523 { 524 $tpl->error( $operatorName, "Unknown locale type: '$type'", $placement ); 525 } 526 } 527 } 528 529 /// \privatesection 530 /// The operator array 531 var $Operators; 532 /// A reference to the locale object 533 var $Locale; 534 535 var $LocaleName; 536 var $DateTimeName; 537 var $CurrentDateName; 538 } 539 540 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |