Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
EZP-29106: Show author name in version list / draft conflict dialog (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored and Łukasz Serwatka committed May 8, 2018
1 parent 6795eab commit a2fab77
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 16 deletions.
42 changes: 42 additions & 0 deletions DependencyInjection/Compiler/ValueObjectVisitorPass.php
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\DependencyInjection\Compiler;

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

/**
* Compiler pass for the ezsystems.platformui.value_object_visitor tag used to
* map an fully qualified class to a value object visitor.
*/
class ValueObjectVisitorPass implements CompilerPassInterface
{
const TAG_NAME = 'ezsystems.platformui.value_object_visitor';
const DISPATCHER_DEFINITION_ID = 'ezsystems.platformui.rest.output.value_object_visitor.dispatcher';

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(self::DISPATCHER_DEFINITION_ID)) {
return;
}

$definition = $container->getDefinition(self::DISPATCHER_DEFINITION_ID);
foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['type'])) {
throw new \LogicException(self::TAG_NAME . ' service tag needs a "type" attribute to identify the field type. None given.');
}

$definition->addMethodCall(
'addVisitor',
[$attribute['type'], new Reference($id)]
);
}
}
}
}
2 changes: 2 additions & 0 deletions EzSystemsPlatformUIBundle.php
Expand Up @@ -10,6 +10,7 @@

use EzSystems\PlatformUIBundle\DependencyInjection\Compiler\ApplicationConfigProviderPass;
use EzSystems\PlatformUIBundle\DependencyInjection\Compiler\TranslationDomainsExtensionsPass;
use EzSystems\PlatformUIBundle\DependencyInjection\Compiler\ValueObjectVisitorPass;
use EzSystems\PlatformUIBundle\DependencyInjection\EzPlatformUIExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -36,5 +37,6 @@ public function build(ContainerBuilder $container)
parent::build($container);
$container->addCompilerPass(new ApplicationConfigProviderPass());
$container->addCompilerPass(new TranslationDomainsExtensionsPass());
$container->addCompilerPass(new ValueObjectVisitorPass());
}
}
32 changes: 32 additions & 0 deletions Resources/config/services.yml
Expand Up @@ -51,6 +51,9 @@ parameters:
# NOTE: value is only used for proxy cache, and staleness is set to be allowed for up to same time as ttl.
# To disable set to 0.
ezsystems.platformui.application_config.combo_loader.cache_ttl: 604800

ezsystems.platformui.rest.output.visitor.json.regexps:
- '(^application/vnd\.ez\.platformui\.[A-Za-z]+\+json$)'
services:
ezsystems.platformui.twig.text_extension:
class: '%ezsystems.platformui.twig.text_extension.class%'
Expand Down Expand Up @@ -272,11 +275,40 @@ services:
class: "%ezsystems.platformui.controller.exception.class%"
parent: twig.controller.exception

## REST
ezsystems.platformui.rest.user_field_type_processor:
class: EzSystems\PlatformUIBundle\Rest\UserFieldTypeProcessor
tags:
- { name: ezpublish_rest.field_type_processor, alias: ezuser }

ezsystems.platformui.rest.output.generator.json:
class: EzSystems\PlatformUIBundle\Rest\Generator\Json
arguments:
- "@ezpublish_rest.output.generator.json.field_type_hash_generator"
calls:
- [ setFormatOutput, [ "%kernel.debug%" ] ]

ezsystems.platformui.rest.output.visitor.json:
class: "%ezpublish_rest.output.visitor.class%"
arguments:
- "@ezsystems.platformui.rest.output.generator.json"
- "@ezsystems.platformui.rest.output.value_object_visitor.dispatcher"
tags:
- { name: ezpublish_rest.output.visitor, regexps: ezsystems.platformui.rest.output.visitor.json.regexps, priority: 100 }

ezsystems.platformui.rest.output.value_object_visitor.dispatcher:
class: EzSystems\PlatformUIBundle\Rest\ValueObjectVisitorDispatcher
arguments:
- '@ezpublish_rest.output.value_object_visitor.dispatcher'

ezsystems.platformui.rest.output.value_object_visitor.version_info:
class: EzSystems\PlatformUIBundle\Rest\ValueObjectVisitor\VersionInfo
parent: ezpublish_rest.output.value_object_visitor.base
arguments:
- "@ezpublish.api.repository"
tags:
- { name: ezsystems.platformui.value_object_visitor, type: eZ\Publish\API\Repository\Values\Content\VersionInfo }

