<?php
/**
 * phpaga
 *
 * pdf output functionality.
 *
 * This file contains the necessary classes and routines to create pdf documents.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: pdf.php 653 2005-10-03 21:53:26Z worf $
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

define("PDFCLASS_FONTDIR", PDFCLASS_DIR."fonts/");
include_once(PDFCLASS_DIR."class.ezpdf.php");

if (strtolower(PHPAGA_PDF_DOCFORMAT) == 'letter')
{
    /* letter */
    define("PHPAGA_PDF_DOCWIDTH",      612);
    define("PHPAGA_PDF_DOCHEIGHT",     792);
}
else
{
    /* a4 */
    define("PHPAGA_PDF_DOCWIDTH",   595);
    define("PHPAGA_PDF_DOCHEIGHT",  842);
}

define("PHPAGA_PDF_FONT_SIZE_BILL",  PHPAGA_PDF_FONT_SIZE_NORMAL);
define("PHPAGA_PDF_BILL_NEXTLINE",   PHPAGA_PDF_FONT_SIZE_BILL * 1.5);
define("PHPAGA_PDF_HEADER_NEXTLINE", PHPAGA_PDF_FONT_SIZE_SMALL * 1.5);

define("PHPAGA_PDF_FONT_SIZE_QUOTATION",  PHPAGA_PDF_FONT_SIZE_NORMAL);
define("PHPAGA_PDF_QUOTATION_NEXTLINE",   PHPAGA_PDF_FONT_SIZE_BILL * 1.5);

define("PHPAGA_PDF_OPLINE_HEIGHT", PHPAGA_PDF_FONT_SIZE_SMALL * 1.25);
define("PHPAGA_PDF_OPLINE_HEIGHT_NEXTOP", PHPAGA_PDF_FONT_SIZE_SMALL * 1.5);

define("PHPAGA_PDF_BORDER_RIGHT", PHPAGA_PDF_BORDER_LEFT);
define("PHPAGA_PDF_START_RIGHT",
       PHPAGA_PDF_DOCWIDTH - PHPAGA_PDF_BORDER_RIGHT);

define("PHPAGA_PDF_BORDER_BOTTOM", PHPAGA_PDF_BORDER_TOP);

define("PHPAGA_PDF_SEPARATOR_TOP_HEIGHT",
       PHPAGA_PDF_DOCHEIGHT - 120);

define("PHPAGA_PDF_SEPARATOR_BOTTOM_HEIGHT", PHPAGA_PDF_BORDER_BOTTOM);

define("PHPAGA_PDF_POS_Y_HEADER_EMAIL",
       PHPAGA_PDF_SEPARATOR_TOP_HEIGHT
       + PHPAGA_PDF_FONT_SIZE_NORMAL * 2.5);

define("PHPAGA_PDF_POS_Y_HEADER_NAME",
       PHPAGA_PDF_POS_Y_HEADER_EMAIL
       + PHPAGA_PDF_FONT_SIZE_HEADER * 2);

define("PHPAGA_PDF_POS_Y_LOGO",
       PHPAGA_PDF_DOCHEIGHT
       - PHPAGA_PDF_BORDER_TOP);

define("PHPAGA_PDF_LOGO_MAXWIDTH", PHPAGA_PDF_DOCWIDTH * 0.25);
define("PHPAGA_PDF_POS_Y_RECIPIENT", PHPAGA_PDF_DOCHEIGHT - 180);
define("PHPAGA_PDF_POS_X_RECIPIENT", PHPAGA_PDF_DOCWIDTH - 250);

define("PHPAGA_PDF_POS_Y_HEADER_RLINE5",
       PHPAGA_PDF_SEPARATOR_TOP_HEIGHT
       + PHPAGA_PDF_FONT_SIZE_NORMAL * 2);

define("PHPAGA_PDF_POS_Y_HEADER_RLINE4",
       PHPAGA_PDF_POS_Y_HEADER_RLINE5
       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);

define("PHPAGA_PDF_POS_Y_HEADER_RLINE3",
       PHPAGA_PDF_POS_Y_HEADER_RLINE4
       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);

