Location: PHPKode > projects > PDFdirectory > doc.php
<?php
define('FPDF_FONTPATH','font/');
define("LAST_PAGE", 4294967295);
require('fpdi.php');
include('./includes/connect.php');

class PDF extends FPDI{ //{{{
    var $_toc=array();
    var $_numbering=false; //this is intermediate because footer is called when next page starts
    var $_numberingFooter=false;
    var $_numPageNum=0;
    var $indexPage=1;
    var $orgname='';
    
    function AddPage($orientation=''){
        parent::AddPage($orientation);
        if($this->_numbering==true) $this->_numPageNum++;
    }
    
    function startPageNums()  {   
        $this->_numberingFooter=true;  
    }
    function stopPageNums()   {   $this->_numberingFooter=false;  }
    function numPageNo()      {   return $this->_numPageNum;   }

    //Table of contents extention
    function TOC_Entry($txt,$level=0){
        $this->_toc[]=array('t'=>$txt,'l'=>$level,'p'=>$this->numPageNo());
    }
    
    function insertTOC($labelSize=20,
                       $entrySize=10,
                       $tocfont='Times',
                       $label='Table of Contents'
                       ){ //{{{
                           
         $location=$this->indexPage;
        //make toc at end

        $this->AddPage();
        $tocstart=$this->page;
        
        $this->SetFont($tocfont,'B',$labelSize);
        $this->Cell(0,5/$this->k,$label,0,1,'C');
        $this->Ln(25/$this->k);

        foreach($this->_toc as $t){

            //Offset
            $level=$t['l'];
            if($level>0) $this->Cell($level*8/$this->k);
            
            $weight='';
            if($level==0) $weight='B';

            $str=$t['t'];
            $this->SetFont($tocfont,$weight,$entrySize);
            $strsize=$this->GetStringWidth($str);

            $this->Cell($strsize+(2/$this->k),$this->FontSize+(2/$this->k),$str);

            //Filling dots
            $this->SetFont($tocfont,'',$entrySize);
            $PageCellSize=$this->GetStringWidth($t['p'])+2/$this->k;
            $w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8/$this->k)-($strsize+2/$this->k);
            $nb=$w/$this->GetStringWidth('.');
            $dots=str_repeat('.',$nb);
            $this->Cell($w,$this->FontSize+(2/$this->k),$dots,0,0,'R');
    
            //Page number
            $this->Cell($PageCellSize,$this->FontSize+(2/$this->k),$t['p'],0,1,'R');
            
        }
        
        //grab it and move to selected location
        $n=$this->page;
        $n_toc = $n - $tocstart + 1;
        $last = array();
        
        //store toc pages
        for($i = $tocstart;$i <= $n;$i++)$last[]=$this->pages[$i];
        
        //move pages
        for($i=$tocstart - 1;$i>=$location-1;$i--)
          $this->pages[$i+$n_toc]=$this->pages[$i];
          
        //Put toc pages at insert point
        for($i = 0;$i < $n_toc;$i++)
        $this->pages[$location + $i]=$last[$i];
    } //}}}
    
    function Footer(){
        //this is always one page behind, hence the extra variable set at end for next time;
        if($this->_numbering){
            //Go to 1.5 cm from bottom
            $this->SetY(-15/$this->k);
            //Select Arial italic 8
            $this->SetFont('Arial','I',8);
            $this->Cell(0,7/$this->k,$this->numPageNo(),0,0,'C'); 
            $this->SetFont('Arial','',4);
            $this->SetY(-25/$this->k);
            $this->Cell(0,5/$this->k,$this->orgname,0,0,'L');
            $this->Line($this->rMargin, $this->h - 27/$this->k, $this->w - $this->rMargin, $this->h - 27/$this->k);
        }
        $this->_numbering = $this->_numberingFooter;
    }
} //}}}


function getPage($id,$loctype){ //{{{
    global $con, $pdf, $org;
    
    $idsql = mysql_real_escape_string($id);
    $loctypesql = mysql_real_escape_string($loctype);
    $orgsql = mysql_real_escape_string($org);
    $sql = "SELECT `id`, `desc` FROM `pages` WHERE locationid='".$idsql."' AND what='".$loctypesql."' AND orgid='".$orgsql."'";
    $result_pages = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
    $numPages = mysql_num_rows($result_pages);
    if($numPages>0){
        if($id==LAST_PAGE) $pdf->stopPageNums();
        $pid=mysql_result($result_pages, 0, 'id');
        getPage($pid, "Page");
        $filename = "./pages/pdf/org".$org."/page".$pid.".pdf";
        $pagecount = $pdf->setSourceFile($filename);
        for($p=0; $p<$pagecount; $p++){
            $pdf->AddPage();
            if($pdf->PageNo() == $pdf->indexPage-1) $pdf->startPageNums();
            $tplidx = $pdf->ImportPage($p+1);
            $pdf->useTemplate($tplidx);
        }
        if($loctype=='Team') $pdf->AddPage();
    }
}
//}}}

