Skip to content

Commit

Permalink
Implemented multilingual articles (#68)
Browse files Browse the repository at this point in the history
* Implemented multilingual articles

* Fixed style ci

* Fixed style ci

* Fixed comment

* Correct index

* Built and clean up

* Fixed tests

* Code clean up

* Added multilingual tests

* Fixed style CI

* Removed not used imports

* Increased memory to 2048

* Removed line break

* Fixed tab language switch bug

* Built js files

* Add comment to linked issue

* Upgrade notes for 0.0.2

* Code clean up

* Fixed bug with route path generation

* Fixed bug when switching language
  • Loading branch information
trickreich authored and wachterjohannes committed Feb 14, 2017
1 parent bcd4348 commit bec7610
Show file tree
Hide file tree
Showing 38 changed files with 851 additions and 182 deletions.
16 changes: 16 additions & 0 deletions Admin/ArticleContentNavigationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Sulu\Bundle\AdminBundle\Navigation\ContentNavigationItem;
use Sulu\Bundle\AdminBundle\Navigation\ContentNavigationProviderInterface;
use Sulu\Bundle\AdminBundle\Navigation\DisplayCondition;

/**
* Provides tabs for article-form.
Expand All @@ -35,20 +36,35 @@ public function getNavigationItems(array $options = [])
$seo->setAction('seo');
$seo->setComponent('articles/edit/seo@suluarticle');
$seo->setDisplay(['edit']);
$seo->setDisplayConditions(
[
new DisplayCondition('type', DisplayCondition::OPERATOR_EQUAL, null),
]
);

$excerpt = new ContentNavigationItem('content-navigation.contents.excerpt');
$excerpt->setId('excerpt');
$excerpt->setPosition(30);
$excerpt->setAction('excerpt');
$excerpt->setComponent('articles/edit/excerpt@suluarticle');
$excerpt->setDisplay(['edit']);
$excerpt->setDisplayConditions(
[
new DisplayCondition('type', DisplayCondition::OPERATOR_EQUAL, null),
]
);

$settings = new ContentNavigationItem('content-navigation.contents.settings');
$settings->setId('settings');
$settings->setPosition(40);
$settings->setAction('settings');
$settings->setComponent('articles/edit/settings@suluarticle');
$settings->setDisplay(['edit']);
$settings->setDisplayConditions(
[
new DisplayCondition('type', DisplayCondition::OPERATOR_EQUAL, null),
]
);

return [$details, $seo, $excerpt, $settings];
}
Expand Down
16 changes: 13 additions & 3 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Component\Content\Form\Exception\InvalidFormException;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\Rest\Exception\MissingParameterException;
Expand All @@ -40,6 +41,7 @@ class ArticleController extends RestController implements ClassResourceInterface
const DOCUMENT_TYPE = 'article';

use RequestParametersTrait;
use ArticleViewDocumentIdTrait;

/**
* Create field-descriptor array.
Expand All @@ -49,7 +51,7 @@ class ArticleController extends RestController implements ClassResourceInterface
private function getFieldDescriptors()
{
return [
'id' => new FieldDescriptor('id', 'public.id', true),
'uuid' => new FieldDescriptor('uuid', 'public.id', true),
'typeTranslation' => new FieldDescriptor(
'typeTranslation',
'sulu_article.list.type',
Expand Down Expand Up @@ -86,6 +88,8 @@ public function cgetFieldsAction()
*/
public function cgetAction(Request $request)
{
$locale = $this->getRequestParameter($request, 'locale', true);

$restHelper = $this->get('sulu_core.list_rest_helper');

/** @var Manager $manager */
Expand All @@ -96,8 +100,12 @@ public function cgetAction(Request $request)
$limit = (int) $restHelper->getLimit();
$page = (int) $restHelper->getPage();

if (null !== $locale) {
$search->addQuery(new TermQuery('locale', $locale));
}

if (count($ids = array_filter(explode(',', $request->get('ids', ''))))) {
$search->addQuery(new IdsQuery($ids));
$search->addQuery(new IdsQuery($this->getViewDocumentIds($ids, $locale)));
$limit = count($ids);
}

Expand Down Expand Up @@ -170,7 +178,7 @@ public function getAction($uuid, Request $request)
$uuid,
$locale,
[
'load_ghost_content' => false,
'load_ghost_content' => true,
'load_shadow_content' => false,
]
);
Expand Down Expand Up @@ -199,6 +207,7 @@ public function postAction(Request $request)
$document->setAuthored(new \DateTime());
if (array_key_exists('authored', $data)) {
$document->setAuthored(new \DateTime($data['authored']));
unset($data['authored']);
}
$document->setAuthors($this->getAuthors($data));

Expand Down Expand Up @@ -240,6 +249,7 @@ public function putAction(Request $request, $uuid)

if (array_key_exists('authored', $data)) {
$document->setAuthored(new \DateTime($data['authored']));
unset($data['authored']);
}
$document->setAuthors($this->getAuthors($data));

Expand Down
59 changes: 55 additions & 4 deletions Document/ArticleViewDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ class ArticleViewDocument implements ArticleViewDocumentInterface
*
* @Id
*/
protected $id;

/**
* @var string
*
* @Property(type="string", options={"analyzer": "keyword"})
*/
protected $uuid;

/**
* @var string
*
* @Property(type="string")
* @Property(type="string", options={"analyzer": "keyword"})
*/
protected $locale;

Expand Down Expand Up @@ -203,14 +210,40 @@ class ArticleViewDocument implements ArticleViewDocumentInterface
*/
protected $publishedState;

/**
* @var LocalizationStateViewObject
*
* @Embedded(class="SuluArticleBundle:LocalizationStateViewObject")
*/
protected $localizationState;

/**
* @param string $uuid
*/
public function __construct($uuid = null)
{
public function __construct(
$uuid = null
) {
$this->uuid = $uuid;
}

/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}

/**
* {@inheritdoc}
*/
public function setId($id)
{
$this->id = $id;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -436,7 +469,7 @@ public function getSeo()
/**
* {@inheritdoc}
*/
public function setSeo($seo)
public function setSeo(SeoViewObject $seo)
{
$this->seo = $seo;

Expand Down Expand Up @@ -548,4 +581,22 @@ public function setPublishedState($publishedState)

return $this;
}

/**
* {@inheritdoc}
*/
public function getLocalizationState()
{
return $this->localizationState;
}

/**
* {@inheritdoc}
*/
public function setLocalizationState(LocalizationStateViewObject $localizationState)
{
$this->localizationState = $localizationState;

return $this;
}
}
34 changes: 33 additions & 1 deletion Document/ArticleViewDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@
*/
interface ArticleViewDocumentInterface
{
/**
* Returns id.
*
* @return string
*/
public function getId();

/**
* Set id.
*
* @param string $id
*
* @return $this
*/
public function setId($id);

/**
* Returns uuid.
*
Expand Down Expand Up @@ -222,7 +238,7 @@ public function getSeo();
*
* @return $this
*/
public function setSeo($seo);
public function setSeo(SeoViewObject $seo);

/**
* Returns authored.
Expand Down Expand Up @@ -319,4 +335,20 @@ public function getPublishedState();
* @return $this
*/
public function setPublishedState($publishedState);

/**
* Get localization state.
*
* @return LocalizationStateViewObject
*/
public function getLocalizationState();

/**
* Set localization state.
*
* @param LocalizationStateViewObject $localizationState
*
* @return $this
*/
public function setLocalizationState(LocalizationStateViewObject $localizationState);
}
130 changes: 130 additions & 0 deletions Document/Index/ArticleGhostIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Document\Index;

use ONGR\ElasticsearchBundle\Service\Manager;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\ExcerptFactory;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\SeoFactory;
use Sulu\Bundle\SecurityBundle\UserManager\UserManager;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Provides methods to index articles.
*/
class ArticleGhostIndexer extends ArticleIndexer
{
/**
* @var WebspaceManagerInterface
*/
private $webspaceManager;

/**
* @var DocumentManagerInterface
*/
private $documentManager;

/**
* @param StructureMetadataFactoryInterface $structureMetadataFactory
* @param UserManager $userManager
* @param DocumentFactoryInterface $documentFactory
* @param Manager $manager
* @param ExcerptFactory $excerptFactory
* @param SeoFactory $seoFactory
* @param EventDispatcherInterface $eventDispatcher
* @param TranslatorInterface $translator
* @param array $typeConfiguration
* @param WebspaceManagerInterface $webspaceManager
* @param DocumentManagerInterface $documentManager
*/
public function __construct(
StructureMetadataFactoryInterface $structureMetadataFactory,
UserManager $userManager,
DocumentFactoryInterface $documentFactory,
Manager $manager,
ExcerptFactory $excerptFactory,
SeoFactory $seoFactory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator,
array $typeConfiguration,
WebspaceManagerInterface $webspaceManager,
DocumentManagerInterface $documentManager
) {
parent::__construct(
$structureMetadataFactory,
$userManager,
$documentFactory,
$manager,
$excerptFactory,
$seoFactory,
$eventDispatcher,
$translator,
$typeConfiguration
);

$this->webspaceManager = $webspaceManager;
$this->documentManager = $documentManager;
}

/**
* {@inheritdoc}
*/
public function index(ArticleDocument $document)
{
$article = $this->createOrUpdateArticle($document, $document->getLocale());
$this->createOrUpdateGhosts($document);
$this->dispatchIndexEvent($document, $article);
}

/**
* {@inheritdoc}
*/
public function remove($document)
{
foreach ($this->webspaceManager->getAllLocalizations() as $localization) {
$articleId = $this->getArticleId($document->getUuid(), $localization->getLocale());
$this->removeArticle($articleId);
}
}

/**
* @param ArticleDocument $document
*/
private function createOrUpdateGhosts(ArticleDocument $document)
{
$documentLocale = $document->getLocale();
/** @var Localization $localization */
foreach ($this->webspaceManager->getAllLocalizations() as $localization) {
$locale = $localization->getLocale();
if ($documentLocale !== $locale) {
// Try index the article ghosts.
$this->createOrUpdateArticle(
$this->documentManager->find(
$document->getUuid(),
$locale,
[
'load_ghost_content' => true,
]
),
$localization->getLocale(),
LocalizationState::GHOST
);
}
}
}
}
Loading

0 comments on commit bec7610

Please sign in to comment.