-
Notifications
You must be signed in to change notification settings - Fork 80
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
Implemented page document and API #114
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Controller; | ||
|
||
use FOS\RestBundle\Routing\ClassResourceInterface; | ||
use JMS\Serializer\SerializationContext; | ||
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument; | ||
use Sulu\Bundle\ArticleBundle\Document\Form\ArticlePageDocumentType; | ||
use Sulu\Bundle\ArticleBundle\Exception\ParameterNotAllowedException; | ||
use Sulu\Component\Content\Form\Exception\InvalidFormException; | ||
use Sulu\Component\Content\Mapper\ContentMapperInterface; | ||
use Sulu\Component\DocumentManager\DocumentManagerInterface; | ||
use Sulu\Component\Rest\Exception\MissingParameterException; | ||
use Sulu\Component\Rest\RequestParametersTrait; | ||
use Sulu\Component\Rest\RestController; | ||
use Sulu\Component\Security\SecuredControllerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Provides API for article-pages. | ||
*/ | ||
class ArticlePageController extends RestController implements ClassResourceInterface, SecuredControllerInterface | ||
{ | ||
const DOCUMENT_TYPE = 'article_page'; | ||
|
||
use RequestParametersTrait; | ||
|
||
/** | ||
* Returns single article-page. | ||
* | ||
* @param string $articleUuid | ||
* @param string $uuid | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function getAction($articleUuid, $uuid, Request $request) | ||
{ | ||
$locale = $this->getRequestParameter($request, 'locale', true); | ||
$document = $this->getDocumentManager()->find( | ||
$uuid, | ||
$locale, | ||
[ | ||
'load_ghost_content' => true, | ||
'load_shadow_content' => false, | ||
] | ||
); | ||
|
||
return $this->handleView( | ||
$this->view($document)->setSerializationContext( | ||
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage']) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Create article-page. | ||
* | ||
* @param string $articleUuid | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function postAction($articleUuid, Request $request) | ||
{ | ||
$action = $request->get('action'); | ||
$document = $this->getDocumentManager()->create(self::DOCUMENT_TYPE); | ||
$locale = $this->getRequestParameter($request, 'locale', true); | ||
$data = $request->request->all(); | ||
|
||
$this->persistDocument($data, $document, $locale, $articleUuid); | ||
$this->handleActionParameter($action, $document, $locale); | ||
$this->getDocumentManager()->flush(); | ||
|
||
return $this->handleView( | ||
$this->view($document)->setSerializationContext( | ||
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage']) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Update article-page. | ||
* | ||
* @param string $articleUuid | ||
* @param string $uuid | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function putAction($articleUuid, $uuid, Request $request) | ||
{ | ||
$locale = $this->getRequestParameter($request, 'locale', true); | ||
$action = $request->get('action'); | ||
$data = $request->request->all(); | ||
|
||
$document = $this->getDocumentManager()->find( | ||
$uuid, | ||
$locale, | ||
[ | ||
'load_ghost_content' => false, | ||
'load_shadow_content' => false, | ||
] | ||
); | ||
|
||
$this->get('sulu_hash.request_hash_checker')->checkHash($request, $document, $document->getUuid()); | ||
|
||
$this->persistDocument($data, $document, $locale, $articleUuid); | ||
$this->handleActionParameter($action, $document, $locale); | ||
$this->getDocumentManager()->flush(); | ||
|
||
return $this->handleView( | ||
$this->view($document)->setSerializationContext( | ||
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage']) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Delete article-page. | ||
* | ||
* @param string $articleUuid | ||
* @param string $uuid | ||
* | ||
* @return Response | ||
*/ | ||
public function deleteAction($articleUuid, $uuid) | ||
{ | ||
$documentManager = $this->getDocumentManager(); | ||
$document = $documentManager->find($uuid); | ||
$documentManager->remove($document); | ||
$documentManager->flush(); | ||
|
||
return $this->handleView($this->view(null)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSecurityContext() | ||
{ | ||
return ArticleAdmin::SECURITY_CONTEXT; | ||
} | ||
|
||
/** | ||
* Persists the document using the given information. | ||
* | ||
* @param array $data | ||
* @param object $document | ||
* @param string $locale | ||
* @param string $articleUuid | ||
* | ||
* @throws InvalidFormException | ||
* @throws MissingParameterException | ||
* @throws ParameterNotAllowedException | ||
*/ | ||
private function persistDocument($data, $document, $locale, $articleUuid) | ||
{ | ||
if (array_key_exists('title', $data)) { | ||
throw new ParameterNotAllowedException('title', ArticlePageDocument::class); | ||
} | ||
if (array_key_exists('template', $data)) { | ||
throw new ParameterNotAllowedException('template', ArticlePageDocument::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least until now we always ignored extra parameters... There's the same happening for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the problem here is that this has a different meaning now title is the article-title and it cannot be changed over this controller. |
||
} | ||
|
||
$article = $this->getDocumentManager()->find($articleUuid, $locale); | ||
$data['template'] = $article->getStructureType(); | ||
|
||
$form = $this->createForm( | ||
ArticlePageDocumentType::class, | ||
$document, | ||
[ | ||
// disable csrf protection, since we can't produce a token, because the form is cached on the client | ||
'csrf_protection' => false, | ||
] | ||
); | ||
$form->submit($data, false); | ||
|
||
$document->setParent($article); | ||
|
||
if (!$form->isValid()) { | ||
throw new InvalidFormException($form); | ||
} | ||
|
||
$this->getDocumentManager()->persist( | ||
$document, | ||
$locale, | ||
[ | ||
'user' => $this->getUser()->getId(), | ||
'clear_missing_content' => false, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Returns document-manager. | ||
* | ||
* @return DocumentManagerInterface | ||
*/ | ||
protected function getDocumentManager() | ||
{ | ||
return $this->get('sulu_document_manager.document_manager'); | ||
} | ||
|
||
/** | ||
* @return ContentMapperInterface | ||
*/ | ||
protected function getMapper() | ||
{ | ||
return $this->get('sulu.content.mapper'); | ||
} | ||
|
||
/** | ||
* Delegates actions by given actionParameter, which can be retrieved from the request. | ||
* | ||
* @param string $actionParameter | ||
* @param object $document | ||
* @param string $locale | ||
*/ | ||
private function handleActionParameter($actionParameter, $document, $locale) | ||
{ | ||
switch ($actionParameter) { | ||
case 'publish': | ||
$this->getDocumentManager()->publish($document, $locale); | ||
break; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