/
SearchDashboard.php
101 lines (89 loc) · 2.63 KB
/
SearchDashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Widget;
use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
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
*
* @package Icinga\Web\Widget
*/
class SearchDashboard extends Dashboard
{
const SEARCH_PANE = 'search';
/**
* All searchUrls provided by Modules
*
* @var array
*/
protected $searchUrls = array();
/**
* Load all available search dashlets from modules
*
* @param $searchQuery
* @return Dashboard|SearchDashboard
*/
public static function search($searchQuery = '')
{
/** @var $dashboard SearchDashboard */
$dashboard = new static('searchDashboard');
$dashboard->loadSearchDashlets($searchQuery);
return $dashboard;
}
/**
* Renders the output
*
* @return string
* @throws \Zend_Controller_Action_Exception
*/
public function render()
{
if (! $this->getPane(self::SEARCH_PANE)->hasComponents()) {
throw new ActionError('Site not found', 404);
}
return parent::render();
}
/**
* Loads search dashlets
*
* @param string $searchString
*/
protected function loadSearchDashlets($searchString)
{
$pane = $this->createPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search'));
$this->activate(self::SEARCH_PANE);
$manager = Icinga::app()->getModuleManager();
foreach ($manager->getLoadedModules() as $module) {
$this->addSearchDashletsFromModule($searchString, $module, $pane);
}
if ($searchString === '' && $pane->hasComponents()) {
$pane->removeComponents();
$pane->add('Ready to search', 'search/hint');
return;
}
}
/**
* Add available search dashlets to the pane
*
* @param string $searchString
* @param Module $module
* @param Pane $pane
*/
protected function addSearchDashletsFromModule($searchString, $module, $pane)
{
$searchUrls = $module->getSearchUrls();
if (! empty($searchUrls)) {
$this->searchUrls[] = $module->getSearchUrls();
foreach ($searchUrls as $search) {
$pane->addComponent(
$search->title . ': ' . $searchString,
Url::fromPath($search->url, array('q' => $searchString))
);
}
}
}
}