Skip to content

Commit

Permalink
fix: Move Content on the Fly to dedicated controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Mar 8, 2018
1 parent d86e1bd commit e7d7b60
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 130 deletions.
126 changes: 0 additions & 126 deletions src/bundle/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions as ApiException;
use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException as AdminInvalidArgumentException;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentCreateData;
Expand All @@ -23,13 +18,6 @@
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use EzSystems\EzPlatformAdminUi\Form\SubmitHandler;
use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface;
use EzSystems\EzPlatformAdminUi\RepositoryForms\Dispatcher\ContentOnTheFlyDispatcher;
use EzSystems\EzPlatformAdminUi\RepositoryForms\View\ContentCreateOnTheFlyView;
use EzSystems\RepositoryForms\Content\View\ContentCreateView;
use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper;
use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -47,9 +35,6 @@ class ContentController extends Controller
/** @var ContentService */
private $contentService;

/** @var LanguageService */
private $languageService;

/** @var FormFactory */
private $formFactory;

Expand All @@ -65,12 +50,6 @@ class ContentController extends Controller
/** @var string */
private $defaultSiteaccess;

/** @var LocationService */
private $locationService;

/** @var ContentOnTheFlyDispatcher */
private $contentActionDispatcher;

/**
* @param NotificationHandlerInterface $notificationHandler
* @param ContentService $contentService
Expand All @@ -83,24 +62,18 @@ class ContentController extends Controller
public function __construct(
NotificationHandlerInterface $notificationHandler,
ContentService $contentService,
LanguageService $languageService,
LocationService $locationService,
FormFactory $formFactory,
SubmitHandler $submitHandler,
TranslatorInterface $translator,
ContentMainLocationUpdateMapper $contentMetadataUpdateMapper,
ContentOnTheFlyDispatcher $contentActionDispatcher,
string $defaultSiteaccess
) {
$this->notificationHandler = $notificationHandler;
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->languageService = $languageService;
$this->formFactory = $formFactory;
$this->submitHandler = $submitHandler;
$this->translator = $translator;
$this->contentMainLocationUpdateMapper = $contentMetadataUpdateMapper;
$this->contentActionDispatcher = $contentActionDispatcher;
$this->defaultSiteaccess = $defaultSiteaccess;
}

Expand Down Expand Up @@ -142,105 +115,6 @@ public function createAction(Request $request): Response
return $this->redirect($this->generateUrl('ezplatform.dashboard'));
}

/**
* @param Request $request
* @param string $languageCode
* @param ContentType $contentType
* @param Location $parentLocation
*
* @return ContentCreateView|Response
*
* @throws ApiException\NotFoundException
*/
public function createOnTheFlyAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation)
{
$language = $this->languageService->loadLanguage($languageCode);

$data = (new ContentCreateMapper())->mapToFormData($contentType, [
'mainLanguageCode' => $language->languageCode,
'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocation->id),
]);

$form = $this->createForm(ContentEditType::class, $data, [
'languageCode' => $language->languageCode,
'mainLanguageCode' => $language->languageCode,
'drafts_enabled' => true,
]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->contentActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}

return new ContentCreateOnTheFlyView(null, [
'form' => $form->createView(),
'language' => $language,
'contentType' => $contentType,
'parentLocation' => $parentLocation,
]);
}

/**
* @param Request $request
* @param string $languageCode
* @param ContentType $contentType
* @param Location $parentLocation
*
* @return JsonResponse
*
* @throws ApiException\InvalidArgumentException
* @throws ApiException\BadStateException
*/
public function hasOnTheFlyCreateAccessAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation)
{
$response = new JsonResponse();

try {
$contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $languageCode);
$locationCreateStruct = $this->locationService->newLocationCreateStruct($parentLocation->id);

$permissionResolver = $this->container->get('ezpublish.api.repository')->getPermissionResolver();

if (!$permissionResolver->canUser('content', 'create', $contentCreateStruct, [$locationCreateStruct])) {
throw new UnauthorizedException(
'content',
'create',
[
'contentTypeIdentifier' => $contentType->identifier,
'parentLocationId' => $locationCreateStruct->parentLocationId,
'languageCode' => $languageCode,
]
);
}

if (!$permissionResolver->canUser('content', 'publish', $contentCreateStruct, [$locationCreateStruct])) {
throw new UnauthorizedException(
'content',
'publish',
[
'contentTypeIdentifier' => $contentType->identifier,
'parentLocationId' => $locationCreateStruct->parentLocationId,
'languageCode' => $languageCode,
]
);
}

$response->setData([
'access' => true,
]);
} catch (ApiException\UnauthorizedException $exception) {
$response->setData([
'access' => false,
'message' => $exception->getMessage(),
]);
}

