Skip to content

Commit

Permalink
EZP-28841: Content On the Fly v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Mar 8, 2018
1 parent c4bb6a8 commit 1406559
Show file tree
Hide file tree
Showing 129 changed files with 768 additions and 141 deletions.
152 changes: 152 additions & 0 deletions src/bundle/Controller/ContentOnTheFlyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?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 ContentOnTheFlyController 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;
}
}
14 changes: 10 additions & 4 deletions src/bundle/ParamConverter/ContentTypeParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class ContentTypeParamConverter implements ParamConverterInterface
{
const PARAMETER_CONTENT_TYPE_ID = 'contentTypeId';
const PARAMETER_CONTENT_TYPE_IDENTIFIER = 'contentTypeIdentifier';

/** @var ContentTypeService */
private $contentTypeService;
Expand All @@ -40,15 +41,20 @@ public function __construct(ContentTypeService $contentTypeGroupService, array $
*/
public function apply(Request $request, ParamConverter $configuration)
{
if (!$request->get(self::PARAMETER_CONTENT_TYPE_ID)) {
if (!$request->get(self::PARAMETER_CONTENT_TYPE_ID) && !$request->get(self::PARAMETER_CONTENT_TYPE_IDENTIFIER)) {
return false;
}

$id = (int)$request->get(self::PARAMETER_CONTENT_TYPE_ID);
if ($request->get(self::PARAMETER_CONTENT_TYPE_ID)) {
$id = (int)$request->get(self::PARAMETER_CONTENT_TYPE_ID);
$contentType = $this->contentTypeService->loadContentType($id, $this->siteAccessLanguages);
} elseif ($request->get(self::PARAMETER_CONTENT_TYPE_IDENTIFIER)) {
$identifier = $request->get(self::PARAMETER_CONTENT_TYPE_IDENTIFIER);
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($identifier, $this->siteAccessLanguages);
}

$contentType = $this->contentTypeService->loadContentType($id, $this->siteAccessLanguages);
if (!$contentType) {
throw new NotFoundHttpException("ContentType $id not found!");
throw new NotFoundHttpException('ContentType ' . ($id ?? $identifier) . ' not found!');
}

$request->attributes->set($configuration->getName(), $contentType);
Expand Down
3 changes: 3 additions & 0 deletions src/bundle/Resources/config/default_parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ parameters:
ezsettings.admin_group.user_edit.templates.update: 'EzPlatformAdminUiBundle:content/content_edit:user_edit.html.twig'
ezsettings.admin_group.user_edit.templates.create: 'EzPlatformAdminUiBundle:content/content_edit:user_create.html.twig'

ezsettings.admin_group.content_on_the_fly.templates.create: 'EzPlatformAdminUiBundle:content/content_on_the_fly:content_create_on_the_fly.html.twig'
ezsettings.default.content_on_the_fly.templates.create: '%ezsettings.admin_group.content_on_the_fly.templates.create%'

ezsettings.global.system_info_view:
pjax_tab:
composer:
Expand Down
21 changes: 21 additions & 0 deletions src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,28 @@ ezplatform.user.delete:
#
# Profile
#

ezplatform.user_profile.change_password:
path: /user/change-password
defaults:
_controller: 'EzPlatformAdminUiBundle:UserProfile\UserPasswordChange:userPasswordChange'

#
# Content on the Fly
#

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

ezplatform.content_on_the_fly.has_access:
path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId}/hasaccess
methods: ['GET']
defaults:
_controller: 'EzPlatformAdminUiBundle::ContentOnTheFly:hasCreateAccess'
options:
expose: true
10 changes: 10 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ services:
arguments: [ '@request_stack' ]
tags:
- { name: knp_menu.voter }

EzSystems\EzPlatformAdminUi\RepositoryForms\View\ViewTemplatesListener:
tags:
- { name: kernel.event_subscriber }
calls:
- [setViewTemplate, ['EzSystems\EzPlatformAdminUi\RepositoryForms\View\ContentCreateOnTheFlyView', '$content_on_the_fly.templates.create$']]

EzSystems\EzPlatformAdminUi\RepositoryForms\Dispatcher\ContentOnTheFlyDispatcher:
calls:
- [setEventDispatcher, ["@event_dispatcher"]]

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/bundle/Resources/public/js/alloyeditor/dist/ezBtnEmbed.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/bundle/Resources/public/js/alloyeditor/dist/ezBtnImage.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 1406559

Please sign in to comment.