Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-28841: Content On the Fly v2 #366

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions src/bundle/Controller/ContentOnTheFlyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?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;

class ContentOnTheFlyController extends Controller
{
/** @var ContentService */
private $contentService;

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

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

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

/**
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\LanguageService $languageService
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \EzSystems\EzPlatformAdminUi\RepositoryForms\Dispatcher\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 \Symfony\Component\HttpFoundation\Request $request
* @param string $languageCode
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation
*
* @return \EzSystems\EzPlatformAdminUi\RepositoryForms\View\ContentCreateOnTheFlyView|null|\Symfony\Component\HttpFoundation\Response
*
* @throws \eZ\Publish\API\Repository\Exceptions\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('@EzPlatformAdminUi/content/content_on_the_fly/content_create_on_the_fly.html.twig', [
'form' => $form->createView(),
'language' => $language,
'contentType' => $contentType,
'parentLocation' => $parentLocation,
]);
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $languageCode
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
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
22 changes: 22 additions & 0 deletions src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ ezplatform.user.delete:
#
# Profile
#

ezplatform.user_profile.change_password:
path: /user/change-password
defaults:
Expand All @@ -548,6 +549,7 @@ ezplatform.user_profile.change_password:
#
# Custom URL alias
#

ezplatform.custom_url.add:
path: /url-alias/add
methods: ['POST']
Expand All @@ -559,3 +561,23 @@ ezplatform.custom_url.remove:
methods: ['POST']
defaults:
_controller: 'EzPlatformAdminUiBundle:UrlAlias:remove'

#
# Content on the Fly
#

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

ezplatform.content_on_the_fly.has_access:
path: /content/create/on-the-fly/{contentTypeIdentifier}/{languageCode}/{locationId}/has-access
methods: ['GET']
defaults:
_controller: 'EzPlatformAdminUiBundle:ContentOnTheFly:hasCreateAccess'
options:
expose: true
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ services:

EzSystems\EzPlatformAdminUi\Form\EventListener\:
resource: '../../../lib/Form/EventListener'

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.

Loading