mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 21:13:57 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,33 +1,19 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 Champs Libres <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Class AdminController
|
||||
* Controller for the custom fields configuration section (in admin section)
|
||||
*
|
||||
* @package Chill\CustomFieldsBundle\Controller
|
||||
* Controller for the custom fields configuration section (in admin section).
|
||||
*/
|
||||
class AdminController extends AbstractController
|
||||
{
|
||||
@@ -35,4 +21,4 @@ class AdminController extends AbstractController
|
||||
{
|
||||
return $this->render('ChillCustomFieldsBundle:Admin:layout.html.twig');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,26 +1,28 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
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
|
||||
*
|
||||
* @package Chill\CustomFieldsBundle\Controller
|
||||
* Class CustomFieldController.
|
||||
*/
|
||||
class CustomFieldController extends AbstractController
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates a new CustomField entity.
|
||||
*
|
||||
*/
|
||||
public function createAction(Request $request)
|
||||
{
|
||||
@@ -34,95 +36,27 @@ class CustomFieldController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans('The custom field has been created'));
|
||||
->trans('The custom field has been created'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_show',
|
||||
array('id' => $entity->getCustomFieldsGroup()->getId())));
|
||||
return $this->redirect($this->generateUrl(
|
||||
'customfieldsgroup_show',
|
||||
['id' => $entity->getCustomFieldsGroup()->getId()]
|
||||
));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->get('translator')
|
||||
->trans("The custom field form contains errors"));
|
||||
->trans('The custom field form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', array(
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomField entity.
|
||||
*
|
||||
* @param CustomField $entity The entity
|
||||
* @param string
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomField $entity, $type)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfield_create',
|
||||
array('type' => $type)),
|
||||
'method' => 'POST',
|
||||
'type' => $type,
|
||||
'group_widget' => ($entity->getCustomFieldsGroup()) ? 'hidden' :'entity'
|
||||
));
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Create'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ($cfGroupId !== null) {
|
||||
$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', array(
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomField entity.
|
||||
*
|
||||
* @deprecated is not used since there is no link to show action
|
||||
*/
|
||||
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', array(
|
||||
'entity' => $entity, ));
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to edit an existing CustomField entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function editAction($id)
|
||||
{
|
||||
@@ -136,35 +70,67 @@ class CustomFieldController extends AbstractController
|
||||
|
||||
$editForm = $this->createEditForm($entity, $entity->getType());
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomField entity.
|
||||
*
|
||||
* @param CustomField $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomField $entity, $type)
|
||||
* Displays a form to create a new CustomField entity.
|
||||
*/
|
||||
public function newAction(Request $request)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfield_update', array('id' => $entity->getId())),
|
||||
'method' => 'PUT',
|
||||
'type' => $type,
|
||||
'group_widget' => 'hidden'
|
||||
));
|
||||
$entity = new CustomField();
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Update'));
|
||||
//add the custom field group if defined in URL
|
||||
$cfGroupId = $request->query->get('customFieldsGroup', null);
|
||||
|
||||
return $form;
|
||||
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)
|
||||
{
|
||||
@@ -183,17 +149,65 @@ class CustomFieldController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans("The custom field has been updated"));
|
||||
->trans('The custom field has been updated'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfield_edit', array('id' => $id)));
|
||||
return $this->redirect($this->generateUrl('customfield_edit', ['id' => $id]));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->get('translator')
|
||||
->trans("The custom field form contains errors"));
|
||||
->trans('The custom field form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@@ -1,47 +1,47 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Controller;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsDefaultGroup;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldsGroupType;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType as FormTypeCustomField;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Doctrine\ORM\Query;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Doctrine\ORM\Query;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsDefaultGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldsGroupType;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldType;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType as FormTypeCustomField;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Class CustomFieldsGroupController
|
||||
*
|
||||
* @package Chill\CustomFieldsBundle\Controller
|
||||
* Class CustomFieldsGroupController.
|
||||
*/
|
||||
class CustomFieldsGroupController extends AbstractController
|
||||
{
|
||||
|
||||
/**
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $customfieldProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
|
||||
/**
|
||||
* CustomFieldsGroupController constructor.
|
||||
*
|
||||
* @param CustomFieldProvider $customFieldProvider
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(
|
||||
CustomFieldProvider $customFieldProvider,
|
||||
@@ -50,76 +50,9 @@ class CustomFieldsGroupController extends AbstractController
|
||||
$this->customfieldProvider = $customFieldProvider;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all CustomFieldsGroup entities.
|
||||
*
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$cfGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findAll();
|
||||
$defaultGroups = $this->getDefaultGroupsId();
|
||||
|
||||
$makeDefaultFormViews = array();
|
||||
foreach ($cfGroups as $group) {
|
||||
if (!in_array($group->getId(), $defaultGroups)){
|
||||
$makeDefaultFormViews[$group->getId()] = $this->createMakeDefaultForm($group)->createView();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:index.html.twig', array(
|
||||
'entities' => $cfGroups,
|
||||
'default_groups' => $defaultGroups,
|
||||
'make_default_forms' => $makeDefaultFormViews
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of CustomFieldsGroupId which are marked as default
|
||||
* for their entity
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function getDefaultGroupsId()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$customFieldsGroupIds = $em->createQuery('SELECT g.id FROM '
|
||||
. 'ChillCustomFieldsBundle:CustomFieldsDefaultGroup d '
|
||||
. 'JOIN d.customFieldsGroup g')
|
||||
->getResult(Query::HYDRATE_SCALAR);
|
||||
|
||||
$result = array();
|
||||
foreach ($customFieldsGroupIds as $row) {
|
||||
$result[] = $row['id'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a form to make the group default
|
||||
*
|
||||
* @param CustomFieldsGroup $group
|
||||
* @return \Symfony\Component\Form\Form
|
||||
*/
|
||||
private function createMakeDefaultForm(CustomFieldsGroup $group = null)
|
||||
{
|
||||
return $this->createFormBuilder($group, array(
|
||||
'method' => 'POST',
|
||||
'action' => $this->generateUrl('customfieldsgroup_makedefault')
|
||||
))
|
||||
->add('id', HiddenType::class)
|
||||
->add('submit', SubmitType::class, array('label' => 'Make default'))
|
||||
->getForm();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CustomFieldsGroup entity.
|
||||
*
|
||||
*/
|
||||
public function createAction(Request $request)
|
||||
{
|
||||
@@ -133,101 +66,24 @@ class CustomFieldsGroupController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans("The custom fields group has been created"));
|
||||
->trans('The custom fields group has been created'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_show', array('id' => $entity->getId())));
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_show', ['id' => $entity->getId()]));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->translator
|
||||
->trans("The custom fields group form contains errors"));
|
||||
->trans('The custom fields group form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', array(
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfieldsgroup_create'),
|
||||
'method' => 'POST',
|
||||
));
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Create'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to create a new CustomFieldsGroup entity.
|
||||
*
|
||||
*/
|
||||
public function newAction()
|
||||
{
|
||||
$entity = new CustomFieldsGroup();
|
||||
$form = $this->createCreateForm($entity);
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomFieldsGroup entity.
|
||||
*
|
||||
*/
|
||||
public function showAction($id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
}
|
||||
|
||||
$options = $this->getOptionsAvailable($entity->getEntity());
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:show.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'create_field_form' => $this->createCreateFieldForm($entity)->createView(),
|
||||
'options' => $options
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of available key option for custom fields group
|
||||
* on the given entity
|
||||
*
|
||||
* @param string $entity the entity to filter
|
||||
*/
|
||||
private function getOptionsAvailable($entity)
|
||||
{
|
||||
$options = $this->getParameter('chill_custom_fields.'
|
||||
. 'customizables_entities');
|
||||
|
||||
foreach($options as $key => $definition) {
|
||||
if ($definition['class'] == $entity) {
|
||||
foreach ($definition['options'] as $key => $value) {
|
||||
yield $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
// [$entity->getEntity()];
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to edit an existing CustomFieldsGroup entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function editAction($id)
|
||||
{
|
||||
@@ -241,102 +97,42 @@ class CustomFieldsGroupController extends AbstractController
|
||||
|
||||
$editForm = $this->createEditForm($entity);
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfieldsgroup_update', array('id' => $entity->getId())),
|
||||
'method' => 'PUT',
|
||||
));
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Update'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
private function createCreateFieldForm(CustomFieldsGroup $customFieldsGroup)
|
||||
{
|
||||
|
||||
$fieldChoices = array();
|
||||
foreach ($this->customfieldProvider->getAllFields()
|
||||
as $key => $customType) {
|
||||
$fieldChoices[$key] = $customType->getName();
|
||||
}
|
||||
|
||||
$customfield = (new CustomField())
|
||||
->setCustomFieldsGroup($customFieldsGroup);
|
||||
|
||||
$builder = $this->get('form.factory')
|
||||
->createNamedBuilder(null, FormType::class, $customfield, array(
|
||||
'method' => 'GET',
|
||||
'action' => $this->generateUrl('customfield_new'),
|
||||
'csrf_protection' => false
|
||||
))
|
||||
->add('type', ChoiceType::class, array(
|
||||
'choices' => array_combine(array_values($fieldChoices),array_keys($fieldChoices)),
|
||||
))
|
||||
->add('customFieldsGroup', HiddenType::class)
|
||||
->add('submit', SubmitType::class);
|
||||
$builder->get('customFieldsGroup')
|
||||
->addViewTransformer(new CustomFieldsGroupToIdTransformer(
|
||||
$this->getDoctrine()->getManager()));
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an existing CustomFieldsGroup entity.
|
||||
*
|
||||
* Lists all CustomFieldsGroup entities.
|
||||
*/
|
||||
public function updateAction(Request $request, $id)
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
$cfGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findAll();
|
||||
$defaultGroups = $this->getDefaultGroupsId();
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
$makeDefaultFormViews = [];
|
||||
|
||||
foreach ($cfGroups as $group) {
|
||||
if (!in_array($group->getId(), $defaultGroups)) {
|
||||
$makeDefaultFormViews[$group->getId()] = $this->createMakeDefaultForm($group)->createView();
|
||||
}
|
||||
}
|
||||
|
||||
$editForm = $this->createEditForm($entity);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans("The custom fields group has been updated"));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_edit', array('id' => $id)));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->translator
|
||||
->trans("The custom fields group form contains errors"));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:index.html.twig', [
|
||||
'entities' => $cfGroups,
|
||||
'default_groups' => $defaultGroups,
|
||||
'make_default_forms' => $makeDefaultFormViews,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the CustomField Group with id $cFGroupId as default
|
||||
* Set the CustomField Group with id $cFGroupId as default.
|
||||
*/
|
||||
public function makeDefaultAction(Request $request)
|
||||
{
|
||||
|
||||
$form = $this->createMakeDefaultForm(null);
|
||||
$form->handleRequest($request);
|
||||
|
||||
@@ -346,16 +142,16 @@ class CustomFieldsGroupController extends AbstractController
|
||||
|
||||
$cFGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findOneById($cFGroupId);
|
||||
|
||||
if(!$cFGroup) {
|
||||
if (!$cFGroup) {
|
||||
throw $this
|
||||
->createNotFoundException("customFieldsGroup not found with "
|
||||
. "id $cFGroupId");
|
||||
->createNotFoundException('customFieldsGroup not found with '
|
||||
. "id {$cFGroupId}");
|
||||
}
|
||||
|
||||
$cFDefaultGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup')
|
||||
->findOneByEntity($cFGroup->getEntity());
|
||||
|
||||
if($cFDefaultGroup) {
|
||||
if ($cFDefaultGroup) {
|
||||
$em->remove($cFDefaultGroup);
|
||||
$em->flush(); /*this is necessary, if not doctrine
|
||||
* will not remove old entity before adding a new one,
|
||||
@@ -372,11 +168,25 @@ class CustomFieldsGroupController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans("The default custom fields group has been changed"));
|
||||
->trans('The default custom fields group has been changed'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to create a new CustomFieldsGroup entity.
|
||||
*/
|
||||
public function newAction()
|
||||
{
|
||||
$entity = new CustomFieldsGroup();
|
||||
$form = $this->createCreateForm($entity);
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function render the customFieldsGroup as a form.
|
||||
*
|
||||
@@ -397,33 +207,223 @@ class CustomFieldsGroupController extends AbstractController
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroups entity.');
|
||||
}
|
||||
|
||||
$form = $this->createForm(FormTypeCustomField::class, null, array('group' => $entity));
|
||||
$form->add('submit_dump', SubmitType::class, array('label' => 'POST AND DUMP'));
|
||||
$form->add('submit_render', SubmitType::class, array('label' => 'POST AND RENDER'));
|
||||
$form = $this->createForm(FormTypeCustomField::class, null, ['group' => $entity]);
|
||||
$form->add('submit_dump', SubmitType::class, ['label' => 'POST AND DUMP']);
|
||||
$form->add('submit_render', SubmitType::class, ['label' => 'POST AND RENDER']);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$this->get('twig.loader')
|
||||
->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/',
|
||||
$namespace = 'test');
|
||||
->addPath(
|
||||
__DIR__ . '/../Tests/Fixtures/App/app/Resources/views/',
|
||||
$namespace = 'test'
|
||||
);
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
if ($form->get('submit_render')->isClicked()) {
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render_for_test.html.twig', array(
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render_for_test.html.twig', [
|
||||
'fields' => $form->getData(),
|
||||
'customFieldsGroup' => $entity
|
||||
));
|
||||
'customFieldsGroup' => $entity,
|
||||
]);
|
||||
}
|
||||
|
||||
//dump($form->getData());
|
||||
//dump(json_enccode($form->getData()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $this
|
||||
->render('@test/CustomField/simple_form_render.html.twig', array(
|
||||
'form' => $form->createView()
|
||||
));
|
||||
->render('@test/CustomField/simple_form_render.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function showAction($id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
}
|
||||
|
||||
$options = $this->getOptionsAvailable($entity->getEntity());
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:show.html.twig', [
|
||||
'entity' => $entity,
|
||||
'create_field_form' => $this->createCreateFieldForm($entity)->createView(),
|
||||
'options' => $options,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an existing CustomFieldsGroup entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function updateAction(Request $request, $id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
}
|
||||
|
||||
$editForm = $this->createEditForm($entity);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans('The custom fields group has been updated'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_edit', ['id' => $id]));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->translator
|
||||
->trans('The custom fields group form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function createCreateFieldForm(CustomFieldsGroup $customFieldsGroup)
|
||||
{
|
||||
$fieldChoices = [];
|
||||
|
||||
foreach ($this->customfieldProvider->getAllFields()
|
||||
as $key => $customType) {
|
||||
$fieldChoices[$key] = $customType->getName();
|
||||
}
|
||||
|
||||
$customfield = (new CustomField())
|
||||
->setCustomFieldsGroup($customFieldsGroup);
|
||||
|
||||
$builder = $this->get('form.factory')
|
||||
->createNamedBuilder(null, FormType::class, $customfield, [
|
||||
'method' => 'GET',
|
||||
'action' => $this->generateUrl('customfield_new'),
|
||||
'csrf_protection' => false,
|
||||
])
|
||||
->add('type', ChoiceType::class, [
|
||||
'choices' => array_combine(array_values($fieldChoices), array_keys($fieldChoices)),
|
||||
])
|
||||
->add('customFieldsGroup', HiddenType::class)
|
||||
->add('submit', SubmitType::class);
|
||||
$builder->get('customFieldsGroup')
|
||||
->addViewTransformer(new CustomFieldsGroupToIdTransformer(
|
||||
$this->getDoctrine()->getManager()
|
||||
));
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, [
|
||||
'action' => $this->generateUrl('customfieldsgroup_create'),
|
||||
'method' => 'POST',
|
||||
]);
|
||||
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Create']);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, [
|
||||
'action' => $this->generateUrl('customfieldsgroup_update', ['id' => $entity->getId()]),
|
||||
'method' => 'PUT',
|
||||
]);
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Update']);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a form to make the group default.
|
||||
*
|
||||
* @param CustomFieldsGroup $group
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form
|
||||
*/
|
||||
private function createMakeDefaultForm(?CustomFieldsGroup $group = null)
|
||||
{
|
||||
return $this->createFormBuilder($group, [
|
||||
'method' => 'POST',
|
||||
'action' => $this->generateUrl('customfieldsgroup_makedefault'),
|
||||
])
|
||||
->add('id', HiddenType::class)
|
||||
->add('submit', SubmitType::class, ['label' => 'Make default'])
|
||||
->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of CustomFieldsGroupId which are marked as default
|
||||
* for their entity.
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function getDefaultGroupsId()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$customFieldsGroupIds = $em->createQuery('SELECT g.id FROM '
|
||||
. 'ChillCustomFieldsBundle:CustomFieldsDefaultGroup d '
|
||||
. 'JOIN d.customFieldsGroup g')
|
||||
->getResult(Query::HYDRATE_SCALAR);
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($customFieldsGroupIds as $row) {
|
||||
$result[] = $row['id'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of available key option for custom fields group
|
||||
* on the given entity.
|
||||
*
|
||||
* @param string $entity the entity to filter
|
||||
*/
|
||||
private function getOptionsAvailable($entity)
|
||||
{
|
||||
$options = $this->getParameter('chill_custom_fields.'
|
||||
. 'customizables_entities');
|
||||
|
||||
foreach ($options as $key => $definition) {
|
||||
if ($definition['class'] == $entity) {
|
||||
foreach ($definition['options'] as $key => $value) {
|
||||
yield $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
// [$entity->getEntity()];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user