Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplateunitoperator.php
Go to the documentation of this file.
1 <?php
38 class eZTemplateUnitOperator
39 {
40 public $SIName;
41 public $Operators;
47 public function __construct( $name = "si" )
48 {
49 $this->SIName = $name;
50 $this->Operators = array( $name );
51 }
52
56 function operatorList()
57 {
58 return $this->Operators;
59 }
60
61 function operatorTemplateHints()
62 {
63 return array( $this->SIName => array( 'input' => true,
64 'output' => true,
65 'parameters' => true,
66 'element-transformation' => true,
67 'transform-parameters' => true,
68 'input-as-parameter' => 'always',
69 'element-transformation-func' => 'operatorTransform' ) );
70 }
71
72 function operatorTransform( $operatorName, &$node, $tpl, &$resourceData,
73 $element, $lastElement, $elementList, $elementTree, &$parameters )
74 {
75 if ( !eZTemplateNodeTool::isConstantElement( $parameters[1] ) ||
76 ( count( $parameters ) > 2 && !eZTemplateNodeTool::isConstantElement( $parameters[2] ) ) )
77 {
78 return false;
79 }
80
81 // We do not support non-static values for decimal_count, decimal_symbol and thousands_separator
82 if ( count( $parameters ) > 3 and
83 !eZTemplateNodeTool::isConstantElement( $parameters[3] ) )
84 return false;
85 if ( count( $parameters ) > 4 and
86 !eZTemplateNodeTool::isConstantElement( $parameters[4] ) )
87 return false;
88 if ( count( $parameters ) > 5 and
89 !eZTemplateNodeTool::isConstantElement( $parameters[5] ) )
90 return false;
91
92 $locale = eZLocale::instance();
93 $decimalCount = $locale->decimalCount();
94 $decimalSymbol = $locale->decimalSymbol();
95 $decimalThousandsSeparator = $locale->thousandsSeparator();
96
97 if ( count( $parameters ) > 2 )
98 {
99 $prefix = eZTemplateNodeTool::elementConstantValue( $parameters[2] );
100 }
101 else
102 {
103 $prefix = 'auto';
104 }
105
106 if ( count( $parameters ) > 3 )
107 $decimalCount = eZTemplateNodeTool::elementConstantValue( $parameters[3] );
108 elseif ( $prefix == 'none' )
109 $decimalCount = 0;
110
111 if ( count( $parameters ) > 4 )
112 $decimalSymbol = eZTemplateNodeTool::elementConstantValue( $parameters[4] );
113 if ( count( $parameters ) > 5 )
114 $decimalThousandsSeparator = eZTemplateNodeTool::elementConstantValue( $parameters[5] );
115
116 $decimalSymbolText = eZPHPCreator::variableText( $decimalSymbol, 0, 0, false );
117 $decimalThousandsSeparatorText = eZPHPCreator::variableText( $decimalThousandsSeparator, 0, 0, false );
118
119 $unit = eZTemplateNodeTool::elementConstantValue( $parameters[1] );
120
121 $ini = eZINI::instance();
122 if ( $prefix == "auto" )
123 {
124 $prefixes = $ini->variableArray( "UnitSettings", "BinaryUnits" );
125 if ( in_array( $unit, $prefixes ) )
126 $prefix = "binary";
127 else
128 $prefix = "decimal";
129 }
130
131 $unit_ini = eZINI::instance( "units.ini" );
132 $use_si = $ini->variable( "UnitSettings", "UseSIUnits" ) == "true";
133 $fake = $use_si ? "" : "Fake";
134 if ( $unit_ini->hasVariable( "Base", $unit ) )
135 {
136 $base = $unit_ini->variable( "Base", $unit );
137 }
138
139 $hasInput = false;
140 $output = false;
141 if ( eZTemplateNodeTool::isConstantElement( $parameters[0] ) )
142 {
143 $output = eZTemplateNodeTool::elementConstantValue( $parameters[0] );
144 $hasInput = true;
145 }
146
147 $prefix_var = "";
148 if ( $prefix == "decimal" )
149 {
150 $prefixes = $unit_ini->group( "DecimalPrefixes" );
151
152 $prefix_group = $unit_ini->group( "DecimalPrefixes" );
153 $prefixes = array();
154 foreach ( $prefix_group as $prefix_item )
155 {
156 $prefixes[] = explode( ";", $prefix_item );
157 }
158 usort( $prefixes, "eZTemplateUnitCompareFactor" );
159 $prefix_var = "";
160
161 if ( $hasInput )
162 {
163 if ( $output >= 0 and $output < 10 )
164 {
165 $prefix_var = '';
166 }
167 else
168 {
169 foreach ( $prefixes as $prefix )
170 {
171 $val = pow( 10, (int)$prefix[0] );
172 if ( $val <= $output )
173 {
174 $prefix_var = $prefix[1];
175 $output = number_format( $output / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
176 break;
177 }
178 }
179 }
180 }
181 else
182 {
183 $values = array();
184 $values[] = $parameters[0];
185 $values[] = array( eZTemplateNodeTool::createArrayElement( $prefixes ) );
186 $values[] = array( eZTemplateNodeTool::createStringElement( $base ) );
187
188 $code = 'if ( %1% >= 0 and %1% < 10 )' . "\n" .
189 '{' . "\n" .
190 ' %tmp3% = \'\';' . "\n" .
191 '}' . "\n" .
192 'else' . "\n" .
193 '{' . "\n" .
194 ' %tmp3% = "";' . "\n" .
195 ' foreach ( %2% as %tmp1% )' . "\n" .
196 ' {' . "\n" .
197 ' %tmp2% = pow( 10, (int)%tmp1%[0] );' . "\n" .
198 ' if ( %tmp2% <= %1% )' . "\n" .
199 ' {' . "\n" .
200 ' %tmp3% = %tmp1%[1];' . "\n" .
201 ' %1% = number_format( %1% / %tmp2%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' );' . "\n" .
202 ' break;' . "\n" .
203 ' }' . "\n" .
204 ' }' . "\n" .
205 '}' . "\n" .
206 '%output% = %1% . \' \' . %tmp3% . %3%;';
207
208 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values, false, 3 ) );
209 }
210 }
211 else if ( $prefix == "binary" )
212 {
213 $prefix_group = $unit_ini->group( $fake . "BinaryPrefixes" );
214 $prefixes = array();
215 foreach ( $prefix_group as $prefix_item )
216 {
217 $prefixes[] = explode( ";", $prefix_item );
218 }
219 usort( $prefixes, "eZTemplateUnitCompareFactor" );
220 $prefix_var = "";
221
222 if ( $hasInput )
223 {
224 if ( $output >= 0 and $output < 10 )
225 {
226 $prefix_var = '';
227 }
228 {
229 foreach ( $prefixes as $prefix )
230 {
231 $val = pow( 2, (int)$prefix[0] );
232 if ( $val <= $output )
233 {
234 $prefix_var = $prefix[1];
235 $output = number_format( $output / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
236 break;
237 }
238 }
239 }
240 }
241 else
242 {
243 $values = array();
244 $values[] = $parameters[0];
245 $values[] = array( eZTemplateNodeTool::createArrayElement( $prefixes ) );
246 $values[] = array( eZTemplateNodeTool::createStringElement( $base ) );
247
248 $code = 'if ( %1% >= 0 and %1% < 10 )' . "\n" .
249 '{' . "\n" .
250 ' %tmp3% = \'\';' . "\n" .
251 '}' . "\n" .
252 'else' . "\n" .
253 '{' . "\n" .
254 ' %tmp3% = "";' . "\n" .
255 ' foreach ( %2% as %tmp1% )' . "\n" .
256 ' {' . "\n" .
257 ' %tmp2% = pow( 2, (int)%tmp1%[0] );' . "\n" .
258 ' if ( %tmp2% <= %1% )' . "\n" .
259 ' {' . "\n" .
260 ' %tmp3% = %tmp1%[1];' . "\n" .
261 ' %1% = number_format( %1% / %tmp2%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' );' . "\n" .
262 ' break;' . "\n" .
263 ' }' . "\n" .
264 ' }' . "\n" .
265 '}' . "\n" .
266 '%output% = %1% . \' \' . %tmp3% . %3%;';
267
268 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values, false, 3 ) );
269 }
270 }
271 else
272 {
273 if ( $unit_ini->hasVariable( "BinaryPrefixes", $prefix ) )
274 {
275 $prefix_base = 2;
276 $prefix_var = $unit_ini->variableArray( "BinaryPrefixes", $prefix );
277 }
278 else if ( $unit_ini->hasVariable( "DecimalPrefixes", $prefix ) )
279 {
280 $prefix_base = 10;
281 $prefix_var = $unit_ini->variableArray( "DecimalPrefixes", $prefix );
282 }
283 else if ( $prefix == "none" )
284 {
285 $prefix_var = '';
286 if ( $hasInput )
287 {
288 $output = number_format( $output, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
289 }
290 else
291 {
292 $values = array();
293 $values[] = $parameters[0];
294 $values[] = array( eZTemplateNodeTool::createStringElement( '' ) );
295 $values[] = array( eZTemplateNodeTool::createStringElement( $base ) );
296
297 $code = '%output% = number_format( %1%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' ) . \' \' . %2% . %3%;';
298
299 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
300 }
301 }
302
303 if ( is_array( $prefix_var ) )
304 {
305 if ( $hasInput )
306 {
307 $val = pow( $prefix_base, (int)$prefix_var[0] );
308 $output = number_format( $output / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
309 $prefix_var = $prefix_var[1];
310 }
311 else
312 {
313 $values = array();
314 $values[] = $parameters[0];
315 $values[] = array( eZTemplateNodeTool::createNumericElement( pow( $prefix_base, (int)$prefix_var[0] ) ) );
316 $values[] = array( eZTemplateNodeTool::createStringElement( $prefix_var[1] ) );
317 $values[] = array( eZTemplateNodeTool::createStringElement( $base ) );
318
319 $code = '%output% = number_format( %1% / %2%, ' . $decimalCount . ', ' . $decimalSymbolText . ', ' . $decimalThousandsSeparatorText . ' ) . \' \' . %3% . %4%;';
320
321 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
322 }
323 }
324 }
325
326 if ( $hasInput )
327 {
328 return array( eZTemplateNodeTool::createStringElement( $output . ' ' . $prefix_var . $base ) );
329 }
330
331 $values = array();
332 $values[] = $parameters[0];
333 $values[] = array( eZTemplateNodeTool::createStringElement( $prefix_var ) );
334 $values[] = array( eZTemplateNodeTool::createStringElement( $base ) );
335
336 $code = '%output% = %1% . \' \' . %2% . %3%;';
337
338 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
339 }
340
344 function namedParameterList()
345 {
346 return array( "unit" => array( "type" => "string",
347 "required" => true,
348 "default" => false ),
349 "prefix" => array( "type" => "string",
350 "required" => false,
351 "default" => "auto" ),
352 "decimal_count" => array( "type" => "integer",
353 "required" => false,
354 "default" => false ),
355 "decimal_symbol" => array( "type" => "string",
356 "required" => false,
357 "default" => false ),
358 "thousands_separator" => array( "type" => "string",
359 "required" => false,
360 "default" => false ) );
361 }
362
366 function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
367 $placement )
368 {
369 $unit = $namedParameters["unit"];
370 $prefix = $namedParameters["prefix"];
371
372 $locale = eZLocale::instance();
373 $decimalCount = $locale->decimalCount();
374 $decimalSymbol = $locale->decimalSymbol();
375 $decimalThousandsSeparator = $locale->thousandsSeparator();
376
377 if ( $namedParameters['decimal_count'] !== false )
378 $decimalCount = $namedParameters['decimal_count'];
379 elseif ( $prefix == 'none' )
380 $decimalCount = 0;
381
382 if ( strlen( $namedParameters['decimal_symbol'] ) > 0 )
383 $decimalSymbol = $namedParameters['decimal_symbol'];
384 if ( strlen( $namedParameters['thousands_separator'] ) > 0 )
385 $decimalThousandsSeparator = $namedParameters['thousands_separator'];
386
387 $ini = eZINI::instance();
388 if ( $prefix == "auto" )
389 {
390 $prefixes = $ini->variableArray( "UnitSettings", "BinaryUnits" );
391 if ( in_array( $unit, $prefixes ) )
392 $prefix = "binary";
393 else
394 $prefix = "decimal";
395 }
396 $unit_ini = eZINI::instance( "units.ini" );
397 $use_si = $ini->variable( "UnitSettings", "UseSIUnits" ) == "true";
398 $fake = $use_si ? "" : "Fake";
399 if ( $unit_ini->hasVariable( "Base", $unit ) )
400 {
401 $base = $unit_ini->variable( "Base", $unit );
402 }
403 else
404 {
405 $tpl->warning( "eZTemplateUnitOperator", "No such unit '$unit'", $placement );
406 return;
407 }
408 $prefix_var = "";
409 if ( $prefix == "decimal" )
410 {
411 if ( $operatorValue >= 0 and $operatorValue < 10 )
412 {
413 $prefix_var = '';
414 }
415 else
416 {
417 $prefix_group = $unit_ini->group( "DecimalPrefixes" );
418 $prefixes = array();
419 foreach ( $prefix_group as $prefix_item )
420 {
421 $prefixes[] = explode( ";", $prefix_item );
422 }
423 usort( $prefixes, "eZTemplateUnitCompareFactor" );
424 $prefix_var = "";
425 $divider = false;
426 foreach ( $prefixes as $prefix )
427 {
428 $val = pow( 10, (int)$prefix[0] );
429 if ( $val <= $operatorValue )
430 {
431 $prefix_var = $prefix[1];
432 $operatorValue = number_format( $operatorValue / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
433 break;
434 }
435 }
436 }
437 }
438 else if ( $prefix == "binary" )
439 {
440 if ( $operatorValue >= 0 and $operatorValue < 10 )
441 {
442 $prefix_var = '';
443 }
444 else
445 {
446 $prefix_group = $unit_ini->group( $fake . "BinaryPrefixes" );
447 $prefixes = array();
448 foreach ( $prefix_group as $prefix_item )
449 {
450 $prefixes[] = explode( ";", $prefix_item );
451 }
452 usort( $prefixes, "eZTemplateUnitCompareFactor" );
453 $prefix_var = "";
454 foreach ( $prefixes as $prefix )
455 {
456 $val = pow( 2, (int)$prefix[0] );
457 if ( $val <= $operatorValue )
458 {
459 $prefix_var = $prefix[1];
460 $operatorValue = number_format( $operatorValue / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
461 break;
462 }
463 }
464 }
465 }
466 else
467 {
468 if ( $unit_ini->hasVariable( "BinaryPrefixes", $prefix ) )
469 {
470 $prefix_base = 2;
471 $prefix_var = $unit_ini->variableArray( "BinaryPrefixes", $prefix );
472 }
473 else if ( $unit_ini->hasVariable( "DecimalPrefixes", $prefix ) )
474 {
475 $prefix_base = 10;
476 $prefix_var = $unit_ini->variableArray( "DecimalPrefixes", $prefix );
477 }
478 else if ( $prefix == "none" )
479 {
480 $prefix_var = "";
481 $operatorValue = number_format( $operatorValue, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
482 }
483 else
484 $tpl->warning( $operatorName, "Prefix \"$prefix\" for unit \"$unit\" not found", $placement );
485 if ( is_array( $prefix_var ) )
486 {
487 $val = pow( $prefix_base, (int)$prefix_var[0] );
488 $operatorValue = number_format( $operatorValue / $val, $decimalCount, $decimalSymbol, $decimalThousandsSeparator );
489 $prefix_var = $prefix_var[1];
490 }
491 }
492 $operatorValue = "$operatorValue $prefix_var" . $base;
493 }
494
495 }
496
501 function eZTemplateUnitCompareFactor( $a, $b )
502 {
503 if ( $a[0] == $b[0] )
504 return 0;
505 return ( $a[0] > $b[0] ) ? -1 : 1;
506 }
507 ?>
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
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\createNumericElement
static createNumericElement( $number, $variablePlacement=false)
Definition: eztemplatenodetool.php:89
eZTemplateNodeTool\createStringElement
static createStringElement( $string, $variablePlacement=false)
Definition: eztemplatenodetool.php:79
eZTemplateNodeTool\createArrayElement
static createArrayElement( $array, $variablePlacement=false)
Definition: eztemplatenodetool.php:123
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
eZTemplateUnitOperator
Handles unit conversion and display using the operator "si".
Definition: eztemplateunitoperator.php:39
eZTemplateUnitOperator\$Operators
$Operators
Definition: eztemplateunitoperator.php:41
eZTemplateUnitOperator\modify
modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement)
Definition: eztemplateunitoperator.php:366
eZTemplateUnitOperator\$SIName
$SIName
Definition: eztemplateunitoperator.php:40
eZTemplateUnitOperator\operatorTemplateHints
operatorTemplateHints()
Definition: eztemplateunitoperator.php:61
eZTemplateUnitOperator\namedParameterList
namedParameterList()
Definition: eztemplateunitoperator.php:344
eZTemplateUnitOperator\operatorList
operatorList()
Definition: eztemplateunitoperator.php:56
eZTemplateUnitOperator\__construct
__construct( $name="si")
Definition: eztemplateunitoperator.php:47
eZTemplateUnitOperator\operatorTransform
operatorTransform( $operatorName, &$node, $tpl, &$resourceData, $element, $lastElement, $elementList, $elementTree, &$parameters)
Definition: eztemplateunitoperator.php:72
eZTemplateUnitCompareFactor
eZTemplateUnitCompareFactor( $a, $b)
Definition: eztemplateunitoperator.php:501
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.