-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[framework] admin now can select the administration locale (#3577)
- Loading branch information
Showing
22 changed files
with
330 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Model/Administrator/AdministratorLocalizationFacade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.