<?php
namespace App\Ox\HoardBundle\Controller\Admin;
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\AncientPlace;
use App\Ox\HoardBundle\Form\AncientPlaceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* AncientPlace controller.
*
* @Route("/ancientplace")
*/
class AncientPlaceController extends AbstractController
{
/**
* Lists all AncientPlace entities.
*
* @Route("/", name="admin_ancientplace", methods={"GET"})
* @Template("@OxHoardBundle/admin/ancientPlace/index.html.twig")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('OxHoardBundle:AncientPlace')->findBy(array(), array('sortValue' => 'DESC', 'id' => 'ASC'));
return array(
'can_edit' => $this->canEditEntries(),
'entities' => $entities,
);
}
/**
* Creates a new AncientPlace entity.
*
* @Route("/", name="admin_ancientplace_create", methods={"POST"})
* @Template("@OxHoardBundle/admin/ancientPlace/new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new AncientPlace();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('admin_ancientplace_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a AncientPlace entity.
*
* @param AncientPlace $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(AncientPlace $entity)
{
$form = $this->createForm(AncientPlaceType::class, $entity, array(
'action' => $this->generateUrl('admin_ancientplace_create'),
'method' => 'POST',
));
$form->add('submit', SubmitType::class, array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new AncientPlace entity.
*
* @Route("/new", name="admin_ancientplace_new", methods={"GET"})
* @Template("@OxHoardBundle/admin/ancientPlace/new.html.twig")
*/
public function newAction()
{
$entity = new AncientPlace();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a AncientPlace entity.
*
* @Route("/{id}", name="admin_ancientplace_show", methods={"GET"})
* @Template("@OxHoardBundle/admin/ancientPlace/show.html.twig")
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find AncientPlace entity.');
}
if(!$this->getUser()) {
$isAuthenticated = false;
} else {
$isAuthenticated = true;
}
$hoards = [];
foreach($entity->getHoards() as $hoard) {
$hoardId = $hoard->getId();
$em->getFilters()->disable('softdeleteable');
$hoard = $em->getRepository('OxHoardBundle:Hoard')->find($hoardId);
if ($isAuthenticated || $hoard->getValidatedByUser()) {
$hoards[] = $hoard;
}
usort($hoards, function ($a, $b) {
return strtolower($a) > strtolower($b);
});
$em->getFilters()->enable('softdeleteable');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'isAuthenticated' => $isAuthenticated,
'can_edit' => $this->canEditEntries(),
'entity' => $entity,
'hoards' => $hoards,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing AncientPlace entity.
*
* @Route("/{id}/edit", name="admin_ancientplace_edit", methods={"GET"})
* @Template("@OxHoardBundle/admin/ancientPlace/edit.html.twig")
*/
public function editAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find AncientPlace entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
$hoards = $request->getSession()->get('hoards');
$request->getSession()->clear('hoards');
return array(
'hoards' => $hoards,
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a AncientPlace entity.
*
* @param AncientPlace $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(AncientPlace $entity)
{
$form = $this->createForm(AncientPlaceType::class, $entity, array(
'action' => $this->generateUrl('admin_ancientplace_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', SubmitType::class, array('label' => 'Update'));
return $form;
}
/**
* Edits an existing AncientPlace entity.
*
* @Route("/{id}", name="admin_ancientplace_update", methods={"PUT"})
* @Template("@OxHoardBundle/admin/ancientPlace/edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find AncientPlace entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
$hoards = [];
if ($editForm->isValid()) {
$em->flush();
$request->getSession()
->getFlashBag()
->add('success', 'Entry has been updated');
return $this->redirect($this->generateUrl('admin_ancientplace_show', array('id' => $id)));
}
return array(
'hoards' => $hoards,
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a AncientPlace entity.
*
* @Route("/{id}", name="admin_ancientplace_delete", methods={"DELETE"})
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
try {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find AncientPlace entity.');
}
// check whether it is in use
$query = $em -> createQuery("SELECT h from OxHoardBundle:Hoard h WHERE h.ancientPlace = :query")
->setParameter('query', $id);
$hoards = $query->getResult();
if(count($hoards) > 0) {
$request->getSession()->set('hoards', $hoards);
return $this->redirect($this->generateUrl('admin_ancientplace_edit', array('id' => $entity->getId())));
} else {
$em->remove($entity);
$em->flush();
return $this->redirect($this->generateUrl('admin_ancientplace'));
}
} catch (\Exception $e) {
$request->getSession()
->getFlashBag()
->add('error', 'Unable to delete AncientPlace. It may currently be in use.');
return $this->redirect($this->generateUrl('admin_ancientplace_edit', array('id' => $entity->getId())));
}
}
}
/**
* Creates a form to delete a AncientPlace 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('admin_ancientplace_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', SubmitType::class, array(
'label' => 'Delete',
'attr' => array( 'class' => 'delete-button btn btn-danger')
))
->getForm()
;
}
private function canEditEntries() {
return $this->userIsListeditor();
}
private function userIsListeditor() {
if($this->getUser() && ($this->getUser()->hasRole('ROLE_LIST_EDITOR') || $this->getUser()->hasRole('ROLE_ADMIN') || $this->getUser()->hasRole('ROLE_SUPER_ADMIN')))
{
return true;
}
return false;
}
}