towards PEAR coding standards

This commit is contained in:
Marc Delisle
2005-12-04 19:28:28 +00:00
parent 88ff1c4181
commit 4a89855656

View File

@@ -7,11 +7,12 @@
*/ */
define('FPDF_FONTPATH','./libraries/fpdf/font/'); define('FPDF_FONTPATH', './libraries/fpdf/font/');
//if ($charset == 'utf-8') { //if ($charset == 'utf-8') {
define('PMA_PDF_FONT', 'FreeSans'); define('PMA_PDF_FONT', 'FreeSans');
require_once('./libraries/fpdf/ufpdf.php'); require_once('./libraries/fpdf/ufpdf.php');
class PMA_FPDF extends UFPDF { class PMA_FPDF extends UFPDF
{
}; };
//} else { //} else {
// define('PMA_PDF_FONT', 'Arial'); // define('PMA_PDF_FONT', 'Arial');
@@ -23,259 +24,262 @@ define('FPDF_FONTPATH','./libraries/fpdf/font/');
// Adapted from a LGPL script by Philip Clarke // Adapted from a LGPL script by Philip Clarke
class PMA_PDF extends PMA_FPDF { class PMA_PDF extends PMA_FPDF
{
var $tablewidths; var $tablewidths;
var $headerset; var $headerset;
var $footerset; var $footerset;
// overloading of a fpdf function: // overloading of a fpdf function:
function _beginpage($orientation) { function _beginpage($orientation)
$this->page++; {
// solved the problem of overwriting a page, if it already exists $this->page++;
if(!isset($this->pages[$this->page])) { // solved the problem of overwriting a page, if it already exists
$this->pages[$this->page]=''; if (!isset($this->pages[$this->page])) {
} $this->pages[$this->page] = '';
$this->state=2;
$this->x=$this->lMargin;
$this->y=$this->tMargin;
$this->lasth=0;
$this->FontFamily='';
//Page orientation
if (!$orientation) {
$orientation=$this->DefOrientation;
} else {
$orientation=strtoupper($orientation{0});
if ($orientation!=$this->DefOrientation){
$this->OrientationChanges[$this->page]=true;
} }
} $this->state = 2;
if ($orientation!=$this->CurOrientation) { $this->x = $this->lMargin;
//Change orientation $this->y = $this->tMargin;
if ($orientation=='P') { $this->lasth = 0;
$this->wPt=$this->fwPt; $this->FontFamily = '';
$this->hPt=$this->fhPt;
$this->w=$this->fw; //Page orientation
$this->h=$this->fh; if (!$orientation) {
$orientation = $this->DefOrientation;
} else { } else {
$this->wPt=$this->fhPt; $orientation = strtoupper($orientation{0});
$this->hPt=$this->fwPt; if ($orientation != $this->DefOrientation) {
$this->w=$this->fh; $this->OrientationChanges[$this->page] = true;
$this->h=$this->fw; }
} }
$this->PageBreakTrigger=$this->h-$this->bMargin; if ($orientation != $this->CurOrientation) {
$this->CurOrientation=$orientation; //Change orientation
} if ($orientation == 'P') {
} $this->wPt = $this->fwPt;
$this->hPt = $this->fhPt;
function Header() { $this->w = $this->fw;
global $maxY; $this->h = $this->fh;
} else {
// Check if header for this page already exists $this->wPt = $this->fhPt;
if (!isset($this->headerset[$this->page])) { $this->hPt = $this->fwPt;
$fullwidth = 0; $this->w = $this->fh;
foreach($this->tablewidths as $width) { $this->h = $this->fw;
$fullwidth += $width; }
$this->PageBreakTrigger = $this->h - $this->bMargin;
$this->CurOrientation = $orientation;
} }
$this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
$this->cellFontSize = $this->FontSizePt ;
$this->SetFont(PMA_PDF_FONT,'',( ( $this->titleFontSize) ? $this->titleFontSize : $this->FontSizePt ));
$this->Cell(0,$this->FontSizePt,$this->titleText,0,1,'C');
$l = ($this->lMargin);
$this->SetFont(PMA_PDF_FONT,'',$this->cellFontSize);
foreach($this->colTitles as $col => $txt) {
$this->SetXY($l,($this->tMargin));
$this->MultiCell($this->tablewidths[$col], $this->FontSizePt,$txt);
$l += $this->tablewidths[$col] ;
$maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
}
$this->SetXY($this->lMargin,$this->tMargin);
$this->setFillColor(200,200,200);
$l = ($this->lMargin);
foreach($this->colTitles as $col => $txt) {
$this->SetXY($l,$this->tMargin);
$this->cell($this->tablewidths[$col],$maxY-($this->tMargin),'',1,0,'L',1);
$this->SetXY($l,$this->tMargin);
$this->MultiCell($this->tablewidths[$col],$this->FontSizePt,$txt,0,'C');
$l += $this->tablewidths[$col];
}
$this->setFillColor(255,255,255);
// set headerset
$this->headerset[$this->page] = 1;
} }
$this->SetY($maxY); function Header()
} {
global $maxY;
function Footer() { // Check if header for this page already exists
if (!isset($this->headerset[$this->page])) {
$fullwidth = 0;
foreach ($this->tablewidths as $width) {
$fullwidth += $width;
}
$this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
$this->cellFontSize = $this->FontSizePt ;
$this->SetFont(PMA_PDF_FONT, '' ,($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt ));
$this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
$l = ($this->lMargin);
$this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
foreach ($this->colTitles as $col => $txt) {
$this->SetXY($l, ($this->tMargin));
$this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
$l += $this->tablewidths[$col] ;
$maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
}
$this->SetXY($this->lMargin, $this->tMargin);
$this->setFillColor(200, 200, 200);
$l = ($this->lMargin);
foreach ($this->colTitles as $col => $txt) {
$this->SetXY($l, $this->tMargin);
$this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
$this->SetXY($l, $this->tMargin);
$this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
$l += $this->tablewidths[$col];
}
$this->setFillColor(255, 255, 255);
// set headerset
$this->headerset[$this->page] = 1;
}
$this->SetY($maxY);
}
function Footer()
{
// Check if footer for this page already exists // Check if footer for this page already exists
if (!isset($this->footerset[$this->page])) { if (!isset($this->footerset[$this->page])) {
$this->SetY(-15); $this->SetY(-15);
//Page number //Page number
$this->Cell(0,10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}','T',0,'C'); $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
// set footerset // set footerset
$this->footerset[$this->page] = 1; $this->footerset[$this->page] = 1;
} }
}
function morepagestable($lineheight=8) {
// some things to set and 'remember'
$l = $this->lMargin;
$startheight = $h = $this->GetY();
$startpage = $currpage = $this->page;
// calculate the whole width
$fullwidth = 0;
foreach ($this->tablewidths as $width) {
$fullwidth += $width;
} }
// Now let's start to write the table function morepagestable($lineheight=8)
$row = 0; {
$tmpheight = array(); // some things to set and 'remember'
$maxpage = 0; $l = $this->lMargin;
$startheight = $h = $this->GetY();
$startpage = $currpage = $this->page;
while ($data = PMA_DBI_fetch_row($this->results)) { // calculate the whole width
$this->page = $currpage; $fullwidth = 0;
// write the horizontal borders foreach ($this->tablewidths as $width) {
$this->Line($l,$h,$fullwidth+$l,$h); $fullwidth += $width;
// write the content and remember the height of the highest col }
foreach($data as $col => $txt) {
// Now let's start to write the table
$row = 0;
$tmpheight = array();
$maxpage = 0;
while ($data = PMA_DBI_fetch_row($this->results)) {
$this->page = $currpage; $this->page = $currpage;
$this->SetXY($l,$h); // write the horizontal borders
$this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]); $this->Line($l, $h, $fullwidth+$l, $h);
// write the content and remember the height of the highest col
foreach ($data as $col => $txt) {
$this->page = $currpage;
$this->SetXY($l, $h);
$this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
$l += $this->tablewidths[$col]; $l += $this->tablewidths[$col];
if (!isset($tmpheight[$row.'-'.$this->page])) { if (!isset($tmpheight[$row.'-'.$this->page])) {
$tmpheight[$row.'-'.$this->page] = 0; $tmpheight[$row.'-'.$this->page] = 0;
}
if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
$tmpheight[$row.'-'.$this->page] = $this->GetY();
}
if ($this->page > $maxpage) {
$maxpage = $this->page;
}
unset($data[$col]);
} }
if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
$tmpheight[$row.'-'.$this->page] = $this->GetY(); // get the height we were in the last used page
} $h = $tmpheight[$row.'-'.$maxpage];
if ($this->page > $maxpage) { // set the "pointer" to the left margin
$maxpage = $this->page; $l = $this->lMargin;
} // set the $currpage to the last page
unset($data[$col]); $currpage = $maxpage;
unset($data[$row]);
$row++;
} }
// draw the borders
// get the height we were in the last used page // we start adding a horizontal line on the last page
$h = $tmpheight[$row.'-'.$maxpage]; $this->page = $maxpage;
// set the "pointer" to the left margin $this->Line($l, $h, $fullwidth+$l, $h);
$l = $this->lMargin; // now we start at the top of the document and walk down
// set the $currpage to the last page for ($i = $startpage; $i <= $maxpage; $i++) {
$currpage = $maxpage; $this->page = $i;
unset($data[$row]); $l = $this->lMargin;
$row++ ; $t = ($i == $startpage) ? $startheight : $this->tMargin;
} $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
// draw the borders $this->Line($l, $t, $l, $lh);
// we start adding a horizontal line on the last page foreach ($this->tablewidths as $width) {
$this->page = $maxpage; $l += $width;
$this->Line($l,$h,$fullwidth+$l,$h); $this->Line($l, $t, $l, $lh);
// now we start at the top of the document and walk down
for ($i = $startpage; $i <= $maxpage; $i++) {
$this->page = $i;
$l = $this->lMargin;
$t = ($i == $startpage) ? $startheight : $this->tMargin;
$lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
$this->Line($l,$t,$l,$lh);
foreach($this->tablewidths as $width) {
$l += $width;
$this->Line($l,$t,$l,$lh);
}
}
// set it to the last page, if not it'll cause some problems
$this->page = $maxpage;
}
function mysql_report($query,$attr=array()){
foreach($attr as $key=>$val){
$this->$key = $val ;
}
// Pass 1 for column widths
// TODO: force here a LIMIT to speed up pass 1 ?
$this->results = PMA_DBI_query($query, NULL, PMA_DBI_QUERY_UNBUFFERED);
$this->numFields = PMA_DBI_num_fields($this->results);
$this->fields = PMA_DBI_get_fields_meta($this->results);
// if column widths not set
if (!isset($this->tablewidths)){
// starting col width
$this->sColWidth = (($this->w-$this->lMargin-$this->rMargin))/$this->numFields;
// loop through results header and set initial col widths/ titles/ alignment
// if a col title is less than the starting col width / reduce that column size
for($i=0;$i<$this->numFields;$i++){
$stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
// set any column titles less than the start width to the column title width
if (($stringWidth) < $this->sColWidth){
$colFits[$i] = $stringWidth ;
}
$this->colTitles[$i] = $this->fields[$i]->name;
switch ($this->fields[$i]->type){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
} }
} }
// set it to the last page, if not it'll cause some problems
$this->page = $maxpage;
}
// loop through the data, any column whose contents is bigger i
// that the col size is resized function mysql_report($query, $attr = array())
while ($row = PMA_DBI_fetch_row($this->results)) { {
foreach($colFits as $key=>$val){ foreach ($attr as $key => $val){
$stringWidth = $this->getstringwidth($row[$key]) + 6 ; $this->$key = $val ;
if( ($stringWidth) > $this->sColWidth ){ }
// any col where row is bigger than the start width is now discarded
unset($colFits[$key]); // Pass 1 for column widths
} else{ // TODO: force here a LIMIT to speed up pass 1 ?
// if text is not bigger than the current column width setting enlarge the column $this->results = PMA_DBI_query($query, NULL, PMA_DBI_QUERY_UNBUFFERED);
if( ($stringWidth) > $val ){ $this->numFields = PMA_DBI_num_fields($this->results);
$colFits[$key] = ($stringWidth) ; $this->fields = PMA_DBI_get_fields_meta($this->results);
// if column widths not set
if (!isset($this->tablewidths)){
// starting col width
$this->sColWidth = ($this->w - $this->lMargin - $this->rMargin) / $this->numFields;
// loop through results header and set initial col widths/ titles/ alignment
// if a col title is less than the starting col width / reduce that column size
for ($i=0; $i < $this->numFields; $i++){
$stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
// set any column titles less than the start width to the column title width
if (($stringWidth) < $this->sColWidth){
$colFits[$i] = $stringWidth ;
}
$this->colTitles[$i] = $this->fields[$i]->name;
switch ($this->fields[$i]->type){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
}
}
// loop through the data, any column whose contents is bigger i
// that the col size is resized
while ($row = PMA_DBI_fetch_row($this->results)) {
foreach ($colFits as $key => $val) {
$stringWidth = $this->getstringwidth($row[$key]) + 6 ;
if ($stringWidth > $this->sColWidth) {
// any col where row is bigger than the start width is now discarded
unset($colFits[$key]);
} else {
// if text is not bigger than the current column width setting enlarge the column
if ($stringWidth > $val) {
$colFits[$key] = ($stringWidth) ;
}
} }
} }
} }
}
$totAlreadyFitted = 0; $totAlreadyFitted = 0;
foreach($colFits as $key=>$val){ foreach ($colFits as $key => $val){
// set fitted columns to smallest size // set fitted columns to smallest size
$this->tablewidths[$key] = $val; $this->tablewidths[$key] = $val;
// to work out how much (if any) space has been freed up // to work out how much (if any) space has been freed up
$totAlreadyFitted += $val; $totAlreadyFitted += $val;
}
$surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted);
for($i=0; $i<$this->numFields; $i++){
if (!in_array($i,array_keys($colFits))){
$this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits)));
} }
$surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
for ($i=0; $i < $this->numFields; $i++) {
if (!in_array($i, array_keys($colFits))) {
$this->tablewidths[$i] = $this->sColWidth + ($surplus / ($this->numFields - sizeof($colFits)));
}
}
ksort($this->tablewidths);
} }
ksort($this->tablewidths); PMA_DBI_free_result($this->results);
} // Pass 2
PMA_DBI_free_result($this->results); $this->results = PMA_DBI_query($query, NULL, PMA_DBI_QUERY_UNBUFFERED);
$this->Open();
$this->setY($this->tMargin);
$this->AddPage();
$this->morepagestable($this->FontSizePt);
PMA_DBI_free_result($this->results);
// Pass 2 } // end of mysql_report function
$this->results = PMA_DBI_query($query, NULL, PMA_DBI_QUERY_UNBUFFERED);
$this->Open();
$this->setY($this->tMargin);
$this->AddPage();
$this->morepagestable($this->FontSizePt);
PMA_DBI_free_result($this->results);
} // end of mysql_report function
} // end of PMA_PDF class } // end of PMA_PDF class
@@ -286,7 +290,8 @@ function mysql_report($query,$attr=array()){
* *
* @return bool Whether it suceeded * @return bool Whether it suceeded
*/ */
function PMA_exportComment($text) { function PMA_exportComment($text)
{
return TRUE; return TRUE;
} }
@@ -297,7 +302,8 @@ function PMA_exportComment($text) {
* *
* @access public * @access public
*/ */
function PMA_exportFooter() { function PMA_exportFooter()
{
return TRUE; return TRUE;
} }
@@ -308,7 +314,8 @@ function PMA_exportFooter() {
* *
* @access public * @access public
*/ */
function PMA_exportHeader() { function PMA_exportHeader()
{
return TRUE; return TRUE;
} }
@@ -321,7 +328,8 @@ function PMA_exportHeader() {
* *
* @access public * @access public
*/ */
function PMA_exportDBHeader($db) { function PMA_exportDBHeader($db)
{
return TRUE; return TRUE;
} }
@@ -334,7 +342,8 @@ function PMA_exportDBHeader($db) {
* *
* @access public * @access public
*/ */
function PMA_exportDBFooter($db) { function PMA_exportDBFooter($db)
{
return TRUE; return TRUE;
} }
@@ -347,7 +356,8 @@ function PMA_exportDBFooter($db) {
* *
* @access public * @access public
*/ */
function PMA_exportDBCreate($db) { function PMA_exportDBCreate($db)
{
return TRUE; return TRUE;
} }
@@ -364,19 +374,20 @@ function PMA_exportDBCreate($db) {
* *
* @access public * @access public
*/ */
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
{
global $what; global $what;
global $pdf_report_title; global $pdf_report_title;
// TODO: user-defined page orientation, paper size // TODO: user-defined page orientation, paper size
$pdf = new PMA_PDF('L','pt','A3'); $pdf = new PMA_PDF('L', 'pt', 'A3');
$pdf->AddFont('FreeSans','','FreeSans.php'); $pdf->AddFont('FreeSans', '', 'FreeSans.php');
$pdf->AddFont('FreeSans','B','FreeSansBold.php'); $pdf->AddFont('FreeSans', 'B', 'FreeSansBold.php');
$pdf->SetFont(PMA_PDF_FONT,'',11.5); $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
$pdf->AliasNbPages(); $pdf->AliasNbPages();
$attr=array('titleFontSize'=>18,'titleText'=>$pdf_report_title); $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
$pdf->mysql_report($sql_query,$attr); $pdf->mysql_report($sql_query, $attr);
// instead of $pdf->Output(): // instead of $pdf->Output():
if ($pdf->state < 3) { if ($pdf->state < 3) {