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 7, 2018
1 parent 1c007dd commit 47070f8
Show file tree
Hide file tree
Showing 129 changed files with 714 additions and 142 deletions.
115 changes: 114 additions & 1 deletion src/bundle/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions as ApiException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
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;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentEditData;
Expand All @@ -18,6 +23,13 @@
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 @@ -35,6 +47,9 @@ class ContentController extends Controller
/** @var ContentService */
private $contentService;

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

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

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

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

private $contentActionDispatcher;

/**
* @param NotificationHandlerInterface $notificationHandler
* @param ContentService $contentService
Expand All @@ -62,18 +82,24 @@ 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 @@ -115,6 +141,93 @@ 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
*
* @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,
]
);
}

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

return $response;
}

/**
* @param Request $request
*
Expand Down
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 @@ -539,7 +539,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.create.on_the_fly:
path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId}
methods: ['GET', 'POST']
defaults:
_controller: 'EzPlatformAdminUiBundle:Content:createOnTheFly'
options:
expose: true

ezplatform.content.create.on_the_fly.has_access:
path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId}/hasaccess
methods: ['GET']
defaults:
_controller: 'EzPlatformAdminUiBundle:Content:hasOnTheFlyCreateAccess'
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"]]
Loading

0 comments on commit 47070f8

Please sign in to comment.