Skip to content

Commit

Permalink
added index pages to article
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Mar 24, 2017
1 parent 52fac7e commit 5c04212
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 58 deletions.
24 changes: 19 additions & 5 deletions Document/ArticlePageDocument.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
<?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;

use Sulu\Component\Content\Document\Behavior\StructureBehavior;
use Sulu\Component\Content\Document\Structure\Structure;
use Sulu\Component\Content\Document\Structure\StructureInterface;
use Sulu\Component\DocumentManager\Behavior\Mapping\LocalizedTitleBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\PathBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\LocalizedTitleBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior;
use Sulu\Component\DocumentManager\Behavior\Path\AutoNameBehavior;

/**
* Represents an article-page in phpcr.
*/
class ArticlePageDocument implements
UuidBehavior, LocalizedTitleBehavior, ParentBehavior, AutoNameBehavior, PathBehavior, StructureBehavior
UuidBehavior,
LocalizedTitleBehavior,
ParentBehavior,
AutoNameBehavior,
PathBehavior,
StructureBehavior
{
/**
* @var string
Expand Down Expand Up @@ -60,7 +74,7 @@ class ArticlePageDocument implements
/**
* @var int
*/
private $page;
private $pageNumber;

public function __construct()
{
Expand Down Expand Up @@ -196,8 +210,8 @@ public function getStructure()
*
* @return int
*/
public function getPage()
public function getPageNumber()
{
return $this->page;
return $this->pageNumber;
}
}
52 changes: 52 additions & 0 deletions Document/ArticlePageViewObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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;

use ONGR\ElasticsearchBundle\Annotation\Object;
use ONGR\ElasticsearchBundle\Annotation\Property;

/**
* Contains page information.
*
* @Object
*/
class ArticlePageViewObject
{
/**
* @var string
*
* @Property(type="string", options={"index"="not_analyzed"})
*/
public $uuid;

/**
* @var string
*
* @Property(
* type="string",
* options={
* "fields"={
* "raw"={"type"="string", "index"="not_analyzed"},
* "value"={"type"="string"}
* }
* }
* )
*/
public $title;

/**
* @var int
*
* @Property(type="integer", options={"index"="not_analyzed"})
*/
public $pageNumber;
}
28 changes: 25 additions & 3 deletions Document/ArticleViewDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,16 @@ class ArticleViewDocument implements ArticleViewDocumentInterface
*/
protected $changerContactId;

/**
* @var ArticlePageViewObject[]
*/
protected $pages = [];

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

Expand Down Expand Up @@ -682,4 +686,22 @@ public function getChangerContactId()
{
return $this->changerContactId;
}

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

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

return $this;
}
}
16 changes: 16 additions & 0 deletions Document/ArticleViewDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,20 @@ public function setChangerContactId($changerContactId);
* @return string
*/
public function getChangerContactId();

/**
* Returns pages.
*
* @return ArticlePageViewObject[]
*/
public function getPages();

/**
* Set pages.
*
* @param ArticlePageViewObject[] $pages
*
* @return $this
*/
public function setPages($pages);
}
2 changes: 0 additions & 2 deletions Document/Form/ArticlePageDocumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
namespace Sulu\Bundle\ArticleBundle\Document\Form;

use Sulu\Bundle\ContentBundle\Form\Type\AbstractStructureBehaviorType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down
79 changes: 59 additions & 20 deletions Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageViewObject;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\ExcerptFactory;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\SeoFactory;
Expand Down Expand Up @@ -151,9 +151,9 @@ private function getTypeTranslation($type)

