public function deploy() { $this->checkExportRights(); $this->setRecordsPerPage(0); parent::deploy(); $this->_width = 0; $colors = array('title' => '#000000', 'subtitle' => '#111111', 'footer' => '#111111', 'header' => '#AAAAAA', 'row1' => '#EEEEEE', 'row2' => '#FFFFFF', 'sqlexp' => '#BBBBBB', 'lines' => '#111111', 'hrow' => '#E4E4F6', 'text' => '#000000', 'filters' => '#F9EDD2', 'filtersBox' => '#DEDEDE'); $this->_deploy['colors'] = array_merge($colors, (array) $this->_deploy['colors']); if (!isset($this->_deploy['save'])) { $this->_deploy['save'] = false; } if (!isset($this->_deploy['download'])) { $this->_deploy['download'] = false; } if ($this->_deploy['save'] != 1 && $this->_deploy['download'] != 1) { throw new Exception('Nothing to do. Please specify download&&|save options'); } if (empty($this->_deploy['name'])) { $this->_deploy['name'] = date('H_m_d_H_i_s'); } if (substr($this->_deploy['name'], -4) == '.xls') { $this->_deploy['name'] = substr($this->_deploy['name'], 0, -4); } if (!isset($this->_deploy['noPagination'])) { $this->_deploy['noPagination'] = 0; } $this->_deploy['dir'] = rtrim($this->_deploy['dir'], '/') . '/'; if (!isset($this->_deploy['dir']) || !is_dir($this->_deploy['dir'])) { throw new Bvb_Grid_Exception($this->_deploy['dir'] . ' is not a dir'); } if (!is_writable($this->_deploy['dir'])) { throw new Bvb_Grid_Exception($this->_deploy['dir'] . ' is not writable'); } $this->_la = 0; $titles = parent::_buildTitles(); $grid = parent::_BuildGrid(); $sqlExp = parent::_buildSqlExp(); $larg = $this->calculateCellSize($titles, $sqlExp, $grid); $lengthTotal = array_sum($larg['larg']); $this->_cellFontSize = $this->getDeployOption('cellFontSize', 8); if (!$this->getInfo('hRow,field')) { $this->_info['hRow']['field'] = ''; } if (strtoupper($this->_deploy['orientation']) == 'LANDSCAPE' && strtoupper($this->_deploy['size']) == 'A4') { $this->_totalPages = ceil(count($grid) / 26); } elseif (strtoupper($this->_deploy['orientation']) == 'LANDSCAPE' && strtoupper($this->_deploy['size']) == 'LETTER') { $this->_totalPages = ceil(count($grid) / 27); } else { $this->_totalPages = ceil(count($grid) / 37); } if ($this->_totalPages < 1) { $this->_totalPages = 1; } $this->_pdf = new Zend_Pdf(); $this->_styles['style'] = new Zend_Pdf_Style(); $this->_styles['style']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['lines'])); $this->_styles['topo'] = new Zend_Pdf_Style(); $this->_styles['topo']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['header'])); $this->_styles['td'] = new Zend_Pdf_Style(); $this->_styles['td']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['row2'])); $this->_styles['styleFilters'] = new Zend_Pdf_Style(); $this->_styles['styleFilters']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['filters'])); $this->_styles['styleFiltersBox'] = new Zend_Pdf_Style(); $this->_styles['styleFiltersBox']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['filtersBox'])); $this->_styles['td2'] = new Zend_Pdf_Style(); $this->_styles['td2']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['row1'])); $this->_styles['hRowStyle'] = new Zend_Pdf_Style(); $this->_styles['hRowStyle']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['hrow'])); $this->_styles['styleSql'] = new Zend_Pdf_Style(); $this->_styles['styleSql']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['sqlexp'])); $this->_styles['styleText'] = new Zend_Pdf_Style(); $this->_styles['styleText']->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['text'])); $this->buildPageStructure($titles, true); $pageWidth = $this->_page->getWidth() - 80; $i = 0; foreach ($larg['larg'] as $final) { $this->_cell[$i] = ceil($final * $pageWidth / $lengthTotal); $i++; } $needed = 0; $fix = $larg['fix']; $larg = $larg['larg']; $perc = array(); $i = 0; foreach ($this->_cell as $key => $value) { $perc[$key] = $value - $fix[$key]; $i++; } $perc = array(); foreach ($this->_cell as $key => $value) { if ($value + 2 < $fix[$key]) { $needed = ceil($fix[$key] - $value); $this->_cell[$key] = $fix[$key]; } if ($value > $fix[$key] + 2 + $needed) { $this->_cell[$key] = $this->_cell[$key] - $needed; $needed = 0; $perc[$key] = $this->_cell[$key] - $fix[$key]; } } if (array_sum($this->_cell) > $pageWidth) { $totalToRemove = array_sum($this->_cell) - $pageWidth; foreach ($perc as $key => $value) { $this->_cell[$key] = $this->_cell[$key] - round($totalToRemove * $value / array_sum($perc)); } } $cellsCount = count($titles); if ($this->getInfo('hRow,title') != '') { $cellsCount--; } $largura = ($this->_page->getWidth() - 80) / $cellsCount; $this->_height = $this->_page->getHeight() - 120; $this->buildPageStructure($titles); $this->_page->setFont($this->_font, $this->_cellFontSize); $this->_page->setStyle($this->_styles['style']); if (is_array($grid)) { if ($this->getInfo('hRow,title') != '') { $bar = $grid; $hbar = trim($this->getInfo('hRow,field')); $p = 0; foreach ($grid[0] as $value) { if ($value['field'] == $hbar) { $hRowIndex = $p; } $p++; } $aa = 0; } $ia = 0; $aa = 0; foreach ($grid as $value) { if ($this->_height <= 80) { $this->buildPageStructure($titles); } $this->_la = 0; $this->_height = $this->_height - 16; $i = 0; $tdf = $ia % 2 ? $this->_styles['td'] : $this->_styles['td2']; $a = 1; if ($this->getInfo('hRow,title') != '') { if ($bar[$aa][$hRowIndex]['value'] != @$bar[$aa - 1][$hRowIndex]['value']) { $centrar = $this->_page->getWidth() - 80; $centrar = round($centrar / 2) + 30; if ((int) $this->_la == 0) { $largura1 = 40; } else { $largura1 = $this->_cell[$i - 1] + $largura1; } $this->_page->setStyle($this->_styles['hRowStyle']); $this->_page->drawRectangle($largura1, $this->_height - 8, $this->_page->getWidth() - 39, $this->_height + 16); $this->_page->setStyle($this->_styles['styleText']); $this->_page->drawText($bar[$aa][$hRowIndex]['value'], $centrar, $this->_height + 2, $this->getCharEncoding()); $this->_la = 0; $this->_height = $this->_height - 16; } } $nl = 0; $tLines = $this->calculateNumerOfLinesForRecord($value); $cellPos = 0; foreach ($value as $value1) { $value1['value'] = strip_tags(trim($value1['value'])); if ($value1['field'] != $this->getInfo('hRow,field') && $this->getInfo('hRow,title') != '' || $this->getInfo('hRow,title') == '') { if ((int) $this->_la == 0) { $largura1 = 40; } else { $largura1 = $this->_cell[$i - 1] + $largura1; } $this->_page->setStyle($tdf); $this->_page->drawRectangle($largura1, $this->_height + 8, $largura1 + $this->_cell[$i] + 1, $this->_height - 8); $this->_page->setStyle($this->_styles['styleText']); $textToShow = $this->getArrayForPdfRecord($value1['value'], $cellPos, $tLines); $heightSupport = 0; for ($ti = 0; $ti < $tLines; $ti++) { if (!isset($textToShow[$ti])) { continue; } if (count($textToShow) == 1) { $extraHeight = round($tLines / count($textToShow)) + 2; } else { $extraHeight = 0; } $this->_page->drawText($textToShow[$ti], $largura1 + 2, $this->_height - $extraHeight - $heightSupport * 8, $this->getCharEncoding()); $heightSupport++; } $this->_la = $largura1; $i++; $nl++; $cellPos++; } $a++; } $aa++; $ia++; } } $this->buildSqlPdf($sqlExp); $this->buildShowFiltersInExport(); $this->_pdf->save($this->_deploy['dir'] . $this->_deploy['name'] . '.pdf'); if ($this->_deploy['download'] == 1) { header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $this->_deploy['name'] . '.pdf"'); readfile($this->_deploy['dir'] . $this->_deploy['name'] . '.pdf'); } if ($this->_deploy['save'] != 1) { unlink($this->_deploy['dir'] . $this->_deploy['name'] . '.pdf'); } die; }
public function deploy() { $this->checkExportRights(); $this->setRecordsPerPage(0); parent::deploy(); $width = 0; $colors = array('title' => '#000000', 'subtitle' => '#111111', 'footer' => '#111111', 'header' => '#AAAAAA', 'row1' => '#EEEEEE', 'row2' => '#FFFFFF', 'sqlexp' => '#BBBBBB', 'lines' => '#111111', 'hrow' => '#E4E4F6', 'text' => '#000000', 'filters' => '#F9EDD2', 'filtersBox' => '#DEDEDE'); $this->_deploy['colors'] = array_merge($colors, (array) $this->_deploy['colors']); $la = ''; if (!isset($this->_deploy['save'])) { $this->_deploy['save'] = false; } if (!isset($this->_deploy['download'])) { $this->_deploy['download'] = false; } if ($this->_deploy['save'] != 1 && $this->_deploy['download'] != 1) { throw new Exception('Nothing to do. Please specify download&&|save options'); } if (empty($this->_deploy['name'])) { $this->_deploy['name'] = date('H_m_d_H_i_s'); } if (substr($this->_deploy['name'], -4) == '.xls') { $this->_deploy['name'] = substr($this->_deploy['name'], 0, -4); } if (!isset($this->_deploy['noPagination'])) { $this->_deploy['noPagination'] = 0; } $this->_deploy['dir'] = rtrim($this->_deploy['dir'], '/') . '/'; if (!isset($this->_deploy['dir']) || !is_dir($this->_deploy['dir'])) { throw new Bvb_Grid_Exception($this->_deploy['dir'] . ' is not a dir'); } if (!is_writable($this->_deploy['dir'])) { throw new Bvb_Grid_Exception($this->_deploy['dir'] . ' is not writable'); } $larg = self::calculateCellSize(); $lengthTotal = array_sum($larg); $cellFontSize = 8; //set font $titulos = parent::_buildTitles(); $sql = parent::_buildSqlExp(); $grid = parent::_BuildGrid(); if (!$this->getInfo('hRow,field')) { $this->_info['hRow']['field'] = ''; } if (strtoupper($this->_deploy['orientation']) == 'LANDSCAPE' && strtoupper($this->_deploy['size']) == 'A4') { $totalPaginas = ceil(count($grid) / 26); } elseif (strtoupper($this->_deploy['orientation']) == 'LANDSCAPE' && strtoupper($this->_deploy['size']) == 'LETTER') { $totalPaginas = ceil(count($grid) / 27); } else { $totalPaginas = ceil(count($grid) / 37); } if ($totalPaginas < 1) { $totalPaginas = 1; } $pdf = new Zend_Pdf(); // Create new Style $style = new Zend_Pdf_Style(); $style->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['lines'])); $topo = new Zend_Pdf_Style(); $topo->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['header'])); $td = new Zend_Pdf_Style(); $td->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['row2'])); $styleFilters = new Zend_Pdf_Style(); $styleFilters->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['filters'])); $styleFiltersBox = new Zend_Pdf_Style(); $styleFiltersBox->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['filtersBox'])); $td2 = new Zend_Pdf_Style(); $td2->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['row1'])); $hRowStyle = new Zend_Pdf_Style(); $hRowStyle->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['hrow'])); $styleSql = new Zend_Pdf_Style(); $styleSql->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['sqlexp'])); $styleText = new Zend_Pdf_Style(); $styleText->setFillColor(new Zend_Pdf_Color_Html($this->_deploy['colors']['text'])); // Add new page to the document if (strtoupper($this->_deploy['size'] = 'LETTER') && strtoupper($this->_deploy['orientation']) == 'LANDSCAPE') { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE); } elseif (strtoupper($this->_deploy['size'] = 'LETTER') && strtoupper($this->_deploy['orientation']) != 'LANDSCAPE') { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER); } elseif (strtoupper($this->_deploy['size'] != 'A4') && strtoupper($this->_deploy['orientation']) == 'LANDSCAPE') { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE); } else { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); } $page->setStyle($style); $pdf->pages[] = $page; $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); $page->setFont($font, 14); //$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); if (file_exists($this->_deploy['logo'])) { $image = Zend_Pdf_Image::imageWithPath($this->_deploy['logo']); list($width, $height, $type, $attr) = getimagesize($this->_deploy['logo']); $page->drawImage($image, 40, $page->getHeight() - $height - 40, 40 + $width, $page->getHeight() - 40); } $page->drawText($this->__($this->_deploy['title']), $width + 70, $page->getHeight() - 70, $this->getCharEncoding()); $page->setFont($font, $cellFontSize); $page->drawText($this->__($this->_deploy['subtitle']), $width + 70, $page->getHeight() - 80, $this->getCharEncoding()); //Iniciar a contagem de páginas $pagina = 1; $page->drawText($this->_deploy['footer'], 40, 40, $this->getCharEncoding()); if (@$this->_deploy['noPagination'] != 1) { $page->drawText($this->__($this->_deploy['page']) . ' ' . $pagina . '/' . $totalPaginas, $page->getWidth() - strlen($this->__($this->_deploy['page'])) * $cellFontSize - 50, 40, $this->getCharEncoding()); } $page->setFont($font, $cellFontSize); $pl = $page->getWidth() - 80; $i = 0; foreach ($larg as $final) { $cell[$i] = round($final * $pl / $lengthTotal); $i++; } $cellsCount = count($titulos); if ($this->getInfo('hRow,title') != '') { $cellsCount--; } $largura = ($page->getWidth() - 80) / $cellsCount; $altura = $page->getHeight() - 120; $i = 0; $page->setFont($font, $cellFontSize + 1); foreach ($titulos as $value) { if ($value['field'] != $this->getInfo('hRow,field') && $this->getInfo('hRow,title') != '' || $this->getInfo('hRow,title') == '') { if ((int) $la == 0) { $largura1 = 40; } else { $largura1 = $cell[$i - 1] + $largura1; } $page->setStyle($topo); $page->drawRectangle($largura1, $altura - 4, $largura1 + $cell[$i] + 1, $altura + 12); $page->setStyle($styleText); $page->drawText($value['value'], $largura1 + 2, $altura, $this->getCharEncoding()); $la = $largura1; $i++; } } $page->setFont($font, $cellFontSize); $page->setStyle($style); if (is_array($grid)) { if ($this->getInfo('hRow,title') != '') { $bar = $grid; $hbar = trim($this->getInfo('hRow,field')); $p = 0; foreach ($grid[0] as $value) { if ($value['field'] == $hbar) { $hRowIndex = $p; } $p++; } $aa = 0; } $ia = 0; $aa = 0; foreach ($grid as $value) { if ($altura <= 80) { // Add new page to the document if (strtoupper($this->_deploy['size'] = 'LETTER') && strtoupper($this->_deploy['orientation']) == 'LANDSCAPE') { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE); } elseif (strtoupper($this->_deploy['size'] = 'LETTER') && strtoupper($this->_deploy['orientation']) != 'LANDSCAPE') { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER); } elseif (strtoupper($this->_deploy['size'] != 'A4') && strtoupper($this->_deploy['orientation']) == 'LANDSCAPE') { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE); } else { $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); } $page->setStyle($style); $pdf->pages[] = $page; $pagina++; $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); $page->setFont($font, 14); //$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); if (file_exists($this->_deploy['logo'])) { $image = Zend_Pdf_Image::imageWithPath($this->_deploy['logo']); list($width, $height, $type, $attr) = getimagesize($this->_deploy['logo']); $page->drawImage($image, 40, $page->getHeight() - $height - 40, 40 + $width, $page->getHeight() - 40); } $page->drawText($this->__($this->_deploy['title']), $width + 70, $page->getHeight() - 70, $this->getCharEncoding()); $page->setFont($font, $cellFontSize); $page->drawText($this->__($this->_deploy['subtitle']), $width + 70, $page->getHeight() - 80, $this->getCharEncoding()); //set font $altura = $page->getHeight() - 120; $page->drawText($this->__($this->_deploy['footer']), 40, 40, $this->getCharEncoding()); if ($this->_deploy['noPagination'] != 1) { $page->drawText($this->__($this->_deploy['page']) . ' ' . $pagina . '/' . $totalPaginas, $page->getWidth() - strlen($this->__($this->_deploy['page'])) * $cellFontSize - 50, 40, $this->getCharEncoding()); } // reset($titulos); $i = 0; $largura1 = 40; $page->setFont($font, $cellFontSize + 1); foreach ($titulos as $title) { if ($title['field'] != $this->getInfo('hRow,field') && $this->getInfo('hRow,title') != '' || $this->getInfo('hRow,title') == '') { if ((int) $la == 0) { $largura1 = 40; } else { $largura1 = $cell[$i - 1] + $largura1; } $page->setStyle($topo); $page->drawRectangle($largura1, $altura - 4, $largura1 + $cell[$i] + 1, $altura + 12); $page->setStyle($style); $page->drawText($title['value'], $largura1 + 2, $altura, $this->getCharEncoding()); $la = $largura1; $i++; } } $page->setFont($font, $cellFontSize); } $la = 0; $altura = $altura - 16; $i = 0; $tdf = $ia % 2 ? $td : $td2; $a = 1; //A linha horizontal if ($this->getInfo('hRow,title') != '') { if ($bar[$aa][$hRowIndex]['value'] != $bar[$aa - 1][$hRowIndex]['value']) { $centrar = $page->getWidth() - 80; $centrar = round($centrar / 2) + 30; if ((int) $la == 0) { $largura1 = 40; } else { $largura1 = $cell[$i - 1] + $largura1; } $page->setStyle($hRowStyle); $page->drawRectangle($largura1, $altura - 4, $page->getWidth() - 40, $altura + 12); $page->setStyle($styleText); $page->drawText($bar[$aa][$hRowIndex]['value'], $centrar, $altura, $this->getCharEncoding()); $la = 0; $altura = $altura - 16; } } //////////// //Vamos saber qauntas linhas tem este registo $nlines = array(); $nl = 0; foreach ($value as $lines) { $line = $this->widthForStringUsingFontSize(strip_tags(trim($lines['value'])), $font, 8); $nlines[] = ceil($line / $cell[$nl]); $nl++; } sort($nlines); $totalLines = end($nlines); $nl = 0; foreach ($value as $value1) { $value1['value'] = strip_tags(trim($value1['value'])); if ($value1['field'] != $this->getInfo('hRow,field') && $this->getInfo('hRow,title') != '' || $this->getInfo('hRow,title') == '') { if ((int) $la == 0) { $largura1 = 40; } else { $largura1 = $cell[$i - 1] + $largura1; } $page->setStyle($tdf); $page->drawRectangle($largura1, $altura - 4, $largura1 + $cell[$i] + 1, $altura + 12); $page->setStyle($styleText); $page->drawText($value1['value'], $largura1 + 2, $altura, $this->getCharEncoding()); $la = $largura1; $i++; $nl++; } $a++; } $aa++; $ia++; } } $la = 0; $altura = $altura - 16; $i = 0; if (is_array($sql)) { foreach ($sql as $value) { if ((int) $la == 0) { $largura1 = 40; } else { $largura1 = $cell[$i - 1] + $largura1; } $page->setStyle($styleSql); $page->drawRectangle($largura1, $altura - 4, $largura1 + $cell[$i], $altura + 12); $page->setStyle($styleText); $page->drawText($value['value'], $largura1 + 2, $altura, $this->getCharEncoding()); $la = $largura1; $la = $largura1; $i++; } } $la = 0; $altura = $altura - 16; $i = 0; if (is_array($this->_showFiltersInExport) || $this->_showFiltersInExport == true) { if (is_array($this->_showFiltersInExport) && is_array($this->_filtersValues)) { $this->_showFiltersInExport = array_merge($this->_showFiltersInExport, $this->_filtersValues); } elseif (is_array($this->_showFiltersInExport)) { $this->_showFiltersInExport = $this->_showFiltersInExport; } elseif (is_array($this->_filtersValues)) { $this->_showFiltersInExport = $this->_filtersValues; } if (count($this->_showFiltersInExport) > 0) { $page->setStyle($styleFilters); $page->drawRectangle(40, $altura - 4, array_sum($cell) + 41, $altura + 12); $page->setStyle($styleText); $tLarg = $this->widthForStringUsingFontSize($this->__('Filtered by:'), $font); $i = 0; $page->setStyle($styleFiltersBox); $page->drawRectangle(40, $altura - 4, $tLarg + 50, $altura + 12); $page->setStyle($styleText); $text = ' ' . $this->__('Filtered by:') . ' '; foreach ($this->_showFiltersInExport as $key => $value) { if ($keyHelper = $this->getField($key)) { $key = $keyHelper['title']; } if (is_array($value)) { foreach ($value as $newName => $newValue) { $text .= $this->__($key) . ' - ' . $this->__(ucfirst($newName)) . ': ' . $this->__($newValue) . ' | '; } } else { $text .= $this->__($key) . ': ' . $this->__($value) . ' | '; } $i++; } $page->drawText($text, $tLarg + 3, $altura, $this->getCharEncoding()); } } $pdf->save($this->_deploy['dir'] . $this->_deploy['name'] . '.pdf'); if ($this->_deploy['download'] == 1) { header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $this->_deploy['name'] . '.pdf"'); readfile($this->_deploy['dir'] . $this->_deploy['name'] . '.pdf'); } if ($this->_deploy['save'] != 1) { unlink($this->_deploy['dir'] . $this->_deploy['name'] . '.pdf'); } die; }