BasketController: allow to download JSON via Web

This commit is contained in:
Thomas Gelf 2018-10-09 14:11:48 +02:00
parent bea52a3091
commit 5d113b3833
3 changed files with 49 additions and 4 deletions

View File

@ -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']

View File

@ -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 = [];

View File

@ -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();