Skip to content

Commit

Permalink
[framework] admin now can select the administration locale (#3577)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav authored Nov 7, 2024
2 parents d9f8f9d + 2805512 commit 394e656
Show file tree
Hide file tree
Showing 22 changed files with 330 additions and 58 deletions.
10 changes: 0 additions & 10 deletions src/Component/Domain/DomainFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,20 @@ class DomainFacade
{
/**
* @param string $domainImagesDirectory
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Component\Domain\DomainIconProcessor $domainIconProcessor
* @param \League\Flysystem\FilesystemOperator $filesystem
* @param \Shopsys\FrameworkBundle\Component\FileUpload\FileUpload $fileUpload
* @param \Shopsys\FrameworkBundle\Component\Image\Processing\ImageProcessor $imageProcessor
*/
public function __construct(
protected readonly string $domainImagesDirectory,
protected readonly Domain $domain,
protected readonly DomainIconProcessor $domainIconProcessor,
protected readonly FilesystemOperator $filesystem,
protected readonly FileUpload $fileUpload,
protected readonly ImageProcessor $imageProcessor,
) {
}

/**
* @return \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig[]
*/
public function getAllDomainConfigs(): array
{
return $this->domain->getAll();
}

/**
* @param int $domainId
* @param string $iconName
Expand Down
50 changes: 50 additions & 0 deletions src/Controller/Admin/LocalizationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Controller\Admin;

use Shopsys\FrameworkBundle\Component\Translation\Translator;
use Shopsys\FrameworkBundle\Model\Administrator\AdministratorLocalizationFacade;
use Shopsys\FrameworkBundle\Model\Localization\Exception\AdminLocaleNotFoundException;
use Shopsys\FrameworkBundle\Model\Localization\Localization;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LocalizationController extends AdminBaseController
{
/**
* @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorLocalizationFacade $administratorLocalizationFacade
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
*/
public function __construct(
protected readonly AdministratorLocalizationFacade $administratorLocalizationFacade,
protected readonly Localization $localization,
) {
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $locale
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route(path: '/administrator/select-locale/{locale}')]
public function selectLocaleAction(Request $request, string $locale): Response
{
$redirectUrl = $request->headers->get('referer', $this->generateUrl('admin_default_dashboard'));

try {
$administrator = $this->getCurrentAdministrator();
$this->administratorLocalizationFacade->setSelectedLocale($administrator, $locale);
$this->addSuccessFlash(t('Administration localization was changed to "%locale%"', ['%locale%' => $this->localization->getLanguageName($locale, $locale)], Translator::DEFAULT_TRANSLATION_DOMAIN, $locale));
} catch (AdminLocaleNotFoundException $exception) {
$this->addErrorFlash(t('Locale "%locale%" is not supported. You can choose only from the following locales: "%supportedLocales%".', [
'%locale%' => $locale,
'%supportedLocales%' => implode('", "', $exception->getPossibleLocales()),
]));
}

return $this->redirect($redirectUrl);
}
}
21 changes: 15 additions & 6 deletions src/Controller/Admin/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@

namespace Shopsys\FrameworkBundle\Controller\Admin;

use Shopsys\FrameworkBundle\Component\Domain\DomainFacade;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Localization\Localization;
use Symfony\Component\HttpFoundation\Response;

class MenuController extends AdminBaseController
{
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\DomainFacade $domainFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
*/
public function __construct(protected readonly DomainFacade $domainFacade)
{
public function __construct(
protected readonly Domain $domain,
protected readonly Localization $localization,
) {
}

public function menuAction()
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function menuAction(): Response
{
return $this->render('@ShopsysFramework/Admin/Inline/Menu/menu.html.twig', [
'domainConfigs' => $this->domainFacade->getAllDomainConfigs(),
'domainConfigs' => $this->domain->getAll(),
'allowedLocales' => $this->localization->getAllowedAdminLocales(),
]);
}
}
7 changes: 5 additions & 2 deletions src/Form/Admin/Blog/BlogCategoryFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory;
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategoryData;
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategoryFacade;
use Shopsys\FrameworkBundle\Model\Localization\Localization;
use Shopsys\FrameworkBundle\Model\Seo\SeoSettingFacade;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -34,11 +35,13 @@ class BlogCategoryFormType extends AbstractType
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategoryFacade $blogCategoryFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Seo\SeoSettingFacade $seoSettingFacade
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
*/
public function __construct(
protected readonly BlogCategoryFacade $blogCategoryFacade,
protected readonly Domain $domain,
protected readonly SeoSettingFacade $seoSettingFacade,
protected readonly Localization $localization,
) {
}

Expand Down Expand Up @@ -98,9 +101,9 @@ private function getCategoryNameForPlaceholder(
private function createSettingsGroup(FormBuilderInterface $builder, array $options): FormBuilderInterface
{
if ($options['blogCategory'] !== null) {
$parentChoices = $this->blogCategoryFacade->getTranslatedAllWithoutBranch($options['blogCategory'], $this->domain->getCurrentDomainConfig());
$parentChoices = $this->blogCategoryFacade->getTranslatedAllWithoutBranch($options['blogCategory'], $this->localization->getAdminLocale());
} else {
$parentChoices = $this->blogCategoryFacade->getTranslatedAll($this->domain->getLocale());
$parentChoices = $this->blogCategoryFacade->getTranslatedAll($this->localization->getAdminLocale());
}

$builderSettingsGroup = $builder->create('settings', GroupType::class, [
Expand Down
51 changes: 51 additions & 0 deletions src/Migrations/Version20241106123834.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\FrameworkBundle\Model\Localization\Exception\AdminLocaleNotFoundException;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Version20241106123834 extends AbstractMigration implements ContainerAwareInterface
{
use MultidomainMigrationTrait;

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$allowedAdminLocales = $this->container->getParameter('shopsys.allowed_admin_locales');
$defaultLocale = reset($allowedAdminLocales);

if ($defaultLocale === false) {
throw new AdminLocaleNotFoundException();
}

if (!in_array($defaultLocale, $this->getAllLocales(), true)) {
throw new AdminLocaleNotFoundException($defaultLocale, $this->getAllLocales());
}

$this->sql(sprintf('ALTER TABLE administrators ADD selected_locale VARCHAR(10) NOT NULL DEFAULT \'%s\'', $defaultLocale));
$this->sql('ALTER TABLE administrators ALTER selected_locale DROP DEFAULT');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}

/**
* @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container
*/
public function setContainer(?ContainerInterface $container = null): void
{
$this->container = $container;
}
}
23 changes: 23 additions & 0 deletions src/Model/Administrator/Administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ class Administrator implements UserInterface, UniqueLoginInterface, TimelimitLog
*/
protected $displayOnlyDomainIds;

/**
* @var string
* @ORM\Column(type="string", length=10)
*/
protected $selectedLocale;

/**
* @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorData $administratorData
*/
Expand All @@ -182,6 +188,7 @@ public function __construct(AdministratorData $administratorData)
$this->transferIssuesLastSeenDateTime = $administratorData->transferIssuesLastSeenDateTime;
$this->uuid = Uuid::uuid4()->toString();
$this->displayOnlyDomainIds = [];
$this->selectedLocale = $administratorData->selectedLocale;
$this->setData($administratorData);
}

Expand Down Expand Up @@ -642,4 +649,20 @@ public function getDisplayOnlyDomainIds()
{
return array_map('intval', $this->displayOnlyDomainIds);
}

/**
* @param string $selectedLocale
*/
public function setSelectedLocale($selectedLocale)
{
$this->selectedLocale = $selectedLocale;
}

/**
* @return string
*/
public function getSelectedLocale()
{
return $this->selectedLocale;
}
}
5 changes: 5 additions & 0 deletions src/Model/Administrator/AdministratorData.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class AdministratorData
*/
public $displayOnlyDomainIds;

/**
* @var string|null
*/
public $selectedLocale;

public function __construct()
{
$this->roles[] = Roles::ROLE_ADMIN;
Expand Down
4 changes: 4 additions & 0 deletions src/Model/Administrator/AdministratorDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace Shopsys\FrameworkBundle\Model\Administrator;

use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Localization\Localization;

class AdministratorDataFactory implements AdministratorDataFactoryInterface
{
/**
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
*/
public function __construct(
protected readonly Localization $localization,
protected readonly Domain $domain,
) {
}
Expand All @@ -32,6 +35,7 @@ public function create(): AdministratorData
$administratorData = $this->createInstance();

$administratorData->displayOnlyDomainIds = $this->domain->getAllIds();
$administratorData->selectedLocale = $this->localization->getDefaultAdminLocale();

return $administratorData;
}
Expand Down
33 changes: 33 additions & 0 deletions src/Model/Administrator/AdministratorLocalizationFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Administrator;

use Doctrine\ORM\EntityManagerInterface;
use Shopsys\FrameworkBundle\Model\Localization\Localization;

class AdministratorLocalizationFacade
{
/**
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
* @param \Doctrine\ORM\EntityManagerInterface $em
*/
public function __construct(
protected readonly Localization $localization,
protected readonly EntityManagerInterface $em,
) {
}

/**
* @param \Shopsys\FrameworkBundle\Model\Administrator\Administrator $administrator
* @param string $locale
*/
public function setSelectedLocale(Administrator $administrator, string $locale): void
{
$this->localization->checkAdminLocaleIsSupported($locale);
$administrator->setSelectedLocale($locale);

$this->em->flush();
}
}
7 changes: 3 additions & 4 deletions src/Model/Blog/Category/BlogCategoryFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Shopsys\FrameworkBundle\Model\Blog\Category;

use Doctrine\ORM\EntityManagerInterface;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Image\ImageFacade;
use Shopsys\FrameworkBundle\Component\Redis\CleanStorefrontCacheFacade;
Expand Down Expand Up @@ -200,12 +199,12 @@ public function getAllVisibleChildrenByBlogCategoryAndDomainId(BlogCategory $cat

/**
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory $blogCategory
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
* @param string $locale
* @return \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory[]
*/
public function getTranslatedAllWithoutBranch(BlogCategory $blogCategory, DomainConfig $domainConfig): array
public function getTranslatedAllWithoutBranch(BlogCategory $blogCategory, string $locale): array
{
return $this->blogCategoryRepository->getTranslatedAllWithoutBranch($blogCategory, $domainConfig);
return $this->blogCategoryRepository->getTranslatedAllWithoutBranch($blogCategory, $locale);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Model/Blog/Category/BlogCategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Blog\Article\BlogArticle;
use Shopsys\FrameworkBundle\Model\Blog\Article\BlogArticleBlogCategoryDomain;
Expand Down Expand Up @@ -88,13 +87,13 @@ public function getRootBlogCategory(): BlogCategory

/**
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory $blogCategoryBranch
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
* @param string $locale
* @return \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory[]
*/
public function getTranslatedAllWithoutBranch(BlogCategory $blogCategoryBranch, DomainConfig $domainConfig): array
public function getTranslatedAllWithoutBranch(BlogCategory $blogCategoryBranch, string $locale): array
{
$queryBuilder = $this->getAllQueryBuilder();
$this->addTranslation($queryBuilder, $domainConfig->getLocale());
$this->addTranslation($queryBuilder, $locale);

return $queryBuilder->andWhere('bc.lft < :branchLft OR bc.rgt > :branchRgt')
->setParameter('branchLft', $blogCategoryBranch->getLft())
Expand Down
Loading

0 comments on commit 394e656

Please sign in to comment.