mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-23 16:13:50 +00:00
move bundle to root dir for inclusion in packagist refs #259
This commit is contained in:
52
Form/AdressType.php
Normal file
52
Form/AdressType.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
|
||||
/**
|
||||
* @internal Ne fonctionne pas encore
|
||||
*/
|
||||
class AdressType extends AbstractType
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('data', 'entity', array(
|
||||
|
||||
))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
// $resolver->setDefaults(array(
|
||||
// 'data_class' => 'Chill\CustomFieldsBundle\Entity\Adress',
|
||||
// 'class' => 'Chill\CustomFieldsBundle\Entity\Adress'
|
||||
// ));
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return 'entity';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'adress';
|
||||
}
|
||||
}
|
69
Form/BlopEntity2Type.php
Normal file
69
Form/BlopEntity2Type.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
|
||||
class BlopEntity2Type extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$em = $options['em'];
|
||||
|
||||
$customFields = $em
|
||||
->getRepository('ChillCustomFieldsBundle:CustomField')
|
||||
->findAll();
|
||||
|
||||
foreach ($customFields as $cf) {
|
||||
if($cf->getType() === 'ManyToOne(Adress)') {
|
||||
$builder->add($cf->getLabel(), 'entity', array(
|
||||
'class' => 'ChillCustomFieldsBundle:Adress',
|
||||
'property' => 'data'
|
||||
));
|
||||
} else if ($cf->getType() === 'ManyToOnePersist(Adress)') {
|
||||
$builder->add($cf->getLabel(), new AdressType());
|
||||
} else if($cf->getType() === 'ManyToMany(Adress)') {
|
||||
$builder->add($cf->getLabel(), 'entity', array(
|
||||
'class' => 'ChillCustomFieldsBundle:Adress',
|
||||
'property' => 'data',
|
||||
'multiple' => true
|
||||
));
|
||||
} else if ($cf->getType() === 'text') {
|
||||
$builder->add($cf->getLabel(), 'text');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\CustomFieldsBundle\Entity\BlopEntity2'
|
||||
));
|
||||
|
||||
// supprimer ça en definissant dans services
|
||||
$resolver->setRequired(array(
|
||||
'em',
|
||||
));
|
||||
|
||||
$resolver->setAllowedTypes(array(
|
||||
'em' => 'Doctrine\Common\Persistence\ObjectManager',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'cl_customfieldsbundle_blopentity2';
|
||||
}
|
||||
}
|
56
Form/BlopEntityType.php
Normal file
56
Form/BlopEntityType.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType;
|
||||
|
||||
class BlopEntityType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$entityManager = $options['em'];
|
||||
|
||||
$builder
|
||||
->add('field1')
|
||||
->add('field2')
|
||||
//->add('adress', new AdressType())
|
||||
->add('customField', 'custom_field')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\CustomFieldsBundle\Entity\BlopEntity',
|
||||
'cascade_validation' => true
|
||||
));
|
||||
|
||||
// supprimer ça en definissant dans services
|
||||
$resolver->setRequired(array(
|
||||
'em',
|
||||
));
|
||||
|
||||
$resolver->setAllowedTypes(array(
|
||||
'em' => 'Doctrine\Common\Persistence\ObjectManager',
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'cl_customfieldsbundle_blopentity';
|
||||
}
|
||||
}
|
80
Form/CustomFieldType.php
Normal file
80
Form/CustomFieldType.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
|
||||
class CustomFieldType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $customFieldProvider;
|
||||
|
||||
private $culture = 'fr';
|
||||
|
||||
|
||||
public function __construct(CustomFieldProvider $compiler)
|
||||
{
|
||||
$this->customFieldProvider = $compiler;
|
||||
}
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
|
||||
$customFieldsList = array();
|
||||
|
||||
foreach ($this->customFieldProvider->getAllFields() as $key => $field) {
|
||||
$customFieldsList[$key] = $field->getName();
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('name', 'text')
|
||||
->add('active')
|
||||
->add('customFieldsGroup', 'entity', array(
|
||||
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
|
||||
'property' => 'name['.$this->culture.']'
|
||||
))
|
||||
->add('ordering', 'number')
|
||||
;
|
||||
|
||||
//add options field
|
||||
$optionsType = $this->customFieldProvider
|
||||
->getCustomFieldByType($options['type'])
|
||||
->buildOptionsForm($builder);
|
||||
if ($optionsType) {
|
||||
$builder->add('options', $optionsType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomField'
|
||||
));
|
||||
|
||||
$resolver->setRequired(array('type'))
|
||||
->addAllowedValues(array('type' =>
|
||||
array_keys($this->customFieldProvider->getAllFields())
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'custom_field_choice';
|
||||
}
|
||||
}
|
66
Form/CustomFieldsGroupType.php
Normal file
66
Form/CustomFieldsGroupType.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
|
||||
class CustomFieldsGroupType extends AbstractType
|
||||
{
|
||||
|
||||
private $customizableEntities;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Translation\TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
public function __construct(array $customizableEntities, TranslatorInterface $translator)
|
||||
{
|
||||
$this->customizableEntities = $customizableEntities;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
//prepare translation
|
||||
$customizableEntites = array();
|
||||
|
||||
foreach($this->customizableEntities as $key => $definition) {
|
||||
$customizableEntites[$definition['class']] = $this->translator->trans($definition['name']);
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('name')
|
||||
->add('entity', 'choice', array(
|
||||
'choices' => $customizableEntites
|
||||
))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'custom_fields_group';
|
||||
}
|
||||
}
|
43
Form/DataTransformer/CustomFieldDataTransformer.php
Normal file
43
Form/DataTransformer/CustomFieldDataTransformer.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
|
||||
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
private $customFieldDefinition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\CustomFieldsBundle\Entity\CustomField
|
||||
*/
|
||||
private $customField;
|
||||
|
||||
public function __construct(CustomFieldInterface $customFieldDefinition,
|
||||
CustomField $customField)
|
||||
{
|
||||
$this->customFieldDefinition = $customFieldDefinition;
|
||||
$this->customField = $customField;
|
||||
}
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
return $this->customFieldDefinition->serialize($value,
|
||||
$this->customField);
|
||||
}
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
return $this->customFieldDefinition->deserialize($value,
|
||||
$this->customField);
|
||||
}
|
||||
|
||||
}
|
145
Form/DataTransformer/JsonCustomFieldToArrayTransformer.php
Normal file
145
Form/DataTransformer/JsonCustomFieldToArrayTransformer.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
|
||||
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
||||
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $om;
|
||||
|
||||
/**
|
||||
* @param ObjectManager $om
|
||||
*/
|
||||
public function __construct(ObjectManager $om)
|
||||
{
|
||||
$this->om = $om;
|
||||
|
||||
$customFields = $this->om
|
||||
->getRepository('ChillCustomFieldsBundle:CustomField')
|
||||
->findAll();
|
||||
|
||||
$customFieldsLablels = array_map(
|
||||
function($e) { return $e->getLabel(); },
|
||||
$customFields);
|
||||
|
||||
$customFieldsByLabel = array_combine($customFieldsLablels, $customFields);
|
||||
|
||||
$this->customField = $customFieldsByLabel;
|
||||
}
|
||||
|
||||
public function transform($customFieldsJSON)
|
||||
{
|
||||
echo $customFieldsJSON;
|
||||
|
||||
if($customFieldsJSON === null) { // lors de la creation
|
||||
$customFieldsArray = array();
|
||||
} else {
|
||||
$customFieldsArray = json_decode($customFieldsJSON,true);
|
||||
}
|
||||
|
||||
/*
|
||||
echo "<br> - 4 - <br>";
|
||||
|
||||
var_dump($customFieldsArray);
|
||||
|
||||
echo "<br> - 5 - <br>";
|
||||
*/
|
||||
|
||||
$customFieldsArrayRet = array();
|
||||
|
||||
foreach ($customFieldsArray as $key => $value) {
|
||||
$traited = false;
|
||||
if(array_key_exists($key, $this->customField)) {
|
||||
$type = $this->customField[$key]->getType();
|
||||
if(strpos($type,'ManyToOne') === 0) {
|
||||
if(strpos($type,'ManyToOnePersist') ===0) {
|
||||
$entityClass = substr($type, 17, -1);
|
||||
} else {
|
||||
$entityClass = substr($type, 10, -1);
|
||||
}
|
||||
|
||||
$customFieldsArrayRet[$key] = $this->om
|
||||
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
|
||||
->findOneById($value);
|
||||
$traited = true;
|
||||
} else if ($type === 'ManyToMany(Adress)') {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if(! $traited) {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
var_dump($customFieldsArrayRet);
|
||||
|
||||
return $customFieldsArrayRet;
|
||||
}
|
||||
|
||||
public function reverseTransform($customFieldsArray)
|
||||
{
|
||||
/*
|
||||
echo "<br> - - 7 - <br>";
|
||||
|
||||
|
||||
var_dump(array_keys($customFieldsArray));
|
||||
|
||||
echo "<br> - - 8 - <br>";
|
||||
|
||||
var_dump(array_keys($this->customField));
|
||||
|
||||
echo "<br> - - 9 - <br>";
|
||||
*/
|
||||
|
||||
//var_dump($customFieldsArray);
|
||||
|
||||
$customFieldsArrayRet = array();
|
||||
|
||||
foreach ($customFieldsArray as $key => $value) {
|
||||
$traited = false;
|
||||
if(array_key_exists($key, $this->customField)) {
|
||||
$type = $this->customField[$key]->getType();
|
||||
if(strpos($type,'ManyToOne') === 0) {
|
||||
// pour le manytoone() faire
|
||||
// un update du form en js ? : http://symfony.com/fr/doc/current/cookbook/form/form_collections.html
|
||||
//
|
||||
//$entityClass = substr($type, 10, -1);
|
||||
//echo $entityClasss;
|
||||
if(strpos($type, 'ManyToOnePersist') === 0) {
|
||||
// PEUT ETRE A FAIRE SI SEULEMENT $value->getId() ne renvoie rien...
|
||||
//
|
||||
//
|
||||
$this->om->persist($value); // pas bon ici
|
||||
// LE PERSIST NE SERT QUE LA PREMIERE FOIS
|
||||
// plutot le mettre dans une var temporaire de adress
|
||||
// et faire le persist qd fait sur l'obj parent
|
||||
// regarder : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
|
||||
// ou : http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
|
||||
// dans yml :
|
||||
// lifecycleCallbacks:
|
||||
// prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersist ]
|
||||
$this->om->flush(); // sinon l'id pose pbm
|
||||
}
|
||||
|
||||
$customFieldsArrayRet[$key] = $value->getId();
|
||||
$traited = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(! $traited) {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//echo json_encode($customFieldsArrayRet);
|
||||
|
||||
return json_encode($customFieldsArrayRet);
|
||||
}
|
||||
}
|
117
Form/Type/CustomFieldType.php
Normal file
117
Form/Type/CustomFieldType.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Chill\CustomFieldsBundle\Form\AdressType;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
|
||||
class CustomFieldType extends AbstractType
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $om;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldCompiler
|
||||
*/
|
||||
private $customFieldCompiler;
|
||||
|
||||
/**
|
||||
* @param ObjectManager $om
|
||||
*/
|
||||
public function __construct(ObjectManager $om, CustomFieldProvider $compiler)
|
||||
{
|
||||
$this->om = $om;
|
||||
$this->customFieldCompiler = $compiler;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$customFields = $this->om
|
||||
->getRepository('ChillCustomFieldsBundle:CustomField')
|
||||
->findAll();
|
||||
|
||||
foreach ($customFields as $cf) {
|
||||
|
||||
//$builder->add(
|
||||
//$builder->create(
|
||||
//$cf->getSlug(),
|
||||
$this->customFieldCompiler
|
||||
->getCustomFieldByType($cf->getType())
|
||||
->buildForm($builder, $cf);
|
||||
/* )
|
||||
->addModelTransformer(new CustomFieldDataTransformer(
|
||||
$this->customFieldCompiler
|
||||
->getCustomFieldByType($cf->getType()),
|
||||
$cf)
|
||||
)*/
|
||||
//);
|
||||
|
||||
// if($cf->getType() === 'ManyToOne(Adress)') {
|
||||
// $builder->add($cf->getLabel(), 'entity', array(
|
||||
// 'class' => 'ChillCustomFieldsBundle:Adress',
|
||||
// 'property' => 'data'
|
||||
// ));
|
||||
// } else if ($cf->getType() === 'ManyToOnePersist(Adress)') {
|
||||
// $builder->add($cf->getLabel(), new AdressType());
|
||||
// } else if($cf->getType() === 'ManyToMany(Adress)') {
|
||||
//
|
||||
// $adress = $this->om
|
||||
// ->getRepository('ChillCustomFieldsBundle:Adress')
|
||||
// ->findAll();
|
||||
//
|
||||
// $adressId = array_map(
|
||||
// function($e) { return $e->getId(); },
|
||||
// $adress);
|
||||
//
|
||||
// $adressLabel = array_map(
|
||||
// function($e) { return (string) $e; },
|
||||
// $adress);
|
||||
//
|
||||
// $addressChoices = array_combine($adressId, $adressLabel);
|
||||
//
|
||||
//
|
||||
// $builder->add($cf->getLabel(), 'choice', array(
|
||||
// 'choices' => $addressChoices,
|
||||
// 'multiple' => true
|
||||
// ));
|
||||
// }
|
||||
// else {
|
||||
// $builder->add($cf->getLabel(), $cf->getType());
|
||||
// }
|
||||
}
|
||||
|
||||
//$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om));
|
||||
}
|
||||
|
||||
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver
|
||||
//->addAllowedTypes(array('context' => 'string'))
|
||||
//->setRequired(array('context'))
|
||||
;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'custom_field';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user