Add pdf export functions

refs #4356
This commit is contained in:
Matthias Jentsch 2014-01-29 18:09:26 +01:00
parent 1a6d1a20fb
commit 80488644d4
3 changed files with 149 additions and 1 deletions

View File

@ -0,0 +1,103 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* 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 the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
class Zend_Controller_Action_Helper_DataFormatSwitch extends Zend_Controller_Action_Helper_ContextSwitch {
protected $autoSerialization = true;
public function setAutoJsonSerialization($value)
{
$this->autoSerialization = $value;
$this->setAutoJsonSerialization($value);
}
public function __construct()
{
$this->setContexts(
array(
'pdf' => array(
'suffix' => 'pdf',
'headers' => array('Content-Type' => 'application/pdf'),
'callbacks' => array(
'init' => 'removeStyles',
'post' => 'postPdfContext'
)
),
'json' => array(
'suffix' => 'json',
'headers' => array('Content-Type' => 'application/json'),
'callbacks' => array(
'init' => 'removeStyles',
'post' => 'postJsonContext'
)
),
'xml' => array(
'suffix' => 'xml',
'headers' => array('Content-Type' => 'application/xml'),
'callbacks' => array(
'init' => 'removeStyles',
'post' => 'postXmlContext'
)
)
)
);
}
private function postXmlContext()
{
if (!$this->autoSerialization) {
return;
}
}
private function postPdfContext()
{
if (!$this->autoSerialization) {
return;
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$helper = new Zend_View_Helper_Pdf();
$this->getResponse()->setBody(
$helper->pdf($viewRenderer->render())
);
}
private function removeStyles()
{
if (!$this->getAutoJsonSerialization()) {
return;
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$view = $viewRenderer->view;
if ($view instanceof Zend_View_Interface) {
$viewRenderer->setNoRender(true);
}
}
}

View File

@ -40,6 +40,9 @@ use Icinga\Util\Translator;
use Icinga\Web\Widget\Tabs;
use Icinga\Web\Url;
use Icinga\File\Pdf;
use \DOMDocument;
/**
* Base class for all core action controllers
*
@ -71,6 +74,7 @@ class ActionController extends Zend_Controller_Action
->setResponse($response)
->_setInvokeArgs($invokeArgs);
$this->_helper = new Zend_Controller_Action_HelperBroker($this);
$this->_helper->addPath('../application/controllers/helpers');
// when noInit is set (e.g. for testing), authentication and init is skipped
if (isset($invokeArgs['noInit'])) {
@ -234,6 +238,38 @@ class ActionController extends Zend_Controller_Action
$this->_helper->layout()->benchmark = $this->renderBenchmark();
}
}
if ($this->_request->getParam('format') === 'pdf' && $this->_request->getControllerName() !== 'static') {
$html = $this->view->render(
$this->_request->getControllerName() . '/' . $this->_request->getActionName() . '.phtml'
);
$this->sendAsPdf($html);
}
}
protected function sendAsPdf($body)
{
$css = $this->view->getHelper('action')->action('stylesheet', 'static', 'application');
$css = <<<EOF
EOF;
$html = '<html><head></head><body><style>' . $css . '</style>' . $body . '</body></html>';
$pdf = new PDF();
$pdf->AddPage();
$pdf->writeHTML($html);
$pdf->Output($this->getRequest()->getActionName() . '.pdf', 'I');
}
/**
* @param DOMDocument $doc
* @param $tag
*/
private function removeNodeByTagName(DOMDocument $doc, $tag)
{
$forms = $doc->getElementsByTagName($tag);
foreach ($forms as $form) {
$form->parentNode->removeChild($form);;
}
}
/**

View File

@ -74,6 +74,16 @@ class Monitoring_ListController extends MonitoringController
$this->view->grapher = Hook::get('grapher');
$this->createTabs();
$this->view->activeRowHref = $this->getParam('detail');
/*
$contextSwitch = $this->_helper->getHelper('DataFormatSwitch');
$contextSwitch = $this->_helper->getHelper('ContextSwitch');
$contextSwitch->addActionContext(true, 'json');
$contextSwitch->setAutoJsonSerialization(true);
$contextSwitch->initContext();
$contextSwitch->addActionContext(true, 'xml');
$contextSwitch->addActionContext(true, 'pdf');
*/
}
/**
@ -135,7 +145,6 @@ class Monitoring_ListController extends MonitoringController
));
$this->handleFormatRequest($query);
$this->view->hosts = $query->paginate();
}
/**