Skip to content

Commit

Permalink
Webspace settings (#336)
Browse files Browse the repository at this point in the history
* Webspace settings frontend

* Fix StyleCI

* Fixed bug

* PHPCR Implementation (sulu#4)

* Implementation finished

* Add tests

* Add ES indexing

* Info just gray

* Check for ES in test

* Add install notes

* Better doc

* Better doc

* Fix bug in addition webspaces preselect

* Fixed bug

* Dataprovider considers webspace settings (sulu#5)

* Implementation finished

* Dataprovider considers webspace settings

* Code clean up

* Corrected property param

* Update ArticleDataProvider.php

* Resolve Sitemap correctly (sulu#6)

* Add Canonical Tag (sulu#7)

* Add Canonical Tag

* Code clean up

* Target webspace (sulu#8)

* Add targetWebspace to ViewDocument

* Fixed test

* Search Subscriber (sulu#10)

* ArticleSearchSubscriber

* Code clean up

* Fix tests

* Add "ignoreWebspaces" param to twig extension (sulu#9)

* Add "ignoreWebspaces" param to twig extension

* Code clean up

* Finishing (sulu#11)

* Add UPGRADE note

* Add NL translations

* Add FR translation

* Fix indention

* Fix StyleCI

* Page tree route (sulu#12)

* Page tree route

* Disable webspace settings form

* Fix StyleCI
  • Loading branch information
trickreich authored Jul 31, 2018
1 parent 2377385 commit 0152236
Show file tree
Hide file tree
Showing 62 changed files with 2,369 additions and 159 deletions.
57 changes: 55 additions & 2 deletions Admin/ArticleJsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\AdminBundle\Admin\JsConfigInterface;
use Sulu\Bundle\ArticleBundle\Metadata\StructureTagTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Webspace\Manager\WebspaceManager;

/**
* Provides js-configuration.
Expand All @@ -22,6 +23,11 @@ class ArticleJsConfig implements JsConfigInterface
{
use StructureTagTrait;

/**
* @var WebspaceManager
*/
private $webspaceManager;

/**
* @var StructureManagerInterface
*/
Expand All @@ -38,12 +44,18 @@ class ArticleJsConfig implements JsConfigInterface
private $parameter;

/**
* @param WebspaceManager $webspaceManager
* @param StructureManagerInterface $structureManager
* @param array $typeConfiguration
* @param array $parameter
*/
public function __construct(StructureManagerInterface $structureManager, array $typeConfiguration, array $parameter)
{
public function __construct(
WebspaceManager $webspaceManager,
StructureManagerInterface $structureManager,
array $typeConfiguration,
array $parameter
) {
$this->webspaceManager = $webspaceManager;
$this->structureManager = $structureManager;
$this->typeConfiguration = $typeConfiguration;
$this->parameter = $parameter;
Expand Down Expand Up @@ -76,6 +88,8 @@ public function getParameters()
];
}

$config = array_merge($config, $this->getWebspaceSettingsConfig());

return $config;
}

Expand All @@ -102,4 +116,43 @@ private function getTitle($type)

return $this->typeConfiguration[$type]['translation_key'];
}

/**
* @return array
*/
private function getWebspaceSettingsConfig()
{
$showWebspaceSettings = count($this->webspaceManager->getWebspaceCollection()->getWebspaces()) > 1;

if (!$showWebspaceSettings) {
return [
'showWebspaceSettings' => $showWebspaceSettings,
];
}

$webspaces = [];
foreach ($this->webspaceManager->getWebspaceCollection()->getWebspaces() as $webspace) {
$webspaces[] = [
'id' => $webspace->getKey(),
'key' => $webspace->getKey(),
'name' => $webspace->getName(),
];
}

$defaultMainWebspace = $this->parameter['defaultMainWebspace'];
if (!$defaultMainWebspace || !$this->webspaceManager->findWebspaceByKey($defaultMainWebspace)) {
throw new \InvalidArgumentException('You have more than one webspace, so you need to set config parameter "sulu_article.default_main_webspace" to one of "' . implode(',', array_column($webspaces, 'key')) . '"');
}

foreach ($this->parameter['defaultAdditionalWebspaces'] as $defaultAdditionalWebspace) {
if (!$this->webspaceManager->findWebspaceByKey($defaultAdditionalWebspace)) {
throw new \InvalidArgumentException('Configured default additional webspace "' . $defaultAdditionalWebspace . '" not found. Available webspaces: "' . implode(',', array_column($webspaces, 'key')) . '"');
}
}

return [
'showWebspaceSettings' => $showWebspaceSettings,
'webspaces' => $webspaces,
];
}
}
51 changes: 47 additions & 4 deletions Content/ArticleDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ protected function getConfigurationBuilder()
*/
public function getDefaultPropertyParameter()
{
return ['type' => new PropertyParameter('type', null)];
return [
'type' => new PropertyParameter('type', null),
'ignoreWebspaces' => new PropertyParameter('ignoreWebspaces', false),
];
}

/**
Expand All @@ -153,7 +156,9 @@ public function resolveDataItems(
$filters['structureTypes'] = $this->getStructureTypesProperty($propertyParameter);
$filters['excluded'] = $this->getExcludedFilter($filters, $propertyParameter);

$queryResult = $this->getSearchResult($filters, $limit, $page, $pageSize, $options['locale']);
$locale = $options['locale'];
$webspaceKey = $this->getWebspaceKey($propertyParameter, $options);
$queryResult = $this->getSearchResult($filters, $limit, $page, $pageSize, $locale, $webspaceKey);

$result = [];
/** @var ArticleViewDocumentInterface $document */
Expand All @@ -179,7 +184,9 @@ public function resolveResourceItems(
$filters['structureTypes'] = $this->getStructureTypesProperty($propertyParameter);
$filters['excluded'] = $this->getExcludedFilter($filters, $propertyParameter);

$queryResult = $this->getSearchResult($filters, $limit, $page, $pageSize, $options['locale']);
$locale = $options['locale'];
$webspaceKey = $this->getWebspaceKey($propertyParameter, $options);
$queryResult = $this->getSearchResult($filters, $limit, $page, $pageSize, $locale, $webspaceKey);

$result = [];
/** @var ArticleViewDocumentInterface $document */
Expand All @@ -199,6 +206,29 @@ public function resolveDatasource($datasource, array $propertyParameter, array $
return;
}

/**
* @param array $propertyParameter
* @param array $options
*
* @return string|null
*/
private function getWebspaceKey(array $propertyParameter, array $options)
{
if (array_key_exists('ignoreWebspaces', $propertyParameter)) {
$value = $propertyParameter['ignoreWebspaces']->getValue();

if (true === $value) {
return null;
}
}

if (array_key_exists('webspaceKey', $options)) {
return $options['webspaceKey'];
}

return null;
}

/**
* Returns flag "hasNextPage".
* It combines the limit/query-count with the page and page-size.
Expand Down Expand Up @@ -228,10 +258,11 @@ private function hasNextPage(\Countable $queryResult, $limit, $page, $pageSize)
* @param int $page
* @param int $pageSize
* @param string $locale
* @param null|string $webspaceKey
*
* @return \Countable
*/
private function getSearchResult(array $filters, $limit, $page, $pageSize, $locale)
private function getSearchResult(array $filters, $limit, $page, $pageSize, $locale, $webspaceKey)
{
$repository = $this->searchManager->getRepository($this->articleDocumentClass);
$search = $this->createSearch($repository->createSearch(), $filters, $locale);
Expand All @@ -246,6 +277,18 @@ private function getSearchResult(array $filters, $limit, $page, $pageSize, $loca
$this->appendSortBy($filters['sortBy'], $sortMethod, $search);
}

if ($webspaceKey) {
$webspaceQuery = new BoolQuery();

// check for mainWebspace
$webspaceQuery->add(new TermQuery('main_webspace', $webspaceKey), BoolQuery::SHOULD);

// check for additionalWebspaces
$webspaceQuery->add(new TermQuery('additional_webspaces', $webspaceKey), BoolQuery::SHOULD);

$search->addQuery($webspaceQuery);
}

return $repository->findDocuments($search);
}

Expand Down
24 changes: 24 additions & 0 deletions Content/ArticleResourceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ public function getContent()
return $this->article;
}

/**
* @return string
*/
public function getTargetWebspace()
{
return $this->article->getTargetWebspace();
}

/**
* @return string
*/
public function getMainWebspace()
{
return $this->article->getMainWebspace();
}

/**
* @return null|string[]
*/
public function getAdditionalWebspaces()
{
return $this->article->getAdditionalWebspaces();
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 4 additions & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ private function persistDocument($data, $document, $locale)
$document->setAuthor(null);
}

if (array_key_exists('additionalWebspaces', $data) && null === $data['additionalWebspaces']) {
$document->setAdditionalWebspaces(null);
}

$this->getDocumentManager()->persist(
$document,
$locale,
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$treeBuilder->root('sulu_article')
->children()
->scalarNode('default_main_webspace')->defaultNull()->end()
->arrayNode('default_additional_webspaces')
->prototype('scalar')->end()
->defaultValue([])
->end()
->arrayNode('smart_content')
->addDefaultsIfNotSet()
->children()
Expand Down
29 changes: 29 additions & 0 deletions DependencyInjection/RouteEnhancerCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class RouteEnhancerCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('sulu_route.routing.router')) {
return;
}

$router = $container->getDefinition('sulu_route.routing.router');
$router->addMethodCall('addRouteEnhancer', [new Reference('sulu_article.route_enhancer'), 100]);
}
}
2 changes: 2 additions & 0 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('sulu_article.default_main_webspace', $config['default_main_webspace']);
$container->setParameter('sulu_article.default_additional_webspaces', $config['default_additional_webspaces']);
$container->setParameter('sulu_article.types', $config['types']);
$container->setParameter('sulu_article.documents', $config['documents']);
$container->setParameter('sulu_article.view_document.article.class', $config['documents']['article']['view']);
Expand Down
50 changes: 49 additions & 1 deletion Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutableBehavior;
use Sulu\Bundle\ArticleBundle\Document\Behavior\WebspaceBehavior;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\ExtensionBehavior;
use Sulu\Component\Content\Document\Behavior\LocalizedAuditableBehavior;
Expand Down Expand Up @@ -54,7 +55,8 @@ class ArticleDocument implements
LocalizedAuthorBehavior,
ChildrenBehavior,
ArticleInterface,
ShadowLocaleBehavior
ShadowLocaleBehavior,
WebspaceBehavior
{
/**
* @var string
Expand Down Expand Up @@ -198,6 +200,20 @@ class ArticleDocument implements
*/
protected $shadowLocale;

/**
* Main webspace.
*
* @var null|string
*/
protected $mainWebspace;

/**
* Additional webspaces.
*
* @var null|string[]
*/
protected $additionalWebspaces;

public function __construct()
{
$this->structure = new Structure();
Expand Down Expand Up @@ -623,4 +639,36 @@ public function setShadowLocaleEnabled($shadowLocaleEnabled)
{
$this->shadowLocaleEnabled = $shadowLocaleEnabled;
}

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

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

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

/**
* {@inheritdoc}
*/
public function setAdditionalWebspaces($additionalWebspaces)
{
$this->additionalWebspaces = $additionalWebspaces;
}
}
Loading

0 comments on commit 0152236

Please sign in to comment.