From 5d113b3833fd0b7336ef375ea7e0066246d24513 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 9 Oct 2018 14:11:48 +0200 Subject: [PATCH] BasketController: allow to download JSON via Web --- application/controllers/BasketController.php | 19 ++++++++++-- .../Automation/BasketSnapshot.php | 31 ++++++++++++++++++- .../Web/Controller/ActionController.php | 3 +- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/application/controllers/BasketController.php b/application/controllers/BasketController.php index 75b26b24..328cc45c 100644 --- a/application/controllers/BasketController.php +++ b/application/controllers/BasketController.php @@ -129,6 +129,12 @@ class BasketController extends ActionController ], $this->db()); $snapSum = bin2hex($snapshot->get('content_checksum')); + if ($this->params->get('action') === 'download') { + $this->getResponse()->setHeader('Content-Type', 'application/json', true); + echo $snapshot->getJsonDump(); + return; + } + $this->addTitle( $this->translate('%s: %s (Snapshot)'), $basket->get('basket_name'), @@ -147,7 +153,16 @@ class BasketController extends ActionController $this->url()->with('action', 'restore'), null, ['class' => 'icon-rewind'] - ) + ), + Link::create( + $this->translate('Download'), + $this->url()->with('action', 'download'), + null, + [ + 'class' => 'icon-download', + 'target' => '_blank' + ] + ), ]); $properties = new NameValueTable(); @@ -251,7 +266,7 @@ class BasketController extends ActionController $type, $key, Link::create( - substr(bin2hex( $snapshot->get('content_checksum')), 0, 7), + substr(bin2hex($snapshot->get('content_checksum')), 0, 7), $snapshotUrl, null, ['data-base-target' => '_next'] diff --git a/library/Director/DirectorObject/Automation/BasketSnapshot.php b/library/Director/DirectorObject/Automation/BasketSnapshot.php index b048b8c6..49e8c15f 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshot.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshot.php @@ -114,6 +114,10 @@ class BasketSnapshot extends DbObject $fields[$id] = DirectorDatafield::loadWithAutoIncId((int) $id, $connection)->export(); } } + + if (empty($this->objects['Datafield'])) { + unset($this->objects['Datafield']); + } } protected function addObjectsChosenByBasket(Basket $basket) @@ -159,7 +163,32 @@ class BasketSnapshot extends DbObject */ public function restoreTo(Db $connection, $replace = true) { - $all = Json::decode($this->getJsonDump()); + static::restoreJson( + $this->getJsonDump(), + $connection, + $replace + ); + } + + public static function restoreJson($string, Db $connection, $replace = true) + { + $snapshot = new static(); + $snapshot->restoreObjects( + Json::decode($string), + $connection, + $replace + ); + } + + /** + * @param $all + * @param Db $connection + * @param bool $replace + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + * @throws \Zend_Db_Adapter_Exception + */ + protected function restoreObjects($all, Db $connection, $replace = true) + { $db = $connection->getDbAdapter(); $db->beginTransaction(); $fieldMap = []; diff --git a/library/Director/Web/Controller/ActionController.php b/library/Director/Web/Controller/ActionController.php index 51644d7b..48a78a04 100644 --- a/library/Director/Web/Controller/ActionController.php +++ b/library/Director/Web/Controller/ActionController.php @@ -183,7 +183,8 @@ abstract class ActionController extends Controller implements ControlsAndContent $viewRenderer = null; } - if ($this->getRequest()->isApiRequest()) { + $cType = $this->getResponse()->getHeader('Content-Type', true); + if ($this->getRequest()->isApiRequest() || ($cType !== null && $cType !== 'text/html')) { $this->_helper->layout()->disableLayout(); if ($viewRenderer) { $viewRenderer->disable();