Retrieve the header button for paste new.

@param ModelInterface       $parentModel The parent model.
@param EnvironmentInterface $environment The environment.

@return null|string
333286
private function getHeaderPasteTopButton(ModelInterface $parentModel, EnvironmentInterface $environment)
    {
        $definition      = $environment->getDataDefinition();
        $basicDefinition = $definition->getBasicDefinition();

        $filter = new Filter();
        $filter->andModelIsFromProvider($basicDefinition->getDataProvider());
        $filter->andParentIsFromProvider($basicDefinition->getParentDataProvider());

        $clipboard = $environment->getClipboard();
        if ($clipboard->isEmpty($filter)) {
            return null;
        }

        if (!($allowPasteTop = ViewHelpers::getManualSortingProperty($environment))) {
            $subFilter = new Filter();
            $subFilter->andActionIsNotIn([ItemInterface::COPY, ItemInterface::DEEP_COPY]);
            $subFilter->andParentIsNot(ModelId::fromModel($parentModel));
            $subFilter->orActionIsIn([ItemInterface::COPY, ItemInterface::DEEP_COPY]);

            $filter = new Filter();
            $filter->andModelIsFromProvider($basicDefinition->getDataProvider());
            $filter->andParentIsFromProvider($basicDefinition->getParentDataProvider());
            $filter->andSub($subFilter);

            $allowPasteTop = (bool) $clipboard->fetch($filter);
        }

        $dispatcher = $environment->getEventDispatcher();
        if ($allowPasteTop) {
            /** @var AddToUrlEvent $urlEvent */
            $urlEvent = $dispatcher->dispatch(
                ContaoEvents::BACKEND_ADD_TO_URL,
                new AddToUrlEvent(
                    'act=paste' .
                    '&amp;pid=' . ModelId::fromModel($parentModel)->getSerialized() .
                    '&amp;after=' . ModelId::fromValues($basicDefinition->getDataProvider(), '0')->getSerialized()
                )
            );

            /** @var GenerateHtmlEvent $imageEvent */
            $imageEvent = $dispatcher->dispatch(
                ContaoEvents::IMAGE_GET_HTML,
                new GenerateHtmlEvent(
                    'pasteafter.svg',
                    $this->translate('pasteafter.0', $definition->getName()),
                    'class="blink"'
                )
            );

            return \sprintf(
                '<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a>',
                $urlEvent->getUrl(),
                StringUtil::specialchars($this->translate('pasteafter.0', $definition->getName())),
                $imageEvent->getHtml()
            );
        }

        /** @var GenerateHtmlEvent $imageEvent */
        $imageEvent = $dispatcher->dispatch(
            ContaoEvents::IMAGE_GET_HTML,
            new GenerateHtmlEvent(
                'pasteafter_.svg',
                $this->translate('pasteafter.0', $definition->getName()),
                'class="blink"'
            )
        );

        return $imageEvent->getHtml();
    }
600107
protected function addDispatchDuplicateModel(EnvironmentInterface $environment)
    {
        $environment->getEventDispatcher()->addListener(
            PostDuplicateModelEvent::NAME,
            function (PostDuplicateModelEvent $event) {
                $this->copiedModel   = $event->getModel();
                $this->originalModel = $event->getSourceModel();
            }
        );
    }
1238352
public static function getRoot(NodeInterface $parent): ?PhpFileModelInterface
    {
        while ($parent !== null) {
            if ($parent instanceof PhpFileModelInterface) {
                return $parent;
            }
            $parent = $parent->getParentNode();
        }
        return null;
    }
829729
public function populate(EnvironmentInterface $environment)
    {
        if (!$this->scopeDeterminator->currentScopeIsBackend()) {
            return;
        }

        $this->populateView($environment);
        $this->populatePanel($environment);
    }
651412
public function getCommonAncestor($model)
    {
        if (empty($this->parentAttribute) || !is_string($this->parentAttribute) ||
            empty($model) || !$model->hasParent()) {
            return null;
        }
        $ancestors = $this->getAncestorChain();
        if (in_array($model->parent->getRefId(), $ancestors)) {
            return $model->parent;
        }
        return $this->getCommonAncestor($model->parent);
    }
146709
private function languageSwitcher(EnvironmentInterface $environment)
    {
        $template = new ContaoBackendViewTemplate('dcbe_general_language_selector');

        $dataProvider = $environment->getDataProvider();
        if (!$dataProvider instanceof MultiLanguageDataProviderInterface) {
            return '';
        }

        /** @var MultiLanguageDataProviderInterface $dataProvider */

        return $template
            ->set('languages', $environment->getController()->getSupportedLanguages(null))
            ->set('language', $dataProvider->getCurrentLanguage())
            ->set('submit', $this->translator->trans('MSC.showSelected', [], 'contao_default'))
            ->set('REQUEST_TOKEN', REQUEST_TOKEN)
            ->parse();
    }
1207440
private function process(Action $action, EnvironmentInterface $environment)
    {
        $inputProvider   = $environment->getInputProvider();
        $translator      = $environment->getTranslator();
        $editInformation = $GLOBALS['container']['dc-general.edit-information'];

        $renderInformation = new \ArrayObject();

        $propertyValueBag = new PropertyValueBag();
        foreach ($this->getOverrideProperties($action, $environment) as $property) {
            $propertyValueBag->setPropertyValue($property->getName(), $property->getDefaultValue());
        }

        if (false !== $inputProvider->hasValue('FORM_INPUTS')) {
            foreach ($inputProvider->getValue('FORM_INPUTS') as $formInput) {
                $propertyValueBag->setPropertyValue($formInput, $inputProvider->getValue($formInput));
            }
        }

        $this->invisibleUnusedProperties($action, $environment);
        $this->handleOverrideCollection($action, $renderInformation, $propertyValueBag, $environment);
        $this->renderFieldSets($action, $renderInformation, $propertyValueBag, $environment);
        $this->updateErrorInformation($renderInformation);

        if (!$editInformation->hasAnyModelError()) {
            $this->handleSubmit($action, $environment);
        }

        return $this->renderTemplate(
            $action,
            [
                'subHeadline' =>
                    $translator->translate('MSC.' . $inputProvider->getParameter('mode') . 'Selected') . ': ' .
                    $translator->translate('MSC.all.0'),
                'fieldsets'   => $renderInformation->offsetGet('fieldsets'),
                'table'       => $environment->getDataDefinition()->getName(),
                'error'       => $renderInformation->offsetGet('error'),
                'breadcrumb'  => $this->renderBreadcrumb($environment),
                'editButtons' => $this->getEditButtons($action, $environment),
                'noReload'    => (bool) $editInformation->hasAnyModelError($action, $environment)
            ]
        );
    }
115667
protected function getModel(EnvironmentInterface $environment)
    {
        $modelId      = ModelId::fromSerialized($environment->getInputProvider()->getParameter('id'));
        $dataProvider = $environment->getDataProvider($modelId->getDataProviderName());
        $model        = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));

        if ($model) {
            return $model;
        }

        $eventDispatcher = $environment->getEventDispatcher();

        $eventDispatcher->dispatch(
            ContaoEvents::SYSTEM_LOG,
            new LogEvent(
                \sprintf(
                    'Could not find ID %s in %s. DC_General show()',
                    $modelId->getId(),
                    $environment->getDataDefinition()->getName()
                ),
                __CLASS__ . '::' . __FUNCTION__,
                TL_ERROR
            )
        );

        $eventDispatcher->dispatch(
            ContaoEvents::CONTROLLER_REDIRECT,
            new RedirectEvent('contao?act=error')
        );

        return null;
    }
1298092
private function fetchModelsFromClipboard(FilterInterface $filter = null, ModelIdInterface $parentModelId = null)
    {
        $environment    = $this->getEnvironment();
        $dataDefinition = $environment->getDataDefinition();

        if (!$filter) {
            $filter = new Filter();
        }

        $basicDefinition   = $dataDefinition->getBasicDefinition();
        $modelProviderName = $basicDefinition->getDataProvider();
        $filter->andModelIsFromProvider($modelProviderName);
        if ($parentModelId) {
            $filter->andParentIsFromProvider($parentModelId->getDataProviderName());
        } else {
            $filter->andHasNoParent();
        }

        $environment = $this->getEnvironment();
        $clipboard   = $environment->getClipboard();
        $items       = $clipboard->fetch($filter);
        $actions     = [];

        foreach ($items as $item) {
            $model = null;

            if (!$item->isCreate() && $item->getModelId()) {
                $model = $this->modelCollector->getModel($item->getModelId()->getId(), $item->getDataProviderName());
            }

            $actions[] = [
                'model' => $model,
                'item'  => $item,
            ];
        }

        return $actions;
    }
1297187
private function queueRelated(ContextualCommandInterface $parentCommand): CommandInterface
    {
        if (empty($this->instance)) {
            return new NullCommand();
        }

        //Related entity store command
        $innerCommand = $this->instance->queueStore(true);

        //Inversed version of BelongsTo
        if (!$this->isSynced($this->parent, $this->instance)) {
            //Syncing FKs after primary command been executed
            $parentCommand->onExecute(function ($outerCommand) use ($innerCommand) {
                $innerCommand->addContext(
                    $this->key(Record::OUTER_KEY),
                    $this->lookupKey(Record::INNER_KEY, $this->parent, $outerCommand)
                );

                if (!empty($morphKey = $this->key(Record::MORPH_KEY))) {
                    //HasOne relation support additional morph key
                    $innerCommand->addContext(
                        $this->key(Record::MORPH_KEY),
                        $this->orm->define(get_class($this->parent), ORMInterface::R_ROLE_NAME)
                    );
                }
            });
        }

        return $innerCommand;
    }
