Skip to content

Commit

Permalink
init article types
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jul 11, 2016
1 parent 68ae0c1 commit 2d29ab7
Show file tree
Hide file tree
Showing 26 changed files with 560 additions and 48 deletions.
1 change: 1 addition & 0 deletions Admin/ArticleAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
class ArticleAdmin extends Admin
{
const STRUCTURE_TAG_TYPE = 'sulu.type';
const SECURITY_CONTEXT = 'sulu.modules.articles';

/**
Expand Down
61 changes: 61 additions & 0 deletions Admin/ArticleJsConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Admin;

use Sulu\Bundle\AdminBundle\Admin\JsConfigInterface;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;

/**
* Provides js-configuration.
*/
class ArticleJsConfig implements JsConfigInterface
{
use TypeTrait;

/**
* @var StructureManagerInterface
*/
private $structureManager;

/**
* @param StructureManagerInterface $structureManager
*/
public function __construct(StructureManagerInterface $structureManager)
{
$this->structureManager = $structureManager;
}

/**
* {@inheritdoc}
*/
public function getParameters()
{
$types = [];
foreach ($this->structureManager->getStructures('article') as $structure) {
$type = $this->getType($structure->getStructure());
if (!array_key_exists($type, $types)) {
$types[$type] = $structure->getKey();
}
}

return $types;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'sulu_article.types';
}
}
6 changes: 3 additions & 3 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use JMS\Serializer\SerializationContext;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\FuzzyQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use Sulu\Bundle\ArticleBundle\Document\ElasticSearchArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
Expand Down Expand Up @@ -88,10 +88,10 @@ public function cgetAction(Request $request)
foreach ($restHelper->getSearchFields() as $searchField) {
$search->addQuery(new FuzzyQuery($searchField, $searchPattern));
}
} else {
$search->addQuery(new MatchAllQuery());
}

$search->addQuery(new TermQuery('type', $request->get('type', 'default')));

$count = $repository->count($search);

if (null !== $restHelper->getSortColumn()) {
Expand Down
58 changes: 58 additions & 0 deletions Controller/TemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\Controller;

use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
* Manages template for articles.
*/
class TemplateController extends Controller
{
use TypeTrait;

/**
* Returns template for given article type.
*
* @param Request $request
*
* @return JsonResponse
*/
public function getAction(Request $request)
{
$structureProvider = $this->get('sulu.content.structure_manager');
$type = $request->get('type', 'default');

$templates = [];
foreach ($structureProvider->getStructures('article') as $structure) {
if ($this->getType($structure->getStructure()) !== $type) {
continue;
}

$templates[] = [
'internal' => $structure->getInternal(),
'template' => $structure->getKey(),
'title' => $structure->getLocalizedTitle($this->getUser()->getLocale()),
];
}

return new JsonResponse(
[
'_embedded' => $templates,
'total' => count($templates),
]
);
}
}
31 changes: 31 additions & 0 deletions Document/ElasticSearchArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class ElasticSearchArticleDocument
*/
protected $title;

/**
* @var string
*
* @Property(type="string")
*/
protected $type;

/**
* @var string
*
Expand Down Expand Up @@ -149,6 +156,30 @@ public function setTitle($title)
return $this;
}

/**
* Returns type.
*
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* Set type.
*
* @param string $type
*
* @return self
*/
public function setType($type)
{
$this->type = $type;

return $this;
}

/**
* Returns changer.
*
Expand Down
16 changes: 15 additions & 1 deletion Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@
use ONGR\ElasticsearchBundle\Service\Manager;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ElasticSearchArticleDocument;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Bundle\SecurityBundle\UserManager\UserManager;
use Sulu\Component\Content\Compat\StructureManagerInterface;

/**
* Provides methods to index articles.
*/
class ArticleIndexer implements IndexerInterface
{
use TypeTrait;

/**
* @var StructureManagerInterface
*/
private $structureManager;

/**
* @var UserManager
*/
Expand All @@ -32,11 +41,13 @@ class ArticleIndexer implements IndexerInterface
private $manager;

/**
* @param StructureManagerInterface $structureManager
* @param UserManager $userManager
* @param Manager $manager
*/
public function __construct(UserManager $userManager, Manager $manager)
public function __construct(StructureManagerInterface $structureManager, UserManager $userManager, Manager $manager)
{
$this->structureManager = $structureManager;
$this->userManager = $userManager;
$this->manager = $manager;
}
Expand All @@ -59,11 +70,14 @@ public function index(ArticleDocument $document)
$article = new ElasticSearchArticleDocument($document->getUuid());
}

$structure = $this->structureManager->getStructure($document->getStructureType(), 'article');

$article->setTitle($document->getTitle());
$article->setChanged($document->getChanged());
$article->setCreated($document->getCreated());
$article->setChanger($this->userManager->getFullNameByUserId($document->getChanger()));
$article->setCreator($this->userManager->getFullNameByUserId($document->getCreator()));
$article->setType($this->getType($structure->getStructure()));

$this->manager->persist($article);
}
Expand Down
73 changes: 73 additions & 0 deletions Document/Serializer/TypeSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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\Serializer;

use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;

/**
* Appends type to article-document.
*/
class TypeSubscriber implements EventSubscriberInterface
{
use TypeTrait;

/**
* @var StructureManagerInterface
*/
private $structureManager;

/**
* @param StructureManagerInterface $structureManager
*/
public function __construct(StructureManagerInterface $structureManager)
{
$this->structureManager = $structureManager;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
[
'event' => Events::POST_SERIALIZE,
'format' => 'json',
'method' => 'onPostSerialize',
],
];
}

/**
* Append type to result.
*
* @param ObjectEvent $event
*/
public function onPostSerialize(ObjectEvent $event)
{
$article = $event->getObject();
$visitor = $event->getVisitor();
$context = $event->getContext();

if (!($article instanceof ArticleDocument)) {
return;
}

$structure = $this->structureManager->getStructure($article->getStructureType(), 'article');
$visitor->addData('type', $context->accept($this->getType($structure->getStructure())));
}
}
10 changes: 10 additions & 0 deletions Resources/config/routing.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="sulu_article.templates" path="/templates">
<default key="_controller">SuluArticleBundle:Template:get</default>
</route>

</routes>
13 changes: 13 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
<tag name="sulu_admin.content_navigation" alias="article"/>
<tag name="sulu.context" context="admin"/>
</service>
<service id="sulu_article.js_config" class="Sulu\Bundle\ArticleBundle\Admin\ArticleJsConfig">
<argument type="service" id="sulu.content.structure_manager"/>

<tag name="sulu.js_config"/>
<tag name="sulu.context" context="admin"/>
</service>

<!-- elastic-search -->
<service id="sulu_article.elastic_search.article_indexer"
class="Sulu\Bundle\ArticleBundle\Document\Index\ArticleIndexer">
<argument type="service" id="sulu.content.structure_manager"/>
<argument type="service" id="sulu_security.user_manager"/>
<argument type="service" id="es.manager"/>
</service>
Expand Down Expand Up @@ -47,5 +54,11 @@
<!-- This needs to happen after the content repository has been initialized !-->
<tag name="sulu_document_manager.initializer" priority="-127"/>
</service>
<service id="sulu_article.serializer.type" class="Sulu\Bundle\ArticleBundle\Document\Serializer\TypeSubscriber">
<argument type="service" id="sulu.content.structure_manager"/>

<tag name="jms_serializer.event_subscriber" />
<tag name="sulu.context" context="admin"/>
</service>
</services>
</container>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d29ab7

Please sign in to comment.