## Section related services
ezsystems.platformui.controller.section:
class: "%ezsystems.platformui.controller.section.class%"
Expand Down
Expand Up @@ -31,7 +31,7 @@ YUI.add('ez-contentinfo-base', function (Y) {
var versions = [],
contentService = options.api.getContentService();

contentService.loadVersions(this.get('id'), function (error, response) {
contentService.loadVersions(this.get('id'), options.acceptHeader, function (error, response) {
if (error) {
callback(error, response);
return;
Expand Down
13 changes: 12 additions & 1 deletion Resources/public/js/models/ez-versioninfomodel.js
Expand Up @@ -128,7 +128,7 @@ YUI.add('ez-versioninfomodel', function (Y) {
REST_STRUCT_ROOT: "VersionInfo",
ATTRS_REST_MAP: [
{"id": "versionId"}, "status", "versionNo",
"creationDate", "modificationDate",
"creationDate", "creatorName", "modificationDate",
"languageCodes", "initialLanguageCode", "names",
],
LINKS_MAP: ['Content', 'Creator'],
Expand Down Expand Up @@ -179,6 +179,17 @@ YUI.add('ez-versioninfomodel', function (Y) {
value: new Date(0)
},

/**
* The creator name of the version.
*
* @attribute creatorName
* @type String
* @default ""
*/
creatorName: {
value: ""
},

/**
* The modification date of the version
*
Expand Down
Expand Up @@ -48,6 +48,7 @@ YUI.add('ez-contenteditplugin', function (Y) {
capi = service.get('capi'),
options = {
api: capi,
acceptHeader: 'application/vnd.ez.platformui.VersionList+json'
};

app.set('loading', true);
Expand Down
Expand Up @@ -58,6 +58,7 @@ YUI.add('ez-versionsplugin', function (Y) {
capi = service.get('capi'),
options = {
api: capi,
acceptHeader: 'application/vnd.ez.platformui.VersionList+json'
};

e.content.loadVersionsSortedByStatus(options, function (error, versions) {
Expand Down
6 changes: 5 additions & 1 deletion Resources/public/templates/confirmbox/draftconflict.hbt
Expand Up @@ -27,7 +27,11 @@
{{languageCodes}}
</td>
<td class="ez-draft-conflict-list-cell">
{{resources.Creator}}
{{#if creatorName}}
{{creatorName}}
{{else}}
{{resources.Creator}}
{{/if}}
</td>
<td class="ez-draft-conflict-list-cell">
{{ formatTime modificationDate day="numeric" month="short" year="numeric" hour="2-digit" minute="2-digit" }}
Expand Down
18 changes: 15 additions & 3 deletions Resources/public/templates/tabs/versions.hbt
Expand Up @@ -39,7 +39,11 @@
{{languageCodes}}
</td>
<td>
{{resources.Creator}}
{{#if creatorName}}
{{creatorName}}
{{else}}
{{resources.Creator}}
{{/if}}
</td>
<td>
{{ formatTime creationDate day="numeric" month="short" year="numeric" hour="2-digit" minute="2-digit" }}
Expand Down Expand Up @@ -99,7 +103,11 @@
{{languageCodes}}
</td>
<td>
{{resources.Creator}}
{{#if creatorName}}
{{creatorName}}
{{else}}
{{resources.Creator}}
{{/if}}
</td>
<td>
{{ formatTime creationDate day="numeric" month="short" year="numeric" hour="2-digit" minute="2-digit" }}
Expand Down Expand Up @@ -150,7 +158,11 @@
{{languageCodes}}
</td>
<td>
{{resources.Creator}}
{{#if creatorName}}
{{creatorName}}
{{else}}
{{resources.Creator}}
{{/if}}
</td>
<td>
{{ formatTime creationDate day="numeric" month="short" year="numeric" hour="2-digit" minute="2-digit" }}
Expand Down
17 changes: 17 additions & 0 deletions Rest/Generator/Json.php
@@ -0,0 +1,17 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Rest\Generator;

use eZ\Publish\Core\REST\Common\Output\Generator\Json as BaseJson;

class Json extends BaseJson
{
protected function generateMediaType($name, $type)
{
return "application/vnd.ez.platformui.{$name}+{$type}";
}
}
55 changes: 55 additions & 0 deletions Rest/ValueObjectVisitor/VersionInfo.php
@@ -0,0 +1,55 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Rest\ValueObjectVisitor;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\REST\Common\Output\Generator;
use eZ\Publish\Core\REST\Common\Output\Visitor;
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\VersionInfo as BaseVersionInfo;
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;

/**
* VersionInfo value object visitor.
*/
class VersionInfo extends BaseVersionInfo
{
/** @var \eZ\Publish\API\Repository\Repository */
private $repository;

/**
* VersionInfo constructor.
*
* @param \eZ\Publish\API\Repository\Repository $repository
*/
public function __construct(Repository $repository)
{
$this->repository = $repository;
}

protected function visitVersionInfoAttributes(Visitor $visitor, Generator $generator, APIVersionInfo $versionInfo)
{
parent::visitVersionInfoAttributes($visitor, $generator, $versionInfo);

$this->visitCreatorName($visitor, $generator, $versionInfo);
}

protected function visitCreatorName(Visitor $visitor, Generator $generator, APIVersionInfo $versionInfo)
{
/** @var \eZ\Publish\API\Repository\Values\User\User $user */
$user = $this->repository->sudo(function (Repository $repository) use ($versionInfo) {
try {
return $repository->getUserService()->loadUser($versionInfo->creatorId);
} catch (NotFoundException $e) {
return null;
}
});

$generator->startValueElement('creatorName', $user ? $user->getName() : '-');
$generator->endValueElement('creatorName');
}
}
49 changes: 49 additions & 0 deletions Rest/ValueObjectVisitorDispatcher.php
@@ -0,0 +1,49 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Rest;

use eZ\Publish\Core\REST\Common\Output\Generator;
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher as BaseValueObjectVisitorDispatcher;
use eZ\Publish\Core\REST\Common\Output\Exceptions\NoVisitorFoundException;
use eZ\Publish\Core\REST\Common\Output\Visitor;

class ValueObjectVisitorDispatcher extends BaseValueObjectVisitorDispatcher
{
/** @var \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher */
private $parentDispatcher;

/**
* ValueObjectVisitorDispatcher constructor.
*
* @param \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher $parentDispatcher
*/
public function __construct(BaseValueObjectVisitorDispatcher $parentDispatcher)
{
$this->parentDispatcher = $parentDispatcher;
}

public function setOutputVisitor(Visitor $outputVisitor)
{
parent::setOutputVisitor($outputVisitor);
$this->parentDispatcher->setOutputVisitor($outputVisitor);
}

public function setOutputGenerator(Generator $outputGenerator)
{
parent::setOutputGenerator($outputGenerator);
$this->parentDispatcher->setOutputGenerator($outputGenerator);
}

public function visit($data)
{
try {
return parent::visit($data);
} catch (NoVisitorFoundException $e) {
return $this->parentDispatcher->visit($data);
}
}
}
48 changes: 48 additions & 0 deletions Tests/DependencyInjection/Compiler/ValueObjectVisitorPassTest.php
@@ -0,0 +1,48 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Tests\DependencyInjection\Compiler;

use EzSystems\PlatformUIBundle\DependencyInjection\Compiler\ValueObjectVisitorPass;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class ValueObjectVisitorPassTest extends AbstractCompilerPassTestCase
{
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new ValueObjectVisitorPass());
}

public function testProcess()
{
$dispatcherDefinition = new Definition();

$visitorId = 'ezsystems.platformui.rest.output.value_object_visitor.test';
$visitorDefinition = new Definition();
$visitorDefinition->addTag(ValueObjectVisitorPass::TAG_NAME, [
'type' => 'test', ]
);

$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
ValueObjectVisitorPass::DISPATCHER_DEFINITION_ID => $dispatcherDefinition,
$visitorId => $visitorDefinition,
]);

$compilerPass = new ValueObjectVisitorPass();
$compilerPass->process($containerBuilder);

$dispatcherMethodCalls = $dispatcherDefinition->getMethodCalls();

$this->assertTrue(isset($dispatcherMethodCalls[0][0]), 'Failed asserting that dispatcher has a method call');
$this->assertEquals('addVisitor', $dispatcherMethodCalls[0][0], "Failed asserting that called method is 'addVisitor'");
$this->assertInstanceOf(Reference::class, $dispatcherMethodCalls[0][1][1], 'Failed asserting that method call is to a Reference object');
$this->assertEquals($visitorId, $dispatcherMethodCalls[0][1][1]->__toString(), "Failed asserting that Referenced service is '$visitorId'");
}
}

0 comments on commit a2fab77

Please sign in to comment.