File: ITX.php

package info (click to toggle)
php4 4.0.3pl1-0potato3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 15,168 kB
  • ctags: 20,556
  • sloc: ansic: 155,237; php: 10,827; sh: 9,608; yacc: 1,874; lex: 1,742; makefile: 788; java: 424; awk: 359; cpp: 335; perl: 181; xml: 57
file content (292 lines) | stat: -rw-r--r-- 9,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Integrated Template Extension - ITX
*
* With this class you get the full power of the phplib template class. 
* You may have one file with blocks in it but you have as well one main file 
* and multiple files one for each block. This is quite usefull when you have
* user configurable websites. Using blocks not in the main template allows
* you to some parts of your layout easily. 
*
* Note that you can replace an existing block and add new blocks add runtime.
* Adding new blocks means changing a variable placeholder to a block.
*
* @author 	Ulf Wendel <uw@netuse.de>
* @access		public
* @version 	1.0 22/09/00
*/
class IntegratedTemplateExtension extends IntegratedTemplate {

	/**
	* Array with all warnings.
	* @var		array
	* @access	public
	* @see		$printWarning, $haltOnWarning, warning()
	*/
	var $warn = array();
	
	/**
	* Print warnings?
	* @var		array
	* @access	public
	* @see		$haltOnWarning, $warn, warning()
	*/
	var $printWarning = false;
	
	/**
	* Call die() on warning?
	* @var 		boolean
	* @access	public
	* @see		$warn, $printWarning, warning()
	*/
	var $haltOnWarning = false;
		
	/**
	* RegExp used to test for a valid blockname.
	* @var	string
	*/	
	var $checkblocknameRegExp = "";
	
	/**
	* Builds some complex regexps and calls the constructor of the parent class.
	*
	* Make sure that you call this constructor if you derive you own 
	* template class from this one.
	*
	* @see	IntegratedTemplate()
	*/
	function IntegratedTemplateExtension() {
	
		$this->checkblocknameRegExp = "@".$this->blocknameRegExp."@";
		$this->IntegratedTemplate();
																							
	} // end func IntegratedTemplateExtension
	
	/**
	* Replaces an existing block with new content. Warning: not implemented yet.
	* 
	* The Replacement does not affect previously added variables. All data is cached.
	* In case the new block does contain less or other variable placeholder the previously
	* passed data that is no longer referenced will be deleted. The internal list 
	* of allowed variables gets updated as well.
	*
	* In case the original block contains other blocks it must eighter have placeholder
	* for the inner blocks or contain them. If you want to use placeholder the placeholder must
	* look like openingDelimiter."__".blockname."__".closingDelimiter .
	*
	* Due to the cache updates replaceBlock() and replaceBlockfile() are "expensive" operations 
	* which means extensive usage will slow down your script. So try to avoid them and if 
	* you can't do so try to use them before you pass lots of variables to the block you're 
	* replacing.
	* 
	* @param	string	Blockname
	* @param	string	Blockcontent
	* @return	boolean	
	* @see		replaceBlockfile(), addBlock(), addBlockfile()
	* @access	public
	*/
	function replaceBlock($block, $template) {
		if (!isset($this->blocklist[$block])) {
			$this->halt("The block '$block' does not exist in the template and thus it can't be replaced.", __FILE__, __LINE__);
			return false;
		}
		if (""==$template) {
			$this->halt("No block content given.", __FILE__, __LINE__);
			return false;
		}
		
		print "This function has not been coded yet.";
		
		// find inner blocks
		// add to variablelist
		// compare variable list
		// update caches
		
		return true;
	} // end func replaceBlock
	
	/**
	* Replaces an existing block with new content from a file. Warning: not implemented yet.
	* @brother replaceBlock()
	* @param	string	Blockname
	* @param	string	Name of the file that contains the blockcontent
	*/
	function replaceBlockfile($block, $filename) {
		return $this->replaceBlock($block, $this->getFile($filename));	
	} // end func replaceBlockfile
	
