ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ITX.php
Go to the documentation of this file.
1<?php
2//
3// +----------------------------------------------------------------------+
4// | Copyright (c) 1997-2005 Ulf Wendel, Pierre-Alain Joye |
5// +----------------------------------------------------------------------+
6// | This source file is subject to the New BSD license, That is bundled |
7// | with this package in the file LICENSE, and is available through |
8// | the world-wide-web at |
9// | http://www.opensource.org/licenses/bsd-license.php |
10// | If you did not receive a copy of the new BSD license and are unable |
11// | to obtain it through the world-wide-web, please send a note to |
12// | pajoye@php.net so we can mail you a copy immediately. |
13// +----------------------------------------------------------------------+
14// | Author: Ulf Wendel <ulf.wendel@phpdoc.de> |
15// | Pierre-Alain Joye <pajoye@php.net> |
16// +----------------------------------------------------------------------+
17//
18// $Id: ITX.php,v 1.16 2006/08/17 15:47:22 dsp Exp $
19//
20
21
40{
47 var $warn = array();
48
55 var $printWarning = false;
56
63 var $haltOnWarning = false;
64
70
75 var $functionPrefix = 'func_';
76
81 var $functionnameRegExp = '[_a-zA-Z]+[A-Za-z_0-9]*';
82
92
98 var $functions = array();
99
105 var $callback = array();
106
116 function __construct($root = '')
117 {
118
119 $this->checkblocknameRegExp = '@' . $this->blocknameRegExp . '@';
120 $this->functionRegExp = '@' . $this->functionPrefix . '(' .
121 $this->functionnameRegExp . ')\s*\‍(@sm';
122
123 parent::__construct($root);
124 } // end func constructor
125
126 function init()
127 {
128 $this->free();
129 $this->buildFunctionlist();
130 $this->findBlocks($this->template);
131 // we don't need it any more
132 $this->template = '';
133 $this->buildBlockvariablelist();
134
135 } // end func init
136
162 function replaceBlock($block, $template, $keep_content = false)
163 {
164 if (!isset($this->blocklist[$block])) {
165 throw (new ilTemplateException("The block "."'$block'".
166 " does not exist in the template and thus it can't be replaced."));
167 }
168
169 if ($template == '') {
170 throw (new ilTemplateException('No block content given.'));
171 }
172
173 if ($keep_content) {
174 $blockdata = $this->blockdata[$block];
175 }
176
177 // remove all kinds of links to the block / data of the block
178 $this->removeBlockData($block);
179
180 $template = "<!-- BEGIN $block -->" . $template . "<!-- END $block -->";
181 $parents = $this->blockparents[$block];
182 $this->findBlocks($template);
183 $this->blockparents[$block] = $parents;
184
185 // KLUDGE: rebuild the list for all block - could be done faster
186 $this->buildBlockvariablelist();
187
188 if ($keep_content) {
189 $this->blockdata[$block] = $blockdata;
190 }
191
192 // old TODO - I'm not sure if we need this
193 // update caches
194
195 return true;
196 } // end func replaceBlock
197
207 function replaceBlockfile($block, $filename, $keep_content = false)
208 {
209 return $this->replaceBlock($block, $this->getFile($filename), $keep_content);
210 } // end func replaceBlockfile
211
243 function addBlock($placeholder, $blockname, $template)
244 {
245 // Don't trust any user even if it's a programmer or yourself...
246 if ($placeholder == '') {
247 throw (new ilTemplateException('No variable placeholder given.'));
248 } elseif ($blockname == '' ||
249 !preg_match($this->checkblocknameRegExp, $blockname)
250 ) {
251 throw (new ilTemplateException("No or invalid blockname '$blockname' given."));
252 } elseif ($template == '') {
253 throw (new ilTemplateException('No block content given.'));
254 } elseif (isset($this->blocklist[$blockname])) {
255 throw (new ilTemplateException('The block '.$blockname.' already exists.'));
256 }
257
258 // find out where to insert the new block
259 $parents = $this->findPlaceholderBlocks($placeholder);
260 if (count($parents) == 0) {
261 throw (new ilTemplateException("The variable placeholder".
262 " '$placeholder' was not found in the template."));
263 return;
264 } elseif (count($parents) > 1) {
265
266 reset($parents);
267 while (list($k, $parent) = each($parents)) {
268 $msg .= "$parent, ";
269 }
270 $msg = substr($parent, -2);
271 throw (new ilTemplateException("The variable placeholder "."'$placeholder'".
272 " must be unique, found in multiple blocks '$msg'."));
273 }
274
275 $template = "<!-- BEGIN $blockname -->" . $template . "<!-- END $blockname -->";
276 $this->findBlocks($template);
277 if ($this->flagBlocktrouble) {
278 return false; // findBlocks() already throws an exception
279 }
280 $this->blockinner[$parents[0]][] = $blockname;
281 $this->blocklist[$parents[0]] = preg_replace(
282 '@' . $this->openingDelimiter . $placeholder .
283 $this->closingDelimiter . '@',
284
285 $this->openingDelimiter . '__' . $blockname . '__' .
286 $this->closingDelimiter,
287
288 $this->blocklist[$parents[0]]
289 );
290
291 $this->deleteFromBlockvariablelist($parents[0], $placeholder);
292 $this->updateBlockvariablelist($blockname);
293
294 return true;
295 } // end func addBlock
296
307 function addBlockfile($placeholder, $blockname, $filename)
308 {
309 return $this->addBlock($placeholder, $blockname, $this->getFile($filename));
310 } // end func addBlockfile
311
326 function placeholderExists($placeholder, $block = '')
327 {
328 if ($placeholder == '') {
329 throw (new ilTemplateException('No placeholder name given.'));
330 }
331
332 if ($block != '' && !isset($this->blocklist[$block])) {
333 throw (new ilTemplateException("Unknown block '$block'."));
334 }
335
336 // name of the block where the given placeholder was found
337 $found = '';
338
339 if ($block != '') {
340 if (is_array($variables = $this->blockvariables[$block])) {
341 // search the value in the list of blockvariables
342 reset($variables);
343 while (list($k, $variable) = each($variables)) {
344 if ($k == $placeholder) {
345 $found = $block;
346 break;
347 }
348 }
349 }
350 } else {
351
352 // search all blocks and return the name of the first block that
353 // contains the placeholder
354 reset($this->blockvariables);
355 while (list($blockname, $variables) = each($this->blockvariables)){
356 if (is_array($variables) && isset($variables[$placeholder])) {
357 $found = $blockname;
358 break;
359 }
360 }
361 }
362
363 return $found;
364 } // end func placeholderExists
365
373 {
374 reset($this->functions);
375 while (list($func_id, $function) = each($this->functions)) {
376 if (isset($this->callback[$function['name']])) {
377 if ($this->callback[$function['name']]['expandParameters']) {
378 $callFunction = 'call_user_func_array';
379 } else {
380 $callFunction = 'call_user_func';
381 }
382
383 if ($this->callback[$function['name']]['object'] != '') {
384 $call =
385 $callFunction(
386 array(
387 &$GLOBALS[$this->callback[$function['name']]['object']],
388 $this->callback[$function['name']]['function']),
389 $function['args']
390 );
391
392 } else {
393 $call =
394 $callFunction(
395 $this->callback[$function['name']]['function'],
396 $function['args']
397 );
398 }
399 $this->variableCache['__function' . $func_id . '__'] = $call;
400 }
401 }
402
403 } // end func performCallback
404
412 {
413 return $this->functions;
414 } // end func getFunctioncalls
415
423 function setFunctioncontent($functionID, $replacement)
424 {
425 $this->variableCache['__function' . $functionID . '__'] = $replacement;
426 } // end func setFunctioncontent
427
475 function
476 setCallbackFunction($tplfunction, $callbackfunction, $callbackobject = '', $expandCallbackParameters=false)
477 {
478 if ($tplfunction == '' || $callbackfunction == '') {
479 throw (new ilTemplateException("No template function "."('$tplfunction')".
480 " and/or no callback function ('$callbackfunction') given."));
481 }
482 $this->callback[$tplfunction] = array(
483 'function' => $callbackfunction,
484 'object' => $callbackobject,
485 'expandParameters' => (boolean) $expandCallbackParameters
486 );
487
488 return true;
489 } // end func setCallbackFunction
490
503 {
504 $this->callback = $functions;
505 } // end func setCallbackFunctiontable
506
513 function removeBlockData($block)
514 {
515 if (isset($this->blockinner[$block])) {
516 foreach ($this->blockinner[$block] as $k => $inner) {
517 $this->removeBlockData($inner);
518 }
519
520 unset($this->blockinner[$block]);
521 }
522
523 unset($this->blocklist[$block]);
524 unset($this->blockdata[$block]);
525 unset($this->blockvariables[$block]);
526 unset($this->touchedBlocks[$block]);
527
528 } // end func removeBlockinner
529
537 function getBlocklist()
538 {
539 $blocklist = array();
540 foreach ($this->blocklist as $block => $content) {
541 $blocklist[$block] = $block;
542 }
543
544 return $blocklist;
545 } // end func getBlocklist
546
555 function blockExists($blockname)
556 {
557 return isset($this->blocklist[$blockname]);
558 } // end func blockExists
559
568 function getBlockvariables($block)
569 {
570 if (!isset($this->blockvariables[$block])) {
571 return array();
572 }
573
574 $variables = array();
575 foreach ($this->blockvariables[$block] as $variable => $v) {
576 $variables[$variable] = $variable;
577 }
578
579 return $variables;
580 } // end func getBlockvariables
581
591 function BlockvariableExists($block, $variable)
592 {
593 return isset($this->blockvariables[$block][$variable]);
594 } // end func BlockvariableExists
595
601 {
602 $this->functions = array();
603
605 $num = 0;
606
607 while (preg_match($this->functionRegExp, $template, $regs)) {
608
609 $pos = strpos($template, $regs[0]);
610 $template = substr($template, $pos + strlen($regs[0]));
611
612 $head = $this->getValue($template, ')');
613 $args = array();
614
615 $search = $regs[0] . $head . ')';
616
617 $replace = $this->openingDelimiter .
618 '__function' . $num . '__' .
620
621 $this->template = str_replace($search, $replace, $this->template);
622 $template = str_replace($search, $replace, $template);
623
624 while ($head != '' && $args2 = $this->getValue($head, ',')) {
625 $arg2 = trim($args2);
626 $args[] = ('"' == $arg2{0} || "'" == $arg2{0}) ?
627 substr($arg2, 1, -1) : $arg2;
628 if ($arg2 == $head) {
629 break;
630 }
631 $head = substr($head, strlen($arg2) + 1);
632 }
633
634 $this->functions[$num++] = array(
635 'name' => $regs[1],
636 'args' => $args
637 );
638 }
639
640 } // end func buildFunctionlist
641
652 function getValue($code, $delimiter) {
653 if ($code == '') {
654 return '';
655 }
656
657 if (!is_array($delimiter)) {
658 $delimiter = array( $delimiter => true );
659 }
660
661 $len = strlen($code);
662 $enclosed = false;
663 $enclosed_by = '';
664
665 if (isset($delimiter[$code[0]])) {
666 $i = 1;
667 } else {
668 for ($i = 0; $i < $len; ++$i) {
669 $char = $code[$i];
670
671 if (
672 ($char == '"' || $char == "'") &&
673 ($char == $enclosed_by || '' == $enclosed_by) &&
674 (0 == $i || ($i > 0 && '\\' != $code[$i - 1]))
675 ) {
676
677 if (!$enclosed) {
678 $enclosed_by = $char;
679 } else {
680 $enclosed_by = "";
681 }
682 $enclosed = !$enclosed;
683
684 }
685
686 if (!$enclosed && isset($delimiter[$char])) {
687 break;
688 }
689 }
690 }
691
692 return substr($code, 0, $i);
693 } // end func getValue
694
703 function deleteFromBlockvariablelist($block, $variables)
704 {
705 if (!is_array($variables)) {
706 $variables = array($variables => true);
707 }
708
709 reset($this->blockvariables[$block]);
710 while (list($varname, $val) = each($this->blockvariables[$block])) {
711 if (isset($variables[$varname])) {
712 unset($this->blockvariables[$block][$varname]);
713 }
714 }
715 } // end deleteFromBlockvariablelist
716
723 function updateBlockvariablelist($block)
724 {
725 preg_match_all( $this->variablesRegExp,
726 $this->blocklist[$block], $regs
727 );
728
729 if (count($regs[1]) != 0) {
730 foreach ($regs[1] as $k => $var) {
731 $this->blockvariables[$block][$var] = true;
732 }
733 } else {
734 $this->blockvariables[$block] = array();
735 }
736
737 // check if any inner blocks were found
738 if (isset($this->blockinner[$block]) &&
739 is_array($this->blockinner[$block]) &&
740 count($this->blockinner[$block]) > 0
741 ) {
742 /*
743 * loop through inner blocks, registering the variable
744 * placeholders in each
745 */
746 foreach ($this->blockinner[$block] as $childBlock) {
747 $this->updateBlockvariablelist($childBlock);
748 }
749 }
750 } // end func updateBlockvariablelist
751
760 function findPlaceholderBlocks($variable)
761 {
762 $parents = array();
763 reset($this->blocklist);
764 while (list($blockname, $content) = each($this->blocklist)) {
765 reset($this->blockvariables[$blockname]);
766 while (
767 list($varname, $val) = each($this->blockvariables[$blockname]))
768 {
769 if ($variable == $varname) {
770 $parents[] = $blockname;
771 }
772 }
773 }
774
775 return $parents;
776 } // end func findPlaceholderBlocks
777
788 function warning($message, $file = '', $line = 0)
789 {
790 $message = sprintf(
791 'HTML_Template_ITX Warning: %s [File: %s, Line: %d]',
792 $message,
793 $file,
794 $line
795 );
796
797 $this->warn[] = $message;
798
799 if ($this->printWarning) {
800 print $message;
801 }
802
803 if ($this->haltOnWarning) {
804 die($message);
805 }
806 } // end func warning
807
808} // end class HTML_Template_ITX
809?>
sprintf('%.4f', $callTime)
if(! $in) print
An exception for terminatinating execution or to throw for unit testing.
setCallbackFuntiontable($functions)
Sets the Callback function lookup table.
Definition: ITX.php:502
removeBlockData($block)
Recursively removes all data assiciated with a block, including all inner blocks.
Definition: ITX.php:513
setFunctioncontent($functionID, $replacement)
Replaces a function call with the given replacement.
Definition: ITX.php:423
setCallbackFunction($tplfunction, $callbackfunction, $callbackobject='', $expandCallbackParameters=false)
Sets a callback function.
Definition: ITX.php:476
updateBlockvariablelist($block)
Updates the variable list of a block.
Definition: ITX.php:723
warning($message, $file='', $line=0)
Handles warnings, saves them to $warn and prints them or calls die() depending on the flags.
Definition: ITX.php:788
getBlockvariables($block)
Returns a list of variables of a block.
Definition: ITX.php:568
findPlaceholderBlocks($variable)
Returns an array of blocknames where the given variable placeholder is used.
Definition: ITX.php:760
addBlockfile($placeholder, $blockname, $filename)
Adds a block taken from a file to the template changing a variable placeholder to a block placeholder...
Definition: ITX.php:307
performCallback()
Checks the list of function calls in the template and calls their callback function.
Definition: ITX.php:372
buildFunctionlist()
Builds a functionlist from the template.
Definition: ITX.php:600
addBlock($placeholder, $blockname, $template)
Adds a block to the template changing a variable placeholder to a block placeholder.
Definition: ITX.php:243
getBlocklist()
Returns a list of blocknames in the template.
Definition: ITX.php:537
replaceBlock($block, $template, $keep_content=false)
Replaces an existing block with new content.
Definition: ITX.php:162
__construct($root='')
Builds some complex regexps and calls the constructor of the parent class.
Definition: ITX.php:116
init()
Clears all datafields of the object and rebuild the internal blocklist.
Definition: ITX.php:126
getFunctioncalls()
Returns a list of all function calls in the current template.
Definition: ITX.php:411
blockExists($blockname)
Checks wheter a block exists.
Definition: ITX.php:555
BlockvariableExists($block, $variable)
Checks wheter a block variable exists.
Definition: ITX.php:591
deleteFromBlockvariablelist($block, $variables)
Deletes one or many variables from the block variable list.
Definition: ITX.php:703
replaceBlockfile($block, $filename, $keep_content=false)
Replaces an existing block with new content from a file.
Definition: ITX.php:207
getValue($code, $delimiter)
Truncates the given code from the first occurence of $delimiter but ignores $delimiter enclosed by " ...
Definition: ITX.php:652
$checkblocknameRegExp
Definition: ITX.php:69
placeholderExists($placeholder, $block='')
Returns the name of the (first) block that contains the specified placeholder.
Definition: ITX.php:326
findBlocks($string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:858
free()
Clears all datafields of the object.
Definition: IT.php:714
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:817
getFile($filename)
Reads a file from disk and returns its content.
Definition: IT.php:905
$code
Definition: example_050.php:99
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
Integrated Template - IT.
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file