2019-11-22 09:52:26 -05:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-13 03:38:01 -04:00
|
|
|
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
|
|
|
|
|
|
2019-11-22 09:52:26 -05:00
|
|
|
namespace Icinga\Module\Icingadb\Model\Behavior;
|
|
|
|
|
|
2022-06-08 09:44:51 -04:00
|
|
|
use ipl\Orm\Contract\RewriteFilterBehavior;
|
|
|
|
|
use ipl\Orm\Contract\RewritePathBehavior;
|
2020-12-03 11:05:51 -05:00
|
|
|
use ipl\Stdlib\Filter;
|
2019-11-22 09:52:26 -05:00
|
|
|
|
2022-06-08 09:44:51 -04:00
|
|
|
class ReRoute implements RewriteFilterBehavior, RewritePathBehavior
|
2019-11-22 09:52:26 -05:00
|
|
|
{
|
|
|
|
|
protected $routes;
|
|
|
|
|
|
2022-06-29 10:40:28 -04:00
|
|
|
/**
|
|
|
|
|
* Tables with mixed object type entries for which servicegroup filters need to be resolved in multiple steps
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2025-10-12 16:45:40 -04:00
|
|
|
public const MIXED_TYPE_RELATIONS = ['downtime', 'comment', 'history', 'notification_history'];
|
2022-06-29 10:40:28 -04:00
|
|
|
|
2019-11-22 09:52:26 -05:00
|
|
|
public function __construct(array $routes)
|
|
|
|
|
{
|
|
|
|
|
$this->routes = $routes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public function getRoutes(): array
|
2020-11-03 03:41:16 -05:00
|
|
|
{
|
|
|
|
|
return $this->routes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 11:05:51 -05:00
|
|
|
public function rewriteCondition(Filter\Condition $condition, $relation = null)
|
2019-11-22 09:52:26 -05:00
|
|
|
{
|
2021-09-30 02:53:11 -04:00
|
|
|
$remainingPath = $condition->metaData()->get('columnName', '');
|
|
|
|
|
if (strpos($remainingPath, '.') === false) {
|
2019-11-22 09:52:26 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 02:53:11 -04:00
|
|
|
if (($path = $this->rewritePath($remainingPath, $relation)) !== null) {
|
2020-12-03 11:05:51 -05:00
|
|
|
$class = get_class($condition);
|
2021-09-30 02:53:11 -04:00
|
|
|
$filter = new $class($relation . $path, $condition->getValue());
|
|
|
|
|
if ($condition->metaData()->has('forceOptimization')) {
|
|
|
|
|
$filter->metaData()->set(
|
|
|
|
|
'forceOptimization',
|
|
|
|
|
$condition->metaData()->get('forceOptimization')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 10:40:28 -04:00
|
|
|
if (
|
|
|
|
|
in_array(substr($relation, 0, -1), self::MIXED_TYPE_RELATIONS)
|
|
|
|
|
&& substr($remainingPath, 0, 13) === 'servicegroup.'
|
|
|
|
|
) {
|
|
|
|
|
$applyAll = Filter::all();
|
|
|
|
|
$applyAll->add(Filter::equal($relation . 'object_type', 'host'));
|
|
|
|
|
|
|
|
|
|
$orgFilter = clone $filter;
|
|
|
|
|
$orgFilter->setColumn($relation . 'host.' . $path);
|
|
|
|
|
|
|
|
|
|
$applyAll->add($orgFilter);
|
|
|
|
|
|
|
|
|
|
$filter = Filter::any($filter, $applyAll);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 02:53:11 -04:00
|
|
|
return $filter;
|
2019-11-22 09:52:26 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-30 02:53:11 -04:00
|
|
|
|
2022-06-08 09:44:51 -04:00
|
|
|
public function rewritePath(string $path, ?string $relation = null): ?string
|
2021-09-30 02:53:11 -04:00
|
|
|
{
|
|
|
|
|
$dot = strpos($path, '.');
|
|
|
|
|
if ($dot !== false) {
|
|
|
|
|
$routeName = substr($path, 0, $dot);
|
|
|
|
|
} else {
|
|
|
|
|
$routeName = $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($this->routes[$routeName])) {
|
|
|
|
|
return $this->routes[$routeName] . ($dot !== false ? substr($path, $dot) : '');
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 09:44:51 -04:00
|
|
|
return null;
|
2021-09-30 02:53:11 -04:00
|
|
|
}
|
2019-11-22 09:52:26 -05:00
|
|
|
}
|