File: PHPReportPage.php

package info (click to toggle)
phpreports 0.4.9-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 684 kB
  • ctags: 1,426
  • sloc: php: 3,377; xml: 203; makefile: 29; sql: 18; sh: 17; python: 10
file content (400 lines) | stat: -rwxr-xr-x 9,673 bytes parent folder | download | duplicates (2)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
	class PHPReportPage extends PHPReportGroup {
		var $_iSize;			// page size
		var $_iPageNum;		// page number
		var $_iRow;				// current row
		var $_iLimit;			// limit to print on memory or file
		var $_sBuffer;			// memory buffer for XML elements
		var $_iBcount;			// buffer count
		var $_sTemp;			// temporary dir
		var $_iCurBehaviour;	// current behaviour 
		var $_iBUFFER = 0;
		var $_iFILE	  = 1;
		var $_sFile;
		var $_fHandle;
		var $_sName = null;
		var $_sTag;
		var $_bOpen;
		var $_iWidth;
		var $_iHeight;
		var $_iCellPadding;
		var $_iBorder;
		var $_sAlign;
		var $_oGroups;
		var $_oDoc;		// document layer
		var $_sClass;	// page class
		var $_oError;

		function PHPReportPage($sXMLOutputFile=null) {
			$this->_sTemp			 = getPHPReportsTmpPath();
			$this->_sName			 = "PAGE LAYER";
			$this->_sTag			 = "PG";
			$this->_bOpen			 = false;
			$this->_iPageNum		 = 0;
			$this->_iSize			 = 50;
			$this->_iRow			 = 1;
			$this->_iLimit			 = 2500;
			$this->_sBuffer		 = "";
			$this->_iBcount		 = 0;
			$this->_fHandle		 = null;
			$this->_iCurBehaviour = $this->_iBUFFER;
			$this->_iPosition		 = 1;
			$this->_iWidth			 = -1;
			$this->_iHeight		 = -1;
			$this->_iCellPadding  = -1;
			$this->_iCellSpacing  = -1;
			$this->_iBorder		 = -1;
			$this->_sAlign			 = "LEFT";
			$this->_oGroups		 = null;
			$this->_sFile			 = $sXMLOutputFile;
			$this->_bReprintHeader= true;
			$this->_oDoc			 = null;
			$this->_sClass			 = null;
			$this->_oError			 = new PHPReportsErrorTr();
			$this->createFileName();
		}

		function setDocument(&$oDoc_){
			$this->_oDoc=&$oDoc_;
		}
		
		function setGroups(&$oGroups_) {
			$this->_oGroups=&$oGroups_;
		}

		function setClass($sClass_=null){
			$this->_sClass=$sClass_;
		}
		
		function getClass(){
			return $this->_sClass;
		}
		
		function setWidth($iWidth_=800) {
			$this->_iWidth=$iWidth_;
		}
		function getWidth() {
			return $this->_iWidth;
		}

		function setHeight($iHeight_=800) {
			$this->_iHeight=$iHeight_;
		}
		function getHeight() {
			return $this->_iHeight;
		}

		function setCellPadding($iCellPadding_=-1) {
			$this->_iCellPadding=$iCellPadding_;
		}
		function getCellPadding() {
			return $this->_iCellPadding;
		}
		
		function setCellSpacing($iCellSpacing_=-1) {
			$this->_iCellSpacing=$iCellSpacing_;
		}
		function getCellSpacing() {
			return $this->_iCellSpacing;
		}
		
		function setBorder($iBorder_=-1) {
			$this->_iBorder=$iBorder_;
		}
		function getBorder() {
			return $this->_iBorder;
		}
		
		function setAlign($sAlign_="LEFT") {
			$this->_sAlign=$sAlign_;
		}
		function getAlign() {
			return $this->_sAlign;
		}
		
		function setSize($iSize_=50) {
			$this->_iSize=$iSize_;
		}

		function getSize() {
			return $this->_iSize;
		}

		function setLimit($iLimit_=2500) {
			$this->_iLimit=$iLimit_;
		}

		function getLimit() {
			return $this->_iLimit;
		}

		function printTag($sRow_) {
			$this->printRow($sRow_);
		}

		function getTemp() {
			return $this->_sTemp;
		}

		function getBufferRows() {
			return $this->_iBcount;
		}

		function getName() {
			return "PAGE";
		}

		function printRow($sRow_,$bCount_=true,$bConsumed_=false) {
			if(!$this->isOpen()){ 
				$this->eventHandler(PAGE_OPEN);
				if($bConsumed_){
					if($this->isDebugging())
						print "(".$this->getName()."):CONSUMED<br>";
					return;
				}
			}

			if($this->isDebugging())
				print "PAGE:printRow:".$sRow_."<br>";

			if($bCount_)
				$this->_iBcount++;
			$this->_iRow+=($bCount_?1:0);
			$this->output($sRow_);

			// check if the buffer limit was reached
			// if so, open the temp file and flush the buffer
			if($this->_iCurBehaviour==$this->_iBUFFER && 
				$this->_iBcount>$this->_iLimit) {
				$this->openFile();
				$this->writeFile($this->_sBuffer);
				$this->_iCurBehaviour = $this->_iFILE;
			}

			// check if have a page break here
			if($this->_iSize>0 &&
				$this->_iRow+$this->getFooterSize()>$this->_iSize) 
				$this->eventHandler(PAGE_CLOSE);
		}

		/**
			Returns the current page number
			@return int page number
		*/
		function getPageNum() {
			return $this->_iPageNum;
		}

		/**
		 * Set the page number
		 */
		function setPageNum($iNum_=0){
			$this->_iPageNum=$iNum_;
			return $this->_iPageNum;
		}

		function &getPage() {
			return $this;
		}

		/**
			Return the number of the current row
		*/
		function getRowNum() {
			return $this->_iRow;
		}

		/**
			Return the number of the next row
		*/
		function getNextRow() {
			return $this->getRowNum()+1;
		}

		function output($sStr_) {
			// store the result, according to the current behaviour
			if($this->_iCurBehaviour==$this->_iBUFFER)
				$this->_sBuffer .= $sStr_;
			else	
				$this->writeFile($sStr_);
		}

		function getBuffer() {
			return $this->_sBuffer;
		}

		function createFileName(){
			if(is_null($this->_sFile)){
				// tempnam returns a temp file name and create it - but without the suffix
				$this->_sFile = tempnam($this->_sTemp,"phprpt");
				
				// so we delete it
				unlink($this->_sFile);
				
				// put the .xml stuff at the end
				$this->_sFile .=".xml";
			}
		}

		function openFile() {
			$this->_iCurBehaviour=$this->_iFILE;
		
			if(is_null($this->_sFile)) 
				$this->createFileName();
			
			$this->_fHandle=@fopen($this->_sFile,"w");
			if(!$this->_fHandle)
				$this->_oError->showMsg("CANTWRITEPAGE",array($this->_sFile));
		}

		function writeFile($sStr_) {
			if(!fputs($this->_fHandle,$sStr_))
				$this->_oError->showMsg("CANTWRITEPAGE",array($sStr_));
		}

		function getFileName() {
			return $this->_sFile;
		}

		function closeFile() {
			fflush($this->_fHandle);
			fclose($this->_fHandle);
		}

		/**
			Don't fire a PAGE_OPEN event here.
			The page will be opened when the first row arrives.
		*/
		function initialize($oDoc_=null) {
			$oRepo  = $this->getReport();
			$sTitle = $oRepo->getTitle();
			$sColor = $oRepo->getBackgroundColor();
			$sImage = $oRepo->getBackgroundImage();
			$sBCSS  = $oRepo->getBookmarksCSS();
			
			$sParm  = (strlen($sTitle)>0?" TITLE=\"".$oRepo->getTitle()."\"":"");
			$sParm .= (strlen($sColor)>0?" BGCOLOR=\"".$oRepo->getBackgroundColor()."\"":"");
			$sParm .= (strlen($sImage)>0?" BACKGROUND=\"".$oRepo->getBackgroundImage()."\"":"");
			$sParm .= (strlen($sBCSS)>0?" BOOKMARKS_CSS=\"".$oRepo->getBookmarksCSS()."\"":"");
			$this->output("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>\n");
			$this->output("<RP$sParm>\n");

			$aCSS = $oRepo->getCSS();
			for($i=0;$i<sizeof($aCSS);$i++){
				$sCSSFile= $aCSS[$i][0];
				$sMedia	= $aCSS[$i][1];
				$this->output("\t<CSS ".(strlen($sMedia)>0?"MEDIA=\"".$aCSS[$i][1]."\"":"").">$sCSSFile</CSS>\n");
			}
			$oForm = $oRepo->getForm();
			if($oForm)
				$this->output("\t".$oForm->getXMLOpen());
		}

		function finalize() {
			// if is only on the buffer yet ...
			if($this->_iCurBehaviour==$this->_iBUFFER){
				$this->openFile();
				$this->writeFile($this->_sBuffer);
			}	
				
			if($this->isOpen()) 
				$this->printFooter(false);

			if($this->isOpen()){	
				$this->writeFile("</".$this->_sTag.">\n");
				$this->setOpen(false);
			}

			// check for a form
			$oRepo = $this->getReport();
			$oForm = $oRepo->getForm();
			if($oForm)
				$this->output($oForm->getXMLClose());
			
			$this->writeFile("</RP>\n");
			$this->closeFile();
		}

		function getIniTag() {
			$sTag  = "<".$this->_sTag." SZ=\"".$this->_iSize."\" AL=\"".$this->_sAlign."\" PN=\"".$this->_iPageNum."\"";
			$sTag	.= ($this->_iWidth >0?" WI=\"".$this->_iWidth."\"":"");
			$sTag	.= ($this->_iHeight>0?" HE=\"".$this->_iHeight."\"":"");
			$sTag	.= ($this->_iCellPadding>=0?" PA=\"".$this->_iCellPadding."\"":"");
			$sTag	.= ($this->_iCellSpacing>=0?" SP=\"".$this->_iCellSpacing."\"":"");
			$sTag	.= ($this->_iBorder>0?" BO=\"".$this->_iBorder."\"":"");
			$sTag .= (!is_null($this->_sClass)?" CL=\"".$this->_sClass."\"":"");
			$sTag .= ">\n";
			return $sTag;
		}
		
		/**
			Event handler
			@param int event
		*/
		function eventHandler($iEvent_=-1,$oObj_=null) {
			switch($iEvent_) {
				case REPORT_OPEN:
					$this->initialize($oObj_);
					break;

				case REPORT_CLOSE:
					$this->finalize();
					break;

				case PUT_DATA:
					$this->putValues($oObj_);
					break;	

				case PROCESS_DATA:
					$this->processValues($oObj_);
					break;	
					
				case PAGE_OPEN:
					if($this->isDebugging())
						print "<font color='#00c000'>(PAGE):PAGE_OPEN:first field value:".$this->getValueByPos(0)."</font><br>";

					$this->_iPageNum++;
					$this->output($this->getIniTag());
					$this->setOpen();

					// now check here if the DOCUMENT layer was opened
					// it not, print it
					if(!$this->_oDoc->isOpen())
						$this->_oDoc->printHeader();
					
					// print the PAGE HEADER
					$this->printHeader();
					
					if(!is_null($this->_oGroups)) {
						$oGroup =& $this->_oGroups;
						if($oGroup->isFirst()){
							if($this->isDebugging())
								print "(".$this->getName()."):PAGE_OPEN:putting data to ".$oGroup->getName()."<br>";
							$oGroup->eventHandler(PUT_DATA,$this->getLastData());
						}	
						$oGroup->eventHandler(PAGE_OPEN);
					}
					break;
					
				case PAGE_CLOSE:
					if($this->_bOpen){
						$this->_iRow=-100;
						$this->printFooter(false);
						$this->_iRow=1;
						$this->output("</".$this->_sTag.">\n");
						$this->_bOpen=false;
						$this->reset(true);
						
						if($this->isDebugging())
							print "<font color='#FF0000'>(PAGE):PAGE_CLOSE:first field value:".$this->getValueByPos(0)."<hr></font><br>";
						
						if(!is_null($this->_oGroups)) {
							$oGroup =& $this->_oGroups;
							$oGroup->eventHandler($iEvent_);
						}
					}
					break;
			}
		}
	}
?>