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

Add article types #2

Merged
merged 3 commits into from
Jul 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}
44 changes: 41 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\ArticleOngrDocument;
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 Expand Up @@ -205,6 +205,44 @@ public function putAction(Request $request, $uuid)
);
}

/**
* Deletes multiple documents.
*
* @param Request $request
*
* @return Response
*/
public function cdeleteAction(Request $request)
{
$ids = array_filter(explode(',', $request->get('ids', '')));

$documentManager = $this->getDocumentManager();
foreach ($ids as $id) {
$document = $documentManager->find($id);
$documentManager->remove($document);
}
$documentManager->flush();

return $this->handleView($this->view(null));
}

/**
* Deletes multiple documents.
*
* @param string $id
*
* @return Response
*/
public function deleteAction($id)
{
$documentManager = $this->getDocumentManager();
$document = $documentManager->find($id);
$documentManager->remove($document);
$documentManager->flush();

return $this->handleView($this->view(null));
}

/**
* Persists the document using the given information.
*
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),
]
);
}
}
76 changes: 75 additions & 1 deletion Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Sulu\Bundle\ArticleBundle\Document;

use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior;
use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\LocalizedAuditableBehavior;
use Sulu\Component\Content\Document\Behavior\LocalizedStructureBehavior;
use Sulu\Component\Content\Document\Behavior\StructureBehavior;
Expand All @@ -35,7 +37,8 @@ class ArticleDocument implements
StructureBehavior,
LocalizedStructureBehavior,
LocalizedAuditableBehavior,
DateShardingBehavior
DateShardingBehavior,
RoutableInterface
{
/**
* @var string
Expand All @@ -62,6 +65,16 @@ class ArticleDocument implements
*/
private $title;

/**
* @var RouteInterface
*/
private $route;

/**
* @var string
*/
private $routePath;

/**
* @var string
*/
Expand Down Expand Up @@ -115,6 +128,16 @@ public function getUuid()
return $this->uuid;
}

/**
* Set uuid.
*
* @param string $uuid
*/
public function setUuid($uuid)
{
$this->uuid = $uuid;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -163,6 +186,47 @@ public function setTitle($title)
$this->title = $title;
}

/**
* Returns route.
*
* @return RouteInterface
*/
public function getRoute()
{
return $this->route;
}

/**
* Set route.
*
* @param RouteInterface $route
*/
public function setRoute(RouteInterface $route)
{
$this->route = $route;
$this->routePath = $route->getPath();
}

/**
* Returns route-path.
*
* @return string
*/
public function getRoutePath()
{
return $this->routePath;
}

/**
* Set route-path.
*
* @param string $routePath
*/
public function setRoutePath($routePath)
{
$this->routePath = $routePath;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -250,4 +314,14 @@ public function getChanged()
{
return $this->changed;
}

/**
* Returns identifier.
*
* @return mixed
*/
public function getId()
{
return $this->getUuid();
}
}
31 changes: 31 additions & 0 deletions Document/ArticleOngrDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class ArticleOngrDocument
*/
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
Loading