Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions Controller/DependentFilteredEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,34 @@ public function getOptionsAction()
$entity_inf = $entities[$entity_alias];

if ($entity_inf['role'] !== 'IS_AUTHENTICATED_ANONYMOUSLY'){
if (false === $this->get('security.context')->isGranted( $entity_inf['role'] )) {
if (false === $this->get('security.authorization_checker')->isGranted( $entity_inf['role'] )) {
throw new AccessDeniedException();
}
}

$classMetaData = $em->getClassMetadata($entity_inf['class']);
$associationsMappings = $classMetaData->getAssociationMappings();

$qb = $this->getDoctrine()
->getRepository($entity_inf['class'])
->createQueryBuilder('e')
->where('e.' . $entity_inf['parent_property'] . ' = :parent_id')
->orderBy('e.' . $entity_inf['order_property'], $entity_inf['order_direction'])
->setParameter('parent_id', $parent_id);
->createQueryBuilder('e');

if(isset($associationsMappings[$entity_inf['parent_property']])){ // if association can have multiple we join and filter by target entity identifier
$qb->join('e.' . $entity_inf['parent_property'], 'r');

$targetClassMetaData = $em->getClassMetadata($associationsMappings[$entity_inf['parent_property']]['targetEntity']);

if (is_array($parent_id)) {
$qb->where('r.' . $targetClassMetaData->getIdentifierFieldNames()[0] . ' IN (:parent_id)');
} else {
$qb->where('r.' . $targetClassMetaData->getIdentifierFieldNames()[0] . ' = :parent_id');
}
}else{
$qb->where('e.' . $entity_inf['parent_property'] . ' = :parent_id');
}

$qb->orderBy('e.' . $entity_inf['order_property'], $entity_inf['order_direction'])
->setParameter('parent_id', $parent_id);


if (null !== $entity_inf['callback']) {
Expand All @@ -61,7 +78,10 @@ public function getOptionsAction()
if ($empty_value !== false)
$html .= '<option value="">' . $translator->trans($empty_value) . '</option>';

$getter = $this->getGetterName($entity_inf['property']);

if ($entity_inf['property'] != null) {
$getter = $this->getGetterName($entity_inf['property']);
}

foreach($results as $result)
{
Expand Down Expand Up @@ -91,7 +111,7 @@ public function getJSONAction()
$entity_inf = $entities[$entity_alias];

if ($entity_inf['role'] !== 'IS_AUTHENTICATED_ANONYMOUSLY'){
if (false === $this->get('security.context')->isGranted( $entity_inf['role'] )) {
if (false === $this->get('security.authorization_checker')->isGranted( $entity_inf['role'] )) {
throw new AccessDeniedException();
}
}
Expand Down Expand Up @@ -146,4 +166,9 @@ private function getGetterName($property)
return $name;

}

private function getRequest()
{
return $this->container->get('request_stack')->getCurrentRequest();
}
}
42 changes: 35 additions & 7 deletions Form/DataTransformer/EntityToIdTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public function transform($entity)
if (null === $entity || '' === $entity) {
return 'null';
}
if (!is_object($entity)) {

if (!is_object($entity) && !is_array($entity)) {
throw new UnexpectedTypeException($entity, 'object');
}
if (!$this->unitOfWork->isInIdentityMap($entity)) {
throw new FormException('Entities passed to the choice field must be managed');
}

return $entity->getId();
if(is_array($entity)){
return array_map([$this, 'doEntityToId'], $entity);
}else{
return $this->doEntityToId($entity);
}
}

public function reverseTransform($id)
Expand All @@ -44,16 +46,42 @@ public function reverseTransform($id)
return null;
}

if (!is_numeric($id)) {
if (!is_numeric($id) && !is_array($id)) {
throw new UnexpectedTypeException($id, 'numeric' . $id);
}

if(is_array($id)){
return array_map([$this, 'doFindEntity'], $id);
}else{
return $this->doFindEntity($id);
}
}

/**
* @param $id
* @return mixed
*/
public function doFindEntity($id)
{
$entity = $this->em->getRepository($this->class)->findOneById($id);

if ($entity === null) {
if($entity === null){
throw new TransformationFailedException(sprintf('The entity with key "%s" could not be found', $id));
}

return $entity;
}

/**
* @param $entity
* @return mixed
*/
public function doEntityToId($entity)
{
if(!$this->unitOfWork->isInIdentityMap($entity)){
throw new FormException('Entities passed to the choice field must be managed');
}

return $entity->getId();
}
}
19 changes: 18 additions & 1 deletion Form/Type/AjaxAutocompleteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Shtumi\UsefulBundle\Form\Type;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Exception\FormException;
use Shtumi\UsefulBundle\Form\DataTransformer\EntityToPropertyTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class AjaxAutocompleteType extends AbstractType
Expand All @@ -30,14 +32,29 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'entity_alias' => null,
'class' => null,
'property' => null,
'compound' => false
));
}