	/**
	* Adds a block to the template changing a variable placeholder to a block placeholder.
	*
	* Add means "replace a variable placeholder by a new block". 
	* This is different to PHPLibs templates. The function loads a 
	* block, creates a handle for it and assigns it to a certain 
	* variable placeholder. To to the same with PHPLibs templates you would 
	* call set_file() to create the handle and parse() to assign the
	* parsed block to a variable. By this PHPLibs templates assume that you tend
	* to assign a block to more than one one placeholder. To assign a parsed block
	* to more than only the placeholder you specify in this function you have
	* to use a combination of getBlock() and setVariable().
	*
	* As no updates to cached data is necessary addBlock() and addBlockfile() 
	* are rather "cheap" meaning quick operations.
	*
	* The block content must not start with <!-- BEGIN blockname --> and end with 
	* <!-- END blockname --> this would cause overhead and produce an error.
	* 
	* @param	string	Name of the variable placeholder, the name must be unique within the template.
	* @param	string	Name of the block to be added
	* @param	string	Content of the block
	* @return	boolean
	* @see		addBlockfile()
	* @access	public
	*/	
	function addBlock($placeholder, $blockname, $template) {
	
		// Don't trust any user even if it's a programmer or yourself...
		if (""==$placeholder) {
		
			$this->halt("No variable placeholder given.", __FILE__, __LINE__);
			return false;
			
		}	else if (""==$blockname || !preg_match($this->checkblocknameRegExp, $blockname) ) {
			
			print $this->checkblocknameRegExp;
			$this->halt("No or invalid blockname '$blockname' given.", __FILE__, __LINE__);
			return false;
			
		} else if (""==$template) {
		
			$this->halt("No block content given.", __FILE__, __LINE__);
			return false;
			
		} else if (isset($this->blocklist[$blockname])) {
		
			$this->halt("The block already exists.", __FILE__, __LINE__);
			return false;
			
		}
		
		// Hmm, we should do some more tests.
		$parents = $this->findPlaceholderBlocks($placeholder);
		if (0==count($parents)) {
		
			$this->halt("The variable placeholder '$placeholder' was not found in the template.", __FILE__, __LINE__);
			return false;
			
		} else if ( count($parents)>1 ) {
			
			reset($parents);
			while (list($k, $parent)=each($parents)) 
				$msg.= "$parent, ";
			$msg = substr($parent, -2);
			
			$this->halt("The variable placeholder '$placeholder' must be unique, found in multiple blocks '$msg'.", __FILE__, __LINE__);
			return false;
						
		}
		
		$template = "<!-- BEGIN $blockname -->".$template."<!-- END $blockname -->";
		$this->findBlocks($template);
		if ($this->flagBlocktrouble) 
			return false;	// findBlocks() already throws an exception
		
		$this->blockinner[$parents[0]][] = $blockname;
		$this->blocklist[$parents[0]] = preg_replace(	"@".$this->openingDelimiter.$placeholder.$this->closingDelimiter."@", 
																									$this->openingDelimiter."__".$blockname."__".$this->closingDelimiter, 
																									$this->blocklist[$parents[0]]
																								);
																								
		$this->deleteFromBlockvariablelist($parents[0], $placeholder);
		$this->updateBlockvariablelist($blockname);
		
		return true;
	} // end func addBlock
	
	/**
	* Adds a block taken from a file to the template changing a variable placeholder to a block placeholder. 
	* 
	* @param		string	Name of the variable placeholder to be converted
	* @param		string	Name of the block to be added
	* @param		string	File that contains the block
	* @brother	addBlock()
	*/
	function addBlockfile($placeholder, $blockname, $filename) {
		return $this->addBlock($placeholder, $blockname, $this->getFile($filename));
	} // end func addBlockfile
	
	/**
	* Deletes one or many variables from the block variable list.
	* @param	string	Blockname
	* @param	mixed		Name of one variable or array of variables ( array ( name => true ) ) to be stripped.
	*/
	function deleteFromBlockvariablelist($block, $variables) {
	
		if (!is_array($variables))
			$variables = array( $variables => true);
			
		reset($this->blockvariables[$block]);
		while (list($k, $varname)=each($this->blockvariables[$block])) 
			if (isset($variables[$varname])) 
				unset($this->blockvariables[$block][$k]);
					
	} // end deleteFromBlockvariablelist

	/**
	* Updates the variable list of a block.
	* @param	string	Blockname
	*/	
	function updateBlockvariablelist($block) {
		
		preg_match_all( $this->variablesRegExp, $this->blocklist[$block], $regs );
		$this->blockvariables[$block] = $regs[1];
			
	} // end func updateBlockvariablelist
	
	/**
	* Returns an array of blocknames where the given variable placeholder is used.
	* @param	string	Variable placeholder
	* @return	array	$parents	parents[0..n] = blockname
	*/
	function findPlaceholderBlocks($variable) {
		
		$parents = array();
		
		reset($this->blocklist);
		while (list($blockname, $content)=each($this->blocklist)) {
			
			reset($this->blockvariables[$blockname]);
			while (list($k, $varname)=each($this->blockvariables[$blockname]))
				if ($variable == $varname) 
					$parents[] = $blockname;
		}
			
		return $parents;
	} // end func findPlaceholderBlocks

	/**
	* Handles warnings, saves them to $warn and prints them or calls die() depending on the flags
	* @param	string	Warning
	* @param	string	File where the warning occured
	* @param	int			Linenumber where thr warning occured
	* @see		$warn, $printWarning, $haltOnWarning
	*/
	function warning($message, $file="", $line=0) {
		
		$message = sprintf("IntegratedTemplateExtension Warning: %s [File: %s, Line: %d]",
													$message,
													$file, 
													$line );

		$this->warn[] = $message;
		
		if ($this->printWarning)
			print $message;
			
		if ($this->haltOnError) 
			die($message);
		
	} // end func warning
	
} // end class IntegratedTemplateExtension
?>