Skip to content

Commit

Permalink
IBX-7051: Preview button with siteaccess (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
tischsoic committed Nov 22, 2023
1 parent 4a2da30 commit 44a14fa
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 13 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9775,11 +9775,6 @@ parameters:
count: 1
path: src/lib/UI/Module/ContentTree/NodeFactory.php

-
message: "#^Parameter \\#9 \\$totalChildrenCount of class Ibexa\\\\AdminUi\\\\REST\\\\Value\\\\ContentTree\\\\Node constructor expects int, int\\|null given\\.$#"
count: 1
path: src/lib/UI/Module/ContentTree/NodeFactory.php

-
message: "#^Property Ibexa\\\\AdminUi\\\\UI\\\\Module\\\\ContentTree\\\\NodeFactory\\:\\:\\$bookmarkService is never read, only written\\.$#"
count: 1
Expand Down
11 changes: 11 additions & 0 deletions src/bundle/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,14 @@ public function updateMainLocationAction(Request $request): Response
* @return \Symfony\Component\HttpFoundation\Response
*/
public function previewAction(
Request $request,
Content $content,
?string $languageCode = null,
?int $versionNo = null,
?Location $location = null
): Response {
$preselectedSiteAccess = $request->query->get('preselectedSiteAccess');

if (null === $languageCode) {
$languageCode = $content->contentInfo->mainLanguageCode;
}
Expand Down Expand Up @@ -379,12 +382,20 @@ public function previewAction(
$siteAccessesList[$siteAccess->name] = $this->siteAccessNameGenerator->generate($siteAccess);
}

if (
$preselectedSiteAccess !== null &&
!array_key_exists($preselectedSiteAccess, $siteAccessesList)
) {
$preselectedSiteAccess = null;
}

return $this->render('@ibexadesign/content/content_preview.html.twig', [
'location' => $location,
'content' => $content,
'language_code' => $languageCode,
'siteaccesses' => $siteAccessesList,
'version_no' => $versionNo ?? $content->getVersionInfo()->versionNo,
'preselected_site_access' => $preselectedSiteAccess,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ ibexa.content.preview:
_controller: 'Ibexa\Bundle\AdminUi\Controller\ContentController::previewAction'
languageCode: ~
locationId: ~
options:
expose: true

# IBX-1079: Translate routes with proxy suffix have to be prioritized to avoid issues with URL generations
ibexa.content.translate.proxy:
Expand Down
5 changes: 5 additions & 0 deletions src/bundle/Resources/translations/ibexa_menu.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<target state="new">Move</target>
<note>key: content__sidebar_right__move</note>
</trans-unit>
<trans-unit id="e200a70bde86beb8c043e30b6fdb0a79e968c61c" resname="content__sidebar_right__preview">
<source>Preview</source>
<target state="new">Preview</target>
<note>key: content__sidebar_right__preview</note>
</trans-unit>
<trans-unit id="bbf7b43821e5ac0ce57d977a3ce156baf9a9d7b7" resname="content__sidebar_right__reveal">
<source>Reveal</source>
<target state="new">Reveal</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% trans_default_domain 'ibexa_content_preview' %}

{% set is_published = content.contentInfo.published and content.contentInfo.mainLocationId is not null %}
{% set preselected_site_access = preselected_site_access|default(siteaccesses|first) %}

{% block body_class %}ibexa-content-preview{% endblock %}

Expand All @@ -23,25 +24,29 @@
</div>
<div class="ibexa-preview-header__item ibexa-preview-header__item--siteaccess">
{% set value = '' %}
{% set choices = siteaccesses|map((site_accesses_name, site_access_label) => {
{% set choices = siteaccesses|map((site_access_name, site_access_label) => {
value: path(
'ibexa.version.preview',
{
'contentId': content.id,
'versionNo': version_no,
'language': language_code,
'siteAccessName': site_accesses_name
'siteAccessName': site_access_name
}
),
label: site_access_label|trans({}, 'ezplatform_siteaccess')
label: site_access_label|trans({}, 'ezplatform_siteaccess'),
is_selected: site_access_name == preselected_site_access,
}) %}
{% set source %}
<select class="form-control ibexa-input">
{% for choice in choices %}
{% if loop.first %}
{% if choice.is_selected %}
{% set value = choice.value %}
{% endif %}
<option value="{{ choice.value }}">
<option
value="{{ choice.value }}"
{% if choice.is_selected %}selected{% endif %}
>
{{ choice.label }}
</option>
{% endfor %}
Expand Down Expand Up @@ -78,7 +83,9 @@
{% block content %}
<div class="ibexa-preview">
<div class="ibexa-preview__iframe ibexa-preview__iframe--desktop">
<iframe src="{{ url('ibexa.version.preview', {'contentId': content.id, 'versionNo': version_no, 'language': language_code, 'siteAccessName': siteaccesses|first}) }}" frameborder="0"></iframe>
<iframe src="{{ url('ibexa.version.preview', {
'contentId': content.id, 'versionNo': version_no, 'language': language_code, 'siteAccessName': preselected_site_access
}) }}" frameborder="0"></iframe>
</div>
</div>
{% endblock %}
Expand Down
68 changes: 67 additions & 1 deletion src/lib/Menu/ContentRightSidebarBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\AdminUi\Menu;

use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
use Ibexa\AdminUi\Siteaccess\SiteaccessResolverInterface;
use Ibexa\AdminUi\Specification\ContentType\ContentTypeIsUser;
use Ibexa\AdminUi\Specification\ContentType\ContentTypeIsUserGroup;
use Ibexa\AdminUi\Specification\Location\IsRoot;
Expand All @@ -21,6 +22,7 @@
use Ibexa\Contracts\Core\Limitation\Target\Builder\VersionBuilder;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use JMS\TranslationBundle\Model\Message;
Expand All @@ -37,6 +39,7 @@ class ContentRightSidebarBuilder extends AbstractBuilder implements TranslationC
{
/* Menu items */
public const ITEM__CREATE = 'content__sidebar_right__create';
public const ITEM__PREVIEW = 'content__sidebar_right__preview';
public const ITEM__EDIT = 'content__sidebar_right__edit';
public const ITEM__SEND_TO_TRASH = 'content__sidebar_right__send_to_trash';
public const ITEM__COPY = 'content__sidebar_right__copy';
Expand Down Expand Up @@ -65,6 +68,8 @@ class ContentRightSidebarBuilder extends AbstractBuilder implements TranslationC
/** @var \Ibexa\Contracts\AdminUi\Permission\PermissionCheckerInterface */
private $permissionChecker;

private SiteaccessResolverInterface $siteaccessResolver;

public function __construct(
MenuItemFactory $factory,
EventDispatcherInterface $eventDispatcher,
Expand All @@ -73,7 +78,8 @@ public function __construct(
ConfigResolverInterface $configResolver,
LocationService $locationService,
UniversalDiscoveryExtension $udwExtension,
PermissionCheckerInterface $permissionChecker
PermissionCheckerInterface $permissionChecker,
SiteaccessResolverInterface $siteaccessResolver
) {
parent::__construct($factory, $eventDispatcher);

Expand All @@ -83,6 +89,7 @@ public function __construct(
$this->locationService = $locationService;
$this->udwExtension = $udwExtension;
$this->permissionChecker = $permissionChecker;
$this->siteaccessResolver = $siteaccessResolver;
}

/**
Expand Down Expand Up @@ -193,6 +200,15 @@ public function createStructure(array $options): ItemInterface
),
]);

$previewItem = $this->getContentPreviewItem(
$location,
$content,
[
'extras' => ['orderNumber' => 12],
],
);
$menu->addChild($previewItem);

$canSendInvitation = $this->permissionResolver->canUser(
'user',
'invite',
Expand Down Expand Up @@ -301,6 +317,7 @@ public static function getTranslationMessages(): array
return [
(new Message(self::ITEM__CREATE, 'ibexa_menu'))->setDesc('Create content'),
(new Message(self::ITEM__EDIT, 'ibexa_menu'))->setDesc('Edit'),
(new Message(self::ITEM__PREVIEW, 'ibexa_menu'))->setDesc('Preview'),
(new Message(self::ITEM__SEND_TO_TRASH, 'ibexa_menu'))->setDesc('Send to trash'),
(new Message(self::ITEM__COPY, 'ibexa_menu'))->setDesc('Copy'),
(new Message(self::ITEM__COPY_SUBTREE, 'ibexa_menu'))->setDesc('Copy Subtree'),
Expand Down Expand Up @@ -443,6 +460,55 @@ private function getCopySubtreeLimit(): int
'subtree_operations.copy_subtree.limit'
);
}

/**
* @param array<string, mixed> $options
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
private function getContentPreviewItem(
Location $location,
Content $content,
array $options
): ItemInterface {
$versionNo = $content->getVersionInfo()->versionNo;
$languageCode = $content->contentInfo->mainLanguageCode;

$siteAccesses = $this->siteaccessResolver->getSiteAccessesListForLocation(
$location,
$versionNo,
$languageCode
);

$canPreview = $this->permissionResolver->canUser(
'content',
'versionread',
$content,
[$location]
);

if ($canPreview && !empty($siteAccesses)) {
$actionOptions = [
'route' => 'ibexa.content.preview',
'routeParameters' => [
'contentId' => $content->contentInfo->getId(),
'versionNo' => $content->getVersionInfo()->versionNo,
'languageCode' => $languageCode,
'locationId' => $location->id,
],
];
} else {
$actionOptions = [
'attributes' => ['disabled' => 'disabled'],
];
}

return $this->createMenuItem(
self::ITEM__PREVIEW,
array_merge($options, $actionOptions)
);
}
}

class_alias(ContentRightSidebarBuilder::class, 'EzSystems\EzPlatformAdminUi\Menu\ContentRightSidebarBuilder');
6 changes: 6 additions & 0 deletions src/lib/REST/Output/ValueObjectVisitor/ContentTree/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function visit(Visitor $visitor, Generator $generator, $data)
$generator->startValueElement('contentId', $data->contentId);
$generator->endValueElement('contentId');

$generator->valueElement('versionNo', $data->versionNo);

$generator->valueElement('translations', implode(',', $data->translations));

$generator->valueElement('previewableTranslations', implode(',', $data->previewableTranslations));

$generator->startValueElement('name', $data->name);
$generator->endValueElement('name');

Expand Down
16 changes: 16 additions & 0 deletions src/lib/REST/Value/ContentTree/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class Node extends RestValue
/** @var int */
public $contentId;

public int $versionNo;

/** @var string[] */
public array $translations;

/** @var string[] */
public array $previewableTranslations;

/** @var string */
public $name;

Expand Down Expand Up @@ -50,6 +58,8 @@ class Node extends RestValue
* @param int $depth
* @param int $locationId
* @param int $contentId
* @param string[] $translations
* @param string[] $previewableTranslations
* @param string $name
* @param string $contentTypeIdentifier
* @param bool $isContainer
Expand All @@ -62,6 +72,9 @@ public function __construct(
int $depth,
int $locationId,
int $contentId,
int $versionNo,
array $translations,
array $previewableTranslations,
string $name,
string $contentTypeIdentifier,
bool $isContainer,
Expand All @@ -75,6 +88,9 @@ public function __construct(
$this->depth = $depth;
$this->locationId = $locationId;
$this->contentId = $contentId;
$this->versionNo = $versionNo;
$this->translations = $translations;
$this->previewableTranslations = $previewableTranslations;
$this->name = $name;
$this->isInvisible = $isInvisible;
$this->contentTypeIdentifier = $contentTypeIdentifier;
Expand Down
Loading

0 comments on commit 44a14fa

Please sign in to comment.