-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SearchBar: Make tag/extra_tag searchable
- Loading branch information
1 parent
b560154
commit 19b5cc8
Showing
6 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\Notifications\Model\Behavior; | ||
|
||
use Icinga\Module\Notifications\Common\Auth; | ||
use ipl\Orm\AliasedExpression; | ||
use ipl\Orm\ColumnDefinition; | ||
use ipl\Orm\Contract\QueryAwareBehavior; | ||
use ipl\Orm\Contract\RewriteColumnBehavior; | ||
use ipl\Orm\Query; | ||
use ipl\Stdlib\Filter; | ||
use ipl\Stdlib\Filter\Rule; | ||
|
||
class ObjectTags implements RewriteColumnBehavior, QueryAwareBehavior | ||
{ | ||
use Auth; | ||
|
||
/** @var Query */ | ||
protected $query; | ||
|
||
public function setQuery(Query $query): self | ||
{ | ||
$this->query = $query; | ||
|
||
return $this; | ||
} | ||
|
||
public function rewriteCondition(Filter\Condition $condition, $relation = null): ?Rule | ||
{ | ||
$filterAll = null; | ||
$column = $condition->metaData()->get('columnName'); | ||
if ($column !== null) { | ||
if (substr($relation, -4) === 'tag.') { | ||
$relation = substr($relation, 0, -4) . 'object_id_tag.'; | ||
} else { // extra_tag. | ||
$relation = substr($relation, 0, -10) . 'object_extra_tag.'; | ||
} | ||
|
||
$nameFilter = Filter::like($relation . 'tag', $column); | ||
$class = get_class($condition); | ||
$valueFilter = new $class($relation . 'value', $condition->getValue()); | ||
|
||
$filterAll = Filter::all($nameFilter, $valueFilter); | ||
} | ||
|
||
return $filterAll; | ||
} | ||
|
||
public function rewriteColumn($column, $relation = null): AliasedExpression | ||
{ | ||
$model = $this->query->getModel(); | ||
$subQuery = $this->query->createSubQuery(new $model(), $relation) | ||
->limit(1) | ||
->columns('value') | ||
->filter(Filter::equal('tag', $column)); | ||
|
||
$this->applyRestrictions($subQuery); | ||
|
||
$alias = $this->query->getDb()->quoteIdentifier([str_replace('.', '_', $relation) . "_$column"]); | ||
|
||
[$select, $values] = $this->query->getDb()->getQueryBuilder()->assembleSelect($subQuery->assembleSelect()); | ||
return new AliasedExpression($alias, "($select)", null, ...$values); | ||
} | ||
|
||
public function rewriteColumnDefinition(ColumnDefinition $def, string $relation): void | ||
{ | ||
$parts = explode('.', substr($relation, 0, -4)); | ||
$objectType = array_pop($parts); | ||
|
||
$name = $def->getName(); | ||
// Programmatically translated since the full definition is available in class ObjectSuggestions | ||
$def->setLabel(sprintf(t(ucfirst($objectType) . ' %s', '..<tag-name>'), $name)); | ||
} | ||
|
||
public function isSelectableColumn(string $name): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* Icinga Notifications Web | (c) 2023 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Notifications\Model; | ||
|
||
use Icinga\Module\Notifications\Model\Behavior\ObjectTags; | ||
use ipl\Orm\Behaviors; | ||
use ipl\Sql\Connection; | ||
|
||
class ExtraTag extends ObjectExtraTag | ||
{ | ||
/** | ||
* @internal Don't use. This model acts only as relation target and is not supposed to be directly used as query | ||
* target. Use {@see CustomvarFlat} instead. | ||
*/ | ||
public static function on(Connection $_) | ||
{ | ||
throw new \LogicException('Documentation says: DO NOT USE. Can\'t you read?'); | ||
} | ||
|
||
public function createBehaviors(Behaviors $behaviors): void | ||
{ | ||
parent::createBehaviors($behaviors); | ||
|
||
$behaviors->add(new ObjectTags()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* Icinga Notifications Web | (c) 2023 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Notifications\Model; | ||
|
||
use Icinga\Module\Notifications\Model\Behavior\ObjectTags; | ||
use ipl\Orm\Behaviors; | ||
use ipl\Sql\Connection; | ||
|
||
class Tag extends ObjectIdTag | ||
{ | ||
/** | ||
* @internal Don't use. This model acts only as relation target and is not supposed to be directly used as query | ||
* target. Use {@see CustomvarFlat} instead. | ||
*/ | ||
public static function on(Connection $_) | ||
{ | ||
throw new \LogicException('Documentation says: DO NOT USE. Can\'t you read?'); | ||
} | ||
|
||
public function createBehaviors(Behaviors $behaviors): void | ||
{ | ||
parent::createBehaviors($behaviors); | ||
|
||
$behaviors->add(new ObjectTags()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters