Reallocates existing route to given document.

@param RoutablePageBehavior $document
@param string $locale

@return RouteInterface
175633
private function reallocateExistingRoute(RoutablePageBehavior $document, $locale)
    {
        $newRoute = $this->routeRepository->findByPath($document->getRoutePath(), $locale);
        if (!$newRoute) {
            return;
        }

        $oldRoute = $this->routeRepository->findByEntity(get_class($document), $document->getUuid(), $locale);
        $history = $this->routeRepository->findHistoryByEntity(get_class($document), $document->getUuid(), $locale);

        /** @var RouteInterface $historyRoute */
        foreach (array_filter(array_merge($history, [$oldRoute])) as $historyRoute) {
            if ($historyRoute->getId() === $newRoute->getId() || $document->getId() !== $historyRoute->getEntityId()) {
                // Mismatch of entity-id's happens because doctrine don't check entities which has been changed in the
                // current session. If the old-route was already reused by a page before it will be returned in the
                // query of line 329.

                continue;
            }

            $historyRoute->setTarget($newRoute);
            $historyRoute->setHistory(true);
            $newRoute->addHistory($historyRoute);
        }

        $newRoute->setEntityClass(get_class($document));
        $newRoute->setEntityId($document->getId());
        $newRoute->setTarget(null);
        $newRoute->setHistory(false);

        return $newRoute;
    }
1085591
public function handleRemove(RemoveEvent $event)
    {
        $document = $event->getDocument();
        if (!$document instanceof RoutableBehavior) {
            return;
        }

        $locales = $this->documentInspector->getLocales($document);
        foreach ($locales as $locale) {
            $localizedDocument = $this->documentManager->find($document->getUuid(), $locale);

            $route = $this->routeRepository->findByEntity(
                $localizedDocument->getClass(),
                $localizedDocument->getUuid(),
                $locale
            );
            if (!$route) {
                continue;
            }

            $this->entityManager->remove($route);

            if ($document instanceof ChildrenBehavior) {
                $this->removeChildRoutes($document, $locale);
            }
        }

        $this->entityManager->flush();
    }
676966
protected function findOrCreateViewDocument(ArticleDocument $document, $locale, $localizationState)
    {
        $articleId = $this->getViewDocumentId($document->getUuid(), $locale);
        /** @var ArticleViewDocumentInterface $article */
        $article = $this->manager->find($this->documentFactory->getClass('article'), $articleId);

        if ($article) {
            // Only index ghosts when the article isn't a ghost himself.
            if (LocalizationState::GHOST === $localizationState
                && LocalizationState::GHOST !== $article->getLocalizationState()->state
            ) {
                return null;
            }

            return $article;
        }

        $article = $this->documentFactory->create('article');
        $article->setId($articleId);
        $article->setUuid($document->getUuid());
        $article->setLocale($locale);

        return $article;
    }
1307550
public function route(string $action, $callback, string $method = 'get', string $title = ''): PageInterface
    {
        $method = strtolower($method);
        $action = $this->parseAction($action, $method);

        $this->routes[$method][$action] = $callback;

        $this->titles[$action] = ! empty($title) ? $title : $this->getTitle();

        $this->registerRouteActions();

        return $this;
    }
46322
public function findTranslatedRouteByPath($path, $locale)
    {
        // check if this url is a translated url
        foreach ($this->translatedRoutes as $route) {
            if ($this->translate($route, $locale) == rawurldecode($path))
                return $route;
        }

        return false;
    }
418317
private function recursivelyRemoveRoutes(RouteBehavior $document)
    {
        $referrers = $this->documentInspector->getReferrers($document);

        foreach ($referrers as $referrer) {
            if (!$referrer instanceof RouteBehavior) {
                continue;
            }

            $this->recursivelyRemoveRoutes($referrer);
            $this->documentManager->remove($referrer);
        }
    }
34371
private function persistDocument($data, $document, $locale, $articleUuid)
    {
        if (array_key_exists('title', $data)) {
            throw new ParameterNotAllowedException('title', get_class($document));
        }

        $article = $this->getDocumentManager()->find($articleUuid, $locale);

        if (!array_key_exists('template', $data)) {
            $data['template'] = $article->getStructureType();
        }

        $formType = $this->getMetadataFactory()->getMetadataForAlias('article_page')->getFormType();

        $form = $this->createForm(
            $formType,
            $document,
            [
                // disable csrf protection, since we can't produce a token, because the form is cached on the client
                'csrf_protection' => false,
            ]
        );
        $form->submit($data, false);

        $document->setParent($article);

        if (!$form->isValid()) {
            throw new InvalidFormException($form);
        }

        $this->getDocumentManager()->persist(
            $document,
            $locale,
            [
                'user' => $this->getUser()->getId(),
                'clear_missing_content' => false,
                'auto_name' => false,
                'auto_rename' => false,
            ]
        );
    }
1067400
public function createTranslatedProperty($property, $locale, StructureInterface $structure = null)
    {
        if ($property instanceof ItemMetadata) {
            $property = $this->createProperty($property, $structure);
        }
        $property = new TranslatedProperty(
            $property,
            $locale,
            $this->namespaceRegistry->getPrefix('content_localized')
        );

        return $property;
    }
246550
private function upgradeNode(NodeInterface $node, $locale, array $properties, $up)
    {
        /** @var BasePageDocument $document */
        $document = $this->documentManager->find($node->getIdentifier(), $locale);
        $documentLocales = $this->documentInspector->getLocales($document);

        if (!in_array($locale, $documentLocales)) {
            return;
        }

        foreach ($properties as $property) {
            if ($property['property'] instanceof BlockMetadata) {
                $this->upgradeBlockProperty($property['property'], $property['components'], $node, $locale, $up);
            } else {
                $this->upgradeProperty($property['property'], $node, $locale, $up);
            }
        }

        $this->documentManager->persist($document, $locale, ['auto_name' => false]);
    }
348986
public function setSchemaPageType($schemaPageType, $locale = null)
    {
        $this->translate($locale, false)->setSchemaPageType($schemaPageType);
        $this->mergeNewTranslations();

        return $this;
    }
1347479
protected function createLinkItem(ArticleViewDocumentInterface $document, $locale, $scheme)
    {
        $url = $this->webspaceManager->findUrlByResourceLocator(
            $document->getRoutePath(),
            $this->environment,
            $locale,
            $document->getTargetWebspace(),
            null,
            $scheme
        );

        return new LinkItem($document->getUuid(), $document->getTitle(), $url, $document->getPublishedState());
    }
776543
public function registerDocument($document, NodeInterface $node, $locale)
    {
        $oid = $this->getObjectIdentifier($document);
        $uuid = $node->getIdentifier();

        // do not allow nodes without UUIDs or reregistration of documents
        $this->validateDocumentRegistration($document, $locale, $node, $oid, $uuid);

        $this->documentMap[$oid] = $document;
        $this->documentNodeMap[$oid] = $uuid;
        $this->nodeMap[$node->getIdentifier()] = $node;
        $this->nodeDocumentMap[$this->getNodeLocaleKey($node->getIdentifier(), $locale)] = $document;
        $this->documentLocaleMap[$oid] = $locale;
    }
592531
protected function copyRedirectsAndStructure(
        $localeSource,
        $localeDestination
    ) {
        $this->output->writeln([
            '------------------------------',
            '<info>Webspace: </info>' . $this->webspaceKeySource . ' => ' . $this->webspaceKeyDestination,
            '<info>Locale: </info>' . $localeSource . ' => ' . $localeDestination,
            '------------------------------',
        ]);

        /** @var HomeDocument $homeDocumentSource */
        $homeDocumentSource = $this->documentManager->find(
            $this->sessionManager->getContentPath($this->webspaceKeySource),
            $localeSource
        );

        // Generate redirect and structure.
        $this->recursiveCopyRedirectsAndStructure(
            $homeDocumentSource,
            $localeDestination
        );

        $this->output->writeln([
            '------------------------------',
            '',
        ]);
    }
530166
public static function changeLocale($locale, $url = null) {
        $app = app();
        //$modeloClass = CrudGenerator::getModel($modelo, substr($modelo,0, strlen($modelo)-1));
        $base = "";
        $currentLocale = $app->getLocale();
        $defaultLocale = $app->config->get('app.locale');
        if ($locale === null) {
            $locale = $currentLocale;
        }
        if (!in_array($locale, $app->config->get('sirgrimorum.crudgenerator.list_locales'))) {
            $locale = $currentLocale;
        }
        if (empty($url)) {
            $url = $app->request->fullUrl();
            $urlLocale = $app->request->segment(1);
            if (in_array($urlLocale, $app->config->get('sirgrimorum.crudgenerator.list_locales'))) {
                $url = str_replace("/" . $urlLocale . "/", "/" . $locale . "/", $url);
            }
        } else {
            if (stripos($url, "/" . $currentLocale . "/") > 0) {
                $url = str_replace("/" . $currentLocale . "/", "/" . $locale . "/", $url);
            } elseif (substr($url, strlen($url) - (strlen($currentLocale) + 1)) == "/" . $currentLocale) {
                $url = substr($url, 0, strlen($url) - (strlen($currentLocale))) . $currentLocale;
            } elseif (stripos($url, "http://") === false && stripos($url, "https://") === false) {
                $url = str_replace("//", "/", $locale . "/" . $url);
            }
        }


        return $url;
    }
609874
public function getRouteNameFromPath($uri, $locale)
    {
        $attributes = Url::extractAttributes($uri);
        $uri        = str_replace([url('/'), "/$locale/"], '', $uri);
        $uri        = trim($uri, '/');

        foreach ($this->translatedRoutes as $routeName) {
            $url = Url::substituteAttributes($attributes, $this->translate($routeName));

            if ($url === $uri) return $routeName;
        }

        return false;
    }
1133272
public function createTranslations($e, $siteFolder, $locale)
    { 
    	$sm = $e->getApplication()->getServiceManager();
    	$translator = $sm->get('translator');
    	
    	$langFileTarget = $_SERVER['DOCUMENT_ROOT'] . '/../module/MelisSites/' .$siteFolder . '/language/' . $locale . '.php';
    	
    	if (!file_exists($langFileTarget))
    	{
    	    $langFileTarget = $_SERVER['DOCUMENT_ROOT'] . '/../module/MelisSites/' .$siteFolder . '/language/en_EN.php';
    	    
    	    if (!file_exists($langFileTarget))
    	    {
    	        $langFileTarget = null;
    	    }
    	}
    	else 
    	{
    	    $langFileTarget = null;
    	}
    	
    	if (!is_null($langFileTarget) && !empty($siteFolder) && !empty($locale))
    	{
    	    $translator->addTranslationFile(
    	        'phparray',
    	        $langFileTarget
	        );
    	}
    }
1370721
public function viewLiveAction(Request $request, $locale, $path = null)
    {
        $request->setLocale($locale);

        return $this->changeViewMode($request, PageInterface::STATUS_PUBLISHED, $path);
    }