diff --git a/application/controllers/helpers/DataFormatSwitch.php b/application/controllers/helpers/DataFormatSwitch.php new file mode 100644 index 000000000..a9a2b4549 --- /dev/null +++ b/application/controllers/helpers/DataFormatSwitch.php @@ -0,0 +1,103 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{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); + } + } +} \ No newline at end of file diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index 10a534910..e08be3778 100755 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -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 = <<' . $body . ''; + $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);; + } } /** diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 88408165a..fdf2ba134 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -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(); - } /**