/**
* @param ArticleDocument $document
* @param ArticleViewDocument $article
* @param ArticleViewDocumentInterface $article
*/
protected function dispatchIndexEvent(ArticleDocument $document, ArticleViewDocument $article)
protected function dispatchIndexEvent(ArticleDocument $document, ArticleViewDocumentInterface $article)
{
$this->eventDispatcher->dispatch(Events::INDEX_EVENT, new IndexEvent($document, $article));
}
Expand All @@ -170,23 +170,7 @@ protected function createOrUpdateArticle(
$locale,
$localizationState = LocalizationState::LOCALIZED
) {
$articleId = $this->getViewDocumentId($document->getUuid(), $locale);
/** @var ArticleViewDocument $article */
$article = $this->manager->find($this->documentFactory->getClass('article'), $articleId);

if (!$article) {
$article = $this->documentFactory->create('article');
$article->setId($articleId);
$article->setUuid($document->getUuid());
$article->setLocale($locale);
} else {
// Only index ghosts when the article isn't a ghost himself.
if (LocalizationState::GHOST === $localizationState
&& LocalizationState::GHOST !== $article->getLocalizationState()->state
) {
return null;
}
}
$article = $this->findOrCreateViewDocument($document, $locale, $localizationState);

$structureMetadata = $this->structureMetadataFactory->getStructureMetadata(
'article',
Expand Down Expand Up @@ -243,11 +227,66 @@ protected function createOrUpdateArticle(
}
}

$this->mapChildren($document, $article);

$this->manager->persist($article);

return $article;
}

/**
* Returns view-document from index or create a new one.
*
* @param ArticleDocument $document
* @param string $locale
* @param string $localizationState
*
* @return ArticleViewDocumentInterface
*/
protected function findOrCreateViewDocument(ArticleDocument $document, $locale, $localizationState)
{
$articleId = $this->getViewDocumentId($document->getUuid(), $locale);
/** @var ArticleViewDocumentInterface $article */
$article = $this->manager->find($this->documentFactory->getClass('article'), $articleId);

if ($article) {
// Only index ghosts when the article isn't a ghost himself.
if (LocalizationState::GHOST === $localizationState
&& LocalizationState::GHOST !== $article->getLocalizationState()->state
) {
return null;
}

return $article;
}

$article = $this->documentFactory->create('article');
$article->setId($articleId);
$article->setUuid($document->getUuid());
$article->setLocale($locale);

return $article;
}

/**
* Maps pages from document to view-document.
*
* @param ArticleDocument $document
* @param ArticleViewDocumentInterface $article
*/
private function mapChildren(ArticleDocument $document, ArticleViewDocumentInterface $article)
{
$pages = [];
foreach ($document->getChildren() as $child) {
$pages[] = $page = new ArticlePageViewObject();
$page->uuid = $child->getUuid();
$page->pageNumber = $child->getPageNumber();
$page->title = $child->getTitle();
}

$article->setPages($pages);
}

/**
* @param string $id
*/
Expand Down
6 changes: 0 additions & 6 deletions Document/Serializer/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\ContentTypeManagerInterface;

/**
* Extends serialization for article-pages.
Expand Down
16 changes: 13 additions & 3 deletions Document/Subscriber/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Indexes article and generate route on persist and removes it from index and routing on delete.
* Handle specialized article events.
*/
class ArticlePageSubscriber implements EventSubscriberInterface
{
Expand Down Expand Up @@ -77,23 +77,33 @@ public function setTitleOnPersist(PersistEvent $event)
$document->setTitle($pageTitle);
}

/**
* Set page-number to document on persist.
*
* @param PersistEvent $event
*/
public function setPageOnPersist(PersistEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticlePageDocument) {
return;
}

$event->getAccessor()->set('page', $event->getNode()->getIndex() + 1);
$event->getAccessor()->set('pageNumber', $event->getNode()->getIndex() + 1);
}

/**
* Set page-number to document on persist.
*
* @param HydrateEvent $event
*/
public function setPageOnHydrate(HydrateEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticlePageDocument) {
return;
}

$event->getAccessor()->set('page', $event->getNode()->getIndex() + 1);
$event->getAccessor()->set('pageNumber', $event->getNode()->getIndex() + 1);
}
}
Loading

0 comments on commit 5c04212

Please sign in to comment.