Commit 3dc68d99 authored by Côme Bernigaud's avatar Côme Bernigaud Committed by Benoit Mortier
Browse files

Fixes: #2595 The code should follow code guidelines

parent b0e03f17
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011 FusionDirectory
Copyright (C) 2011-2013 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -21,16 +20,16 @@
*/
/*!
* \file class_PDF.inc
* \file class_PDF.php
* Source code for class PDF
*/
/*!
* \brief This class contains all the functions to manage pdf
*/
/* Avoid autoloading this class */ class PDF extends FPDF
class PDF extends FPDF
{
var $headline= "";
var $headline = "";
/*!
* \brief Set the headline
......@@ -39,7 +38,7 @@
*/
function setHeadline($headline)
{
$this->headline= $headline;
$this->headline = $headline;
}
/*!
......@@ -47,7 +46,7 @@
*/
function Header()
{
$this->SetFont('Helvetica','B',10);
$this->SetFont('Helvetica', 'B', 10);
$this->Cell(0, 0, $this->headline, 0, 0, 'L');
$this->Ln(5);
}
......@@ -58,10 +57,9 @@
function Footer()
{
$this->SetY(-15);
$this->SetFont('Helvetica','I',8);
$this->Cell(0,10, _("Page")." ".$this->PageNo().'/{nb}',0,0,'C');
$this->SetFont('Helvetica', 'I', 8);
$this->Cell(0, 10, _("Page")." ".$this->PageNo().'/{nb}', 0, 0, 'C');
}
}
?>
......@@ -43,35 +43,36 @@ class csvExporter
*
* \param array $columns
*/
function csvExporter($headline, $header, $entries, $columns= array()) {
function __construct($headline, $header, $entries, $columns = array())
{
// If no preset, render all columns
if (!count($columns)) {
foreach ($header as $index => $dummy) {
$columns[]= $index;
$columns[] = $index;
}
}
// Generate header
$this->result= "#";
$this->result = "#";
foreach ($columns as $index) {
if (isset($header[$index])){
$this->result.= trim(html_entity_decode($header[$index], ENT_QUOTES, 'UTF-8').";");
if (isset($header[$index])) {
$this->result .= trim(html_entity_decode($header[$index], ENT_QUOTES, 'UTF-8').";");
} else {
$this->result.= ";";
$this->result .= ";";
}
}
$this->result= preg_replace('/;$/', '', $this->result)."\n";
$this->result = preg_replace('/;$/', '', $this->result)."\n";
// Append entries
foreach ($entries as $row) {
foreach ($columns as $index) {
if (isset($row["_sort$index"])){
$this->result.= trim(html_entity_decode($row["_sort$index"], ENT_QUOTES, 'UTF-8')).";";
if (isset($row["_sort$index"])) {
$this->result .= trim(html_entity_decode($row["_sort$index"], ENT_QUOTES, 'UTF-8')).";";
} else {
$this->result.= ";";
$this->result .= ";";
}
}
$this->result= preg_replace('/;$/', '', $this->result)."\n";
$this->result = preg_replace('/;$/', '', $this->result)."\n";
}
}
......@@ -89,7 +90,7 @@ class csvExporter
*/
static function getInfo()
{
return array("exportCVS" => array( "label" => _("CSV"), "image" => "images/lists/csv.png", "class"=> "csvExporter", "mime" => "text/x-csv", "filename" => "export.csv" ));
return array("exportCVS" => array( "label" => _("CSV"), "image" => "images/lists/csv.png", "class" => "csvExporter", "mime" => "text/x-csv", "filename" => "export.csv" ));
}
}
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011 FusionDirectory
Copyright (C) 2011-2013 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -29,8 +28,8 @@
@include_once(FPDF);
// Load supporter class only if FPDF is loaded
$classes= get_declared_classes();
if(in_array('FPDF', $classes)) {
$classes = get_declared_classes();
if (in_array('FPDF', $classes)) {
include('class_PDF.php');
}
......@@ -52,64 +51,63 @@ class pdfExporter
*
* \param array $columns
*/
function pdfExporter($headline, $header, $entries, $columns= array()) {
function __construct($headline, $header, $entries, $columns = array())
{
// Bail out if no FPDF available
if(!class_exists('FPDF')) {
if (!class_exists('FPDF')) {
die(_("No PDF export possible: there is no FPDF library installed."));
}
// If no preset, render all columns
if (!count($columns)) {
foreach ($header as $index => $dummy) {
$columns[]= $index;
}
$columns = array_keys($header);
}
// Create new PDF
$this->result=new PDF('L', 'mm', 'A4');
$this->result = new PDF('L', 'mm', 'A4');
$this->result->AliasNbPages();
$this->result->SetFont('Helvetica', '', 10);
$this->result->setHeadline(utf8_decode($headline));
$this->result->AddPage();
// Analyze for width
$width= $this->calcWidth($header, $entries, $columns);
$width = $this->calcWidth($header, $entries, $columns);
// Render head
$this->result->SetFont('','B');
$this->result->SetFont('', 'B');
$this->result->SetTextColor(0);
$this->result->SetDrawColor(0,0,0);
$this->result->SetDrawColor(0, 0, 0);
$this->result->SetLineWidth(.3);
// Height calculator
$height= 0;
$height = 0;
$fill= false;
$fill = FALSE;
foreach ($entries as $row) {
// Render header
if ($height == 0) {
// Generate header
$this->result->SetFillColor(230,230,230);
$this->result->SetFont('','B');
$this->result->SetFillColor(230, 230, 230);
$this->result->SetFont('', 'B');
foreach ($columns as $order => $index) {
if (isset($header[$index])){
if (isset($header[$index])) {
$this->result->Cell($width[$order], 7, utf8_decode($header[$index]), 1, 0, 'C', 1);
} else {
$this->result->Cell($width[$order], 7, '', 1, 0, 'C', 1);
}
}
$this->result->Ln();
$height= 7;
$height = 7;
// Set entry collors
$this->result->SetFillColor(240,240,240);
$this->result->SetFillColor(240, 240, 240);
$this->result->SetFont('');
}
foreach ($columns as $order => $index) {
if (isset($row["_sort$index"])){
if (isset($row["_sort$index"])) {
$this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
} else {
$this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
......@@ -119,14 +117,14 @@ class pdfExporter
$this->result->Ln();
// Increase height to eventually create new page
$height+= 8;
$height += 8;
if ($height > 220) {
$height= 0;
$height = 0;
$this->result->Cell(array_sum($width), 0, '', 'T');
$this->result->AddPage();
$fill= false;
$fill = FALSE;
} else {
$fill= !$fill;
$fill = !$fill;
}
}
$this->result->Cell(array_sum($width), 0, '', 'T');
......@@ -143,37 +141,38 @@ class pdfExporter
*/
function calcWidth($header, $entries, $columns)
{
$width= array();
$width = array();
// Locate longest value for each column
foreach ($columns as $order => $index) {
$max= 0;
foreach ($columns as $index) {
$max = 0;
if (isset($header[$index])){
$len= $this->result->GetStringWidth($header[$index]);
if (isset($header[$index])) {
$len = $this->result->GetStringWidth($header[$index]);
if ($len > $max) {
$max= $len;
$max = $len;
}
}
foreach ($entries as $row) {
if (isset($row["_sort$index"])){
$len= $this->result->GetStringWidth($row["_sort$index"]);
if (isset($row["_sort$index"])) {
$len = $this->result->GetStringWidth($row["_sort$index"]);
if ($len > $max) {
$max= $len;
$max = $len;
}
}
}
$width[]= $max;
$width[] = $max;
}
// Scale to page width
$printWidth= 280;
$scale= $printWidth / array_sum($width);
foreach ($width as $index => $w) {
$width[$index]= (int)($width[$index] * $scale);
$printWidth = 280;
$scale = $printWidth / array_sum($width);
foreach ($width as &$w) {
$w *= $scale;
}
unset($w);
return $width;
}
......@@ -192,13 +191,12 @@ class pdfExporter
static function getInfo()
{
// Check if class defined
$classes= get_declared_classes();
if(in_array('FPDF', $classes)) {
return array("exportPDF" => array( "label" => _("PDF"), "image" => "images/lists/pdf.png", "class"=> "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
$classes = get_declared_classes();
if (in_array('FPDF', $classes)) {
return array("exportPDF" => array( "label" => _("PDF"), "image" => "images/lists/pdf.png", "class" => "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
} else {
return null;
return NULL;
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment