Skip to content

Commit

Permalink
Added Translation check
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Bieniek committed Mar 10, 2020
1 parent 28d7a28 commit 0671b4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 60 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ By default last used SiteAccess is taken and this Bundle changes this behavior s
is better suited for the Multisite setup.

If Landing Page is outside of last used SiteAccess Subtree, first SiteAccess that shares
Subtree with Landing Page will be used.
Subtree with Landing Page will be used. In addition, selected Translation will be taken into account when determining
correct SiteAccess. If SiteAccess with matching Language *and* Subtree can't be find,

The same behavior is used when the Landing Page is created.

Expand Down
79 changes: 31 additions & 48 deletions src/bundle/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace MateuszBieniek\EzPlatformPageBuilderMultisiteSiteaccessPickerBundle\Controller;

use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\API\Repository\Values\Content\Location;
use EzSystems\EzPlatformAdminUi\View\ContentTranslateView;
use EzSystems\EzPlatformAdminUiBundle\Controller\Controller;
use EzSystems\EzPlatformPageBuilder\PageBuilder;
use EzSystems\EzPlatformPageBuilder\Siteaccess\SiteaccessService;
use EzSystems\EzPlatformPageBuilder\View\PageView;
use EzSystems\EzPlatformPageBuilderBundle\Controller\PageController as PageBuilderPageController;
Expand All @@ -15,7 +17,6 @@
use EzSystems\RepositoryForms\Content\View\ContentEditView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use EzSystems\EzPlatformPageBuilder\PageBuilder;
use Symfony\Component\HttpFoundation\Session\Session;

class PageController extends Controller
Expand Down Expand Up @@ -50,13 +51,6 @@ public function previewAction(Request $request)
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param int $locationId
* @param int|null $versionNo
* @param string|null $siteaccessName
*
* @return \EzSystems\EzPlatformPageBuilder\View\PageView
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Expand All @@ -73,11 +67,6 @@ public function locationViewAction(
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $url
*
* @return \EzSystems\EzPlatformPageBuilder\View\PageView
*
* @throws \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @throws \Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException
* @throws \Symfony\Component\Translation\Exception\InvalidArgumentException
Expand All @@ -94,19 +83,16 @@ public function urlViewAction(Request $request, string $url): PageView
}

/**
* @param \EzSystems\RepositoryForms\Content\View\ContentEditView $view
*
* @return \EzSystems\RepositoryForms\Content\View\ContentEditView
*
* @throws \Exception
*/
public function editAction(Request $request, ContentEditView $view): ContentEditView
{
$language = $view->getLanguage();
$location = $view->getLocation();
$siteaccesses = $this->pageBuilderPermissionAwareConfigurationResolver->getSiteaccessList();
$currentSiteaccess = $this->session->get(EzPlatformPageBuilderExtension::SESSION_KEY_SITEACCESS, reset($siteaccesses));

$currentSiteaccess = $this->getAvailableSiteaccess($currentSiteaccess, $siteaccesses, $location);
$currentSiteaccess = $this->getAvailableSiteaccess($currentSiteaccess, $siteaccesses, $location, $language);

if (!$currentSiteaccess) {
throw new \RuntimeException('No SiteAccess available for this Page');
Expand All @@ -118,11 +104,6 @@ public function editAction(Request $request, ContentEditView $view): ContentEdit
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \EzSystems\RepositoryForms\Content\View\ContentCreateView $view
*
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateView
*
* @throws \Exception
*/
public function createAction(Request $request, ContentCreateView $view): ContentCreateView
Expand All @@ -142,13 +123,6 @@ public function createAction(Request $request, ContentCreateView $view): Content
return $this->pageController->createAction($request, $view);
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param int $locationId
* @param string|null $siteaccessName
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createDraftAction(
Request $request,
int $locationId,
Expand All @@ -158,11 +132,6 @@ public function createDraftAction(
}

/**
* @param \EzSystems\EzPlatformAdminUi\View\ContentTranslateView $view
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \EzSystems\EzPlatformAdminUi\View\ContentTranslateView
*
* @throws \Exception
*/
public function translateAction(ContentTranslateView $view, Request $request): ContentTranslateView
Expand All @@ -171,11 +140,6 @@ public function translateAction(ContentTranslateView $view, Request $request): C
}

/**
* @param string $siteaccess
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return boolean
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
Expand All @@ -188,24 +152,43 @@ private function isLocationInSiteaccessSubTree(string $siteaccess, Location $loc
: false;
}

private function isLanguageSuportedBySiteaccess(string $siteaccess, Language $language): bool
{
return \in_array($language->languageCode, $this->siteaccessService->getLanguages($siteaccess));
}

/**
* @param $currentSiteaccess
* @param $siteaccesses
* @param $location
*
* @return string|null
* @param string[] $siteaccesses
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function getAvailableSiteaccess($currentSiteaccess, $siteaccesses, $location): ?string
{
private function getAvailableSiteaccess(
string $currentSiteaccess,
array $siteaccesses,
Location $location,
Language $language
): ?string {
if (!$this->isLocationInSiteaccessSubTree($currentSiteaccess, $location)) {
$currentSiteaccess = null;

foreach ($siteaccesses as $availableSiteaccess) {
if ($this->isLocationInSiteaccessSubTree($availableSiteaccess, $location)) {
if (
$this->isLocationInSiteaccessSubTree($availableSiteaccess, $location)
) {
$currentSiteaccess = $availableSiteaccess;

break;
}
}
}

if (!$this->isLanguageSuportedBySiteaccess($currentSiteaccess, $language)) {
foreach ($siteaccesses as $availableSiteaccess) {
if (
$this->isLocationInSiteaccessSubTree($availableSiteaccess, $location) &&
$this->isLanguageSuportedBySiteaccess($availableSiteaccess, $language)
) {
$currentSiteaccess = $availableSiteaccess;

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace MateuszBieniek\EzPlatformPageBuilderMultisiteSiteaccessPickerBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class EzPlatformPageBuilderMultisiteSiteaccessPickerExtension extends Extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use eZ\Publish\API\Repository\Values\Content\Location;
use EzSystems\EzPlatformAdminUi\Menu\Event\ConfigureMenuEvent;
use EzSystems\EzPlatformPageBuilder\PageBuilder\ConfigurationResolverInterface;
use EzSystems\EzPlatformPageBuilder\Siteaccess\SiteaccessService;
use EzSystems\EzPlatformPageBuilderBundle\Menu\EventListener\ConfigureMainMenuListener as PageBuilderConfigureMainMenuListener;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use EzSystems\EzPlatformPageBuilderBundle\Menu\EventListener\ConfigureMainMenuListener as PageBuilderConfigureMainMenuListener;
use EzSystems\EzPlatformPageBuilder\Siteaccess\SiteaccessService;

class ConfigureMainMenuListener implements TranslationContainerInterface
{
Expand Down Expand Up @@ -45,16 +45,14 @@ public function __construct(
}

/**
* @param \EzSystems\EzPlatformAdminUi\Menu\Event\ConfigureMenuEvent $event
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function onMenuConfigure(ConfigureMenuEvent $event)
{
$event->stopPropagation();

if(!$this->overrideSiteaccessList) {
if (!$this->overrideSiteaccessList) {
return $this->configureMainMenuListener->onMenuConfigure($event);
}

Expand Down Expand Up @@ -122,11 +120,6 @@ public static function getTranslationMessages()
}

/**
* @param string $siteaccess
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return null|\eZ\Publish\API\Repository\Values\Content\Location
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
Expand Down

0 comments on commit 0671b4c

Please sign in to comment.