Merge branch 'bugfix/search-priority-8668'

fixes #8668
This commit is contained in:
Eric Lippmann 2015-03-12 18:52:20 +01:00
commit cf271a2f8f
4 changed files with 47 additions and 48 deletions

View File

@ -184,14 +184,18 @@ class Module
protected $searchUrls = array(); protected $searchUrls = array();
/** /**
* Provide a search URL
*
* @param string $title * @param string $title
* @param string $url * @param string $url
* @param int $priority
*/ */
public function provideSearchUrl($title, $url) public function provideSearchUrl($title, $url, $priority = 0)
{ {
$searchUrl = (object) array( $searchUrl = (object) array(
'title' => $title, 'title' => (string) $title,
'url' => $url 'url' => (string) $url,
'priority' => (int) $priority
); );
$this->searchUrls[] = $searchUrl; $this->searchUrls[] = $searchUrl;

View File

@ -3,37 +3,26 @@
namespace Icinga\Web\Widget; namespace Icinga\Web\Widget;
use Zend_Controller_Action_Exception;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Web\Url; use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard\Pane;
use Zend_Controller_Action_Exception as ActionError;
/** /**
* Class SearchDashboard display multiple search views on a single search page * Class SearchDashboard display multiple search views on a single search page
*
* @package Icinga\Web\Widget
*/ */
class SearchDashboard extends Dashboard class SearchDashboard extends Dashboard
{ {
const SEARCH_PANE = 'search'; const SEARCH_PANE = 'search';
/**
* All searchUrls provided by Modules
*
* @var array
*/
protected $searchUrls = array();
/** /**
* Load all available search dashlets from modules * Load all available search dashlets from modules
* *
* @param string $searchString * @param string $searchString
*
* @return Dashboard|SearchDashboard * @return Dashboard|SearchDashboard
*/ */
public static function search($searchString = '') public static function search($searchString = '')
{ {
/** @var $dashboard SearchDashboard */
$dashboard = new static('searchDashboard'); $dashboard = new static('searchDashboard');
$dashboard->loadSearchDashlets($searchString); $dashboard->loadSearchDashlets($searchString);
return $dashboard; return $dashboard;
@ -43,12 +32,13 @@ class SearchDashboard extends Dashboard
* Renders the output * Renders the output
* *
* @return string * @return string
* @throws \Zend_Controller_Action_Exception *
* @throws Zend_Controller_Action_Exception
*/ */
public function render() public function render()
{ {
if (! $this->getPane(self::SEARCH_PANE)->hasDashlets()) { if (! $this->getPane(self::SEARCH_PANE)->hasDashlets()) {
throw new ActionError('Site not found', 404); throw new Zend_Controller_Action_Exception(t('Page not found'), 404);
} }
return parent::render(); return parent::render();
} }
@ -64,37 +54,42 @@ class SearchDashboard extends Dashboard
$this->activate(self::SEARCH_PANE); $this->activate(self::SEARCH_PANE);
$manager = Icinga::app()->getModuleManager(); $manager = Icinga::app()->getModuleManager();
$searchUrls = array();
foreach ($manager->getLoadedModules() as $module) { foreach ($manager->getLoadedModules() as $module) {
$this->addSearchDashletsFromModule($searchString, $module, $pane); $moduleSearchUrls = $module->getSearchUrls();
if (! empty($moduleSearchUrls)) {
if ($searchString === '') {
$pane->add(t('Ready to search'), 'search/hint');
return;
}
$searchUrls = array_merge($searchUrls, $moduleSearchUrls);
}
} }
if ($searchString === '' && $pane->hasDashlets()) { usort($searchUrls, array($this, 'compareSearchUrls'));
$pane->removeDashlets();
$pane->add('Ready to search', 'search/hint'); foreach (array_reverse($searchUrls) as $searchUrl) {
return; $pane->addDashlet(
$searchUrl->title . ': ' . $searchString,
Url::fromPath($searchUrl->url, array('q' => $searchString))
);
} }
} }
/** /**
* Add available search dashlets to the pane * Compare search URLs based on their priority
* *
* @param string $searchString * @param object $a
* @param Module $module * @param object $b
* @param Pane $pane *
* @return int
*/ */
protected function addSearchDashletsFromModule($searchString, $module, $pane) private function compareSearchUrls($a, $b)
{ {
$searchUrls = $module->getSearchUrls(); if ($a->priority === $b->priority) {
return 0;
if (! empty($searchUrls)) {
$this->searchUrls[] = $module->getSearchUrls();
foreach ($searchUrls as $search) {
$pane->addDashlet(
$search->title . ': ' . $searchString,
Url::fromPath($search->url, array('q' => $searchString))
);
}
} }
return ($a->priority < $b->priority) ? -1 : 1;
} }
} }

View File

@ -21,4 +21,4 @@ $section->add($this->translate('Developer - Style'), array(
'priority' => 200, 'priority' => 200,
)); ));
$this->provideSearchUrl($this->translate('Doc'), 'doc/search'); $this->provideSearchUrl($this->translate('Doc'), 'doc/search', -10);

View File

@ -85,10 +85,10 @@ $this->provideSetupWizard('Icinga\Module\Monitoring\MonitoringWizard');
/* /*
* Available Search Urls * Available Search Urls
*/ */
$this->provideSearchUrl($this->translate('Hosts'), 'monitoring/list/hosts?sort=host_severity&limit=10'); $this->provideSearchUrl($this->translate('Hosts'), 'monitoring/list/hosts?sort=host_severity&limit=10', 99);
$this->provideSearchUrl($this->translate('Services'), 'monitoring/list/services?sort=service_severity&limit=10'); $this->provideSearchUrl($this->translate('Services'), 'monitoring/list/services?sort=service_severity&limit=10', 98);
$this->provideSearchUrl($this->translate('Hostgroups'), 'monitoring/list/hostgroups?limit=10'); $this->provideSearchUrl($this->translate('Hostgroups'), 'monitoring/list/hostgroups?limit=10', 97);
$this->provideSearchUrl($this->translate('Servicegroups'), 'monitoring/list/servicegroups?limit=10'); $this->provideSearchUrl($this->translate('Servicegroups'), 'monitoring/list/servicegroups?limit=10', 96);
/* /*
* Problems Section * Problems Section