-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?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 | ||
* | ||
* @throws ApiException\BadStateException | ||
* @throws ApiException\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; | ||
} | ||
} |
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.