216 lines
6.4 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\CustomFieldsBundle\Controller;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Chill\CustomFieldsBundle\Form\CustomFieldType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
/**
* Class CustomFieldController.
*/
class CustomFieldController extends AbstractController
{
/**
* Creates a new CustomField entity.
*/
public function createAction(Request $request)
{
$entity = new CustomField();
$form = $this->createCreateForm($entity, $request->query->get('type', null));
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$this->addFlash('success', $this->get('translator')
->trans('The custom field has been created'));
return $this->redirect($this->generateUrl(
'customfieldsgroup_show',
['id' => $entity->getCustomFieldsGroup()->getId()]
));
}
$this->addFlash('error', $this->get('translator')
->trans('The custom field form contains errors'));
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', [
'entity' => $entity,
'form' => $form->createView(),
]);
}
/**
* Displays a form to edit an existing CustomField entity.
*
* @param mixed $id
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository(CustomField::class)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find CustomField entity.');
}
$editForm = $this->createEditForm($entity, $entity->getType());
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', [
'entity' => $entity,
'edit_form' => $editForm->createView(),
]);
}
/**
* Displays a form to create a new CustomField entity.
*/
public function newAction(Request $request)
{
$entity = new CustomField();
//add the custom field group if defined in URL
$cfGroupId = $request->query->get('customFieldsGroup', null);
if (null !== $cfGroupId) {
$cfGroup = $this->getDoctrine()->getManager()
->getRepository(CustomFieldsGroup::class)
->find($cfGroupId);
if (!$cfGroup) {
throw $this->createNotFoundException('CustomFieldsGroup with id '
. $cfGroupId . ' is not found !');
}
$entity->setCustomFieldsGroup($cfGroup);
}
$form = $this->createCreateForm($entity, $request->query->get('type'));
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', [
'entity' => $entity,
'form' => $form->createView(),
]);
}
/**
* Finds and displays a CustomField entity.
*
* @deprecated is not used since there is no link to show action
*
* @param mixed $id
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository(CustomField::class)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find CustomField entity.');
}
return $this->render('ChillCustomFieldsBundle:CustomField:show.html.twig', [
'entity' => $entity, ]);
}
/**
* Edits an existing CustomField entity.
*
* @param mixed $id
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomField::class)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find CustomField entity.');
}
$editForm = $this->createEditForm($entity, $entity->getType());
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
$this->addFlash('success', $this->get('translator')
->trans('The custom field has been updated'));
return $this->redirect($this->generateUrl('customfield_edit', ['id' => $id]));
}
$this->addFlash('error', $this->get('translator')
->trans('The custom field form contains errors'));
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', [
'entity' => $entity,
'edit_form' => $editForm->createView(),
]);
}
/**
* Creates a form to create a CustomField entity.
*
* @param CustomField $entity The entity
* @param string
* @param mixed $type
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(CustomField $entity, $type)
{
$form = $this->createForm(CustomFieldType::class, $entity, [
'action' => $this->generateUrl(
'customfield_create',
['type' => $type]
),
'method' => 'POST',
'type' => $type,
'group_widget' => ($entity->getCustomFieldsGroup()) ? 'hidden' : 'entity',
]);
$form->add('submit', SubmitType::class, ['label' => 'Create']);
return $form;
}
/**
* Creates a form to edit a CustomField entity.
*
* @param CustomField $entity The entity
* @param mixed $type
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(CustomField $entity, $type)
{
$form = $this->createForm(CustomFieldType::class, $entity, [
'action' => $this->generateUrl('customfield_update', ['id' => $entity->getId()]),
'method' => 'PUT',
'type' => $type,
'group_widget' => 'hidden',
]);
$form->add('submit', SubmitType::class, ['label' => 'Update']);
return $form;
}
}