2014-09-04 10:31:10 -04:00
|
|
|
<?php
|
2026-03-26 12:46:27 -04:00
|
|
|
|
|
|
|
|
// SPDX-FileCopyrightText: 2018 Icinga GmbH <https://icinga.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2014-09-04 10:31:10 -04:00
|
|
|
|
|
|
|
|
namespace Icinga\Web\Widget;
|
|
|
|
|
|
2015-03-12 13:48:59 -04:00
|
|
|
use Zend_Controller_Action_Exception;
|
2014-09-04 10:31:10 -04:00
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
|
use Icinga\Web\Url;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class SearchDashboard display multiple search views on a single search page
|
|
|
|
|
*/
|
|
|
|
|
class SearchDashboard extends Dashboard
|
|
|
|
|
{
|
|
|
|
|
/**
|
2015-07-23 06:53:08 -04:00
|
|
|
* Name for the search pane
|
2015-03-12 13:44:23 -04:00
|
|
|
*
|
2015-07-23 06:53:08 -04:00
|
|
|
* @var string
|
2014-09-04 10:31:10 -04:00
|
|
|
*/
|
2015-07-23 06:53:08 -04:00
|
|
|
const SEARCH_PANE = 'search';
|
2014-09-04 10:31:10 -04:00
|
|
|
|
2015-08-03 07:00:03 -04:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function getTabs()
|
|
|
|
|
{
|
|
|
|
|
if ($this->tabs === null) {
|
|
|
|
|
$this->tabs = new Tabs();
|
|
|
|
|
$this->tabs->add(
|
|
|
|
|
'search',
|
|
|
|
|
array(
|
|
|
|
|
'title' => t('Show Search', 'dashboard.pane.tooltip'),
|
|
|
|
|
'label' => t('Search'),
|
|
|
|
|
'url' => Url::fromRequest()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $this->tabs;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 10:31:10 -04:00
|
|
|
/**
|
2015-07-23 06:53:08 -04:00
|
|
|
* Load all available search dashlets from modules
|
2015-03-12 13:48:59 -04:00
|
|
|
*
|
2015-07-23 06:53:08 -04:00
|
|
|
* @param string $searchString
|
2014-09-04 10:31:10 -04:00
|
|
|
*
|
2015-07-23 06:53:08 -04:00
|
|
|
* @return $this
|
2014-09-04 10:31:10 -04:00
|
|
|
*/
|
2015-07-23 06:53:08 -04:00
|
|
|
public function search($searchString = '')
|
2014-09-04 10:31:10 -04:00
|
|
|
{
|
|
|
|
|
$pane = $this->createPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search'));
|
|
|
|
|
$this->activate(self::SEARCH_PANE);
|
|
|
|
|
|
|
|
|
|
$manager = Icinga::app()->getModuleManager();
|
2015-03-12 13:11:06 -04:00
|
|
|
$searchUrls = array();
|
2014-09-04 10:31:10 -04:00
|
|
|
|
|
|
|
|
foreach ($manager->getLoadedModules() as $module) {
|
2015-07-23 06:53:08 -04:00
|
|
|
if ($this->getUser()->can($manager::MODULE_PERMISSION_NS . $module->getName())) {
|
|
|
|
|
$moduleSearchUrls = $module->getSearchUrls();
|
|
|
|
|
if (! empty($moduleSearchUrls)) {
|
|
|
|
|
if ($searchString === '') {
|
|
|
|
|
$pane->add(t('Ready to search'), 'search/hint');
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
$searchUrls = array_merge($searchUrls, $moduleSearchUrls);
|
2015-03-12 13:47:34 -04:00
|
|
|
}
|
2015-03-12 13:11:06 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usort($searchUrls, array($this, 'compareSearchUrls'));
|
|
|
|
|
|
|
|
|
|
foreach (array_reverse($searchUrls) as $searchUrl) {
|
2015-08-24 06:36:59 -04:00
|
|
|
$pane->createDashlet(
|
2015-03-12 13:11:06 -04:00
|
|
|
$searchUrl->title . ': ' . $searchString,
|
|
|
|
|
Url::fromPath($searchUrl->url, array('q' => $searchString))
|
2015-08-25 10:51:50 -04:00
|
|
|
)->setProgressLabel(t('Searching'));
|
2014-09-04 10:31:10 -04:00
|
|
|
}
|
2015-07-23 06:53:08 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Renders the output
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* @throws Zend_Controller_Action_Exception
|
|
|
|
|
*/
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
if (! $this->getPane(self::SEARCH_PANE)->hasDashlets()) {
|
|
|
|
|
throw new Zend_Controller_Action_Exception(t('Page not found'), 404);
|
|
|
|
|
}
|
|
|
|
|
return parent::render();
|
2014-09-04 10:31:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-12 13:11:06 -04:00
|
|
|
* Compare search URLs based on their priority
|
2014-09-04 10:31:10 -04:00
|
|
|
*
|
2015-03-12 13:11:06 -04:00
|
|
|
* @param object $a
|
|
|
|
|
* @param object $b
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
2014-09-04 10:31:10 -04:00
|
|
|
*/
|
2015-03-12 13:11:06 -04:00
|
|
|
private function compareSearchUrls($a, $b)
|
2014-09-04 10:31:10 -04:00
|
|
|
{
|
2015-03-12 13:11:06 -04:00
|
|
|
if ($a->priority === $b->priority) {
|
|
|
|
|
return 0;
|
2014-09-04 10:31:10 -04:00
|
|
|
}
|
2015-03-12 13:11:06 -04:00
|
|
|
return ($a->priority < $b->priority) ? -1 : 1;
|
2014-09-04 10:31:10 -04:00
|
|
|
}
|
|
|
|
|
}
|