return $response;
}

/**
* @param Request $request
*
Expand Down
154 changes: 154 additions & 0 deletions src/bundle/Controller/ContentOnTheFlyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?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\EzPlatformAdminUiBundle\Controller;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions as ApiException;
use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use EzSystems\EzPlatformAdminUi\RepositoryForms\Dispatcher\ContentOnTheFlyDispatcher;
use EzSystems\EzPlatformAdminUi\RepositoryForms\View\ContentCreateOnTheFlyView;
use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper;
use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ContentController extends Controller
{

/** @var ContentService */
private $contentService;

/** @var LanguageService */
private $languageService;

/** @var LocationService */
private $locationService;

/** @var ContentOnTheFlyDispatcher */
private $contentActionDispatcher;

/**
* @param ContentService $contentService
* @param LanguageService $languageService
* @param LocationService $locationService
* @param ContentOnTheFlyDispatcher $contentActionDispatcher
*/
public function __construct(
ContentService $contentService,
LanguageService $languageService,
LocationService $locationService,
ContentOnTheFlyDispatcher $contentActionDispatcher
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->languageService = $languageService;
$this->contentActionDispatcher = $contentActionDispatcher;
}

/**
* @param Request $request
* @param string $languageCode
* @param ContentType $contentType
* @param Location $parentLocation
*
* @return ContentCreateOnTheFlyView|Response
*
* @throws ApiException\NotFoundException
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
*/
public function createContentAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation)
{
$language = $this->languageService->loadLanguage($languageCode);

$data = (new ContentCreateMapper())->mapToFormData($contentType, [
'mainLanguageCode' => $language->languageCode,
'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocation->id),
]);

$form = $this->createForm(ContentEditType::class, $data, [
'languageCode' => $language->languageCode,
'mainLanguageCode' => $language->languageCode,
'drafts_enabled' => true,
]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->contentActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}

return new ContentCreateOnTheFlyView(null, [
'form' => $form->createView(),
'language' => $language,
'contentType' => $contentType,
'parentLocation' => $parentLocation,
]);
}

/**
* @param Request $request
* @param string $languageCode
* @param ContentType $contentType
* @param Location $parentLocation
*
* @return JsonResponse
*/
public function hasCreateAccessAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation)
{
$response = new JsonResponse();

try {
$contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $languageCode);
$locationCreateStruct = $this->locationService->newLocationCreateStruct($parentLocation->id);

$permissionResolver = $this->container->get('ezpublish.api.repository')->getPermissionResolver();

if (!$permissionResolver->canUser('content', 'create', $contentCreateStruct, [$locationCreateStruct])) {
throw new UnauthorizedException(
'content',
'create',
[
'contentTypeIdentifier' => $contentType->identifier,
'parentLocationId' => $locationCreateStruct->parentLocationId,
'languageCode' => $languageCode,
]
);
}

if (!$permissionResolver->canUser('content', 'publish', $contentCreateStruct, [$locationCreateStruct])) {
throw new UnauthorizedException(
'content',
'publish',
[
'contentTypeIdentifier' => $contentType->identifier,
'parentLocationId' => $locationCreateStruct->parentLocationId,
'languageCode' => $languageCode,
]
);
}

$response->setData([
'access' => true,
]);
} catch (ApiException\UnauthorizedException $exception) {
$response->setData([
'access' => false,
'message' => $exception->getMessage(),
]);
}

return $response;
}
}

8 changes: 4 additions & 4 deletions src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,18 @@ ezplatform.user_profile.change_password:
# Content on the Fly
#

ezplatform.content.create.on_the_fly:
ezplatform.content_on_the_fly.create:
path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId}
methods: ['GET', 'POST']
defaults:
_controller: 'EzPlatformAdminUiBundle:Content:createOnTheFly'
_controller: 'EzPlatformAdminUiBundle:ContentOnTheFly:createContent'
options:
expose: true

ezplatform.content.create.on_the_fly.has_access:
ezplatform.content_on_the_fly.has_access:
path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId}/hasaccess
methods: ['GET']
defaults:
_controller: 'EzPlatformAdminUiBundle:Content:hasOnTheFlyCreateAccess'
_controller: 'EzPlatformAdminUiBundle::ContentOnTheFly:hasCreateAccess'
options:
expose: true

0 comments on commit e7d7b60

Please sign in to comment.