MYPDF::ColoredTable PHP Method

ColoredTable() public method

Print the table containing the detailed information.
public ColoredTable ( array $header, array $data )
$header array String with the column headers.
$data array Data to print.
    public function ColoredTable($header, $data)
    {
        global $kga;
        $dateWidth = max($this->GetStringWidth($header[0]), $this->GetStringWidth($this->dateformat(mktime(0, 0, 0, 12, 31, 2000))));
        $dateWidth += 4;
        $w = array($dateWidth, $this->getPageWidth() - $this->pagedim[$this->page]['lm'] - $this->pagedim[$this->page]['rm'] - $dateWidth, 0, 0);
        if (isset($this->columns['wage'])) {
            $w[3] = 30;
            $w[1] -= 30;
        }
        if (isset($_REQUEST['time_type'])) {
            $w[2] = 30;
            $w[1] -= 30;
        }
        // Header
        $this->printHeader($w, $header);
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
        // Data
        $fill = 0;
        $moneySum = 0;
        if ($_REQUEST['time_type'] == "dec_time") {
            $timeSum = 0;
        } else {
            $timeSum = "0:00";
        }
        foreach ($data as $row) {
            $show_comment = !empty($row['comment']) && isset($_REQUEST['print_comments']);
            // check if page break is nessessary
            if ($this->getPageHeight() - $this->pagedim[$this->page]['bm'] - ($this->getY() + 20 + ($show_comment ? 6 : 0)) < 0) {
                $this->Cell(array_sum($w), 0, '', 'T');
                if (isset($this->columns['wage']) || isset($this->columns['dec_time']) || isset($this->columns['time'])) {
                    $this->Ln();
                    $this->Cell($w[0] + $w[1], 6, $kga['lang']['export_extension']['subtotal'] . ':', '', 0, 'R', false);
                    if ($_REQUEST['time_type'] == "dec_time") {
                        if (isset($this->columns['dec_time'])) {
                            $this->Cell($w[2], 6, $this->timespan($timeSum), isset($this->columns['wage']) ? 'R' : '', 0, 'R', true);
                        }
                    } else {
                        if (isset($this->columns['time'])) {
                            $this->Cell($w[2], 6, $this->time_unit($timeSum), isset($this->columns['wage']) ? 'R' : '', 0, 'R', true);
                        }
                    }
                    if (isset($this->columns['wage'])) {
                        $this->Cell($w[3], 6, $this->money($moneySum), 'L', 0, 'R', true);
                    }
                }
                $this->Ln();
                $this->AddPage();
                $this->printHeader($w, $header);
                // Color and font restoration
                $this->SetFillColor(224, 235, 255);
                $this->SetTextColor(0);
                $this->SetFont('');
            }
            $this->Cell($w[0], 6, $this->dateformat($row['time_in']), 'LR', 0, 'C', $fill);
            if (isset($this->columns['trackingNumber'])) {
                $trackingnumber = " (#" . $row['trackingNumber'] . ") - ";
            } else {
                $trackingnumber = "";
            }
            $this->Cell($w[1], 6, $trackingnumber . $row['customerName'] . ' - ' . $row['activityName'], 'LR', 0, 'L', $fill);
            if ($_REQUEST['time_type'] == "dec_time") {
                if (isset($this->columns['dec_time'])) {
                    $this->Cell($w[2], 6, $this->timespan(isset($row['decimalDuration']) ? $row['decimalDuration'] : 0), 'LR', 0, 'R', $fill);
                }
            } else {
                if (isset($this->columns['time'])) {
                    $this->Cell($w[2], 6, $this->time_unit(isset($row['formattedDuration']) ? $row['formattedDuration'] : 0), 'LR', 0, 'R', $fill);
                }
            }
            if (isset($this->columns['wage'])) {
                $this->Cell($w[3], 6, $this->money($row['wage']), 'LR', 0, 'R', $fill);
            }
            $this->Ln();
            //Kommentar anzeigen:
            if ($show_comment) {
                // comment line width
                $comment_line_width = 58;
                // split comment in lines
                $comment_lines = explode("\n", wordwrap(stripslashes($row['comment']), $comment_line_width, "\n", true));
                // loop through all comment lines an add a cell for each line
                if (is_array($comment_lines)) {
                    // determine font sizes to work with
                    $current_font_size = $this->getFontSizePt();
                    if ($current_font_size <= 0) {
                        $current_font_size = 12;
                    }
                    $comment_font_size = $current_font_size - 2;
                    foreach ($comment_lines as $comment_line) {
                        $this->Cell($w[0], 6, '', 'L', 0, 'C', $fill);
                        $this->SetFont('', 'I', $comment_font_size);
                        //$this->Cell($w[1], 6, $kga['lang']['comment'].': '.nl2br(Kimai_Format::addEllipsis($row['comment'],40)), 'LR', 0, 'L', $fill);
                        $this->Cell($w[1], 6, $comment_line, 'LR', 0, 'L', $fill);
                        $this->SetFont('', '', $current_font_size);
                        if ($_REQUEST['time_type'] == "dec_time") {
                            if (isset($this->columns['dec_time'])) {
                                $this->Cell($w[2], 6, '', 'LR', 0, 'R', $fill);
                            }
                        } else {
                            if (isset($this->columns['time'])) {
                                $this->Cell($w[2], 6, '', 'LR', 0, 'R', $fill);
                            }
                        }
                        if (isset($this->columns['wage'])) {
                            $this->Cell($w[3], 6, '', 'LR', 0, 'R', $fill);
                        }
                        $this->Ln();
                    }
                }
            }
            $fill = !$fill;
            $moneySum += $row['wage'];
            if ($_REQUEST['time_type'] == "dec_time") {
                $timeSum += $row['decimalDuration'] == -1 ? 0 : $row['decimalDuration'];
            } else {
                $timeSum = $this->SumStdTime($row['formattedDuration'] == -1 ? 0 : $row['formattedDuration'], $timeSum);
            }
        }
        $this->Cell(array_sum($w), 0, '', 'T');
        $this->Ln();
        if (isset($this->columns['wage']) || isset($this->columns['dec_time'])) {
            $this->Cell($w[0] + $w[1], 6, $kga['lang']['export_extension']['finalamount'] . ':', '', 0, 'R', false);
            $this->SetFont('', 'B');
            if ($_REQUEST['time_type'] == "dec_time") {
                if (isset($this->columns['dec_time'])) {
                    $this->Cell($w[2], 6, $this->timespan($timeSum), isset($this->columns['wage']) ? 'R' : '', 0, 'R', true);
                }
            } else {
                if (isset($this->columns['time'])) {
                    $this->Cell($w[2], 6, $this->time_unit($timeSum), isset($this->columns['wage']) ? 'R' : '', 0, 'R', true);
                }
            }
            if (isset($this->columns['wage'])) {
                $this->Cell($w[3], 6, $this->money($moneySum), 'L', 0, 'R', true);
            }
        }
    }

Usage Example

Example #1
0
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 12);
// add a page
$pdf->AddPage();
//Column titles
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
//Data loading
$data = $pdf->LoadData('../cache/table_data_demo.txt');
// print colored table
$pdf->ColoredTable($header, $data);
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_011.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
All Usage Examples Of MYPDF::ColoredTable