Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Contao\\ManagerPlugin\\Bundle\\Config\\BundleConfig",
"Contao\\ManagerPlugin\\Bundle\\Parser\\ParserInterface",
"Contao\\ManagerPlugin\\Routing\\RoutingPluginInterface",
"InspiredMinds\\ContaoFileUsage\\Result\\ResultInterface"
"InspiredMinds\\ContaoFileUsage\\Result\\ResultInterface",
"Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2024 The MetaModels team.
* (c) 2012-2026 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -14,7 +14,7 @@
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2024 The MetaModels team.
* @copyright 2012-2026 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand All @@ -37,24 +37,24 @@ class FilterSettingTypeRendererCore extends AbstractFilterSettingTypeRenderer
#[\Override]
protected function getTypes()
{
return ['idlist', 'simplelookup', 'customsql', 'conditionand', 'conditionor'];
return ['idlist', 'simplelookup', 'customsql', 'conditionand', 'conditionor', 'expression_rule'];
}

/**
* Retrieve the parameters for the label.
*
* @param EnvironmentInterface $environment The translator in use.
*
* @param ModelInterface $model The model.
*
* @return array
*/
#[\Override]
protected function getLabelParameters(EnvironmentInterface $environment, ModelInterface $model)
{
if ($model->getProperty('type') == 'simplelookup') {
if ($model->getProperty('type') === 'simplelookup') {
return $this->getLabelParametersWithAttributeAndUrlParam($environment, $model);
}

return $this->getLabelParametersNormal($environment, $model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2023 The MetaModels team.
* (c) 2012-2026 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -14,7 +14,7 @@
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2023 The MetaModels team.
* @copyright 2012-2026 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand All @@ -25,9 +25,11 @@
use ContaoCommunityAlliance\DcGeneral\Clipboard\Filter;
use ContaoCommunityAlliance\DcGeneral\Clipboard\ItemInterface;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetPasteButtonEvent;
use ContaoCommunityAlliance\DcGeneral\Controller\ModelCollector;
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use MetaModels\Filter\Setting\IFilterSettingFactory;
use MetaModels\Filter\Setting\IFilterSettingTypeFactory;

/**
* This class takes care of enabling and disabling of the paste button.
Expand All @@ -41,6 +43,8 @@ class PasteButtonListener
*/
private IFilterSettingFactory $filterFactory;

private \SplObjectStorage $parents;

/**
* Create a new instance.
*
Expand All @@ -49,6 +53,7 @@ class PasteButtonListener
public function __construct(IFilterSettingFactory $filterFactory)
{
$this->filterFactory = $filterFactory;
$this->parents = new \SplObjectStorage();
}

/**
Expand All @@ -57,6 +62,9 @@ public function __construct(IFilterSettingFactory $filterFactory)
* @param GetPasteButtonEvent $event The event.
*
* @return void
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function handle(GetPasteButtonEvent $event)
{
Expand All @@ -79,11 +87,50 @@ public function handle(GetPasteButtonEvent $event)

return;
}
$factory = $this->filterFactory->getTypeFactory($model->getProperty('type'));

$factory = $this->getFactoryFor($model);
if (null === $factory) {
// Unknown type, disallow paste.
$event->setPasteIntoDisabled(true);
$event->setPasteAfterDisabled(true);
return;
}

// If setting does not support children, omit them.
if ($model->getId() && !($factory && $factory->isNestedType())) {
if ($model->getId() && !($factory->isNestedType())) {
$event->setPasteIntoDisabled(true);
}

$collector = new ModelCollector($event->getEnvironment());
if ($factory->isNestedType() && (null !== ($maxChildren = $factory->getMaxChildren()))) {
if ($maxChildren < count($collector->collectDirectChildrenOf($model))) {
$event->setPasteIntoDisabled(true);
}
}

if (!$this->parents->contains($model)) {
$this->parents[$model] = $collector->searchParentOf($model);
}

$parent = $this->parents[$model];
if (!$parent) {
return;
}

$parentFactory = $this->getFactoryFor($parent);
if (!$parentFactory?->isNestedType() || (null === ($maxChildren = $parentFactory->getMaxChildren()))) {
return;
}

$siblings = $collector->collectSiblingsOf($model, $parent->getId());
// FIXME: Except, if we are already contained and just get moved within parent :(
if ($maxChildren <= $siblings->length()) {
$event->setPasteAfterDisabled(true);
}
}

private function getFactoryFor(ModelInterface $model): ?IFilterSettingTypeFactory
{
return $this->filterFactory->getTypeFactory($model->getProperty('type'));
}
}
8 changes: 8 additions & 0 deletions src/CoreBundle/Resources/config/filter-settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ services:
- '@translator'
tags:
- { name: metamodels.filter_factory }

MetaModels\Filter\Setting\ExpressionRuleFilterSettingTypeFactory:
arguments:
- '@metamodels.expression-language'
- '@request_stack'
- '@translator'
tags:
- { name: metamodels.filter_factory }
3 changes: 3 additions & 0 deletions src/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,6 @@ services:
decorates: 'cca.backend-help-provider'
arguments:
$previous: '@.inner'

metamodels.expression-language:
class: Symfony\Component\ExpressionLanguage\ExpressionLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@
'remote' => 'id',
'operation' => '=',
],
]
],
'inverse' => [
[
'local' => 'pid',
'remote' => 'id',
'operation' => '=',
],
],
]
],
'rootEntries' => [
Expand Down Expand Up @@ -223,6 +230,14 @@
'stop_after_match'
]
],
'expression_rule extends default' => [
'config' => [
'expression_rule'
],
'+fefilter' => [
'onlypossible',
],
],
'idlist extends default' => [
'+config' => [
'items'
Expand Down Expand Up @@ -450,6 +465,19 @@
],
'sql' => "char(1) NOT NULL default ''"
],
'expression_rule' => [
'label' => 'expression_rule.label',
'description' => 'expression_rule.description',
'exclude' => true,
'inputType' => 'text',
'eval' => [
'alwaysSave' => true,
'decodeEntities' => true,
'mandatory' => true,
'tl_class' => 'clr',
],
'sql' => "text NOT NULL default ''"
],
'label' => [
'label' => 'label.label',
'description' => 'label.description',
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@
<trans-unit id="typenames.conditionand" resname="typenames.conditionand">
<source>AND condition</source>
</trans-unit>
<trans-unit id="typenames.expression_rule" resname="typenames.expression_rule">
<source>Expression rule</source>
</trans-unit>
<trans-unit id="error.condition_max_children" resname="error.condition_max_children">
<source>The condition or rule "%name%" can only have "%max%" children max.</source>
</trans-unit>
<trans-unit id="typedesc._attribute_" resname="typedesc._attribute_">
<source>&lt;em&gt;[%colName%, "%name%"]&lt;/em&gt;</source>
</trans-unit>
Expand All @@ -322,12 +328,22 @@
<trans-unit id="typedesc.conditionand" resname="typedesc.conditionand">
<source>%1$s &lt;strong&gt;%2$s&lt;/strong&gt; %4$s</source>
</trans-unit>
<trans-unit id="typedesc.expression_rule" resname="typedesc.expression_rule">
<source>%1$s &lt;strong&gt;%2$s&lt;/strong&gt; %4$s</source>
</trans-unit>
<trans-unit id="items.label" resname="items.label">
<source>Items</source>
</trans-unit>
<trans-unit id="items.description" resname="items.description">
<source>Please enter the IDs of the items for filtering as comma-separated list.</source>
</trans-unit>
<trans-unit id="expression_rule.label" resname="expression_rule.label">
<source>Expression</source>
</trans-unit>
<trans-unit id="expression_rule.description" resname="expression_rule.description">
<source>Enter an expression here that is to be evaluated. If the condition is met, the
first filter rule is executed; if not, the second one is executed.</source>
</trans-unit>
<trans-unit id="deleteConfirm" resname="deleteConfirm">
<source>Do you really want to delete filter setting ID %id%?</source>
</trans-unit>
Expand Down
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function setServiceContainer(IMetaModelsServiceContainer $serviceContaine
if ($deprecationNotice) {
// @codingStandardsIgnoreStart
@trigger_error(
'"' .__METHOD__ . '" is deprecated and will get removed.',
'"' . __METHOD__ . '" is deprecated and will get removed.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
Expand Down Expand Up @@ -116,7 +116,7 @@ public function getServiceContainer()

// @codingStandardsIgnoreStart
@trigger_error(
'"' .__METHOD__ . '" is deprecated - use the services from the service container.',
'"' . __METHOD__ . '" is deprecated - use the services from the service container.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
Expand Down
39 changes: 39 additions & 0 deletions src/Filter/Rules/ExpressionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace MetaModels\Filter\Rules;

use MetaModels\Filter\IFilter;
use MetaModels\Filter\IFilterRule;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

/**
* This is the MetaModel filter interface.
*/
final readonly class ExpressionRule implements IFilterRule
{
public function __construct(
private string $expression,
private array $parameters,
private ExpressionLanguage $expressionLanguage,
private ?IFilter $ifTrue,
private ?IFilter $ifFalse,
) {
}

/** @return list<string>|null */
#[\Override]
public function getMatchingIds(): ?array
{
if ((bool) $this->expressionLanguage->evaluate($this->expression, $this->parameters)) {
return $this->ifTrue?->getMatchingIds();
}

if (null !== $this->ifFalse) {
return $this->ifFalse->getMatchingIds();
}

return [];
}
}
6 changes: 3 additions & 3 deletions src/Filter/Setting/ConditionAndFilterSettingTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2024 The MetaModels team.
* (c) 2012-2026 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -14,7 +14,7 @@
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2024 The MetaModels team.
* @copyright 2012-2026 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand Down Expand Up @@ -43,7 +43,7 @@ public function __construct(

$this
->setTypeName('conditionand')
->setTypeIcon('/bundles/metamodelscore/images/icons/filter_and.png')
->setTypeIcon('bundles/metamodelscore/images/icons/filter_and.png')
->setTypeClass(ConditionAnd::class)
->allowAttributeTypes();
}
Expand Down
Loading
Loading