define("PHPAGA_PDF_POS_Y_HEADER_RLINE2",
       PHPAGA_PDF_POS_Y_HEADER_RLINE3
       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);

define("PHPAGA_PDF_POS_Y_HEADER_RLINE1",
       PHPAGA_PDF_POS_Y_HEADER_RLINE2
       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);

define("PHPAGA_PDF_POS_Y_FOOTER",
       PHPAGA_PDF_SEPARATOR_BOTTOM_HEIGHT
       - PHPAGA_PDF_FONT_SIZE_SMALL - 1);


/**
 * This class allows the creation and management of pdf-documents.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

class phpaga_pdf_document
{
    var $handle;
    var $creator;
    var $author;
    var $title;
    var $subject;
    var $filename;
    var $tpl_letterhead;
    var $tpl_oplist;

    /**
     * Constructor
     *
     * Creates a new pdf document and opens the corresponding file.
     *
     * @param string   $filename   Name of the pdf document
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function phpaga_pdf_document($filename = null)
    {
        if (!isset($filename) || !strlen($filename))
            $this->filename = "";
        else
            $this->filename = $filename;

        $this->handle =& new Cezpdf();
        $this->handle->selectFont(PDFCLASS_FONTDIR."Helvetica.afm");

    }

    /**
     * Sets the file name.
     *
     * @param string   $filename  File name
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.3
     */

    function set_filename($filename)
    {
        if (strlen(trim($filename)))
            $this->filename = $filename;
    }

    /**
     * Sets the document information.
     *
     * @param string   $title     document title
     * @param string   $subject   subject
     * @param string   $creator   creator
     * @param string   $author    author
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function set_info($title = " ",
                      $subject = " ",
                      $creator = " ",
                      $author = " ")
    {
        $this->handle->addInfo("Title", $title);
        $this->handle->addInfo("Subject", $subject);
        $this->handle->addInfo("Creator", $creator);
        $this->handle->addInfo("Author", $author);
    }

    /**
     * Begin a new page
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function next_page()
    {
        $this->handle->ezNewPage();
    }

    /**
     * Add some basic page layout with the company information.
     *
     * @param  string $logo_filename   File name of the logo
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function add_letterhead($logo_filename)
    {
        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/letterhead.tpl.php");
        $this->handle->execTemplate($tid, array("logo_filename" => $logo_filename));
    }

    /**
     * Spit the pdf document out.
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */
    function spitout()
    {
        $this->handle->ezStream(array("Content-Disposition" =>
                                      $this->filename));
    }

    /**
     * Add recipient information to the page.
     *
     * @param array   $cpnInfo_recipient array containing information
     *                                   about the recipient
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function add_recipient($cpnInfo_recipient)
    {
        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/recipient.tpl.php");
        $this->handle->execTemplate($tid, array("recipient" => $cpnInfo_recipient));
    }

    /**
     * Add invoice information to the page.
     *
     * @param array   $billInfo  array containing invoice information
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function add_invoice($billInfo, $options = null)
    {

        if (!isset($billInfo) || !is_array($billInfo))
            return PHPAGA_ERR_INVALID_PARAMTYPE;

        if ($options == null)
            $options = array("recipient" => null,
                             "letterhead" => false);

        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/invoice.tpl.php");
        $this->handle->execTemplate($tid, array("type" => PHPAGA_TYPE_INVOICE,
                                                "info" => $billInfo,
                                                "recipient" => $options["recipient"],
                                                "letterhead" => $options["letterhead"],
                                                "logo_filename" => PHPAGA_LETTERHEAD_LOGO_FILE));
    }

    /**
     * Add quotation information to the page.
     *
     * @param array   $quotInfo           array containing quotation information
     * @param array   $cpnInfo_recipient  array containing information about who receives
     *                                          the quotation
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function add_quotation($quotInfo, $options = null)
    {

        if (!isset($quotInfo) || !is_array($quotInfo))
            return PHPAGA_ERR_INVALID_PARAMTYPE;

         if ($options == null)
             $options = array("recipient" => null,
                             "letterhead" => false);

        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/invoice.tpl.php");
        $this->handle->execTemplate($tid, array("type" => PHPAGA_TYPE_QUOTATION,
                                                "info" => $quotInfo,
                                                "recipient" => $options["recipient"],
                                                "letterhead" => $options["letterhead"],
                                                "logo_filename" => PHPAGA_LETTERHEAD_LOGO_FILE));
}

    /**
     * Add task list to the page.
     *
     * @param array $rows           array containing operations
     * @param array $search_params  search parameters to be displayed on
     *                              the document (optional)
     * @param array $show_fields    fields to be shown in the list
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.1
     */

    function add_oplist($rows, $search_params = "", $show_fields = "", $title = null, $insert_page = true)
    {

        if ($title == null)
           $title = _("Tasks");

        if (!is_array($show_fields))
            $show_fields = array("date" => _("Date"),
                                 "duration" => _("Duration"),
                                 "project" => _("Project"),
                                 "person" => _("Person"),
                                 "task" => _("Task"),
                                 "deadline" => _("Deadline"));

        $h_date = "";
        $show_operations = array();

        /* cycle through the operations and pick the fields we want
         * to print
         */

        foreach ($rows as $op)
        {

            $h_date = $op["op_startdate"]." ".$op["op_starthours"]." - ";

            if ($op["op_startdate"] != $op["op_enddate"])
                $h_date .= "\n".$op["op_enddate"]." ";

            $h_date .= $op["op_endhours"];

            $show_operations[] = array("date" => $h_date,
                                       "duration" => $op["op_duration_hrs"],
                                       "project" => $op["prj_title"],
                                       "person" => $op["op_person"],
                                       "task" => $op["op_desc"],
                                       "status" => $op["prjstat_title"],
                                       "deadline" => $op["prj_deadline"]
                );
        }

        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/operations.tpl.php");
        $this->handle->execTemplate($tid, array("show_operations" => $show_operations,
                                                "show_fields" => $show_fields,
                                                "search_params" => $search_params,
                                                "title" => $title,
                                                "insert_page" => $insert_page));
    }

    /**
     * Add project list to the page.
     *
     * @param array $rows           array containing operations
     * @param array $search_params  search parameters to be displayed on
     *                              the document (optional)
     * @param array $show_fields    fields to be shown in the list
     *
     * @return void
     *
     * @author Mark Parssey <markpa@users.sourceforge.net>
     * @since phpaga 0.3
     */

    function add_prjlist($rows, $search_params = "", $show_fields = "")
    {

        if (!is_array($show_fields))
            $show_fields = array("date" => _("Date"),
                                 "title" => _("Project"),
                                 "company" => _("Company"),
                                 "desc" => _("Description"),
                                 "status" => _("Status"),
                                 "deadline" => _("Deadline"));

        $h_date = "";
        $show_projects = array();

        /* cycle through the projests and pick the fields we want
         * to print
         */

        foreach ($rows as $prj)
        {

            $h_date = $prj["prj_startdate"];

            $show_projects[] = array("date" => $h_date,
                                     "title" => $prj["prj_title"],
                                     "company" => $prj["cpn_name"],
                                     "desc" => $prj["prj_desc"],
                                     "status" => $prj["prjstat_title"],
                                     "deadline" => $prj["prj_deadline"]
                );
        }

        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/projects.tpl.php");
        $this->handle->execTemplate($tid, array("show_projects" => $show_projects,
                                                "show_fields" => $show_fields,
                                                "search_params" => $search_params));
    }

    /**
     * Add yearly report.
     *
     * @param int  $year   Year (defaults to current year)
     *
     * @return void
     *
     * @author Florian Lanthaler <florian@phpaga.net>
     * @since phpaga 0.3
     */

    function add_report_yearly($params)
    {
        if ($params["year"] == null)
            $params["year"] = date("Y");

        if (!isset($params["year"]) || !is_numeric($params["year"]))
            return PHPAGA_ERR_INVALID_PARAMTYPE;

        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/report_yearly.tpl.php");
        $this->handle->execTemplate($tid, $params);
    }

}

?>