SearchDashboard: Provide support for the enhanced dashboards

This commit is contained in:
Yonas Habteab 2022-03-11 16:41:06 +01:00
parent 48b54f6025
commit c94284e9b2
2 changed files with 44 additions and 28 deletions

View File

@ -3,23 +3,21 @@
namespace Icinga\Controllers; namespace Icinga\Controllers;
use Icinga\Web\Controller\ActionController;
use Icinga\Web\Widget;
use Icinga\Web\Widget\SearchDashboard; use Icinga\Web\Widget\SearchDashboard;
use ipl\Web\Compat\CompatController;
/** /**
* Search controller * Search controller
*/ */
class SearchController extends ActionController class SearchController extends CompatController
{ {
public function indexAction() public function indexAction()
{ {
$searchDashboard = new SearchDashboard(); $searchDashboard = new SearchDashboard();
$searchDashboard->setUser($this->Auth()->getUser()); $searchDashboard->setUser($this->Auth()->getUser());
$this->view->dashboard = $searchDashboard->search($this->params->get('q'));
// NOTE: This renders the dashboard twice. Remove this once we can catch exceptions thrown in view scripts. $this->controls->setTabs($searchDashboard->getTabs());
$this->view->dashboard->render(); $this->content = $searchDashboard->search($this->getParam('q'));
} }
public function hintAction() public function hintAction()

View File

@ -3,15 +3,23 @@
namespace Icinga\Web\Widget; namespace Icinga\Web\Widget;
use Zend_Controller_Action_Exception; use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Web\Navigation\DashboardHome;
use Icinga\Web\Url; use Icinga\Web\Url;
/** /**
* Class SearchDashboard display multiple search views on a single search page * Class SearchDashboard display multiple search views on a single search page
*/ */
class SearchDashboard extends Dashboard class SearchDashboard extends \Icinga\Web\Dashboard\Dashboard
{ {
/**
* Name for the search home
*
* @var string
*/
const SEARCH_HOME = 'Search Home';
/** /**
* Name for the search pane * Name for the search pane
* *
@ -20,12 +28,23 @@ class SearchDashboard extends Dashboard
const SEARCH_PANE = 'search'; const SEARCH_PANE = 'search';
/** /**
* {@inheritdoc} * Dashboard home of this search dashboard
*
* @var DashboardHome
*/ */
protected $searchHome;
public function __construct()
{
$this->searchHome = new DashboardHome(self::SEARCH_HOME);
$this->searchHome->setActive();
}
public function getTabs() public function getTabs()
{ {
if ($this->tabs === null) { if ($this->tabs === null) {
$this->tabs = new Tabs(); $this->tabs = new \ipl\Web\Widget\Tabs();
$this->tabs->add( $this->tabs->add(
'search', 'search',
array( array(
@ -35,9 +54,15 @@ class SearchDashboard extends Dashboard
) )
); );
} }
return $this->tabs; return $this->tabs;
} }
public function getActiveHome()
{
return $this->searchHome;
}
/** /**
* Load all available search dashlets from modules * Load all available search dashlets from modules
* *
@ -47,18 +72,18 @@ class SearchDashboard extends Dashboard
*/ */
public function search($searchString = '') public function search($searchString = '')
{ {
$pane = $this->createPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search')); $pane = $this->searchHome->addPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search'));
$this->activate(self::SEARCH_PANE); $this->activate(self::SEARCH_PANE);
$manager = Icinga::app()->getModuleManager(); $manager = Icinga::app()->getModuleManager();
$searchUrls = array(); $searchUrls = array();
foreach ($manager->getLoadedModules() as $module) { foreach ($manager->getLoadedModules() as $module) {
if ($this->getUser()->can($manager::MODULE_PERMISSION_NS . $module->getName())) { if (self::getUser()->can($manager::MODULE_PERMISSION_NS . $module->getName())) {
$moduleSearchUrls = $module->getSearchUrls(); $moduleSearchUrls = $module->getSearchUrls();
if (! empty($moduleSearchUrls)) { if (! empty($moduleSearchUrls)) {
if ($searchString === '') { if ($searchString === '') {
$pane->add(t('Ready to search'), 'search/hint'); $pane->addDashlet(t('Ready to search'), 'search/hint');
return $this; return $this;
} }
$searchUrls = array_merge($searchUrls, $moduleSearchUrls); $searchUrls = array_merge($searchUrls, $moduleSearchUrls);
@ -69,28 +94,21 @@ class SearchDashboard extends Dashboard
usort($searchUrls, array($this, 'compareSearchUrls')); usort($searchUrls, array($this, 'compareSearchUrls'));
foreach (array_reverse($searchUrls) as $searchUrl) { foreach (array_reverse($searchUrls) as $searchUrl) {
$pane->createDashlet( $title = $searchUrl->title . ': ' . $searchString;
$searchUrl->title . ': ' . $searchString, $pane->addDashlet($title, Url::fromPath($searchUrl->url, array('q' => $searchString)));
Url::fromPath($searchUrl->url, array('q' => $searchString)) $pane->getDashlet($title)->setProgressLabel(t('Searching'));
)->setProgressLabel(t('Searching'));
} }
return $this; return $this;
} }
/** protected function assemble()
* Renders the output
*
* @return string
*
* @throws Zend_Controller_Action_Exception
*/
public function render()
{ {
if (! $this->getPane(self::SEARCH_PANE)->hasDashlets()) { if (! $this->searchHome->getPane(self::SEARCH_PANE)->hasDashlets()) {
throw new Zend_Controller_Action_Exception(t('Page not found'), 404); throw new HttpNotFoundException(t('Page not found'));
} }
return parent::render();
$this->add($this->searchHome->getPane(self::SEARCH_PANE)->getDashlets());
} }
/** /**