Skip to content

Instantly share code, notes, and snippets.

@dionisos2
Created April 26, 2013 18:22
Show Gist options
  • Save dionisos2/5469308 to your computer and use it in GitHub Desktop.
Save dionisos2/5469308 to your computer and use it in GitHub Desktop.
<?php
namespace Ukratio\TrouveToutBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Ukratio\TrouveToutBundle\Form\EventListener\AddOwnerElementSubscriber;
use Ukratio\TrouveToutBundle\Form\EventListener\AddChildElementSubscriber;
use Ukratio\TrouveToutBundle\Form\EventListener\AddElementSubscriber;
use Ukratio\TrouveToutBundle\Form\DataTransformer\TrueElementToElementTransformer;
use Ukratio\TrouveToutBundle\Entity\Element;
use Ukratio\TrouveToutBundle\Entity\Type;
use Ukratio\TrouveToutBundle\Entity\ConceptRepository;
use Ukratio\TrouveToutBundle\Service\CaractTypeManager;
use Doctrine\ORM\EntityManager;
class ElementType extends AbstractType
{
private $em;
private $conceptRepo;
private $caractTypeManager;
public function __construct(EntityManager $em, ConceptRepository $conceptRepo, CaractTypeManager $caractTypeManager)
{
$this->em = $em;
$this->conceptRepo = $conceptRepo;
$this->caractTypeManager = $caractTypeManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$type = Type::getEnumerator($options['typeOfValue']);
$builder->addEventSubscriber(new AddElementSubscriber($builder->getFormFactory(), $this->em, $this->conceptRepo, $type, $this->caractTypeManager));
$builder->addEventSubscriber(new AddOwnerElementSubscriber($builder->getFormFactory(), $this->em));
$builder->addEventSubscriber(new AddChildElementSubscriber($builder->getFormFactory(), $this->em, $type, $this->caractTypeManager));
$builder->addModelTransformer(new TrueElementToElementTransformer($this->em));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Ukratio\TrouveToutBundle\Entity\Element',
'constraintOnValue' => false,
'typeOfValue' => 'name',
));
}
public function getName()
{
return 'TrouveTout_Element';
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
}
}
CaractsManager.prototype.getOrBuildChildForm = function (caractForm, index) {
var childFormSelect;
var type;
var childFormSelectDiv;
childFormSelect = this.getChildForm(caractForm);
if (childFormSelect.length > 0) {
return childFormSelect;
} else {
type = caractForm.find('[id$=type]').val();
childFormSelectDiv = this.prototype_specify[type].replace(/childElement/g, 'caracts_' + (index).toString() + '_value_childElement');
childFormSelectDiv = childFormSelectDiv.replace(/(name=.*?)\[.*?\]/g, '$1[caracts][' + (index).toString() + '][value][childElement]');
caractForm.find('[id$=value_value]').parent().parent().parent().find('div:first').prepend(childFormSelectDiv);
childFormSelect = this.getChildForm(caractForm);
return childFormSelect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment