]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/pdf/class.ezpdf.php
Add .gitignore
[Github/sugarcrm.git] / include / pdf / class.ezpdf.php
1 <?php
2
3 include_once('include/pdf/class.pdf.php');
4
5 class Cezpdf extends Cpdf {
6 //==============================================================================
7 // this class will take the basic interaction facilities of the Cpdf class
8 // and make more useful functions so that the user does not have to 
9 // know all the ins and outs of pdf presentation to produce something pretty.
10 //
11 // IMPORTANT NOTE
12 // there is no warranty, implied or otherwise with this software.
13 // 
14 // version 009 (versioning is linked to class.pdf.php)
15 //
16 // released under a public domain licence.
17 //
18 // Wayne Munro, R&OS Ltd, http://www.ros.co.nz/pdf
19 //==============================================================================
20
21 var $ez=array('fontSize'=>10); // used for storing most of the page configuration parameters
22 var $y; // this is the current vertical positon on the page of the writing point, very important
23 var $ezPages=array(); // keep an array of the ids of the pages, making it easy to go back and add page numbers etc.
24 var $ezPageCount=0;
25 var $lastWidth; //this keeps track of the last width of an object (table/image);
26 // ------------------------------------------------------------------------------
27
28 function Cezpdf($paper='a4',$orientation='portrait'){
29         // Assuming that people don't want to specify the paper size using the absolute coordinates
30         // allow a couple of options:
31         // orientation can be 'portrait' or 'landscape'
32         // or, to actually set the coordinates, then pass an array in as the first parameter.
33         // the defaults are as shown.
34         // 
35         // -------------------------
36         // 2002-07-24 - Nicola Asuni (info@tecnick.com):
37         // Added new page formats (45 standard ISO paper formats and 4 american common formats)
38         // paper cordinates are calculated in this way: (inches * 72) where 1 inch = 2.54 cm
39         // 
40         // Now you may also pass a 2 values array containing the page width and height in centimeters
41         // -------------------------
42
43         if (!is_array($paper)){
44                 switch (strtoupper($paper)){
45                         case '4A0': {$size = array(0,0,4767.87,6740.79); break;}
46                         case '2A0': {$size = array(0,0,3370.39,4767.87); break;}
47                         case 'A0': {$size = array(0,0,2383.94,3370.39); break;}
48                         case 'A1': {$size = array(0,0,1683.78,2383.94); break;}
49                         case 'A2': {$size = array(0,0,1190.55,1683.78); break;}
50                         case 'A3': {$size = array(0,0,841.89,1190.55); break;}
51                         case 'A4': default: {$size = array(0,0,595.28,841.89); break;}
52                         case 'A5': {$size = array(0,0,419.53,595.28); break;}
53                         case 'A6': {$size = array(0,0,297.64,419.53); break;}
54                         case 'A7': {$size = array(0,0,209.76,297.64); break;}
55                         case 'A8': {$size = array(0,0,147.40,209.76); break;}
56                         case 'A9': {$size = array(0,0,104.88,147.40); break;}
57                         case 'A10': {$size = array(0,0,73.70,104.88); break;}
58                         case 'B0': {$size = array(0,0,2834.65,4008.19); break;}
59                         case 'B1': {$size = array(0,0,2004.09,2834.65); break;}
60                         case 'B2': {$size = array(0,0,1417.32,2004.09); break;}
61                         case 'B3': {$size = array(0,0,1000.63,1417.32); break;}
62                         case 'B4': {$size = array(0,0,708.66,1000.63); break;}
63                         case 'B5': {$size = array(0,0,498.90,708.66); break;}
64                         case 'B6': {$size = array(0,0,354.33,498.90); break;}
65                         case 'B7': {$size = array(0,0,249.45,354.33); break;}
66                         case 'B8': {$size = array(0,0,175.75,249.45); break;}
67                         case 'B9': {$size = array(0,0,124.72,175.75); break;}
68                         case 'B10': {$size = array(0,0,87.87,124.72); break;}
69                         case 'C0': {$size = array(0,0,2599.37,3676.54); break;}
70                         case 'C1': {$size = array(0,0,1836.85,2599.37); break;}
71                         case 'C2': {$size = array(0,0,1298.27,1836.85); break;}
72                         case 'C3': {$size = array(0,0,918.43,1298.27); break;}
73                         case 'C4': {$size = array(0,0,649.13,918.43); break;}
74                         case 'C5': {$size = array(0,0,459.21,649.13); break;}
75                         case 'C6': {$size = array(0,0,323.15,459.21); break;}
76                         case 'C7': {$size = array(0,0,229.61,323.15); break;}
77                         case 'C8': {$size = array(0,0,161.57,229.61); break;}
78                         case 'C9': {$size = array(0,0,113.39,161.57); break;}
79                         case 'C10': {$size = array(0,0,79.37,113.39); break;}
80                         case 'RA0': {$size = array(0,0,2437.80,3458.27); break;}
81                         case 'RA1': {$size = array(0,0,1729.13,2437.80); break;}
82                         case 'RA2': {$size = array(0,0,1218.90,1729.13); break;}
83                         case 'RA3': {$size = array(0,0,864.57,1218.90); break;}
84                         case 'RA4': {$size = array(0,0,609.45,864.57); break;}
85                         case 'SRA0': {$size = array(0,0,2551.18,3628.35); break;}
86                         case 'SRA1': {$size = array(0,0,1814.17,2551.18); break;}
87                         case 'SRA2': {$size = array(0,0,1275.59,1814.17); break;}
88                         case 'SRA3': {$size = array(0,0,907.09,1275.59); break;}
89                         case 'SRA4': {$size = array(0,0,637.80,907.09); break;}
90                         case 'LETTER': {$size = array(0,0,612.00,792.00); break;}
91                         case 'LEGAL': {$size = array(0,0,612.00,1008.00); break;}
92                         case 'EXECUTIVE': {$size = array(0,0,521.86,756.00); break;}
93                         case 'FOLIO': {$size = array(0,0,612.00,936.00); break;}
94                         case 'TABLOID': {$size = array(0,0,792.00,1224.00); break;} // cn: bug 3627 - added Tabloid size
95                 }
96                 switch (strtolower($orientation)){
97                         case 'landscape':
98                                 $a=$size[3];
99                                 $size[3]=$size[2];
100                                 $size[2]=$a;
101                                 break;
102                 }
103         } else {
104                 if (count($paper)>2) {
105                         // then an array was sent it to set the size
106                         $size = $paper;
107                 }
108                 else { //size in centimeters has been passed
109                         $size[0] = 0;
110                         $size[1] = 0;
111                         $size[2] = ( $paper[0] / 2.54 ) * 72;
112                         $size[3] = ( $paper[1] / 2.54 ) * 72;
113                 }
114         }
115         $this->Cpdf($size);
116         $this->ez['pageWidth']=$size[2];
117         $this->ez['pageHeight']=$size[3];
118         
119         // also set the margins to some reasonable defaults
120         $this->ez['topMargin']=30;
121         $this->ez['bottomMargin']=30;
122         $this->ez['leftMargin']=30;
123         $this->ez['rightMargin']=30;
124         
125         // set the current writing position to the top of the first page
126         $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
127         // and get the ID of the page that was created during the instancing process.
128         $this->ezPages[1]=$this->getFirstPageId();
129         $this->ezPageCount=1;
130 }
131
132 // ------------------------------------------------------------------------------
133 // 2002-07-24: Nicola Asuni (info@tecnick.com)
134 // Set Margins in centimeters
135 function ezSetCmMargins($top,$bottom,$left,$right){
136         $top = ( $top / 2.54 ) * 72;
137         $bottom = ( $bottom / 2.54 ) * 72;
138         $left = ( $left / 2.54 ) * 72;
139         $right = ( $right / 2.54 ) * 72;
140         $this->ezSetMargins($top,$bottom,$left,$right);
141 }
142 // ------------------------------------------------------------------------------
143
144
145 function ezColumnsStart($options=array()){
146   // start from the current y-position, make the set number of columne
147   if (isset($this->ez['columns']) && $this->ez['columns']==1){
148     // if we are already in a column mode then just return.
149     return;
150   }
151   $def=array('gap'=>10,'num'=>2);
152   foreach($def as $k=>$v){
153     if (!isset($options[$k])){
154       $options[$k]=$v;
155     }
156   }
157   // setup the columns
158   $this->ez['columns']=array('on'=>1,'colNum'=>1);
159
160   // store the current margins
161   $this->ez['columns']['margins']=array(
162      $this->ez['leftMargin']
163     ,$this->ez['rightMargin']
164     ,$this->ez['topMargin']
165     ,$this->ez['bottomMargin']
166   );
167   // and store the settings for the columns
168   $this->ez['columns']['options']=$options;
169   // then reset the margins to suit the new columns
170   // safe enough to assume the first column here, but start from the current y-position
171   $this->ez['topMargin']=$this->ez['pageHeight']-$this->y;
172   $width=($this->ez['pageWidth']-$this->ez['leftMargin']-$this->ez['rightMargin']-($options['num']-1)*$options['gap'])/$options['num'];
173   $this->ez['columns']['width']=$width;
174   $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
175   
176 }
177 // ------------------------------------------------------------------------------
178 function ezColumnsStop(){
179   if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
180     $this->ez['columns']['on']=0;
181     $this->ez['leftMargin']=$this->ez['columns']['margins'][0];
182     $this->ez['rightMargin']=$this->ez['columns']['margins'][1];
183     $this->ez['topMargin']=$this->ez['columns']['margins'][2];
184     $this->ez['bottomMargin']=$this->ez['columns']['margins'][3];
185   }
186 }
187 // ------------------------------------------------------------------------------
188 function ezInsertMode($status=1,$pageNum=1,$pos='before'){
189   // puts the document into insert mode. new pages are inserted until this is re-called with status=0
190   // by default pages wil be inserted at the start of the document
191   switch($status){
192     case '1':
193       if (isset($this->ezPages[$pageNum])){
194         $this->ez['insertMode']=1;
195         $this->ez['insertOptions']=array('id'=>$this->ezPages[$pageNum],'pos'=>$pos);
196       }
197       break;
198     case '0':
199       $this->ez['insertMode']=0;
200       break;
201   }
202 }
203 // ------------------------------------------------------------------------------
204
205 function ezNewPage(){
206   $pageRequired=1;
207   if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
208     // check if this is just going to a new column
209     // increment the column number
210 //echo 'HERE<br>';
211     $this->ez['columns']['colNum']++;
212 //echo $this->ez['columns']['colNum'].'<br>';
213     if ($this->ez['columns']['colNum'] <= $this->ez['columns']['options']['num']){
214       // then just reset to the top of the next column
215       $pageRequired=0;
216     } else {
217       $this->ez['columns']['colNum']=1;
218       $this->ez['topMargin']=$this->ez['columns']['margins'][2];
219     }
220
221     $width = $this->ez['columns']['width'];
222     $this->ez['leftMargin']=$this->ez['columns']['margins'][0]+($this->ez['columns']['colNum']-1)*($this->ez['columns']['options']['gap']+$width);
223     $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
224   }
225 //echo 'left='.$this->ez['leftMargin'].'   right='.$this->ez['rightMargin'].'<br>';
226
227   if ($pageRequired){
228     // make a new page, setting the writing point back to the top
229     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
230     // make the new page with a call to the basic class.
231     $this->ezPageCount++;
232     if (isset($this->ez['insertMode']) && $this->ez['insertMode']==1){
233       $id = $this->ezPages[$this->ezPageCount] = $this->newPage(1,$this->ez['insertOptions']['id'],$this->ez['insertOptions']['pos']);
234       // then manipulate the insert options so that inserted pages follow each other
235       $this->ez['insertOptions']['id']=$id;
236       $this->ez['insertOptions']['pos']='after';
237     } else {
238       $this->ezPages[$this->ezPageCount] = $this->newPage();
239     }
240   } else {
241     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
242   }
243 }
244
245 // ------------------------------------------------------------------------------
246
247 function ezSetMargins($top,$bottom,$left,$right){
248   // sets the margins to new values
249   $this->ez['topMargin']=$top;
250   $this->ez['bottomMargin']=$bottom;
251   $this->ez['leftMargin']=$left;
252   $this->ez['rightMargin']=$right;
253   // check to see if this means that the current writing position is outside the 
254   // writable area
255   if ($this->y > $this->ez['pageHeight']-$top){
256     // then move y down
257     $this->y = $this->ez['pageHeight']-$top;
258   }
259   if ( $this->y < $bottom){
260     // then make a new page
261     $this->ezNewPage();
262   }
263 }  
264
265 // ------------------------------------------------------------------------------
266
267 function ezGetCurrentPageNumber(){
268   // return the strict numbering (1,2,3,4..) number of the current page
269   return $this->ezPageCount;
270 }
271
272 // ------------------------------------------------------------------------------
273
274 function ezStartPageNumbers($x,$y,$size,$pos='left',$pattern='{PAGENUM} of {TOTALPAGENUM}',$num=''){
275   // put page numbers on the pages from here.
276   // place then on the 'pos' side of the coordinates (x,y).
277   // pos can be 'left' or 'right'
278   // use the given 'pattern' for display, where (PAGENUM} and {TOTALPAGENUM} are replaced
279   // as required.
280   // if $num is set, then make the first page this number, the number of total pages will
281   // be adjusted to account for this.
282   // Adjust this function so that each time you 'start' page numbers then you effectively start a different batch
283   // return the number of the batch, so that they can be stopped in a different order if required.
284   if (!$pos || !strlen($pos)){
285     $pos='left';
286   }
287   if (!$pattern || !strlen($pattern)){
288     $pattern='{PAGENUM} of {TOTALPAGENUM}';
289   }
290   if (!isset($this->ez['pageNumbering'])){
291     $this->ez['pageNumbering']=array();
292   }
293   $i = count($this->ez['pageNumbering']);
294   $this->ez['pageNumbering'][$i][$this->ezPageCount]=array('x'=>$x,'y'=>$y,'pos'=>$pos,'pattern'=>$pattern,'num'=>$num,'size'=>$size);
295   return $i;
296 }
297
298 // ------------------------------------------------------------------------------
299
300 function ezWhatPageNumber($pageNum,$i=0){
301   // given a particular generic page number (ie, document numbered sequentially from beginning),
302   // return the page number under a particular page numbering scheme ($i)
303   $num=0;
304   $start=1;
305   $startNum=1;
306   if (!isset($this->ez['pageNumbering']))
307   {
308     $this->addMessage('WARNING: page numbering called for and wasn\'t started with ezStartPageNumbers');
309     return 0;
310   }
311   foreach($this->ez['pageNumbering'][$i] as $k=>$v){
312     if ($k<=$pageNum){
313       if (is_array($v)){
314         // start block
315         if (strlen($v['num'])){
316           // a start was specified
317           $start=$v['num'];
318           $startNum=$k;
319           $num=$pageNum-$startNum+$start;
320         }
321       } else {
322         // stop block
323         $num=0;
324       }
325     }
326   }
327   return $num;
328 }
329
330 // ------------------------------------------------------------------------------
331
332 function ezStopPageNumbers($stopTotal=0,$next=0,$i=0){
333   // if stopTotal=1 then the totalling of pages for this number will stop too
334   // if $next=1, then do this page, but not the next, else do not do this page either
335   // if $i is set, then stop that particular pagenumbering sequence.
336   if (!isset($this->ez['pageNumbering'])){
337     $this->ez['pageNumbering']=array();
338   }
339   if ($next && isset($this->ez['pageNumbering'][$i][$this->ezPageCount]) && is_array($this->ez['pageNumbering'][$i][$this->ezPageCount])){
340     // then this has only just been started, this will over-write the start, and nothing will appear
341     // add a special command to the start block, telling it to stop as well
342     if ($stopTotal){
343       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stoptn']=1;
344     } else {
345       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stopn']=1;
346     }
347   } else {
348     if ($stopTotal){
349       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stopt';
350     } else {
351       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stop';
352     }
353     if ($next){
354       $this->ez['pageNumbering'][$i][$this->ezPageCount].='n';
355     }
356   }
357 }
358
359 // ------------------------------------------------------------------------------
360
361 function ezPRVTpageNumberSearch($lbl,&$tmp){
362   foreach($tmp as $i=>$v){
363     if (is_array($v)){
364       if (isset($v[$lbl])){
365         return $i;
366       }
367     } else {
368       if ($v==$lbl){
369         return $i;
370       }
371     }
372   }
373   return 0;
374 }
375
376 // ------------------------------------------------------------------------------
377
378 function ezPRVTaddPageNumbers(){
379   // this will go through the pageNumbering array and add the page numbers are required
380   if (isset($this->ez['pageNumbering'])){
381     $totalPages1 = $this->ezPageCount;
382     $tmp1=$this->ez['pageNumbering'];
383     $status=0;
384     foreach($tmp1 as $i=>$tmp){
385       // do each of the page numbering systems
386       // firstly, find the total pages for this one
387       $k = $this->ezPRVTpageNumberSearch('stopt',$tmp);
388       if ($k && $k>0){
389         $totalPages = $k-1;
390       } else {
391         $l = $this->ezPRVTpageNumberSearch('stoptn',$tmp);
392         if ($l && $l>0){
393           $totalPages = $l;
394         } else {
395           $totalPages = $totalPages1;
396         }
397       }
398       foreach ($this->ezPages as $pageNum=>$id){
399         if (isset($tmp[$pageNum])){
400           if (is_array($tmp[$pageNum])){
401             // then this must be starting page numbers
402             $status=1;
403             $info = $tmp[$pageNum];
404             $info['dnum']=$info['num']-$pageNum;
405             // also check for the special case of the numbering stopping and starting on the same page
406             if (isset($info['stopn']) || isset($info['stoptn']) ){
407               $status=2;
408             }
409           } else if ($tmp[$pageNum]=='stop' || $tmp[$pageNum]=='stopt'){
410             // then we are stopping page numbers
411             $status=0;
412           } else if ($status==1 && ($tmp[$pageNum]=='stoptn' || $tmp[$pageNum]=='stopn')){
413             // then we are stopping page numbers
414             $status=2;
415           }
416         }
417         if ($status){
418           // then add the page numbering to this page
419           if (strlen($info['num'])){
420             $num=$pageNum+$info['dnum'];
421           } else {
422             $num=$pageNum;
423           }
424           $total = $totalPages+$num-$pageNum;
425           $pat = str_replace('{PAGENUM}',$num,$info['pattern']);
426           $pat = str_replace('{TOTALPAGENUM}',$total,$pat);
427           $this->reopenObject($id);
428           switch($info['pos']){
429             case 'right':
430               $this->addText($info['x'],$info['y'],$info['size'],$pat);
431               break;
432             default:
433               $w=$this->getTextWidth($info['size'],$pat);
434               $this->addText($info['x']-$w,$info['y'],$info['size'],$pat);
435               break;
436           }
437           $this->closeObject();
438         }
439         if ($status==2){
440           $status=0;
441         }
442       }
443     }
444   }
445 }
446
447 // ------------------------------------------------------------------------------
448
449 function ezPRVTcleanUp(){
450   $this->ezPRVTaddPageNumbers();
451 }
452
453 // ------------------------------------------------------------------------------
454
455 function ezStream($options=''){
456   $this->ezPRVTcleanUp();
457   $this->stream($options);
458 }
459
460 // ------------------------------------------------------------------------------
461
462 function ezOutput($options=0){
463   $this->ezPRVTcleanUp();
464   return $this->output($options);
465 }
466
467 // ------------------------------------------------------------------------------
468
469 function ezSetY($y){
470   // used to change the vertical position of the writing point.
471   $this->y = $y;
472   if ( $this->y < $this->ez['bottomMargin']){
473     // then make a new page
474     $this->ezNewPage();
475   }
476 }
477
478 // ------------------------------------------------------------------------------
479
480 function ezSetDy($dy,$mod=''){
481   // used to change the vertical position of the writing point.
482   // changes up by a positive increment, so enter a negative number to go
483   // down the page
484   // if $mod is set to 'makeSpace' and a new page is forced, then the pointed will be moved 
485   // down on the new page, this will allow space to be reserved for graphics etc.
486   $this->y += $dy;
487   if ( $this->y < $this->ez['bottomMargin']){
488     // then make a new page
489     $this->ezNewPage();
490     if ($mod=='makeSpace'){
491       $this->y += $dy;
492     }
493   }
494 }
495
496 // ------------------------------------------------------------------------------
497
498 function ezPrvtTableDrawLines($pos,$gap,$x0,$x1,$y0,$y1,$y2,$col,$inner,$outer,$opt=1){
499   $x0=1000;
500   $x1=0;
501   $this->setStrokeColor($col[0],$col[1],$col[2]);
502   $cnt=0;
503   $n = count($pos);
504   foreach($pos as $x){
505     $cnt++;
506     if ($cnt==1 || $cnt==$n){
507       $this->setLineStyle($outer);
508     } else {
509       $this->setLineStyle($inner);
510     }
511     $this->line($x-$gap/2,$y0,$x-$gap/2,$y2);
512     if ($x>$x1){ $x1=$x; };
513     if ($x<$x0){ $x0=$x; };
514   }
515   $this->setLineStyle($outer);
516   $this->line($x0-$gap/2-$outer/2,$y0,$x1-$gap/2+$outer/2,$y0);
517   // only do the second line if it is different to the first, AND each row does not have
518   // a line on it.
519   if ($y0!=$y1 && $opt<2){
520     $this->line($x0-$gap/2,$y1,$x1-$gap/2,$y1);
521   }
522   $this->line($x0-$gap/2-$outer/2,$y2,$x1-$gap/2+$outer/2,$y2);
523 }
524
525 // ------------------------------------------------------------------------------
526
527 function ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$gap,$size,&$y,$optionsAll=array(), $startX0 = 0, $startX1=0){
528   // uses ezText to add the text, and returns the height taken by the largest heading
529   // this page will move the headings to a new page if they will not fit completely on this one
530   // transaction support will be used to implement this
531
532   if (isset($optionsAll['cols'])){
533     $options = $optionsAll['cols'];
534   } else {
535     $options = array();
536   }
537   
538   $mx=0;
539   $startPage = $this->ezPageCount;
540   $secondGo=0;
541
542   // $y is the position at which the top of the table should start, so the base
543   // of the first text, is $y-$height-$gap-$decender, but ezText starts by dropping $height
544   
545   // the return from this function is the total cell height, including gaps, and $y is adjusted
546   // to be the postion of the bottom line
547   
548   // begin the transaction
549   $this->transaction('start');
550   $ok=0;
551 //  $y-=$gap-$decender;
552   $y-=$gap;
553   while ($ok==0){
554     
555 //NEW CODE
556 if(!empty($optionsAll['shadeHeadings'])){
557                                 $shadeHeaderObject = $this->openObject();
558                                 $this->closeObject();
559                                 $this->addObject($shadeHeaderObject);
560                                 $this->reopenObject($shadeHeaderObject);
561                                 $startHeight = $y;
562                                 $this->setColor($optionsAll['headTextCol'][0], $optionsAll['headTextCol'][1], $optionsAll['headTextCol'][2], 1);
563 }       
564                 //END NEW CODE
565     foreach($cols as $colName=>$colHeading){
566       $this->ezSetY($y);
567       if (isset($options[$colName]) && isset($options[$colName]['justification'])){
568         $justification = $options[$colName]['justification'];
569       } else {
570         $justification = 'left';
571       }
572       $this->ezText($colHeading,$size,array('aleft'=> $pos[$colName],'aright'=>($maxWidth[$colName]+$pos[$colName]),'justification'=>$justification));
573       $dy = $y-$this->y;
574       if ($dy>$mx){
575         $mx=$dy;
576       }
577     }
578
579         //NEW CODE
580 if(!empty($optionsAll['shadeHeadings'])){
581                         $this->closeObject();
582                         $headingHeight = $mx + $gap * 2 - $decender;
583                         $this->setColor($optionsAll['headCol'][0],$optionsAll['headCol'][1],$optionsAll['headCol'][2],1);
584                 $this->filledRectangle($startX0 - $gap/2 ,$y-2,$startX1 - $startX0 - 1, -1 * $mx);
585 }               
586 //END NEW 
587
588     $y = $y - $mx - $gap + $decender;
589 //    $y -= $mx-$gap+$decender;
590     
591     // now, if this has moved to a new page, then abort the transaction, move to a new page, and put it there
592     // do not check on the second time around, to avoid an infinite loop
593     if ($this->ezPageCount != $startPage && $secondGo==0){
594       $this->transaction('rewind');
595       $this->ezNewPage();
596       $y = $this->y - $gap-$decender;
597       $ok=0;
598       $secondGo=1;
599 //      $y = $store_y;
600       $mx=0;
601
602     } else {
603       $this->transaction('commit');
604       $ok=1;
605     }
606   }
607
608   return $mx+$gap*2-$decender;
609 }
610
611 // ------------------------------------------------------------------------------
612
613 function ezPrvtGetTextWidth($size,$text){
614   // will calculate the maximum width, taking into account that the text may be broken
615   // by line breaks.
616   $mx=0;
617   $lines = explode("\n",$text);
618   foreach ($lines as $line){
619     $w = $this->getTextWidth($size,$line);
620     if ($w>$mx){
621       $mx=$w;
622     }
623   }
624   return $mx;
625 }
626
627 // ------------------------------------------------------------------------------
628
629
630 function getDisplayCount($cnt){
631          $kCount = floor(($cnt ) / 1000);
632      $dispCount = $cnt - 1000 * $kCount;
633      if($kCount > 0){
634         $kCount .='K';
635      }else{
636         $kCount = '';
637      }
638      return $kCount . $dispCount;       
639 }
640 function ezTable(&$data,$cols='',$title='',$options=''){
641   // add a table of information to the pdf document
642   // $data is a two dimensional array
643   // $cols (optional) is an associative array, the keys are the names of the columns from $data
644   // to be presented (and in that order), the values are the titles to be given to the columns
645   // $title (optional) is the title to be put on the top of the table
646   //
647   // $options is an associative array which can contain:
648   // 'showLines'=> 0,1,2, default is 1 (show outside and top lines only), 2=> lines on each row
649   // 'showHeadings' => 0 or 1
650   // 'shaded'=> 0,1,2,3 default is 1 (1->alternate lines are shaded, 0->no shading, 2-> both shaded,4-> shade columns alternating second uses shadeCol2)
651   // 'shadeCol' => (r,g,b) array, defining the colour of the shading, default is (0.8,0.8,0.8)
652   // 'shadeCol2' => (r,g,b) array, defining the colour of the shading of the other blocks, default is (0.7,0.7,0.7)
653   // 'fontSize' => 10
654   // 'textCol' => (r,g,b) array, text colour
655   // 'titleFontSize' => 12
656   // 'rowGap' => 2 , the space added at the top and bottom of each row, between the text and the lines
657   // 'colGap' => 5 , the space on the left and right sides of each cell
658   // 'lineCol' => (r,g,b) array, defining the colour of the lines, default, black.
659   // 'xPos' => 'left','right','center','centre',or coordinate, reference coordinate in the x-direction
660   // 'xOrientation' => 'left','right','center','centre', position of the table w.r.t 'xPos' 
661   // 'width'=> <number> which will specify the width of the table, if it turns out to not be this
662   //    wide, then it will stretch the table to fit, if it is wider then each cell will be made
663   //    proportionalty smaller, and the content may have to wrap.
664   // 'maxWidth'=> <number> similar to 'width', but will only make table smaller than it wants to be
665   // 'options' => array(<colname>=>array('justification'=>'left','width'=>100,'link'=>linkDataName),<colname>=>....)
666   //             allow the setting of other paramaters for the individual columns
667   // 'minRowSpace'=> the minimum space between the bottom of each row and the bottom margin, in which a new row will be started
668   //                  if it is less, then a new page would be started, default=-100
669   // 'innerLineThickness'=>1
670   // 'outerLineThickness'=>1
671   // 'splitRows'=>0, 0 or 1, whether or not to allow the rows to be split across page boundaries
672   // 'protectRows'=>number, the number of rows to hold with the heading on page, ie, if there less than this number of
673   //                rows on the page, then move the whole lot onto the next page, default=1
674   //
675   // note that the user will have had to make a font selection already or this will not 
676   // produce a valid pdf file.
677   
678   if (!is_array($data)){
679     return;
680   }
681   
682   if (!is_array($cols)){
683     // take the columns from the first row of the data set
684     reset($data);
685     list($k,$v)=each($data);
686     if (!is_array($v)){
687       return;
688     }
689     $cols=array();
690     foreach($v as $k1=>$v1){
691       $cols[$k1]=$k1;
692     }
693   }
694   
695   if (!is_array($options)){
696     $options=array();
697   }
698
699   $defaults = array(
700    'shaded'=>1,'showLines'=>1,'shadeCol'=>array(0.8,0.8,0.8),'shadeCol2'=>array(0.7,0.7,0.7),'fontSize'=>10,'titleFontSize'=>12
701     ,'titleGap'=>5,'lineCol'=>array(0,0,0),'gap'=>5,'xPos'=>'centre','xOrientation'=>'centre'
702     ,'showHeadings'=>1,'textCol'=>array(0,0,0),'width'=>0,'maxWidth'=>0,'cols'=>array(),'minRowSpace'=>-100,'rowGap'=>2,'colGap'=>5
703     ,'innerLineThickness'=>1,'outerLineThickness'=>1,'splitRows'=>0,'protectRows'=>1, 'shadeHeadings'=>0, 'headCol'=>array(.7,.7,.7), 'headTextCol'=>array(0,0,0), 'display_footer'=>0, 'footer'=>array('SugarCRM','http://www.sugarcrm.com'),
704     'display_header'=>0, 'footer'=>'SugarCRM Report','showRowCount'=>0,
705     );
706
707   foreach($defaults as $key=>$value){
708     if (is_array($value)){
709       if (!isset($options[$key]) || !is_array($options[$key])){
710         $options[$key]=$value;
711       }
712     } else {
713       if (!isset($options[$key])){
714         $options[$key]=$value;
715       }
716     }
717   }
718   //edit the left margin if the showing of row numbers will cause bad things
719  
720   
721   $options['gap']=2*$options['colGap'];
722   
723   $middle = ($this->ez['pageWidth']-$this->ez['rightMargin'])/2+($this->ez['leftMargin'])/2;
724   // figure out the maximum widths of the text within each column
725   $maxWidth=array();
726   foreach($cols as $colName=>$colHeading){
727     $maxWidth[$colName]=0;
728   }
729   // find the maximum cell widths based on the data
730   foreach($data as $row){
731     foreach($cols as $colName=>$colHeading){
732       $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$row[$colName])*1.01;
733       if ($w > $maxWidth[$colName]){
734         $maxWidth[$colName]=$w;
735       }
736     }
737   }
738   // and the maximum widths to fit in the headings
739   foreach($cols as $colName=>$colTitle){
740     $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$colTitle)*1.01;
741     if ($w > $maxWidth[$colName]){
742       $maxWidth[$colName]=$w;
743     }
744   }
745   
746   // calculate the start positions of each of the columns
747   $pos=array();
748   $x=0;
749   $t=$x;
750   $adjustmentWidth=0;
751   $setWidth=0;
752   foreach($maxWidth as $colName => $w){
753     $pos[$colName]=$t;
754     // if the column width has been specified then set that here, also total the
755     // width avaliable for adjustment
756     if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['width']) && $options['cols'][$colName]['width']>0){
757       $t=$t+$options['cols'][$colName]['width'];
758       $maxWidth[$colName] = $options['cols'][$colName]['width']-$options['gap'];
759       $setWidth += $options['cols'][$colName]['width'];
760     } else {
761       $t=$t+$w+$options['gap'];
762       $adjustmentWidth += $w;
763       $setWidth += $options['gap'];
764     }
765   }
766   $pos['_end_']=$t;
767
768   // if maxWidth is specified, and the table is too wide, and the width has not been set,
769   // then set the width.
770   if ($options['width']==0 && $options['maxWidth'] && ($t-$x)>$options['maxWidth']){
771     // then need to make this one smaller
772     $options['width']=$options['maxWidth'];
773   }
774
775   if ($options['width'] && $adjustmentWidth>0 && $setWidth<$options['width']){
776     // first find the current widths of the columns involved in this mystery
777     $cols0 = array();
778     $cols1 = array();
779     $xq=0;
780     $presentWidth=0;
781     $last='';
782     foreach($pos as $colName=>$p){
783       if (!isset($options['cols'][$last]) || !isset($options['cols'][$last]['width']) || $options['cols'][$last]['width']<=0){
784         if (strlen($last)){
785           $cols0[$last]=$p-$xq -$options['gap'];
786           $presentWidth += ($p-$xq - $options['gap']);
787         }
788       } else {
789         $cols1[$last]=$p-$xq;
790       }
791       $last=$colName;
792       $xq=$p;
793     }
794     // $cols0 contains the widths of all the columns which are not set
795     $neededWidth = $options['width']-$setWidth;
796     // if needed width is negative then add it equally to each column, else get more tricky
797     if ($presentWidth<$neededWidth){
798       foreach($cols0 as $colName=>$w){
799         $cols0[$colName]+= ($neededWidth-$presentWidth)/count($cols0);
800       }
801     } else {
802     
803       $cnt=0;
804       while ($presentWidth>$neededWidth && $cnt<100){
805         $cnt++; // insurance policy
806         // find the widest columns, and the next to widest width
807         $aWidest = array();
808         $nWidest=0;
809         $widest=0;
810         foreach($cols0 as $colName=>$w){
811           if ($w>$widest){
812             $aWidest=array($colName);
813             $nWidest = $widest;
814             $widest=$w;
815           } else if ($w==$widest){
816             $aWidest[]=$colName;
817           }
818         }
819         // then figure out what the width of the widest columns would have to be to take up all the slack
820         $newWidestWidth = $widest - ($presentWidth-$neededWidth)/count($aWidest);
821         if ($newWidestWidth > $nWidest){
822           // then there is space to set them to this
823           foreach($aWidest as $colName){
824             $cols0[$colName] = $newWidestWidth;
825           }
826           $presentWidth=$neededWidth;
827         } else {
828           // there is not space, reduce the size of the widest ones down to the next size down, and we
829           // will go round again
830           foreach($aWidest as $colName){
831             $cols0[$colName] = $nWidest;
832           }
833           $presentWidth=$presentWidth-($widest-$nWidest)*count($aWidest);
834         }
835       }
836     }
837     // $cols0 now contains the new widths of the constrained columns.
838     // now need to update the $pos and $maxWidth arrays
839     $xq=0;
840     foreach($pos as $colName=>$p){
841       $pos[$colName]=$xq;
842       if (!isset($options['cols'][$colName]) || !isset($options['cols'][$colName]['width']) || $options['cols'][$colName]['width']<=0){
843         if (isset($cols0[$colName])){
844           $xq += $cols0[$colName] + $options['gap'];
845           $maxWidth[$colName]=$cols0[$colName];
846         }
847       } else {
848         if (isset($cols1[$colName])){
849           $xq += $cols1[$colName];
850         }
851       }
852     }
853
854     $t=$x+$options['width'];
855     $pos['_end_']=$t;
856   }
857
858   // now adjust the table to the correct location across the page
859   switch ($options['xPos']){
860     case 'left':
861       $xref = $this->ez['leftMargin'];
862       break;
863     case 'right':
864       $xref = $this->ez['pageWidth'] - $this->ez['rightMargin'];
865       break;
866     case 'centre':
867     case 'center':
868       $xref = $middle;
869       break;
870     default:
871       $xref = $options['xPos'];
872       break;
873   }
874   switch ($options['xOrientation']){
875     case 'left':
876       $dx = $xref-$t;
877       break;
878     case 'right':
879       $dx = $xref;
880       break;
881     case 'centre':
882     case 'center':
883       $dx = $xref-$t/2;
884       break;
885   }
886
887
888   foreach($pos as $k=>$v){
889     $pos[$k]=$v+$dx;
890   }
891   $x0=$x+$dx;
892   $x1=$t+$dx;
893
894   $baseLeftMargin = $this->ez['leftMargin'];
895   $basePos = $pos;
896   $baseX0 = $x0;
897   $baseX1 = $x1;
898   
899   // ok, just about ready to make me a table
900   $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2]);
901   $this->setStrokeColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2]);
902
903   $middle = ($x1+$x0)/2;
904
905   // start a transaction which will be used to regress the table, if there are not enough rows protected
906   if ($options['protectRows']>0){
907     $this->transaction('start');
908     $movedOnce=0;
909   }
910   $abortTable = 1;
911   while ($abortTable){
912   $abortTable=0;
913
914   $dm = $this->ez['leftMargin']-$baseLeftMargin;
915   foreach($basePos as $k=>$v){
916     $pos[$k]=$v+$dm;
917   }
918   $x0=$baseX0+$dm;
919   $x1=$baseX1+$dm;
920   $middle = ($x1+$x0)/2;
921
922
923   // if the title is set, then do that
924   if (strlen($title)){
925     $w = $this->getTextWidth($options['titleFontSize'],$title);
926     $this->y -= $this->getFontHeight($options['titleFontSize']);
927     if ($this->y < $this->ez['bottomMargin']){
928       $this->ezNewPage();
929         // margins may have changed on the newpage
930         $dm = $this->ez['leftMargin']-$baseLeftMargin;
931         foreach($basePos as $k=>$v){
932           $pos[$k]=$v+$dm;
933         }
934         $x0=$baseX0+$dm;
935         $x1=$baseX1+$dm;
936         $middle = ($x1+$x0)/2;
937       $this->y -= $this->getFontHeight($options['titleFontSize']);
938     }
939     $this->addText($middle-$w/2,$this->y,$options['titleFontSize'],$title);
940     $this->y -= $options['titleGap'];
941   }
942
943         // margins may have changed on the newpage
944         $dm = $this->ez['leftMargin']-$baseLeftMargin;
945         foreach($basePos as $k=>$v){
946           $pos[$k]=$v+$dm;
947         }
948         $x0=$baseX0+$dm;
949         $x1=$baseX1+$dm;
950   
951   $y=$this->y; // to simplify the code a bit
952   
953   // make the table
954   $height = $this->getFontHeight($options['fontSize']);
955   $decender = $this->getFontDecender($options['fontSize']);
956
957   
958   
959   $y0=$y+$decender;
960   $dy=0;
961   if ($options['showHeadings']){
962                 
963         
964     
965      $this->setColor($options['headTextCol'][0],$options['headTextCol'][1],$options['headTextCol'][2],1);
966    $dm = $this->ez['leftMargin']-$baseLeftMargin;
967     foreach($basePos as $k=>$v){
968       $pos[$k]=$v+$dm;
969     }
970     $x0=$baseX0+$dm;
971     $x1=$baseX1+$dm;
972
973          // this function will move the start of the table to a new page if it does not fit on this one
974     $headingHeight = $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options, $x0, $x1);
975     $y0 = $y+$headingHeight;
976     $y1 = $y;
977
978 /*
979     $dm = $this->ez['leftMargin']-$baseLeftMargin;
980     foreach($basePos as $k=>$v){
981       $pos[$k]=$v+$dm;
982     }
983     $x0=$baseX0+$dm;
984     $x1=$baseX1+$dm;
985 */
986     $this->lastWidth = $x1 - $x0;
987     
988      
989
990   } else {
991     $y1 = $y0;
992   }
993   $firstLine=1;
994
995   
996   // open an object here so that the text can be put in over the shading
997   if ($options['shaded'] && $options['shaded'] != 4 ){
998     $this->saveState();
999     $textObjectId = $this->openObject();
1000     $this->closeObject();
1001     $this->addObject($textObjectId);
1002     $this->reopenObject($textObjectId);
1003   }
1004   
1005   $cnt=0;
1006   $newPage=0;
1007  
1008   
1009   foreach($data as $row){
1010     $cnt++;
1011     // the transaction support will be used to prevent rows being split
1012     if ($options['splitRows']==0){
1013       $pageStart = $this->ezPageCount;
1014       if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
1015         $columnStart = $this->ez['columns']['colNum'];
1016       }
1017       $this->transaction('start');
1018       $row_orig = $row;
1019       $y_orig = $y;
1020       $y0_orig = $y0;
1021       $y1_orig = $y1;
1022     }
1023     $ok=0;
1024     $secondTurn=0;
1025     while(!$abortTable && $ok == 0){
1026
1027     $mx=0;
1028     $newRow=1;
1029     while(!$abortTable && ($newPage || $newRow)){
1030       
1031       $y-=$height;
1032       if ($newPage || $y<$this->ez['bottomMargin'] || (isset($options['minRowSpace']) && $y<($this->ez['bottomMargin']+$options['minRowSpace'])) ){
1033         // check that enough rows are with the heading
1034         if ($options['protectRows']>0 && $movedOnce==0 && $cnt<=$options['protectRows']){
1035           // then we need to move the whole table onto the next page
1036           $movedOnce = 1;
1037           $abortTable = 1;
1038         }
1039       
1040         $y2=$y-$mx+2*$height+$decender-$newRow*$height;
1041         if ($options['showLines']){
1042           if (!$options['showHeadings']){
1043             $y0=$y1;
1044           }
1045           $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
1046         }
1047         if ($options['shaded'] && $options['shaded'] != 4){
1048           $this->closeObject();
1049           $this->restoreState();
1050         }
1051   if($options['display_header'] && isset($options['header'])){
1052                 $header = $options['header'];
1053                 $headerX = ($this->ez['pageWidth'] - $this->getTextWidth( $options['fontSize'], $header) )/2 ;
1054                 $headerY = ($this->ez['pageHeight'] - ($this->getFontHeight($options['fontSize']) * 3));
1055                 $this->addText($headerX , $headerY, $options['fontSize'] , $header);
1056    }
1057         if($options['display_footer'] && isset($options['footer'])){
1058                 $footer = $options['footer'];
1059                 $footerX = ($this->ez['pageWidth'] - $this->getTextWidth( $options['fontSize'], $footer[0]) )/2 ;
1060                 $footerY = $this->getFontHeight($options['fontSize']);
1061                 $this->addText($footerX , $footerY, $options['fontSize'] , $footer[0]);
1062                 if(!empty($footer[1])){
1063                         $this->addLink($footer[1],$footerX, $footerY ,  $footerX +  $this->getTextWidth( $options['fontSize'], $footer[0]), 2 * $footerY);
1064                 
1065                 }
1066          }
1067         $this->ezNewPage();
1068         // and the margins may have changed, this is due to the possibility of the columns being turned on
1069         // as the columns are managed by manipulating the margins
1070
1071         $dm = $this->ez['leftMargin']-$baseLeftMargin;
1072         foreach($basePos as $k=>$v){
1073           $pos[$k]=$v+$dm;
1074         }
1075 //        $x0=$x0+$dm;
1076 //        $x1=$x1+$dm;
1077         $x0=$baseX0+$dm;
1078         $x1=$baseX1+$dm;
1079                 
1080         if ($options['shaded'] && $options['shaded'] != 4){
1081           $this->saveState();
1082           $textObjectId = $this->openObject();
1083           $this->closeObject();
1084           $this->addObject($textObjectId);
1085           $this->reopenObject($textObjectId);
1086         }
1087         $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2],1);
1088         $y = $this->ez['pageHeight']-$this->ez['topMargin'];
1089         $y0=$y+$decender;
1090         $mx=0;
1091         if ($options['showHeadings']){
1092                 
1093           $this->setColor($options['headTextCol'][0],$options['headTextCol'][1],$options['headTextCol'][2],1);
1094           $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
1095           $y1=$y;
1096         } else {
1097           $y1=$y0;
1098         }
1099         $firstLine=1;
1100         $y -= $height;
1101       }
1102       $newRow=0;
1103       // write the actual data
1104       // if these cells need to be split over a page, then $newPage will be set, and the remaining
1105       // text will be placed in $leftOvers
1106       $newPage=0;
1107       $leftOvers=array();
1108           $colIndex = 0;
1109         
1110       foreach($cols as $colName=>$colTitle){
1111         if ($options['shaded']== 4 && $colIndex % 2 == 0 ){
1112           $this->saveState();
1113           $textObjectId = $this->openObject();
1114           $this->closeObject();
1115           $this->addObject($textObjectId);
1116           $this->reopenObject($textObjectId);
1117         }
1118                 $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2],1);
1119         $this->ezSetY($y+$height);
1120         $colNewPage=0;
1121         if (isset($row[$colName])){
1122           if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['link']) && strlen($options['cols'][$colName]['link'])){
1123             
1124             $lines = explode("\n",$row[$colName]);
1125             if (isset($row[$options['cols'][$colName]['link']]) && strlen($row[$options['cols'][$colName]['link']])){
1126               foreach($lines as $k=>$v){
1127                 $lines[$k]='<c:alink:'.$row[$options['cols'][$colName]['link']].'>'.$v.'</c:alink>';
1128               }
1129             }
1130           } else {
1131             $lines = explode("\n",$row[$colName]);
1132           }
1133         } else {
1134           $lines = array();
1135         }
1136         $this->y -= $options['rowGap'];
1137         foreach ($lines as $line){
1138           $line = $this->ezProcessText($line);
1139           $start=1;
1140
1141           while (strlen($line) || $start){
1142             $start=0;
1143             if (!$colNewPage){
1144               $this->y=$this->y-$height;
1145             }
1146             if ($this->y < $this->ez['bottomMargin']){
1147   //            $this->ezNewPage();
1148               $newPage=1;  // whether a new page is required for any of the columns
1149               $colNewPage=1; // whether a new page is required for this column
1150             }
1151             if ($colNewPage){
1152               if (isset($leftOvers[$colName])){
1153                 $leftOvers[$colName].="\n".$line;
1154               } else {
1155                 $leftOvers[$colName] = $line;
1156               }
1157               $line='';
1158             } else {
1159               if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['justification']) ){
1160                 $just = $options['cols'][$colName]['justification'];
1161               } else {
1162                 $just='left';
1163               }
1164               $line=$this->addTextWrap($pos[$colName],$this->y,$maxWidth[$colName],$options['fontSize'],$line,$just);
1165             }
1166           }
1167            if ($options['shaded'] == 4 && $colIndex%2==0){
1168                  $this->closeObject();
1169                  $this->setColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2],1);
1170                  $this->filledRectangle($pos[$colName],$this->y - 1,$maxWidth[$colName],$options['fontSize'] );
1171              $this->restoreState();
1172       }
1173           $colIndex++;
1174         }
1175   
1176         $dy=$y+$height-$this->y+$options['rowGap'];
1177         if ($dy-$height*$newPage>$mx){
1178           $mx=$dy-$height*$newPage;
1179         }
1180       }
1181       // set $row to $leftOvers so that they will be processed onto the new page
1182       $row = $leftOvers;
1183       // now add the shading underneath
1184       if ($options['shaded'] && $options['shaded'] != 4 && $cnt%2==0){
1185         $this->closeObject();
1186         $this->setColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2],1);
1187         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
1188         $this->reopenObject($textObjectId);
1189       }
1190
1191       if ($options['shaded']==2 && $cnt%2==1){
1192         $this->closeObject();
1193         $this->setColor($options['shadeCol2'][0],$options['shadeCol2'][1],$options['shadeCol2'][2],1);
1194         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
1195         $this->reopenObject($textObjectId);
1196       }
1197
1198       if ($options['showLines']>1){
1199         // then draw a line on the top of each block
1200 //        $this->closeObject();
1201         $this->saveState();
1202         $this->setStrokeColor($options['lineCol'][0],$options['lineCol'][1],$options['lineCol'][2],1);
1203 //        $this->line($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
1204         if ($firstLine){
1205           $this->setLineStyle($options['outerLineThickness']);
1206           $firstLine=0;
1207         } else {
1208           $this->setLineStyle($options['innerLineThickness']);
1209         }
1210         $this->line($x0-$options['gap']/2,$y+$decender+$height,$x1-$options['gap']/2,$y+$decender+$height);
1211         $this->restoreState();
1212 //        $this->reopenObject($textObjectId);
1213       }
1214     } // end of while 
1215     $y=$y-$mx+$height;
1216     
1217     // checking row split over pages
1218     if ($options['splitRows']==0){
1219       if ( ( ($this->ezPageCount != $pageStart) || (isset($this->ez['columns']) && $this->ez['columns']['on']==1 && $columnStart != $this->ez['columns']['colNum'] ))  && $secondTurn==0){
1220         // then we need to go back and try that again !
1221         $newPage=1;
1222         $secondTurn=1;
1223         $this->transaction('rewind');
1224         $row = $row_orig;
1225         $y = $y_orig;
1226         $y0 = $y0_orig;
1227         $y1 = $y1_orig;
1228         $ok=0;
1229
1230         $dm = $this->ez['leftMargin']-$baseLeftMargin;
1231         foreach($basePos as $k=>$v){
1232           $pos[$k]=$v+$dm;
1233         }
1234         $x0=$baseX0+$dm;
1235         $x1=$baseX1+$dm;
1236
1237       } else {
1238         if($options['showRowCount'] && $cnt > 0){
1239                 $dispCount = $cnt;
1240                 $this->addText($this->ez['leftMargin']  - $this->getTextWidth($options['fontSize'] - 2, $dispCount) -10, $this->y, $options['fontSize'] - 2, $dispCount);
1241         }
1242         $this->transaction('commit');
1243         $ok=1;
1244       }
1245     } else {
1246         
1247       $ok=1;  // don't go round the loop if splitting rows is allowed
1248     }
1249     
1250     }  // end of while to check for row splitting
1251     if ($abortTable){
1252       if ($ok==0){
1253         $this->transaction('abort');
1254       }
1255       // only the outer transaction should be operational
1256       $this->transaction('rewind');
1257       $this->ezNewPage();
1258       break;
1259     }
1260     
1261   } // end of foreach ($data as $row)
1262   
1263   } // end of while ($abortTable)
1264
1265   // table has been put on the page, the rows guarded as required, commit.
1266   $this->transaction('commit');
1267
1268   $y2=$y+$decender;
1269   if ($options['showLines']){
1270     if (!$options['showHeadings']){
1271       $y0=$y1;
1272     }
1273     $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
1274   }
1275
1276   // close the object for drawing the text on top
1277   if ($options['shaded'] && $options['shaded'] != 4){
1278     $this->closeObject();
1279     $this->restoreState();
1280   }
1281   if($options['display_header'] && isset($options['header'])){
1282                 $header = $options['header'];
1283                 $headerX = ($this->ez['pageWidth'] - $this->getTextWidth( $options['fontSize'], $header) )/2 ;
1284                 $headerY = ($this->ez['pageHeight'] - ($this->getFontHeight($options['fontSize']) * 3));
1285                 $this->addText($headerX , $headerY, $options['fontSize'] , $header);
1286    }
1287    if($options['display_footer'] && isset($options['footer'])){
1288                 $footer = $options['footer'];
1289                 $footerX = ($this->ez['pageWidth'] - $this->getTextWidth( $options['fontSize'], $footer[0]) )/2 ;
1290                 $footerY = $this->getFontHeight($options['fontSize']);
1291                 $this->addText($footerX , $footerY, $options['fontSize'] , $footer[0]);
1292                 if(!empty($footer[1])){
1293                         $this->addLink($footer[1],$footerX, $footerY ,  $footerX +  $this->getTextWidth( $options['fontSize'], $footer[0]), 2 * $footerY);
1294                 
1295                 }
1296          }
1297   $oY = $this->y;
1298   $oX = 
1299   $this->y=$y;
1300   return $y;
1301 }
1302
1303 // ------------------------------------------------------------------------------
1304 function ezProcessText($text){
1305   // this function will intially be used to implement underlining support, but could be used for a range of other
1306   // purposes
1307   $search = array('<u>','<U>','</u>','</U>');
1308   $replace = array('<c:uline>','<c:uline>','</c:uline>','</c:uline>');
1309   return str_replace($search,$replace,$text);
1310 }
1311
1312 // ------------------------------------------------------------------------------
1313
1314 function ezText($text,$size=0,$options=array(),$test=0){
1315   // this will add a string of text to the document, starting at the current drawing
1316   // position.
1317   // it will wrap to keep within the margins, including optional offsets from the left
1318   // and the right, if $size is not specified, then it will be the last one used, or
1319   // the default value (12 I think).
1320   // the text will go to the start of the next line when a return code "\n" is found.
1321   // possible options are:
1322   // 'left'=> number, gap to leave from the left margin
1323   // 'right'=> number, gap to leave from the right margin
1324   // 'aleft'=> number, absolute left position (overrides 'left')
1325   // 'aright'=> number, absolute right position (overrides 'right')
1326   // 'justification' => 'left','right','center','centre','full'
1327
1328   // only set one of the next two items (leading overrides spacing)
1329   // 'leading' => number, defines the total height taken by the line, independent of the font height.
1330   // 'spacing' => a real number, though usually set to one of 1, 1.5, 2 (line spacing as used in word processing)
1331   
1332   // if $test is set then this should just check if the text is going to flow onto a new page or not, returning true or false
1333   
1334   // apply the filtering which will make the underlining function.
1335   $text = $this->ezProcessText($text);
1336   
1337   $newPage=false;
1338   $store_y = $this->y;
1339   
1340   if (is_array($options) && isset($options['aleft'])){
1341     $left=$options['aleft'];
1342   } else {
1343     $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
1344   }
1345   if (is_array($options) && isset($options['aright'])){
1346     $right=$options['aright'];
1347   } else {
1348     $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
1349   }
1350   if ($size<=0){
1351     $size = $this->ez['fontSize'];
1352   } else {
1353     $this->ez['fontSize']=$size;
1354   }
1355   
1356   if (is_array($options) && isset($options['justification'])){
1357     $just = $options['justification'];
1358   } else {
1359     $just = 'left';
1360   }
1361   
1362   // modifications to give leading and spacing based on those given by Craig Heydenburg 1/1/02
1363   if (is_array($options) && isset($options['leading'])) { ## use leading instead of spacing
1364     $height = $options['leading'];
1365         } else if (is_array($options) && isset($options['spacing'])) {
1366     $height = $this->getFontHeight($size) * $options['spacing'];
1367         } else {
1368                 $height = $this->getFontHeight($size);
1369         }
1370
1371   
1372   $lines = explode("\n",$text);
1373   foreach ($lines as $line){
1374     $start=1;
1375     while (strlen($line) || $start){
1376       $start=0;
1377       $this->y=$this->y-$height;
1378       if ($this->y < $this->ez['bottomMargin']){
1379         if ($test){
1380           $newPage=true;
1381         } else {
1382           $this->ezNewPage();
1383           // and then re-calc the left and right, in case they have changed due to columns
1384         }
1385       }
1386       if (is_array($options) && isset($options['aleft'])){
1387         $left=$options['aleft'];
1388       } else {
1389         $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
1390       }
1391       if (is_array($options) && isset($options['aright'])){
1392         $right=$options['aright'];
1393       } else {
1394         $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
1395       }
1396       $line=$this->addTextWrap($left,$this->y,$right-$left,$size,$line,$just,0,$test);
1397     }
1398   }
1399
1400   if ($test){
1401     $this->y=$store_y;
1402     return $newPage;
1403   } else {
1404     return $this->y;
1405   }
1406 }
1407
1408 // ------------------------------------------------------------------------------
1409
1410 function ezImage($image,$pad = 5,$width = 0,$resize = 'full',$just = 'center',$border = ''){
1411         //beta ezimage function
1412         if (stristr($image,'://'))//copy to temp file
1413         {
1414                 $fp = @fopen($image,"rb");
1415                 while(!feof($fp))
1416                 {
1417                         $cont.= fread($fp,1024);
1418                 }
1419                 fclose($fp);
1420                 $this->lastWidth = $width;
1421                 $image = tempnam ("/tmp", "php-pdf");
1422                 $fp2 = @fopen($image,"w");
1423                 fwrite($fp2,$cont);
1424                 fclose($fp2);
1425                 $temp = true;
1426         }
1427
1428         if (!(file_exists($image))) return false; //return immediately if image file does not exist
1429         $imageInfo = getimagesize($image);
1430         switch ($imageInfo[2]){
1431                 case 2:
1432                         $type = "jpeg";
1433                         break;
1434                 case 3:
1435                         $type = "png";
1436                         break;
1437                 default:
1438                         return false; //return if file is not jpg or png
1439         }
1440         if ($width == 0) $width = $imageInfo[0]; //set width
1441         $ratio = $imageInfo[0]/$imageInfo[1];
1442
1443         //get maximum width of image
1444         if (isset($this->ez['columns']) && $this->ez['columns']['on'] == 1)
1445         {
1446                 $bigwidth = $this->ez['columns']['width'] - ($pad * 2);
1447         }
1448         else
1449         {
1450                 $bigwidth = $this->ez['pageWidth'] - ($pad * 2);
1451         }
1452         //fix width if larger than maximum or if $resize=full
1453         if ($resize == 'full' || $resize == 'width' || $width > $bigwidth)
1454         {
1455                 $width = $bigwidth;
1456
1457         }
1458
1459         $height = ($width/$ratio); //set height
1460
1461         //fix size if runs off page
1462         if ($height > ($this->y - $this->ez['bottomMargin'] - ($pad * 2)))
1463         {
1464                 if ($resize != 'full')
1465                 {
1466                         $this->ezNewPage();
1467                 }
1468                 else
1469                 {
1470                         $height = ($this->y - $this->ez['bottomMargin'] - ($pad * 2)); //shrink height
1471                         $width = ($height*$ratio); //fix width
1472                 }
1473         }
1474
1475         //fix x-offset if image smaller than bigwidth
1476         if ($width < $bigwidth)
1477         {
1478                 //center if justification=center
1479                 if ($just == 'center')
1480                 {
1481                         $offset = ($bigwidth - $width) / 2;
1482                 }
1483                 //move to right if justification=right
1484                 if ($just == 'right')
1485                 {
1486                         $offset = ($bigwidth - $width);
1487                 }
1488                 //leave at left if justification=left
1489                 if ($just == 'left')
1490                 {
1491                         $offset = 0;
1492                 }
1493         }
1494
1495
1496         //call appropriate function
1497         if ($type == "jpeg"){
1498                 $this->addJpegFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
1499         }
1500
1501         if ($type == "png"){
1502                 $this->addPngFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
1503         }
1504         //draw border
1505         if ($border != '')
1506         {
1507         if (!(isset($border['color'])))
1508         {
1509                 $border['color']['red'] = .5;
1510                 $border['color']['blue'] = .5;
1511                 $border['color']['green'] = .5;
1512         }
1513         if (!(isset($border['width']))) $border['width'] = 1;
1514         if (!(isset($border['cap']))) $border['cap'] = 'round';
1515         if (!(isset($border['join']))) $border['join'] = 'round';
1516         
1517
1518         $this->setStrokeColor($border['color']['red'],$border['color']['green'],$border['color']['blue']);
1519         $this->setLineStyle($border['width'],$border['cap'],$border['join']);
1520         $this->rectangle($this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width,$height);
1521
1522         }
1523         // move y below image
1524         $this->y = $this->y - $pad - $height;
1525         //remove tempfile for remote images
1526         if ($temp == true) unlink($image);
1527
1528 }
1529 // ------------------------------------------------------------------------------
1530
1531 // note that templating code is still considered developmental - have not really figured
1532 // out a good way of doing this yet.
1533 function loadTemplate($templateFile){
1534   // this function will load the requested template ($file includes full or relative pathname)
1535   // the code for the template will be modified to make it name safe, and then stored in 
1536   // an array for later use
1537   // The id of the template will be returned for the user to operate on it later
1538   if (!file_exists($templateFile)){
1539     return -1;
1540   }
1541
1542   $code = implode('',file($templateFile));
1543   if (!strlen($code)){
1544     return;
1545   }
1546
1547   $code = trim($code);
1548   if (substr($code,0,5)=='<?php'){
1549     $code = substr($code,5);
1550   }
1551   if (substr($code,-2)=='?>'){
1552     $code = substr($code,0,strlen($code)-2);
1553   }
1554   if (isset($this->ez['numTemplates'])){
1555     $newNum = $this->ez['numTemplates'];
1556     $this->ez['numTemplates']++;
1557   } else {
1558     $newNum=0;
1559     $this->ez['numTemplates']=1;
1560     $this->ez['templates']=array();
1561   }
1562
1563   $this->ez['templates'][$newNum]['code']=$code;
1564
1565   return $newNum;
1566 }
1567
1568 // ------------------------------------------------------------------------------
1569
1570 function execTemplate($id,$data=array(),$options=array()){
1571   // execute the given template on the current document.
1572   if (!isset($this->ez['templates'][$id])){
1573     return;
1574   }
1575   eval($this->ez['templates'][$id]['code']);
1576 }
1577
1578 // ------------------------------------------------------------------------------
1579 function ilink($info){
1580   $this->alink($info,1);
1581 }
1582
1583 function alink($info,$internal=0){
1584   // a callback function to support the formation of clickable links within the document
1585   $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
1586   switch($info['status']){
1587     case 'start':
1588     case 'sol':
1589       // the beginning of the link
1590       // this should contain the URl for the link as the 'p' entry, and will also contain the value of 'nCallback'
1591       if (!isset($this->ez['links'])){
1592         $this->ez['links']=array();
1593       }
1594       $i = $info['nCallback'];
1595       $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height'],'url'=>$info['p']);
1596       if ($internal==0){
1597         $this->saveState();
1598         $this->setColor(0,0,1);
1599         $this->setStrokeColor(0,0,1);
1600         $thick = $info['height']*$lineFactor;
1601         $this->setLineStyle($thick);
1602       }
1603       break;
1604     case 'end':
1605     case 'eol':
1606       // the end of the link
1607       // assume that it is the most recent opening which has closed
1608       $i = $info['nCallback'];
1609       $start = $this->ez['links'][$i];
1610       // add underlining
1611       if ($internal){
1612         $this->addInternalLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
1613       } else {
1614         $a = deg2rad((float)$start['angle']-90.0);
1615         $drop = $start['height']*$lineFactor*1.5;
1616         $dropx = cos($a)*$drop;
1617         $dropy = -sin($a)*$drop;
1618         $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
1619         $this->addLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
1620         $this->restoreState();
1621       }
1622       break;
1623   }
1624 }
1625
1626 // ------------------------------------------------------------------------------
1627
1628 function uline($info){
1629   // a callback function to support underlining
1630   $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
1631   switch($info['status']){
1632    
1633 case 'start':
1634     case 'sol':
1635     
1636       // the beginning of the underline zone
1637       if (!isset($this->ez['links'])){
1638         $this->ez['links']=array();
1639       }
1640       $i = $info['nCallback'];
1641       $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
1642       $this->saveState();
1643       $thick = $info['height']*$lineFactor;
1644       $this->setLineStyle($thick);
1645       break;
1646     case 'end':
1647     case 'eol':
1648       // the end of the link
1649       // assume that it is the most recent opening which has closed
1650       $i = $info['nCallback'];
1651       $start = $this->ez['links'][$i];
1652       // add underlining
1653       $a = deg2rad((float)$start['angle']-90.0);
1654       $drop = $start['height']*$lineFactor*1.5;
1655       $dropx = cos($a)*$drop;
1656       $dropy = -sin($a)*$drop;
1657       $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
1658       $this->restoreState();
1659       break;
1660   }
1661 }
1662
1663 // ------------------------------------------------------------------------------
1664
1665 }
1666 ?>