Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat support ezplatformrichtext #27

Open
wants to merge 12 commits into
base: 1.0
Choose a base branch
from
Open
14 changes: 14 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return EzSystems\EzPlatformCodeStyle\PhpCsFixer\EzPlatformInternalConfigFactory::build()
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude([
'.cs',
'.platform',
'vendor',
])
->files()->name('*.php')
)
;
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ cache:
- vendor

php:
- 7.1
- 7.2

before_install:
Expand All @@ -19,7 +18,6 @@ install:
- composer info -i

script:
- php vendor/bin/phpcs --standard=.cs/cs_ruleset.xml --extensions=php bundle/
- php vendor/bin/phpcs --standard=.cs/cs_ruleset.xml --extensions=php lib/
- php vendor/bin/phpcs --standard=.cs/cs_ruleset.xml --extensions=php tests/
- php vendor/bin/php-cs-fixer fix -v --dry-run --show-progress=estimating
- php vendor/bin/phpunit

80 changes: 39 additions & 41 deletions bundle/Command/TranslateContentCommand.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
<?php

/**
* eZ Automated Translation Bundle.
*
* @package EzSystems\eZAutomatedTranslationBundle
*
* @author Novactive <[email protected]>
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAutomatedTranslationBundle\Command;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\UserService;
use EzSystems\EzPlatformAutomatedTranslation\ClientProvider;
use EzSystems\EzPlatformAutomatedTranslation\RepositoryAware;
use EzSystems\EzPlatformAutomatedTranslation\Translator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class TranslateContentCommand.
*/
class TranslateContentCommand extends Command
final class TranslateContentCommand extends Command
{
use RepositoryAware;
/**
* @var Translator
*/
private const ADMINISTRATOR_USER_ID = 14;

/** @var Translator */
private $translator;

/**
* @var ClientProvider
*/
/** @var ClientProvider */
private $clientProvider;

/**
* TranslateContentCommand constructor.
*
* @param Translator $translator
*/
public function __construct(Translator $translator, ClientProvider $clientProvider)
{
/** @var \eZ\Publish\API\Repository\ContentService */
private $contentService;

/** @var \eZ\Publish\API\Repository\PermissionResolver */
private $permissionResolver;

/** @var \eZ\Publish\API\Repository\UserService */
private $userService;

public function __construct(
Translator $translator,
ClientProvider $clientProvider,
ContentService $contentService,
PermissionResolver $permissionResolver,
UserService $userService
) {
$this->clientProvider = $clientProvider;
parent::__construct();
$this->translator = $translator;
$this->contentService = $contentService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
Expand All @@ -69,31 +71,27 @@ protected function configure(): void
->addOption('to', '--to', InputOption::VALUE_REQUIRED, 'Target Language');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$contentId = (int) $input->getArgument('contentId');
$content = $this->repository->getContentService()->loadContent($contentId);
$draft = $this->translator->getTranslatedContent(
$content = $this->contentService->loadContent($contentId);
$draft = $this->translator->getTranslatedContent(
$input->getOption('from'),
$input->getOption('to'),
$input->getArgument('service'),
$content
);
$this->repository->getContentService()->publishVersion($draft->versionInfo);
$this->contentService->publishVersion($draft->versionInfo);
$output->writeln("Translation to {$contentId} Done.");

return Command::SUCCESS;
}

/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
$this->repository->getPermissionResolver()->setCurrentUserReference(
$this->repository->getUserService()->loadUser(14)
$this->permissionResolver->setCurrentUserReference(
$this->userService->loadUser(self::ADMINISTRATOR_USER_ID)
);
}
}
109 changes: 92 additions & 17 deletions bundle/Controller/TranslationController.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,133 @@
<?php

/**
* eZ Automated Translation Bundle.
*
* @package EzSystems\eZAutomatedTranslationBundle
*
* @author Novactive <[email protected]>
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAutomatedTranslationBundle\Controller;

use EzSystems\EzPlatformAdminUiBundle\Controller\TranslationController as BaseTranslationController;
use EzSystems\EzPlatformAdminUiBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouterInterface;

/**
* Class TranslationController.
*/
class TranslationController extends BaseTranslationController
{
/**
* {@inheritdoc}
*/
// /** @var \EzSystems\EzPlatformAdminUiBundle\Controller\TranslationController */
// private $translationController;
//
// public function __construct(BaseTranslationController $translationController)
// {
// $this->translationController = $translationController;
// }

public function addAction(Request $request): Response
{
$response = parent::addAction($request);

if (!$response instanceof RedirectResponse) {
return $response;
}

$targetUrl = $response->getTargetUrl();
$pattern = str_replace(
$targetUrl = $response->getTargetUrl();
$contentTranslatePattern = str_replace(
'/',
'\/?',
urldecode(
$this->generateUrl(
'ezplatform.content.translate',
[
'contentId' => '([0-9]*)',
'contentId' => '([0-9]*)',
'fromLanguageCode' => '([a-zA-Z-]*)',
'toLanguageCode' => '([a-zA-Z-]*)',
'toLanguageCode' => '([a-zA-Z-]*)',
]
)
)
);


try {
// admin-ui v3.3.6 introduces different route `ibexa.content.translate_with_location.proxy`
// when translated content is created.
$contentTranslateWithLocationPattern = str_replace(
'/',
'\/?',
urldecode(
$this->generateUrl(
'ibexa.content.translate_with_location.proxy',
[
'contentId' => '([0-9]*)',
'fromLanguageCode' => '([a-zA-Z-]*)',
'toLanguageCode' => '([a-zA-Z-]*)',
'locationId' => '([0-9]*)',
]
)
)
);
} catch (\Symfony\Component\Routing\Exception\RouteNotFoundException $exception) {
$contentTranslateWithLocationPattern = 'NOP';
}

try {
$contentTranslateWithLocationPatternWithOutProxy = str_replace(
'/',
'\/?',
urldecode(
$this->generateUrl(
// path: /content/{contentId}/location/{locationId}/translate/{toLanguageCode}/{fromLanguageCode}
'ibexa.content.translate_with_location',
[
'contentId' => '([0-9]*)',
'locationId' => '([0-9]*)',
'fromLanguageCode' => '([a-zA-Z-]*)',
'toLanguageCode' => '([a-zA-Z-]*)',
]
)
)
);
} catch (\Symfony\Component\Routing\Exception\RouteNotFoundException $exception) {
$contentTranslateWithLocationPatternWithOutProxy = 'NOP';
}

$serviceAlias = $request->request->get('add-translation')['translatorAlias'] ?? '';
if (1 !== preg_match("#{$pattern}#", $targetUrl) || '' === $serviceAlias) {

// dump([
// '$targetUrl' => $targetUrl,
// '$serviceAlias' => $serviceAlias,
// '$contentTranslatePattern' => $contentTranslatePattern,
// '$contentTranslateWithLocationPattern' => $contentTranslateWithLocationPattern,
// '$contentTranslateWithLocationPatternWithOutProxy' => $contentTranslateWithLocationPatternWithOutProxy,
// ]);

if ('' === $serviceAlias || (
!$this->targetUrlContainsPattern($targetUrl, $contentTranslatePattern) &&
!$this->targetUrlContainsPattern($targetUrl, $contentTranslateWithLocationPattern) &&
!$this->targetUrlContainsPattern($targetUrl, $contentTranslateWithLocationPatternWithOutProxy)
)) {
// dump(__LINE__);
// die(__METHOD__);
return $response;
}

$response->setTargetUrl(sprintf('%s?translatorAlias=%s', $targetUrl, $serviceAlias));

// dump(__LINE__);
// die(__METHOD__);
return $response;
}

public function removeAction(Request $request): Response
{
return parent::removeAction($request);
}

private function targetUrlContainsPattern(string $targetUrl, string $pattern): bool
{
return 1 === preg_match("#{$pattern}#", $targetUrl);
}
}
21 changes: 6 additions & 15 deletions bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?php

/**
* eZ Platform Automated Translation Bundle.
*
* @package EzSystems\EzPlatformAutomatedTranslationBundle
*
* @author Novactive <[email protected]>
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

Expand All @@ -15,23 +11,18 @@
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

/**
* Class Configuration.
*/
class Configuration extends SiteAccessAware\Configuration
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('ez_platform_automated_translation');
$systemNode = $this->generateScopeBaseNode($rootNode);
$systemNode = $this->generateScopeBaseNode($rootNode);
$systemNode
->variableNode('configurations')->end()
->arrayNode('nontranslatablecharacters')->end()
->arrayNode('nontranslatabletags')->end();
->arrayNode('non_translatable_characters')->end()
->arrayNode('non_translatable_tags')->end()
->arrayNode('non_translatable_self_closed_tags')->end();

return $treeBuilder;
}
Expand Down
Loading