forked from sulu/SuluArticleBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented page document and API (sulu#114)
* implemented basic page document * added index pages to article
- Loading branch information
1 parent
6311862
commit b4d7192
Showing
29 changed files
with
1,593 additions
and
59 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,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); | ||
} | ||
|
||
$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; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.