2013-09-25 14:04:42 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-09-25 14:04:42 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-09-25 14:04:42 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
2013-09-25 14:04:42 +02:00
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-10-14 13:25:25 +02:00
|
|
|
// @codingStandardsIgnoreStart
|
2013-09-25 14:04:42 +02:00
|
|
|
|
|
|
|
use Icinga\Web\Form;
|
|
|
|
use Icinga\Web\Controller\ActionController;
|
|
|
|
use Icinga\Filter\Filter;
|
|
|
|
use Icinga\Filter\FilterAttribute;
|
|
|
|
use Icinga\Filter\Type\TextFilter;
|
|
|
|
use Icinga\Application\Logger;
|
|
|
|
use Icinga\Web\Url;
|
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
/**
|
|
|
|
* Application wide interface for filtering
|
|
|
|
*/
|
2013-09-25 14:04:42 +02:00
|
|
|
class FilterController extends ActionController
|
|
|
|
{
|
|
|
|
/**
|
2013-10-14 13:25:25 +02:00
|
|
|
* The current filter registry
|
|
|
|
*
|
2013-09-25 14:04:42 +02:00
|
|
|
* @var Filter
|
|
|
|
*/
|
|
|
|
private $registry;
|
|
|
|
|
2013-10-17 19:48:46 +02:00
|
|
|
private $moduleRegistry;
|
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
/**
|
|
|
|
* Entry point for filtering, uses the filter_domain and filter_module request parameter
|
|
|
|
* to determine which filter registry should be used
|
|
|
|
*/
|
2013-09-25 14:04:42 +02:00
|
|
|
public function indexAction()
|
|
|
|
{
|
|
|
|
$this->registry = new Filter();
|
2013-10-17 19:48:46 +02:00
|
|
|
$query = $this->getRequest()->getParam('query', '');
|
|
|
|
$target = $this->getRequest()->getParam('filter_domain', '');
|
2013-09-25 14:04:42 +02:00
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
if ($this->getRequest()->getHeader('accept') == 'application/json') {
|
2013-09-25 14:04:42 +02:00
|
|
|
$this->getResponse()->setHeader('Content-Type', 'application/json');
|
2013-10-14 13:25:25 +02:00
|
|
|
$this->setupQueries(
|
2013-10-17 19:48:46 +02:00
|
|
|
$target,
|
2013-10-14 13:25:25 +02:00
|
|
|
$this->getParam('filter_module', '')
|
|
|
|
);
|
2013-10-17 19:48:46 +02:00
|
|
|
$this->_helper->json($this->parse($query, $target));
|
2013-10-14 13:25:25 +02:00
|
|
|
} else {
|
2013-10-17 19:48:46 +02:00
|
|
|
$this->setupQueries(
|
|
|
|
$target,
|
|
|
|
$this->getParam('filter_module')
|
|
|
|
);
|
|
|
|
$urlTarget = $this->parse($query, $target);
|
|
|
|
$this->redirect($urlTarget['urlParam']);
|
2013-09-25 14:04:42 +02:00
|
|
|
}
|
2013-10-17 19:48:46 +02:00
|
|
|
|
|
|
|
|
2013-09-25 14:04:42 +02:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
/**
|
|
|
|
* Set up the query handler for the given domain and module
|
|
|
|
*
|
|
|
|
* @param string $domain The domain to use
|
|
|
|
* @param string $module The module to use
|
|
|
|
*/
|
|
|
|
private function setupQueries($domain, $module = 'default')
|
2013-09-25 14:04:42 +02:00
|
|
|
{
|
2013-10-14 13:25:25 +02:00
|
|
|
$class = '\\Icinga\\Module\\' . ucfirst($module) . '\\Filter\\Registry';
|
|
|
|
$factory = strtolower($domain) . 'Filter';
|
2013-10-17 19:48:46 +02:00
|
|
|
$this->moduleRegistry = $class;
|
2013-10-14 13:25:25 +02:00
|
|
|
$this->registry->addDomain($class::$factory());
|
2013-09-25 14:04:42 +02:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
/**
|
|
|
|
* Parse the given query text and returns the json as expected by the semantic search box
|
|
|
|
*
|
|
|
|
* @param String $text The query to parse
|
|
|
|
* @return array The result structure to be returned in json format
|
|
|
|
*/
|
2013-10-17 19:48:46 +02:00
|
|
|
private function parse($text, $target)
|
2013-09-25 14:04:42 +02:00
|
|
|
{
|
|
|
|
try {
|
2013-10-14 13:25:25 +02:00
|
|
|
|
2013-10-17 19:48:46 +02:00
|
|
|
$queryTree = $this->registry->createQueryTreeForFilter($text);
|
|
|
|
$registry = $this->moduleRegistry;
|
2013-10-14 13:25:25 +02:00
|
|
|
return array(
|
|
|
|
'state' => 'success',
|
|
|
|
'proposals' => $this->registry->getProposalsForQuery($text),
|
2013-10-21 17:04:05 +02:00
|
|
|
'urlParam' => $registry::getUrlForTarget($target, $queryTree),
|
|
|
|
'valid' => count($this->registry->getIgnoredQueryParts()) === 0
|
2013-10-14 13:25:25 +02:00
|
|
|
);
|
2013-09-25 14:04:42 +02:00
|
|
|
} catch (\Exception $exc) {
|
|
|
|
Logger::error($exc);
|
2013-10-14 13:25:25 +02:00
|
|
|
$this->getResponse()->setHttpResponseCode(500);
|
|
|
|
return array(
|
|
|
|
'state' => 'error',
|
|
|
|
'message' => 'Search service is currently not available'
|
|
|
|
);
|
2013-09-25 14:04:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-14 13:25:25 +02:00
|
|
|
// @codingStandardsIgnoreEnd
|