379694
public function createInvokeHandlerContext(
        MessageHandlerInterface $messageHandler,
        IncomingLogicalMessageContext $parentContext
    ) {
        return new InvokeHandlerContext(
            $messageHandler,
            $parentContext->getMessage()->getMessageInstance(),
            $parentContext->getMessageId(),
            $parentContext->getHeaders(),
            $parentContext->getIncomingPhysicalMessage(),
            $parentContext->getPendingTransportOperations(),
            $this->busOperations,
            $this->outgoingOptionsFactory,
            $parentContext->getEndpointControlToken(),
            $parentContext
        );
    }
538760
final public function internalSetParent(ParentNodeInterface $parent)
    {
        $this->emit('parent.change', [$parent, $this]);
        $this->parentNode = $parent;
    }
1046893
private function parentsOf($model, &$parents)
    {
        $environment    = $this->getEnvironment();
        $dataDefinition = $environment->getDataDefinition();
        $collector      = new ModelCollector($this->getEnvironment());
        $relationships  = new RelationshipManager(
            $dataDefinition->getModelRelationshipDefinition(),
            $dataDefinition->getBasicDefinition()->getMode()
        );

        if (!$relationships->isRoot($model)) {
            $parent = $collector->searchParentOf($model);
            if (!isset($parents[$model->getProviderName()][$parent->getId()])) {
                $this->parentsOf($parent, $parents);
            }
        }

        $parents[$model->getProviderName()][$model->getId()] = 1;
    }
469917
public function remove(Model\EnvironmentInterface $environment)
    {
        $this->pmProxyClient->delete('/environments/' . $environment->getNormalizedName());
        if ($environment->hasChildren()) {
            foreach ($environment->getChildren() as $child) {
                /** @var Model\EnvironmentInterface $child */
                $this->remove($child);
            }
        }
        return $this;
    }
1108951
private function getActionsFromSource(ModelIdInterface $source, ModelIdInterface $parentModelId = null)
    {
        $basicDefinition = $this->getEnvironment()->getDataDefinition()->getBasicDefinition();

        $filter = new Filter();
        $filter->andModelIsFromProvider($basicDefinition->getDataProvider());
        if ($basicDefinition->getParentDataProvider()) {
            $filter->andParentIsFromProvider($basicDefinition->getParentDataProvider());
        } else {
            $filter->andHasNoParent();
        }
        $filter->andModelIs($source);
        $item = $this->getEnvironment()->getClipboard()->fetch($filter)[0];

        $action  = $item ? $item->getAction() : ItemInterface::CUT;
        $model   = $this->modelCollector->getModel($source);
        $actions = [
            [
                'model' => $model,
                'item'  => new Item($action, $parentModelId, ModelId::fromModel($model))
            ]
        ];

        return $actions;
    }
979411
protected function renderPasteTopButton(EnvironmentInterface $environment, $sorting)
    {
        $definition     = $environment->getDataDefinition();
        $dispatcher     = $environment->getEventDispatcher();
        $languageDomain = 'contao_' . $definition->getName();

        $filter = new Filter();
        $filter->andModelIsFromProvider($definition->getBasicDefinition()->getDataProvider());

        if (!$sorting || $environment->getClipboard()->isEmpty($filter)) {
            return null;
        }
        if (!ViewHelpers::getManualSortingProperty($environment)) {
            return null;
        }

        /** @var AddToUrlEvent $urlEvent */
        $urlEvent = $dispatcher->dispatch(
            ContaoEvents::BACKEND_ADD_TO_URL,
            new AddToUrlEvent(
                'act=paste&after=' . ModelId::fromValues($definition->getName(), '0')->getSerialized()
            )
        );

        /** @var GenerateHtmlEvent $imageEvent */
        $imageEvent = $dispatcher->dispatch(
            ContaoEvents::IMAGE_GET_HTML,
            new GenerateHtmlEvent(
                'pasteafter.svg',
                $this->translate('pasteafter.0', $languageDomain),
                'class="blink"'
            )
        );

        return \sprintf(
            '<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a>',
            $urlEvent->getUrl(),
            StringUtil::specialchars($this->translate('pasteafter.0', $languageDomain)),
            $imageEvent->getHtml()
        );
    }
718314
public function save(Model\EnvironmentInterface $environment, Model\EnvironmentInterface $cloneFrom = null)
    {
        $content = $this->getEnvironmentHydrator()->extract($environment);
        if ($cloneFrom != null) {
            $content['cloneFrom'] = strval($cloneFrom->getNormalizedName());
        }
        $this->pmProxyClient->put('/environments/' . $environment->getNormalizedName(), $content);
        if ($environment->hasChildren()) {
            foreach ($environment->getChildren() as $child) {
                /** @var Model\EnvironmentInterface $child */
                if ($cloneFrom) {
                    $this->save($child, $cloneFrom->getChildByName($child->getName()));
                } else {
                    $this->save($child);
                }
            }
        }
        return $this;
    }