public function getName()
{
return 'shtumi_ajax_autocomplete';
}

public function getBlockPrefix()
{
return 'shtumi_ajax_autocomplete';
}

public function getParent()
{
return 'text';
return TextType::class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
Expand Down
18 changes: 17 additions & 1 deletion Form/Type/AjaxFileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
namespace Shtumi\UsefulBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Shtumi\UsefulBundle\Form\DataTransformer\AjaxFileTransformer;

Expand All @@ -28,16 +30,30 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'default' => null,
'compound' => false,
'multiple' => false
));
}

public function getParent()
{
return 'text';
return TextType::class;
}

public function getName()
{
return 'shtumi_ajaxfile';
}

public function getBlockPrefix()
{
return 'shtumi_ajaxfile';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new AjaxFileTransformer(
Expand Down
17 changes: 16 additions & 1 deletion Form/Type/DateRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
namespace Shtumi\UsefulBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToStringTransformer;
use Shtumi\UsefulBundle\Form\DataTransformer\DateRangeToValueTransformer;
Expand Down Expand Up @@ -39,16 +41,29 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'default' => null,
'compound' => false,
));
}

public function getParent()
{
return 'text';
return TextType::class;
}

public function getName()
{
return 'shtumi_daterange';
}

public function getBlockPrefix()
{
return 'shtumi_daterange';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{

Expand Down
28 changes: 27 additions & 1 deletion Form/Type/DependentFilteredEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Shtumi\UsefulBundle\Form\DataTransformer\EntityToIdTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DependentFilteredEntityType extends AbstractType
Expand All @@ -25,20 +27,37 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'empty_value' => '',
'entity_alias' => null,
'parent_field' => null,
'multiple' => true,
'compound' => false
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'empty_value' => '',
'entity_alias' => null,
'parent_field' => null,
'multiple' => false,
'compound' => false
));
}

public function getParent()
{
return 'form';
return FormType::class;
}

public function getName()
{
return 'shtumi_dependent_filtered_entity';
}

public function getBlockPrefix()
{
return 'shtumi_dependent_filtered_entity';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{

Expand Down Expand Up @@ -66,6 +85,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['entity_alias'] = $form->getConfig()->getAttribute('entity_alias');
$view->vars['no_result_msg'] = $form->getConfig()->getAttribute('no_result_msg');
$view->vars['empty_value'] = $form->getConfig()->getAttribute('empty_value');

if ($options['multiple']) {
// Add "[]" to the name in case a select tag with multiple options is
// displayed. Otherwise only one of the selected options is sent in the
// POST request.
$view->vars['full_name'] .= '[]';
}
}

}
19 changes: 18 additions & 1 deletion Form/Type/DependentFilteredSelect2Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Shtumi\UsefulBundle\Form\DataTransformer\EntityToIdTransformer;
use Shtumi\UsefulBundle\Form\DataTransformer\EntityToSelect2ValueTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DependentFilteredSelect2Type extends AbstractType
Expand All @@ -30,16 +32,31 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'empty_value' => '',
'entity_alias' => null,
'parent_field' => null,
'compound' => false
));
}

public function getParent()
{
return 'form';
return FormType::class;
}

public function getName()
{
return 'shtumi_dependent_filtered_select2';
}

public function getBlockPrefix()
{
return 'shtumi_dependent_filtered_select2';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{

Expand Down
Loading