187 lines
4.8 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Form\ScopeType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ScopeController.
*/
class ScopeController extends AbstractController
{
/**
* Creates a new Scope entity.
*/
public function createAction(Request $request)
{
$scope = new Scope();
$form = $this->createCreateForm($scope);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($scope);
$em->flush();
return $this->redirect($this->generateUrl('admin_scope_show', ['id' => $scope->getId()]));
}
return $this->render('@ChillMain/Scope/new.html.twig', [
'entity' => $scope,
'form' => $form->createView(),
]);
}
/**
* Displays a form to edit an existing Scope entity.
*
* @param mixed $id
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$scope = $em->getRepository(\Chill\MainBundle\Entity\Scope::class)->find($id);
if (!$scope) {
throw $this->createNotFoundException('Unable to find Scope entity.');
}
$editForm = $this->createEditForm($scope);
return $this->render('@ChillMain/Scope/edit.html.twig', [
'entity' => $scope,
'edit_form' => $editForm->createView(),
]);
}
/**
* Lists all Scope entities.
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository(\Chill\MainBundle\Entity\Scope::class)->findAll();
return $this->render('@ChillMain/Scope/index.html.twig', [
'entities' => $entities,
]);
}
/**
* Displays a form to create a new Scope entity.
*/
public function newAction()
{
$scope = new Scope();
$form = $this->createCreateForm($scope);
return $this->render('@ChillMain/Scope/new.html.twig', [
'entity' => $scope,
'form' => $form->createView(),
]);
}
/**
* Finds and displays a Scope entity.
*
* @param mixed $id
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$scope = $em->getRepository(\Chill\MainBundle\Entity\Scope::class)->find($id);
if (!$scope) {
throw $this->createNotFoundException('Unable to find Scope entity.');
}
return $this->render('@ChillMain/Scope/show.html.twig', [
'entity' => $scope,
]);
}
/**
* Edits an existing Scope entity.
*
* @param mixed $id
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$scope = $em->getRepository(\Chill\MainBundle\Entity\Scope::class)->find($id);
if (!$scope) {
throw $this->createNotFoundException('Unable to find Scope entity.');
}
$editForm = $this->createEditForm($scope);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('admin_scope_edit', ['id' => $id]));
}
return $this->render('@ChillMain/Scope/edit.html.twig', [
'entity' => $scope,
'edit_form' => $editForm->createView(),
]);
}
/**
* Creates a form to create a Scope entity.
*
* @param Scope $scope The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Scope $scope)
{
$form = $this->createForm(ScopeType::class, $scope, [
'action' => $this->generateUrl('admin_scope_create'),
'method' => 'POST',
]);
$form->add('submit', SubmitType::class, ['label' => 'Create']);
return $form;
}
/**
* Creates a form to edit a Scope entity.
*
* @param Scope $scope The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Scope $scope)
{
$form = $this->createForm(ScopeType::class, $scope, [
'action' => $this->generateUrl('admin_scope_update', ['id' => $scope->getId()]),
'method' => 'PUT',
]);
$form->add('submit', SubmitType::class, ['label' => 'Update']);
return $form;
}
}