function getTeam($id){  //{{{
    global $con, $pdf, $width, $lvl;
    
    //$lvl++;
    $tab = '';
    $idsql = mysql_real_escape_string($id);
    $sql = "SELECT id, name, rank FROM `team` WHERE team.owner='".$idsql."' ORDER BY rank";
    $result_teams = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
    $numTeams = mysql_num_rows($result_teams);
    
    if($numTeams>0){
        if($pdf->PageNo() == $pdf->indexPage-1) $pdf->startPageNums();
        for($t=0;$t<$numTeams; $t++){
            getPage(mysql_result($result_teams, $t, 'id'), 'Team');
            

            $sql = "SELECT members.last, members.first, members.address, members.city, "
            ."members.state, members.zip, members.phone, titles.title FROM members "
            ."INNER JOIN teamref ON members.id = teamref.memberid "
            ."INNER JOIN titles ON teamref.titleid = titles.id "
            ."WHERE teamref.teamid='".mysql_result($result_teams, $t, 'team.id')."' ORDER BY titles.rank";
            $result_members = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
            $numMembers = mysql_num_rows($result_members);
            if($numMembers>0){
                teamHeader($t, $result_teams, $numMembers, $pdf);
                for($m=0;$m<$numMembers; $m++){
                    teamMember($m, $result_members, $pdf);
                }
            }
        }
    }
    //$lvl--;
} //}}}

function getGroup($id){  //{{{
    global $con, $pdf, $width, $lvl;
    
    $lvl++;
    getTeam($id);
    
    $idsql = mysql_real_escape_string($id);
    $sql = "SELECT id, orgid, owner, name, break, rank FROM `group` WHERE group.owner='".$idsql."' ORDER BY rank";
    $result_group = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
    $numRows = mysql_num_rows($result_group);
    
    if($numRows>0){
        if($pdf->PageNo() == $pdf->indexPage-1) $pdf->startPageNums();
        for($g=0;$g<$numRows;$g++){
            getPage(mysql_result($result_group, $g, 'id'), 'Group');
            
            if(mysql_result($result_group, $g, 'break')==1)
                $pdf->AddPage();
            groupHeader($g, $result_group, $pdf);

            getGroup(mysql_result($result_group, $g, 'id')); //that's me I'm calling
        }
    } 
    
    $lvl--;
}//getGroup() }}}

function units($u){  //{{{
    switch($u){
        case 'point':
            return 'pt';
        case 'millimeter':
            return 'mm';
        case 'centimeter':
            return 'cm';
        case 'inch':
            return 'in';
    }
}
//}}}

// *** Code starts here *** {{{
session_start();
ini_set('display_errors', 'Off');
if($_SESSION['logon']!='logon'){
    include('includes/logon.php');
}else{
    $org = $_SESSION['orgid'];
    $orgsql = mysql_real_escape_string($org);
    $root = $_SESSION['rootid'];
    $booklet=false;
    if(isset($_GET['booklet'])) $booklet=true;

    $sql = "SELECT * FROM `config` WHERE orgid='".$orgsql."'";
    $result_config = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
    
    include('./languages/'.strtolower(mysql_result($result_config, 0, 'language')).'.inc');
    include('./templates/'.strtolower(mysql_result($result_config, 0, 'template')).'.php');
    
    $width = mysql_result($result_config, 0, 'width'); 
    $height = mysql_result($result_config, 0, 'height'); 
    $units = units(mysql_result($result_config, 0, 'units'));
    $indexPage = mysql_result($result_config, 0, 'tocpage');
    $teambreak = mysql_result($result_config, 0, 'teambreak');
    
    $size = array($width, $height);
    $pdf= new PDF('p', $units, $size); 
    $pdf->SetFont('Arial','B',12);
    $pdf->SetMargins(mysql_result($result_config, 0, 'leftmargin')
                     , mysql_result($result_config, 0, 'topmargin')
                     , mysql_result($result_config, 0, 'rightmargin'));
    $dm = 'real';
    $pdf->setDisplayMode($dm);
    $pdf->indexPage=$indexPage;

    $sql = "SELECT name FROM `org` WHERE id='".$orgsql."'";
    $result_org = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
    $pdf->orgname=mysql_result($result_org, 0, 'name');

    include('./activate.php');
    
    $lvl = -1;
    getGroup($root); //start at root, recursive loop
    
    $pdf->stopPageNums();

    // insert index
    $sql = "SELECT * FROM `members` WHERE active='1' AND orgid='".$orgsql."' order by last";
    $result = mysql_query($sql, $con) or die("Invalid query: " . mysql_error());
    $num = mysql_num_rows($result);

    $pdf->AddPage();
    $pdf->TOC_Entry($MSG_LANG['index'],0);
    $pdf->Cell(0,24/$pdf->k,$MSG_LANG['index'],0,1,'C');
    $pdf->SetFont('Times','',10);
    for($m=0;$m<$num; $m++){
        $pdf->Write(12/$pdf->k,mysql_result($result, $m, 'last').", "
        .mysql_result($result, $m, 'first')
        );
        $phone=mysql_result($result, $m, 'phone');
        $strsize=$pdf->GetStringWidth($phone);
        $w=$pdf->w-$pdf->rMargin-$pdf->GetX()-($strsize+2/$pdf->k);
        $nb=$w/$pdf->GetStringWidth('.');
        $dot=str_repeat('.',$nb);

        $pdf->Cell($PageCellSize,$pdf->FontSize +2/$pdf->k,$dot.$phone,0,1,'R');
    }
    getPage(LAST_PAGE, 'Group');
    
    //Insert TOC
    $pdf->insertTOC(20,10,'Times',$MSG_LANG['toc']);
    $gutter = mysql_result($result_config, 0, 'gutter');

    $filename = "./temp/doc.pdf";
    if(($gutter > 0) || $booklet){
        $pdf->Output($filename);
    }else{
        $pdf->Output();
    }
    
    if($booklet) include('./booklet.php'); else if($gutter > 0)include('./gutter.php');
    
    // below is for security, to not leave member data available in a file
    $pdf = null; // have to do or file won't delete
    if(file_exists($filename)) 
        unlink($filename);
    
} //}}}
?>
Return current item: PDFdirectory