<?php
namespace App\Ox\HoardBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use App\Ox\HoardBundle\Entity\ContentPage;
use App\Ox\HoardBundle\Form\ContentPageType;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
/**
* ContentPage controller.
*
* @Route("/content")
*/
class ContentPageController extends AbstractController
{
/**
* Lists all ContentPage entities.
*
* @Route("/index", name="content", methods={"GET"})
* @Template("@OxHoardBundle/contentPage/index.html.twig")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('OxHoardBundle:ContentPage')->findAll();
return array(
'entities' => $entities,
'is_authorised_to_edit' => $this->userIsAdmin(),
);
}
/**
* Lists all ContentPage entities of type News Items.
*
* @Route("/news", name="news", methods={"GET"})
* @Template("@OxHoardBundle/contentPage/news_list.html.twig")
*/
public function newsItemsAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('OxHoardBundle:ContentPage')->findByType(0);
return array(
'entities' => $entities,
'is_authorised_to_edit' => $this->userIsAdmin(),
);
}
/**
* Creates a new ContentPage entity.
*
* @Route("/create", name="content_create", methods={"POST"})
* @Template("@OxHoardBundle/contentPage/new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new ContentPage();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('content_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a ContentPage entity.
*
* @param ContentPage $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(ContentPage $entity)
{
$form = $this->createForm(ContentPageType::class, $entity, array(
'action' => $this->generateUrl('content_create'),
'method' => 'POST',
));
$form->add('submit', SubmitType::class, array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new ContentPage entity.
*
* @Route("/new", name="content_new", methods={"GET"})
* @Template("@OxHoardBundle/contentPage/new.html.twig")
*/
public function newAction()
{
$entity = new ContentPage();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a ContentPage entity.
*
* @Route("/{id}", name="content_show", requirements={
* "id": "\d+"
* }, methods={"GET"})
* @Template("@OxHoardBundle/contentPage/show.html.twig")
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:ContentPage')->find($id);
return $this->present($entity);
}
/**
* Finds and displays a ContentPage entity by path.
*
* @Route("/{path}", name="content_show_path", methods={"GET"})
* @Template("@OxHoardBundle/contentPage/show.html.twig")
*/
public function showByPathAction($path)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:ContentPage')->findOneByPath($path);
return $this->present($entity);
}
private function present($contentItem)
{
if (!$contentItem) {
throw $this->createNotFoundException('Unable to find ContentPage entity.');
}
$deleteForm = $this->createDeleteForm($contentItem->getId());
return array(
'entity' => $contentItem,
'delete_form' => $deleteForm->createView(),
'is_authorised_to_edit' => $this->checkAccess($contentItem, 'edit', false),
);
}
/**
* Displays a form to edit an existing ContentPage entity.
*
* @Route("/{id}/edit", name="content_edit", methods={"GET"})
* @Template("@OxHoardBundle/contentPage/edit.html.twig")
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:ContentPage')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find ContentPage entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a ContentPage entity.
*
* @param ContentPage $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(ContentPage $entity)
{
$form = $this->createForm(ContentPageType::class, $entity, array(
'action' => $this->generateUrl('content_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', SubmitType::class, array('label' => 'Update'));
return $form;
}
/**
* Edits an existing ContentPage entity.
*
* @Route("/{id}", name="content_update", methods={"PUT"})
* @Template("@OxHoardBundle/contentPage/edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:ContentPage')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find ContentPage entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('content_show_path', array('path' => $entity->getPath())));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a ContentPage entity.
*
* @Route("/{id}", name="content_delete", methods={"DELETE"})
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:ContentPage')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find ContentPage entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('content'));
}
/**
* Creates a form to delete a ContentPage entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('content_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', ButtonType::class, array(
'label' => 'Delete this content',
'attr' => array(
'class' => 'delete-button btn-danger'
)
))
->getForm();
}
/**
* checks permission of user's current request
*
* @param mixed $entity The entity being validated
*
* @param string $attribute - 'view' or 'edit' or 'delete'
*
* @param boolean $throwException - whether to throw an exception if false - defaults to true
*
* @return boolean
*
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
*/
private function checkAccess($entity, $attribute, $throwException = true) {
// call security voter(s)
if (!$this->userIsAdmin()) {
if ($throwException) {
throw new AccessDeniedException('Unauthorised access!');
}
return false;
}
return true;
}
private function userIsAdmin() {
if($this->getUser() && ($this->getUser()->hasRole('ROLE_ADMIN') || $this->getUser()->hasRole('ROLE_SUPER_ADMIN')))
{
return true;
}
return false;
}
}