make changes according to refs #242

This commit is contained in:
Julien Fastré 2014-10-22 07:12:41 +02:00
parent cd2a7bff53
commit 2254acd8ee
20 changed files with 560 additions and 79 deletions

View File

@ -3,7 +3,13 @@
namespace CL\CustomFieldsBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use CL\CustomFieldsBundle\DependencyInjection\CustomFieldCompilerPass;
class CLCustomFieldsBundle extends Bundle
{
public function build(\Symfony\Component\DependencyInjection\ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new CustomFieldCompilerPass());
}
}

View File

@ -62,7 +62,7 @@ class CustomFieldController extends Controller
*/
private function createCreateForm(CustomField $entity)
{
$form = $this->createForm(new CustomFieldType(), $entity, array(
$form = $this->createForm('custom_field_choice', $entity, array(
'action' => $this->generateUrl('customfield_create'),
'method' => 'POST',
));
@ -142,7 +142,7 @@ class CustomFieldController extends Controller
*/
private function createEditForm(CustomField $entity)
{
$form = $this->createForm(new CustomFieldType(), $entity, array(
$form = $this->createForm('custom_field_choice', $entity, array(
'action' => $this->generateUrl('customfield_update', array('id' => $entity->getId())),
'method' => 'PUT',
));

View File

@ -0,0 +1,69 @@
<?php
namespace CL\CustomFieldsBundle\CustomFields;
use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use CL\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManagerInterface;
use CL\CustomFieldsBundle\Form\AdressType;
/**
* Description of CustomFieldAddress
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldAddress implements CustomFieldInterface
{
public $om;
public function __construct(EntityManagerInterface $om)
{
$this->om = $om;
}
public function buildFormType(FormBuilderInterface $builder, CustomField $customField)
{
switch ($customField->getRelation())
{
case CustomField::ONE_TO_ONE :
$builder->build(
$builder->create($customField->getSlug(),
new AddressType()
)
);
break;
case CustomField::ONE_TO_MANY :
$builder->build(
$builder->create($customField->getSlug(),
new AddressType(),
array(
'multiple' => true
))
);
break;
}
}
public function getName()
{
return 'CF address';
}
public function render($value, CustomField $customField)
{
}
public function transformFromEntity($value, CustomField $customField)
{
}
public function transformToEntity($value, CustomField $customField)
{
}
}

View File

@ -0,0 +1,19 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace;
/**
* Description of CustomFieldChoiceWithOther
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldChoiceWithOther
{
//put your code here
}

View File

@ -0,0 +1,54 @@
<?php
namespace CL\CustomFieldsBundle\CustomFields;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Receive all service tagged with 'chill.custom_field'
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldCompiler implements ContainerAwareInterface
{
private $servicesByType = array();
/**
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
public function addCustomField($serviceName, $type)
{
$this->servicesByType[$type] = $serviceName;
}
/**
*
* @param string $type
* @return CustomFieldInterface
*/
public function getCustomFieldByType($type)
{
if (isset($this->servicesByType[$type])) {
return $this->servicesByType[$type];
} else {
throw new \LogicException('the custom field with type '.$type.' '
. 'is not found');
}
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function getAllFields()
{
return $this->servicesByType;
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace CL\CustomFieldsBundle\CustomFields;
use Symfony\Component\Form\FormBuilderInterface;
use CL\CustomFieldsBundle\Entity\CustomField;
/**
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
interface CustomFieldInterface
{
/**
*
* @param \CL\CustomFieldsBundle\CustomField\FormBuilderInterface $builder
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
* @return \Symfony\Component\Form\FormTypeInterface the form type
*/
public function buildFormType(FormBuilderInterface $builder, CustomField $customField);
/**
*
* @param type $value
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
*/
public function transformToEntity($value, CustomField $customField);
/**
*
* @param type $value
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
*/
public function transformFromEntity($value, CustomField $customField);
/**
*
* @param type $value
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
*/
public function render($value, CustomField $customField);
public function getName();
}

View File

@ -0,0 +1,45 @@
<?php
namespace CL\CustomFieldsBundle\CustomFields;
use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use CL\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormBuilderInterface;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldText implements CustomFieldInterface
{
public function buildFormType(FormBuilderInterface $builder, CustomField $customField)
{
$builder->add($customField->getSlug(), 'text', array(
'label' => $customField->getLabel()
));
}
public function render($value, CustomField $customField)
{
}
public function transformFromEntity($value, CustomField $customField)
{
return $value;
}
public function transformToEntity($value, CustomField $customField)
{
return $value;
}
public function getName()
{
return 'text field';
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace CL\CustomFieldsBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('chill.custom_field_compiler')) {
throw new \LogicException('service chill.custom_field_compiler '
. 'is not defined.');
}
$definition = $container->getDefinition(
'chill.custom_field_compiler'
);
$taggedServices = $container->findTaggedServiceIds(
'chill.custom_field'
);
foreach ($taggedServices as $id => $tagAttributes) {
foreach ($tagAttributes as $attributes) {
$definition->addMethodCall(
'addCustomField',
array(new Reference($id), $attributes["type"])
);
}
}
}
}

View File

@ -25,7 +25,7 @@ class BlopEntity
/**
* @var array
*/
private $customField;
private $customField = array();
private $adress;
@ -112,7 +112,7 @@ class BlopEntity
*
* @return BlopEntity
*/
public function setCustomField($customField)
public function setCustomField(array $customField)
{
$this->customField = $customField;
@ -126,6 +126,7 @@ class BlopEntity
*/
public function getCustomField()
{
return $this->customField;
}

View File

@ -17,6 +17,8 @@ class CustomField
*/
private $label;
private $slug;
/**
* @var string
*/
@ -27,6 +29,17 @@ class CustomField
*/
private $active;
private $options = array();
/**
*
* @var int
*/
private $relation = 1;
const ONE_TO_ONE = 1;
const ONE_TO_MANY = 2;
/**
* Get id
@ -38,6 +51,11 @@ class CustomField
return $this->id;
}
function getSlug()
{
return $this->slug;
}
/**
* Set label
*
@ -49,9 +67,18 @@ class CustomField
{
$this->label = $label;
if ($this->slug === NULL) {
$this->slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $label);
}
return $this;
}
function getOptions()
{
return $this->options;
}
/**
* Get label
*
@ -86,6 +113,19 @@ class CustomField
return $this->type;
}
function getRelation()
{
return $this->relation;
}
function setRelation($relation)
{
$this->relation = $relation;
return $this;
}
/**
* Set active
*

View File

@ -6,18 +6,23 @@ 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')
;
}
// /**
// * @param FormBuilderInterface $builder
// * @param array $options
// */
// public function buildForm(FormBuilderInterface $builder, array $options)
// {
// $builder
// ->add('data')
// ;
// }
/**
* @param OptionsResolverInterface $resolver
@ -25,15 +30,21 @@ class AdressType extends AbstractType
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CL\CustomFieldsBundle\Entity\Adress'
'data_class' => 'CL\CustomFieldsBundle\Entity\Adress',
'class' => 'CL\CustomFieldsBundle\Entity\Adress'
));
}
public function getParent()
{
return 'entity';
}
/**
* @return string
*/
public function getName()
{
return 'cl_customfieldsbundle_adress';
return 'adress';
}
}

View File

@ -20,8 +20,8 @@ class BlopEntityType extends AbstractType
$builder
->add('field1')
->add('field2')
->add('adress', new AdressType())
->add('customField',new CustomFieldType($entityManager))
//->add('adress', new AdressType())
->add('customField', 'custom_field')
;
}

View File

@ -5,19 +5,49 @@ namespace CL\CustomFieldsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler;
use CL\CustomFieldsBundle\Entity\CustomField;
class CustomFieldType extends AbstractType
{
/**
*
* @var \CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler
*/
private $customFieldCompiler;
public function __construct(CustomFieldCompiler $compiler)
{
$this->customFieldCompiler = $compiler;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$customFieldsList = array();
foreach ($this->customFieldCompiler->getAllFields() as $key => $field) {
$customFieldsList[$key] = $field->getName();
}
$builder
->add('label')
->add('type')
->add('type', 'choice', array(
'choices' => $customFieldsList,
'expanded' => false,
'multiple' => false
))
->add('active')
->add('relation', 'choice', array(
'choices' => array(
CustomField::ONE_TO_ONE => 'one to one ',
CustomField::ONE_TO_MANY => 'one to many'
)
))
;
}
@ -36,6 +66,6 @@ class CustomFieldType extends AbstractType
*/
public function getName()
{
return 'cl_customfieldsbundle_customfield';
return 'custom_field_choice';
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace CL\CustomFieldsBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use CL\CustomFieldsBundle\Entity\CustomField;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldDataTransformer implements DataTransformerInterface
{
private $customFieldDefinition;
/**
*
* @var \CL\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->transformFromEntity($value,
$this->customField);
}
public function transform($value)
{
return $this->customFieldDefinition->transformToEntity($value,
$this->customField);
}
}

View File

@ -16,20 +16,30 @@ use Symfony\Component\Form\FormBuilderInterface;
use CL\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use CL\CustomFieldsBundle\Form\AdressType;
use CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler;
use CL\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
class CustomFieldType extends AbstractType
{
/**
* @var ObjectManager
*/
private $om;
/**
*
* @var CustomFieldCompiler
*/
private $customFieldCompiler;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
public function __construct(ObjectManager $om, CustomFieldCompiler $compiler)
{
$this->om = $om;
$this->customFieldCompiler = $compiler;
}
public function buildForm(FormBuilderInterface $builder, array $options)
@ -39,45 +49,70 @@ class CustomFieldType extends AbstractType
->findAll();
foreach ($customFields as $cf) {
if($cf->getType() === 'ManyToOne(Adress)') {
$builder->add($cf->getLabel(), 'entity', array(
'class' => 'CLCustomFieldsBundle: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('CLCustomFieldsBundle:Adress')
->findAll();
$builder->add(
$builder->create(
$cf->getSlug(),
$this->customFieldCompiler
->getCustomFieldByType($cf->getType())
->buildFormType($builder, $cf),
array('mapped' => true)
)
->addModelTransformer(new CustomFieldDataTransformer(
$this->customFieldCompiler
->getCustomFieldByType($cf->getType()),
$cf)
)
);
$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());
}
// if($cf->getType() === 'ManyToOne(Adress)') {
// $builder->add($cf->getLabel(), 'entity', array(
// 'class' => 'CLCustomFieldsBundle: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('CLCustomFieldsBundle: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));
//$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';
}
}

View File

@ -11,9 +11,15 @@ CL\CustomFieldsBundle\Entity\CustomField:
label:
type: string
length: 255
slug:
type: string
length: 255
type:
type: string
length: 255
relation:
type: integer
default: 1
active:
type: boolean
lifecycleCallbacks: { }

View File

@ -2,6 +2,34 @@ parameters:
# cl_custom_fields.example.class: CL\CustomFieldsBundle\Example
services:
# cl_custom_fields.example:
# class: %cl_custom_fields.example.class%
# arguments: [@service_id, "plain_value", %parameter%]
chill.custom_field_compiler:
class: CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler
call:
- [setContainer, ["@service_container"]]
chill.custom_field.custom_field_choice_type:
class: CL\CustomFieldsBundle\Form\CustomFieldType
arguments:
- "@chill.custom_field_compiler"
tags:
- { name: 'form.type', alias: 'custom_field_choice' }
chill.custom_field.custom_field_type:
class: CL\CustomFieldsBundle\Form\Type\CustomFieldType
arguments:
- "@doctrine.orm.entity_manager"
- "@chill.custom_field_compiler"
tags:
- { name: 'form.type', alias: 'custom_field' }
chill.custom_field.text:
class: CL\CustomFieldsBundle\CustomFields\CustomFieldText
tags:
- { name: 'chill.custom_field', type: 'text' }
chill.custom_field.address:
class: CL\CustomFieldsBundle\CustomFields\CustomFieldAddress
arguments:
- "@doctrine.orm.entity_manager"
tags:
- { name: 'chill.custom_field', type: 'address' }

View File

@ -3,7 +3,13 @@
{% block body -%}
<h1>BlopEntity edit</h1>
{{ form(edit_form) }}
{{ form_start(edit_form) }}
{{ form_row(edit_form.customField.label0) }}
{{ form_rest(edit_form) }}
{{ form_end(edit_form) }}
<ul class="record_actions">
<li>

View File

@ -19,7 +19,7 @@
<td><a href="{{ path('blopentity_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
<td>{{ entity.field1 }}</td>
<td>{{ entity.field2 }}</td>
<td>{{ entity.customField }}</td>
<td>{{ dump(entity.customField) }}</td>
<td>
<ul>
<li>

View File

@ -19,7 +19,7 @@
</tr>
<tr>
<th>Customfield</th>
<td>{{ entity.customField }}</td>
<td>{{ dump(entity.customField) }}</td>
</tr>
</tbody>
</table>