Skip to content

Commit af2ec53

Browse files
committed
Merge tag '2.0.8'
Maintenance release Fixes: - Use collectDirectChildrenOf() for paste button See contao-community-alliance/dc-general#442. - Fix slicing bug in MetaModel::getIdsFromFilter The method `MetaModel::getIdsFromFilter` lost support for offset and limit when we introduced the id cache. The functionality has been restored.
2 parents 8a2f88d + 363ff6b commit af2ec53

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/MetaModels/DcGeneral/Events/Table/InputScreenCondition/Subscriber.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\ModelToLabelEvent;
3434
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\ManipulateWidgetEvent;
3535
use ContaoCommunityAlliance\DcGeneral\Controller\ModelCollector;
36+
use ContaoCommunityAlliance\DcGeneral\Controller\RelationshipManager;
3637
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
38+
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
3739
use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface;
3840
use ContaoCommunityAlliance\DcGeneral\Factory\DcGeneralFactory;
3941
use ContaoCommunityAlliance\Translator\TranslatorInterface;
@@ -210,9 +212,6 @@ public function handleModelToLabel(ModelToLabelEvent $event)
210212
* @param GetPasteButtonEvent $event The event.
211213
*
212214
* @return void
213-
*
214-
* @SuppressWarnings(PHPMD.Superglobals)
215-
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
216215
*/
217216
public function generatePasteButton(GetPasteButtonEvent $event)
218217
{
@@ -234,18 +233,19 @@ public function generatePasteButton(GetPasteButtonEvent $event)
234233
return;
235234
}
236235

237-
$flags = $GLOBALS['METAMODELS']['inputscreen_conditions'][$model->getProperty('type')];
238236
// If setting does not support children, omit them.
239-
if ($model->getId() &&
240-
(!$flags['nestingAllowed'])
241-
) {
237+
$collector = new ModelCollector($environment);
238+
if ($model->getId() && !$this->acceptsAnotherChild($model, $collector)) {
242239
$event->setPasteIntoDisabled(true);
243-
return;
244240
}
245241

246-
$collector = new ModelCollector($environment);
247-
if (isset($flags['maxChildren']) && count($collector->collectChildrenOf($model)) > $flags['maxChildren']) {
248-
$event->setPasteIntoDisabled(true);
242+
$definition = $environment->getDataDefinition();
243+
$mode = $definition->getBasicDefinition()->getMode();
244+
$relationships = new RelationshipManager($definition->getModelRelationshipDefinition(), $mode);
245+
if (!$relationships->isRoot($model)
246+
&& ($parent = $collector->searchParentOf($model))
247+
&& !$this->acceptsAnotherChild($parent, $collector)) {
248+
$event->setPasteAfterDisabled(true);
249249
}
250250
}
251251

@@ -488,6 +488,33 @@ public function encodeValueValue(EncodePropertyValueFromWidgetEvent $event)
488488
}
489489
}
490490

491+
/**
492+
* Test if a model accepts another child.
493+
*
494+
* @param ModelInterface $model The model that shall be checked.
495+
* @param ModelCollector $collector The collector to use.
496+
*
497+
* @return bool
498+
*
499+
* @SuppressWarnings(PHPMD.Superglobals)
500+
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
501+
*/
502+
private function acceptsAnotherChild(ModelInterface $model, ModelCollector $collector)
503+
{
504+
$conditionType = $model->getProperty('type');
505+
$flags = $GLOBALS['METAMODELS']['inputscreen_conditions'][$conditionType];
506+
if (!$flags['nestingAllowed']) {
507+
return false;
508+
}
509+
510+
if (isset($flags['maxChildren'])
511+
&& count($collector->collectDirectChildrenOf($model)) > $flags['maxChildren']) {
512+
return false;
513+
}
514+
515+
return true;
516+
}
517+
491518
/**
492519
* Obtain the values of a property within a dc-general instance.
493520
*

src/MetaModels/MetaModel.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,16 @@ public function getIdsFromFilter($objFilter, $strSortBy = '', $intOffset = 0, $i
729729
// Build the right key for the cache.
730730
$sortKey = \sprintf('%s-%s', $strSortBy, \strtolower($strSortOrder));
731731
// Used the cached ID list, and make a list of wanted ID's with the sorting of the cache.
732-
$cacheResult = array_intersect((array) $this->existingIds[$sortKey], $arrFilteredIds);
732+
if (!isset($this->existingIds[$sortKey])) {
733+
$this->existingIds[$sortKey] = [];
734+
}
735+
$cacheResult = array_intersect($this->existingIds[$sortKey], $arrFilteredIds);
733736
// Check if we have all ID's or if we have one missing, now we are using the order of the MM Filter.
734737
if (array_intersect($arrFilteredIds, $cacheResult) === $arrFilteredIds) {
735-
return $cacheResult;
738+
if ($intOffset > 0 || $intLimit > 0) {
739+
return array_values(array_slice($cacheResult, $intOffset, $intLimit ?: null));
740+
}
741+
return array_values($cacheResult);
736742
}
737743

738744
// Merge the already known and the new one.

0 commit comments

Comments
 (0)