2013-09-24 15:26:10 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-10-23 15:10:33 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Icinga Web 2.
|
|
|
|
*
|
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @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-24 15:26:10 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Module\Monitoring\DataView;
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
use Icinga\Data\BaseQuery;
|
2013-10-14 13:25:25 +02:00
|
|
|
use Icinga\Filter\Filterable;
|
|
|
|
use Icinga\Filter\Query\Tree;
|
2013-09-24 15:26:10 +02:00
|
|
|
use Icinga\Module\Monitoring\Backend;
|
2013-10-14 13:25:25 +02:00
|
|
|
use Icinga\Module\Monitoring\Filter\UrlViewFilter;
|
2013-09-24 15:26:10 +02:00
|
|
|
use Icinga\Web\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A read-only view of an underlying Query
|
|
|
|
*/
|
2013-10-14 13:25:25 +02:00
|
|
|
abstract class DataView implements Filterable
|
2013-09-24 15:26:10 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Sort in ascending order, default
|
|
|
|
*/
|
2013-10-15 19:56:33 +02:00
|
|
|
const SORT_ASC = BaseQuery::SORT_ASC;
|
2013-09-24 15:26:10 +02:00
|
|
|
/**
|
|
|
|
* Sort in reverse order
|
|
|
|
*/
|
2013-10-15 19:56:33 +02:00
|
|
|
const SORT_DESC = BaseQuery::SORT_DESC;
|
2013-10-14 13:25:25 +02:00
|
|
|
/**
|
|
|
|
* The query used to populate the view
|
|
|
|
*
|
2013-10-15 19:56:33 +02:00
|
|
|
* @var BaseQuery
|
2013-10-14 13:25:25 +02:00
|
|
|
*/
|
|
|
|
private $query;
|
2013-09-24 15:26:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new view
|
|
|
|
*
|
2013-10-14 13:25:25 +02:00
|
|
|
* @param Backend $ds Which backend to query
|
|
|
|
* @param array $columns Select columns
|
2013-09-24 15:26:10 +02:00
|
|
|
*/
|
|
|
|
public function __construct(Backend $ds, array $columns = null)
|
|
|
|
{
|
|
|
|
$this->query = $ds->select()->from(static::getTableName(), $columns === null ? $this->getColumns() : $columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the queried table name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getTableName()
|
|
|
|
{
|
|
|
|
$tableName = explode('\\', get_called_class());
|
2013-10-14 12:56:06 +02:00
|
|
|
$tableName = end($tableName);
|
2013-09-24 15:26:10 +02:00
|
|
|
return $tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve columns provided by this view
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function getColumns();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create view from request
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2013-10-14 13:25:25 +02:00
|
|
|
* @param array $columns
|
2013-09-24 15:26:10 +02:00
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
2013-10-07 16:46:20 +02:00
|
|
|
public static function fromRequest($request, array $columns = null)
|
2013-09-24 15:26:10 +02:00
|
|
|
{
|
2013-10-20 16:08:01 +02:00
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
$view = new static(Backend::createBackend($request->getParam('backend')), $columns);
|
2013-10-15 19:56:33 +02:00
|
|
|
$parser = new UrlViewFilter($view);
|
2013-10-17 19:48:46 +02:00
|
|
|
$view->getQuery()->setFilter($parser->fromRequest($request));
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
$order = $request->getParam('dir');
|
|
|
|
if ($order !== null) {
|
|
|
|
if (strtolower($order) === 'desc') {
|
|
|
|
$order = self::SORT_DESC;
|
|
|
|
} else {
|
|
|
|
$order = self::SORT_ASC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$view->sort(
|
|
|
|
$request->getParam('sort'),
|
|
|
|
$order
|
|
|
|
);
|
|
|
|
return $view;
|
|
|
|
}
|
|
|
|
|
2013-10-10 11:33:41 +02:00
|
|
|
/**
|
|
|
|
* Create view from params
|
|
|
|
*
|
2013-10-14 13:25:25 +02:00
|
|
|
* @param array $params
|
|
|
|
* @param array $columns
|
2013-10-10 11:33:41 +02:00
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function fromParams(array $params, array $columns = null)
|
|
|
|
{
|
|
|
|
$view = new static(Backend::createBackend($params['backend']), $columns);
|
2013-10-17 19:48:46 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
foreach ($params as $key => $value) {
|
2013-10-17 19:48:46 +02:00
|
|
|
if ($view->isValidFilterTarget($key)) {
|
|
|
|
$view->getQuery()->where($key, $value);
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
}
|
2013-10-10 11:33:41 +02:00
|
|
|
$order = isset($params['order']) ? $params['order'] : null;
|
|
|
|
if ($order !== null) {
|
|
|
|
if (strtolower($order) === 'desc') {
|
|
|
|
$order = self::SORT_DESC;
|
|
|
|
} else {
|
|
|
|
$order = self::SORT_ASC;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2013-10-10 11:33:41 +02:00
|
|
|
$view->sort(
|
|
|
|
isset($params['sort']) ? $params['sort'] : null,
|
|
|
|
$order
|
|
|
|
);
|
|
|
|
return $view;
|
|
|
|
}
|
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
/**
|
|
|
|
* Check whether the given column is a valid filter column, i.e. the view actually provides the column or it's
|
|
|
|
* a non-queryable filter column
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-10-14 13:25:25 +02:00
|
|
|
public function isValidFilterTarget($column)
|
2013-09-24 15:26:10 +02:00
|
|
|
{
|
|
|
|
return in_array($column, $this->getColumns()) || in_array($column, $this->getFilterColumns());
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
public function getFilterColumns()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
/**
|
|
|
|
* Sort the rows, according to the specified sort column and order
|
|
|
|
*
|
2013-10-14 13:25:25 +02:00
|
|
|
* @param string $column Sort column
|
|
|
|
* @param int $order Sort order, one of the SORT_ constants
|
2013-09-24 15:26:10 +02:00
|
|
|
*
|
|
|
|
* @see DataView::SORT_ASC
|
|
|
|
* @see DataView::SORT_DESC
|
|
|
|
*/
|
|
|
|
public function sort($column = null, $order = null)
|
|
|
|
{
|
|
|
|
$sortRules = $this->getSortRules();
|
2013-10-20 16:08:01 +02:00
|
|
|
if ($sortRules !== null) {
|
|
|
|
if ($column === null) {
|
|
|
|
$sortColumns = reset($sortRules);
|
2013-09-24 15:26:10 +02:00
|
|
|
if (!isset($sortColumns['columns'])) {
|
2013-10-20 16:08:01 +02:00
|
|
|
$sortColumns['columns'] = array(key($sortRules));
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-20 16:08:01 +02:00
|
|
|
if (isset($sortRules[$column])) {
|
|
|
|
$sortColumns = $sortRules[$column];
|
|
|
|
if (!isset($sortColumns['columns'])) {
|
|
|
|
$sortColumns['columns'] = array($column);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sortColumns = array(
|
|
|
|
'columns' => array($column),
|
|
|
|
'order' => $order
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2013-10-20 16:08:01 +02:00
|
|
|
$order = $order === null ? (isset($sortColumns['order']) ? $sortColumns['order'] : self::SORT_ASC) : $order;
|
|
|
|
$order = ($order === self::SORT_ASC) ? 'ASC' : 'DESC';
|
2013-10-15 19:56:33 +02:00
|
|
|
|
2013-10-20 16:08:01 +02:00
|
|
|
foreach ($sortColumns['columns'] as $column) {
|
|
|
|
$this->query->order($column, $order);
|
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
return $this;
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:25:25 +02:00
|
|
|
/**
|
|
|
|
* Retrieve default sorting rules for particular columns. These involve sort order and potential additional to sort
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-10-20 16:08:01 +02:00
|
|
|
public function getSortRules()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-14 13:25:25 +02:00
|
|
|
|
|
|
|
public function getMappedField($field)
|
|
|
|
{
|
|
|
|
return $this->query->getMappedField($field);
|
|
|
|
}
|
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
/**
|
|
|
|
* Return the query which was created in the constructor
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getQuery()
|
|
|
|
{
|
|
|
|
return $this->query;
|
|
|
|
}
|
2013-10-14 13:25:25 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function applyFilter()
|
|
|
|
{
|
|
|
|
$this->query->applyFilter();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearFilter()
|
|
|
|
{
|
|
|
|
$this->query->clearFilter();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addFilter($filter)
|
2013-10-14 13:25:25 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->query->addFilter($filter);
|
|
|
|
return $this;
|
2013-10-14 13:25:25 +02:00
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|