From 1b6e7177a38ae4d2328b5acd1feec7a9ee19d79a Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 7 Dec 2016 16:43:43 +0100 Subject: [PATCH] Allow to export the host and service detail views to JSON resolves #12820 --- .../Controller/MonitoredObjectController.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php index f4936fd99..9ea05f02b 100644 --- a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php +++ b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php @@ -75,6 +75,7 @@ abstract class MonitoredObjectController extends Controller } } $this->object->populate(); + $this->handleFormatRequest(); $toggleFeaturesForm = new ToggleObjectFeaturesCommandForm(array( 'backend' => $this->backend, 'objects' => $this->object @@ -134,6 +135,26 @@ abstract class MonitoredObjectController extends Controller return $form; } + /** + * Export to JSON if requested + */ + protected function handleFormatRequest($query = null) + { + if ($this->params->get('format') === 'json' + || $this->getRequest()->getHeader('Accept') === 'application/json' + ) { + $payload = (array) $this->object->properties; + $payload += array( + 'contacts' => $this->object->contacts->fetchPairs(), + 'contact_groups' => $this->object->contactgroups->fetchPairs(), + 'vars' => $this->object->customvars + ); + $groupName = $this->object->getType() . 'groups'; + $payload[$groupName] = $this->object->$groupName; + $this->getResponse()->json()->setSuccessData($payload)->sendResponse(); + } + } + /** * Acknowledge a problem */