2019-11-26 03:57:31 -05:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-13 03:38:01 -04:00
|
|
|
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
|
|
|
|
|
|
2019-11-26 03:57:31 -05:00
|
|
|
namespace Icinga\Module\Icingadb\Model\Behavior;
|
|
|
|
|
|
|
|
|
|
use ipl\Orm\Contract\PropertyBehavior;
|
|
|
|
|
use ipl\Orm\Contract\RewriteFilterBehavior;
|
2020-12-03 11:05:51 -05:00
|
|
|
use ipl\Stdlib\Filter\Condition;
|
2019-11-26 03:57:31 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Bitmask
|
|
|
|
|
*
|
|
|
|
|
* @method void __construct(array $properties) Pass property names as keys and their bitmap ([value => bit]) as value
|
|
|
|
|
*/
|
|
|
|
|
class Bitmask extends PropertyBehavior implements RewriteFilterBehavior
|
|
|
|
|
{
|
|
|
|
|
public function fromDb($bits, $key, $context)
|
|
|
|
|
{
|
|
|
|
|
$values = [];
|
|
|
|
|
foreach ($context as $value => $bit) {
|
|
|
|
|
if ($bits & $bit) {
|
|
|
|
|
$values[] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toDb($value, $key, $context)
|
|
|
|
|
{
|
|
|
|
|
if (! is_array($value)) {
|
|
|
|
|
if (is_int($value) || ctype_digit($value)) {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isset($context[$value]) ? $context[$value] : -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits = [];
|
|
|
|
|
$allBits = 0;
|
|
|
|
|
foreach ($value as $v) {
|
|
|
|
|
if (isset($context[$v])) {
|
|
|
|
|
$bits[] = $context[$v];
|
|
|
|
|
$allBits |= $context[$v];
|
|
|
|
|
} elseif (is_int($v) || ctype_digit($v)) {
|
|
|
|
|
$bits[] = $v;
|
|
|
|
|
$allBits |= $v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits[] = $allBits;
|
|
|
|
|
return $bits;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 11:05:51 -05:00
|
|
|
public function rewriteCondition(Condition $condition, $relation = null)
|
2019-11-26 03:57:31 -05:00
|
|
|
{
|
2021-03-19 09:04:24 -04:00
|
|
|
$column = $condition->metaData()->get('columnName');
|
2019-11-26 03:57:31 -05:00
|
|
|
if (! isset($this->properties[$column])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 11:05:51 -05:00
|
|
|
$values = $condition->getValue();
|
2019-11-26 03:57:31 -05:00
|
|
|
if (! is_array($values)) {
|
2022-01-14 07:54:27 -05:00
|
|
|
if (is_int($values) || ctype_digit($values)) {
|
2019-11-26 03:57:31 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$values = [$values];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits = 0;
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
|
if (isset($this->properties[$column][$value])) {
|
|
|
|
|
$bits |= $this->properties[$column][$value];
|
|
|
|
|
} elseif (is_int($value) || ctype_digit($value)) {
|
|
|
|
|
$bits |= $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 11:05:51 -05:00
|
|
|
$condition->setColumn(sprintf('%s & %s', $condition->getColumn(), $bits));
|
2019-11-26 03:57:31 -05:00
|
|
|
}
|
|
|
|
|
}
|