From 8dfb61e332f32db22c8c516b6e40eca157680234 Mon Sep 17 00:00:00 2001 From: anderson.peterle@previdencia.gov.br Date: Thu, 13 Nov 2008 16:57:10 +0000 Subject: [PATCH] Revisao do modulo para exportacao de relatorios nos formatos PDF, ODS, CSV e HTML. --- include/Relatorio.php | 26 ++++++++++++++++++++++++++ include/RelatorioHTML.php | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------- include/RelatorioPDF.php | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------- include/fpdf.php | 2 +- relatorios/patrimonio/rel_patrimonio.php | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------- 5 files changed, 255 insertions(+), 235 deletions(-) diff --git a/include/Relatorio.php b/include/Relatorio.php index 9945e17..40c38f7 100755 --- a/include/Relatorio.php +++ b/include/Relatorio.php @@ -4,6 +4,8 @@ abstract class Relatorio private $m_titulo; private $m_header; private $m_body = array(); + private $m_rowAttr = array(); + private $m_cellColor = array(); public function setTitulo($titulo) { @@ -15,6 +17,30 @@ abstract class Relatorio return $this->m_titulo; } + public function setRowColor($row, $r, $g, $b) + { + $this->m_rowAttr[$row] = array($r, $g, $b); + } + + public function getRowColor($row) + { + return $this->m_rowAttr[$row]; + } + + public function setCellColor($row, $col, $rgb) + { + $this->m_cellColor[$row][$col] = $rgb; + } + + public function getCellColor($row, $col) + { + if (!isset($this->m_cellColor[$row][$col])) + { + return FALSE; + } + return $this->m_cellColor[$row][$col]; + } + public function setTableHeader(array $header) { $this->m_header = $header; diff --git a/include/RelatorioHTML.php b/include/RelatorioHTML.php index 4de3486..2f0ca0f 100755 --- a/include/RelatorioHTML.php +++ b/include/RelatorioHTML.php @@ -3,52 +3,71 @@ require_once('Relatorio.php'); class RelatorioHTML extends Relatorio { + public function rgb2html($r, $g=-1, $b=-1) + { + if (is_array($r) && sizeof($r) == 3) + list($r, $g, $b) = $r; + + $r = intval($r); $g = intval($g); + $b = intval($b); + + $r = dechex($r<0?0:($r>255?255:$r)); + $g = dechex($g<0?0:($g>255?255:$g)); + $b = dechex($b<0?0:($b>255?255:$b)); + + $color = (strlen($r) < 2?'0':'').$r; + $color .= (strlen($g) < 2?'0':'').$g; + $color .= (strlen($b) < 2?'0':'').$b; + return '#'.$color; + } + public function output() { $cor = FALSE; $titulo = $this->getTitulo(); ?> - - + + - $titulo"; - ?> - - - - - - - - - - - + ?> + + + + + +
+ + + + + - - - - - - + + + + + + + + + + - - -

Gerado - em

Gerado + em

-
-

- Exportar: PDF | ODS | CSV +
+
+ Exportar: PDF | ODS | CSV

'; //Data + $i = 0; foreach ($this->getBody() as $row) { - echo 'getRowColor($i++); + if (!$attr) { - echo 'bgcolor="#E1E1E1"'; - } - echo '>'; + if ($cor) + { + $attr = 'bgcolor="#E1E1E1"'; + } + } + else + { + $attr = 'bgcolor="'.$this->rgb2html($attr[0], $attr[1], $attr[2]).'"'; + } + echo ""; foreach ($row as $cell) { @@ -84,11 +111,11 @@ class RelatorioHTML extends Relatorio ?>

-

Relatório - gerado pelo CACIC - Configurador Automático e Coletor - de Informações Computacionais
- Software desenvolvido pela Dataprev - Unidade Regional Espírito Santo

- +

Relatório + gerado pelo CACIC - Configurador Automático e Coletor + de Informações Computacionais
+ Software desenvolvido pela Dataprev - Unidade Regional Espírito Santo

+ widths=$w; - $fullwidth = 0; - foreach ($w as $wh) - { - $fullwidth += $wh; - } - #$this->hPt=$this->fwPt; - #$this->w = $fullwidth; + $this->widths=$w; + $fullwidth = 0; + foreach ($w as $wh) + { + $fullwidth += $wh; + } $this->fhPt = ($fullwidth + $this->lMargin + $this->rMargin) * $this->k; } @@ -33,12 +30,11 @@ class PDFMTable extends FPDF $this->aligns=$a; } - function Row($data) + function Row($data, $fillcolor, $fill, $cellColors) { + $this->SetFillColor($fillcolor[0], $fillcolor[1], $fillcolor[2]); //Calculate the height of the row $nb=0; - #for($i=0;$iNbLines($this->widths[$i],$data[$i])); $i = 0; foreach ($data as $value) { @@ -48,18 +44,17 @@ class PDFMTable extends FPDF $h=5*$nb; //Issue a page break first if needed $this->CheckPageBreak($h); - //Draw the cells of the row - #for($i=0;$iSetTextColor($cellColors[$i][0], $cellColors[$i][1], $cellColors[$i][2]); $w=$this->widths[$i]; $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L'; //Save the current position $x=$this->GetX(); $y=$this->GetY(); - if ($this->fill) + if ($fill) { $style = 'DF'; } @@ -70,12 +65,11 @@ class PDFMTable extends FPDF //Draw the border $this->Rect($x,$y,$w,$h, $style); //Print the text - $this->MultiCell($w,5,$value,0,$a/*, $this->fill*/); + $this->MultiCell($w,5,$value,0,$a); //Put the position to the right of the cell $this->SetXY($x+$w,$y); $i++; } - $this->fill = !$this->fill; //Go to the next line $this->Ln($h); } @@ -145,9 +139,9 @@ class RelatorioPDF extends Relatorio private function getMaxTextWidth($fpdf) { $maxWidths = array(); - $mstr = array(); - $arrays = array(array($this->getHeader()), $this->getBody()); - foreach ($arrays as $array) + $mstr = array(); + $arrays = array(array($this->getHeader()), $this->getBody()); + foreach ($arrays as $array) { foreach ($array as $row) { @@ -186,11 +180,30 @@ class RelatorioPDF extends Relatorio } $i++; } - } + } } return $maxWidths; } + public function html2rgb($color) + { + if ($color[0] == '#') + $color = substr($color, 1); + + if (strlen($color) == 6) + list($r, $g, $b) = array($color[0].$color[1], + $color[2].$color[3], + $color[4].$color[5]); + elseif (strlen($color) == 3) + list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]); + else + return false; + + $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); + + return array($r, $g, $b); + } + public function output() { $pdf = new PDFMTable(); @@ -203,8 +216,8 @@ class RelatorioPDF extends Relatorio $pdf->SetLineWidth(.3); $pdf->SetFont('Arial'); //Header - $w = $this->getMaxTextWidth($pdf); - $fullwidth = array_sum($w); + $w = $this->getMaxTextWidth($pdf); + $fullwidth = array_sum($w); $pdf->AddPage('L'); $pdf->SetWidths($w); @@ -222,7 +235,9 @@ class RelatorioPDF extends Relatorio $pdf->Ln(); //Color and font restoration $pdf->SetFillColor(224, 235, 255); - $pdf->SetTextColor(0); + $defaultFillColor = array(224, 235, 255); + $defaultTextColor = 0; + $pdf->SetTextColor($defaultTextColor); $pdf->SetFont('Arial'); //Data $fill = 0; @@ -231,31 +246,26 @@ class RelatorioPDF extends Relatorio $size = count($body); for ($i = 0; $i < $size; $i++) { - #echo 'ROW: '.$i.'
'; $row = &$body[$i]; - //unset($body[$i]); - //$px = $pdf->GetX(); - //$py = $pdf->GetY(); - //for ($i = 0; $i < count($row); $i++) - //{ - // $pn = $pdf->PageNo(); - // $pdf->MultiCell($w[$i], 6, iconv('utf-8' ,'iso-8859-1', /*'X: '.$px.*/'Y: '.$py/*$row[$i]*/), 1, 'L', $fill); - // $px += $w[$i]; - // if ($pdf->PageNo() > $pn) - // { - // $py = 0; - // } - // $pdf->SetXY($px, $py); - //} + $fillcolor = $defaultFillColor; + $color = $this->getRowColor($i); + if ($color) + { + $fillcolor = $color; + $fill = 1; + } $count = count($row); + $j = 0; + $cellColors = array(); foreach ($row as $key => $value) { + $cellColors[$j] = $this->getCellColor($i, $j); $row[$key] = $this->relstrip($value);// iconv('utf-8' ,'iso-8859-1', 'AAA'/*strip_tags($row[$j])*/); + $j++; } - $pdf->Row($row); + $pdf->Row($row, $fillcolor, $fill, $cellColors); $fill = !$fill; } - //$pdf->Cell(array_sum($w), 0, '', 'T'); $pdf->Output('relatorio.pdf', 'D'); } diff --git a/include/fpdf.php b/include/fpdf.php index a4f11b3..42e8bf6 100755 --- a/include/fpdf.php +++ b/include/fpdf.php @@ -1036,7 +1036,7 @@ function Output($name='',$dest='') else header('Content-Type: application/octet-stream'); if(headers_sent()) - $this->Error('Saome data has already been output to browser, can\'t send PDF file'); + $this->Error('Some data has already been output to browser, can\'t send PDF file'); header('Content-Length: '.strlen($this->buffer)); header('Content-disposition: attachment; filename="'.$name.'"'); echo $this->buffer; diff --git a/relatorios/patrimonio/rel_patrimonio.php b/relatorios/patrimonio/rel_patrimonio.php index 7163fda..d1ad98c 100755 --- a/relatorios/patrimonio/rel_patrimonio.php +++ b/relatorios/patrimonio/rel_patrimonio.php @@ -19,73 +19,34 @@ session_start(); */ if(!isset($_SESSION['id_usuario'])) die('Acesso negado (Access denied)!'); -else { // Inserir regras para outras verificações (ex: permissões do usuário)! -} +else + { // Inserir regras para outras verificações (ex: permissões do usuário)! + } +require_once('../../include/library.php'); if($_POST['submit']) { - $_SESSION["list2"] = $_POST['list2']; - $_SESSION["list4"] = $_POST['list4']; - $_SESSION["list6"] = $_POST['list6']; - $_SESSION["list6o"] = $_POST['list6']; - $_SESSION["list8"] = $_POST['list8']; - $_SESSION["list12"] = $_POST['list12']; - $_SESSION["cs_situacao"] = $_POST["cs_situacao"]; - $_SESSION['orderby'] = ''; - $_SESSION['post'] = $_POST; -} + $_SESSION["list2"] = $_POST['list2']; + $_SESSION["list4"] = $_POST['list4']; + $_SESSION["list6"] = $_POST['list6']; + $_SESSION["list6o"] = $_POST['list6']; + $_SESSION["list8"] = $_POST['list8']; + $_SESSION["list12"] = $_POST['list12']; + $_SESSION["cs_situacao"] = $_POST["cs_situacao"]; + $_SESSION['orderby'] = ''; + $_SESSION['post'] = $_POST; + } else -{ - $_SESSION["list6"] = $_SESSION['list6o']; -} - -?> - - -Relatório de informações de Patrimônio e Localização Física - - - - - - - - - - - - - - - - - - - - -
  
CACIC - - Relatório de informações de Patrimônio e Localização - Física

Gerado - em

-
-1 && $_SESSION['cs_nivel_administracao']<>2) { - //if($_SESSION["cs_situacao"] == 'S') //{ // Aqui pego todas as redes selecionadas e faço uma query p/ condição de redes @@ -348,19 +309,32 @@ else $relatorio->setTitulo('CACIC - Relatório de informações de Patrimônio e Localização Física'); + // String com nomes dos campos que não devem ser mostrados, concatenando-os com # para fins de busca em substring. + $strNaoMostrarCamposNomes = '#dt_hr_alteracao#'; + + // String para indices das colunas que não serão mostradas, concatenando-os com # para fins de busca em substring. + $strNaoMostrarCamposIndices = ''; + $in_destacar_duplicidade_tmp = ''; + $header = array('#'); + + //Table Header for ($i=4; $i < mysql_num_fields($result); $i++) - { //Table Header - $header[] = ''. mysql_field_name($result, $i) .''; - if ($in_destacar_duplicidade_total && in_array(mysql_field_name($result, $i), $arr_in_destacar_duplicidade_total)) { - if ($in_destacar_duplicidade_tmp) $in_destacar_duplicidade_tmp .= '#'; - $in_destacar_duplicidade_tmp .= $i; + $boolNaoMostrar = stripos2($strNaoMostrarCamposNomes,'#'.mysql_field_name($result, $i).'#',false); + if (!$boolNaoMostrar) + { + $header[] = ''. mysql_field_name($result, $i) .''; + if ($in_destacar_duplicidade_total && in_array(mysql_field_name($result, $i), $arr_in_destacar_duplicidade_total)) + { + if ($in_destacar_duplicidade_tmp) $in_destacar_duplicidade_tmp .= '#'; + $in_destacar_duplicidade_tmp .= $i; + } + } + else + $strNaoMostrarCamposIndices .= '#'.$i.'#'; } - } -else - { $relatorio->setTableHeader($header); @@ -368,30 +342,23 @@ else @mysql_data_seek($result,0); $table = array(); while ($row = mysql_fetch_row($result)) - {//pre Table body + {//pre Table body // ja existe entrada para este MAC? if (isset($table[$row[0]])) - { + { // acesso mais rencente? if ($row[2] > $table[$row[0]][2]) - { $table[$row[0]] = $row; - } + + // desempatar pela entrada de patrimonio mais recente if ($row[2] == $table[$row[0]][2]) - { - // desempatar pela entrada de patrimonio mais recente if ($row[3] > $table[$row[0]][3]) - { $table[$row[0]] = $row; - } } - } else - { $table[$row[0]] = $row; } - } if ($in_destacar_duplicidade_tmp) { @@ -399,99 +366,89 @@ else $v_arr_campos_valores = array(); $num_registro = 1; foreach ($table as $row) - { - for ($i = 5; $i < $fields; $i++) { + for ($i = 5; $i < $fields; $i++) if (trim($row[$i])<>'' && in_array($i,$arr_in_destacar_duplicidade)) - { array_push($v_arr_campos_valores,$i . ',' . trim(strtolower($row[$i]))); - } } - $num_registro ++; } - $v_arr_total_campos_valores = array(); - $v_arr_total_campos_valores = array_count_values($v_arr_campos_valores); + $v_arr_total_campos_valores = array(); + $v_arr_total_campos_valores = array_count_values($v_arr_campos_valores); - $num_registro = 1; - $registros_valores_duplicados = array(); - foreach ($table as $row) + $num_registro = 1; + $registros_valores_duplicados = array(); + + foreach ($table as $row) { - for ($i = 5; $i < $fields; $i++) + for ($i = 5; $i < $fields; $i++) { - if (trim($row[$i]) != '' && in_array($i, $arr_in_destacar_duplicidade)) + if (trim($row[$i]) != '' && in_array($i, $arr_in_destacar_duplicidade)) { - $v_chave = $i . ',' . trim($row[$i]); - // se o valor aparece mais de 1 vez - if ($v_arr_total_campos_valores[strtolower($v_chave)] > 1) + $v_chave = $i . ',' . trim($row[$i]); + // se o valor aparece mais de 1 vez + if ($v_arr_total_campos_valores[strtolower($v_chave)] > 1) { - if (!isset($registros_valores_duplicados[$num_registro])) - { - $registros_valores_duplicados[$num_registro] = array(); - } - $registros_valores_duplicados[$num_registro][] = $i; + if (!isset($registros_valores_duplicados[$num_registro])) + $registros_valores_duplicados[$num_registro] = array(); + + $registros_valores_duplicados[$num_registro][] = $i; } } } - $num_registro++; + $num_registro++; } - } $cor = FALSE; $num_registro = 1; foreach ($table as $key => $row) - { //Table body - $possuiCampoDuplicado = isset($registros_valores_duplicados[$num_registro]); - if ($possuiCampoDuplicado) - { - $relatorio->setRowColor($num_registro - 1, 0xFF, 0xFF, 0x99); //'bgcolor="#FFFF99"'); - } - + { //Table body $c1 = '' . $num_registro . ''; $c2 = "" . $row[4] .""; - unset($row[0]); unset($row[1]); unset($row[2]); unset($row[3]); unset($row[4]); - for ($i=5; $i < $fields; $i++) - { - $cell = 'setCellColor($num_registro - 1, $i - 3, array(0xFF, 0, 0)); - } - } - - $cell .= '>'; - if ($campoDuplicado) - { - $cell .= ''; - } - $cell .= $row[$i]; - - if ($campoDuplicado) - { - $cell .= ''; - } - - $row[$i] = $cell; - } - + + for ($i=4; $i < $fields; $i++) + { + $campoDuplicado = FALSE; + $boolNaoMostrar = stripos2($strNaoMostrarCamposIndices,'#'.$i.'#',false); + if (!$boolNaoMostrar) + { + $possuiCampoDuplicado = isset($registros_valores_duplicados[$num_registro]); + if ($possuiCampoDuplicado) + $relatorio->setRowColor($num_registro - 1, 0xFF, 0xFF, 0x99); //'bgcolor="#FFFF99"'); + + $cell = 'setCellColor($num_registro - 1, $i - 3, array(0xFF, 0, 0)); + } + + $cell .= '>'; + if ($campoDuplicado) + $cell .= ''; + + $cell .= $row[$i]; + if ($campoDuplicado) + $cell .= ''; + $row[$i] = $cell; + $cell = ''; + } + else + unset($row[$i]); + } array_unshift($row, $c1, $c2); $relatorio->addRow($row); $cor = !$cor; - $num_registro++; - - - } + $num_registro++; + } $relatorio->output(); -} -?> + } + ?> \ No newline at end of file -- libgit2 0.21.2