-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move Content on the Fly to dedicated controller
- Loading branch information
1 parent
d86e1bd
commit e7d7b60
Showing
3 changed files
with
158 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters