Skip to content

Commit

Permalink
EZP-31784: Enable autosave feature for Content on the Fly
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Oct 13, 2020
1 parent 55b627a commit a618b6f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/bundle/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\SPI\Limitation\Target;
use EzSystems\EzPlatformAdminUi\Event\ContentProxyCreateEvent;
use EzSystems\EzPlatformAdminUi\Event\Options;
use EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException as AdminInvalidArgumentException;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\ContentVisibilityUpdateData;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentCreateData;
Expand Down Expand Up @@ -181,7 +182,8 @@ public function proxyCreateAction(
new ContentProxyCreateEvent(
$contentType,
$languageCode,
$parentLocationId
$parentLocationId,
new Options()
)
);

Expand Down
62 changes: 55 additions & 7 deletions src/bundle/Controller/ContentOnTheFlyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use EzSystems\EzPlatformAdminUi\Event\ContentProxyCreateEvent;
use EzSystems\EzPlatformAdminUi\Event\Options;
use EzSystems\EzPlatformAdminUi\Form\ActionDispatcher\CreateContentOnTheFlyDispatcher;
use EzSystems\EzPlatformAdminUi\Specification\ContentType\ContentTypeIsUser;
use EzSystems\EzPlatformAdminUi\View\CreateContentOnTheFlyView;
use EzSystems\EzPlatformAdminUi\View\EditContentOnTheFlyView;
use EzSystems\EzPlatformAdminUi\View\EditContentOnTheFlySuccessView;
use EzSystems\EzPlatformContentForms\Data\Mapper\ContentCreateMapper;
use EzSystems\EzPlatformContentForms\Data\Mapper\ContentUpdateMapper;
use EzSystems\EzPlatformContentForms\Form\ActionDispatcher\ActionDispatcherInterface;
use EzSystems\EzPlatformContentForms\Form\Type\Content\ContentEditType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class ContentOnTheFlyController extends Controller
{
Expand Down Expand Up @@ -60,6 +64,12 @@ class ContentOnTheFlyController extends Controller
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
private $eventDispatcher;

/** @var \EzSystems\EzPlatformContentForms\Form\ActionDispatcher\ActionDispatcherInterface */
private $contentActionDispatcher;

public function __construct(
ContentService $contentService,
LanguageService $languageService,
Expand All @@ -68,7 +78,9 @@ public function __construct(
PermissionResolver $permissionResolver,
UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
CreateContentOnTheFlyDispatcher $createContentActionDispatcher,
ConfigResolverInterface $configResolver
ConfigResolverInterface $configResolver,
EventDispatcherInterface $eventDispatcher,
ActionDispatcherInterface $contentActionDispatcher
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
Expand All @@ -78,6 +90,8 @@ public function __construct(
$this->userLanguagePreferenceProvider = $userLanguagePreferenceProvider;
$this->createContentActionDispatcher = $createContentActionDispatcher;
$this->configResolver = $configResolver;
$this->eventDispatcher = $eventDispatcher;
$this->contentActionDispatcher = $contentActionDispatcher;
}

/**
Expand Down Expand Up @@ -152,6 +166,22 @@ public function createContentAction(
]);
}

/** @var \EzSystems\EzPlatformAdminUi\Event\ContentProxyCreateEvent $event */
$event = $this->eventDispatcher->dispatch(
new ContentProxyCreateEvent(
$contentType,
$languageCode,
$parentLocation->id,
new Options([
'isOnTheFly' => true,
])
)
);

if ($event->hasResponse()) {
return $event->getResponse();
}

$language = $this->languageService->loadLanguage($languageCode);

$data = (new ContentCreateMapper())->mapToFormData($contentType, [
Expand Down Expand Up @@ -198,11 +228,15 @@ public function editContentAction(
string $languageCode,
int $contentId,
int $versionNo,
int $locationId
?int $locationId
) {
$content = $this->contentService->loadContent($contentId, [$languageCode], $versionNo);
$versionInfo = $content->getVersionInfo();
$location = $this->locationService->loadLocation($locationId);

$location = null;
if (!empty($locationId)) {
$location = $this->locationService->loadLocation($locationId);
}

$contentType = $this->contentTypeService->loadContentType(
$content->contentInfo->contentTypeId,
Expand Down Expand Up @@ -253,17 +287,31 @@ public function editContentAction(
}

if ($form->isSubmitted() && $form->isValid() && null !== $form->getClickedButton()) {
$this->createContentActionDispatcher->dispatchFormAction(
$actionName = $form->getClickedButton()->getName();

$actionDispatcher = $actionName === 'autosave'
? $this->contentActionDispatcher
: $this->createContentActionDispatcher;

if (empty($location)) {
$contentInfo = $this->contentService->loadContentInfo($content->id);

if (!empty($contentInfo->mainLocationId)) {
$location = $this->locationService->loadLocation($contentInfo->mainLocationId);
}
}

$actionDispatcher->dispatchFormAction(
$form,
$form->getData(),
$form->getClickedButton()->getName(),
['referrerLocation' => $location]
);

if ($this->createContentActionDispatcher->getResponse()) {
if ($actionDispatcher->getResponse()) {
$view = new EditContentOnTheFlySuccessView('@ezdesign/ui/on_the_fly/content_edit_response.html.twig');
$view->addParameters([
'locationId' => $location->id,
'locationId' => empty($location) ? null : $location->id,
]);

return $view;
Expand All @@ -287,7 +335,7 @@ private function createContentCreateStruct(
private function buildEditView(
Content $content,
Language $language,
Location $location,
?Location $location,
FormInterface $form,
ContentType $contentType
): EditContentOnTheFlyView {
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Controller/UserOnTheFlyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private function createContentCreateStruct(
private function buildEditView(
Content $content,
Language $language,
Location $location,
?Location $location,
FormInterface $form,
ContentType $contentType
): EditUserOnTheFlyView {
Expand Down
1 change: 1 addition & 0 deletions src/bundle/Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ ezplatform.content_on_the_fly.edit:
methods: ['GET', 'POST']
defaults:
_controller: 'EzSystems\EzPlatformAdminUiBundle\Controller\ContentOnTheFlyController::editContentAction'
locationId: ~
options:
expose: true

Expand Down
1 change: 1 addition & 0 deletions src/bundle/Resources/config/services/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
autowire: true
arguments:
$configResolver: '@ezpublish.config.resolver'
$contentActionDispatcher: '@ezplatform.content_forms.action_dispatcher.content'
tags:
- controller.service_arguments

Expand Down
12 changes: 11 additions & 1 deletion src/lib/Event/ContentProxyCreateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ class ContentProxyCreateEvent extends Event
/** @var int */
protected $parentLocationId;

/** @var \EzSystems\EzPlatformAdminUi\Event\Options */
protected $options;

public function __construct(
ContentType $contentType,
string $languageCode,
int $parentLocationId
int $parentLocationId,
Options $options
) {
$this->contentType = $contentType;
$this->languageCode = $languageCode;
$this->parentLocationId = $parentLocationId;
$this->options = $options;
}

public function getContentType(): ContentType
Expand All @@ -51,6 +56,11 @@ public function getParentLocationId(): int
return $this->parentLocationId;
}

public function getOptions(): Options
{
return $this->options;
}

public function getResponse(): ?Response
{
return $this->response;
Expand Down
41 changes: 41 additions & 0 deletions src/lib/Event/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Event;

use eZ\Publish\SPI\Options\OptionsBag;

final class Options implements OptionsBag
{
/** @var array */
private $options;

public function __construct(array $options = [])
{
$this->options = $options;
}

public function all(): array
{
return $this->options;
}

public function get(
string $key,
$default = null
) {
return $this->has($key)
? $this->options[$key]
: $default;
}

public function has(string $key): bool
{
return isset($this->options[$key]);
}
}
25 changes: 18 additions & 7 deletions src/lib/EventListener/ContentProxyCreateDraftListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ public function createDraft(ContentProxyCreateEvent $event)
[]
);

$response = new RedirectResponse(
$this->router->generate('ezplatform.content.draft.edit', [
'contentId' => $contentDraft->id,
'versionNo' => $contentDraft->getVersionInfo()->versionNo,
'language' => $event->getLanguageCode(),
])
);
if (!$event->getOptions()->get('isOnTheFly', false)) {
$response = new RedirectResponse(
$this->router->generate('ezplatform.content.draft.edit', [
'contentId' => $contentDraft->id,
'versionNo' => $contentDraft->getVersionInfo()->versionNo,
'language' => $event->getLanguageCode(),
])
);
} else {
$response = new RedirectResponse(
$this->router->generate('ezplatform.content_on_the_fly.edit', [
'contentId' => $contentDraft->id,
'versionNo' => $contentDraft->getVersionInfo()->versionNo,
'languageCode' => $event->getLanguageCode(),
'locationId' => $contentDraft->contentInfo->mainLocationId,
])
);
}

$event->setResponse($response);
}
Expand Down

0 comments on commit a618b6f

Please # to comment.