Created
June 17, 2015 16:02
-
-
Save yuklia/9d13a98d2307e0a7371a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: yuklia | |
* Date: 6/16/15 | |
* Time: 3:51 PM | |
*/ | |
namespace Groups\Forms; | |
use Application\Groupleades\Entity\Grouplead; | |
use Application\Groups\GroupleadForm; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\Validator\Constraints\Length; | |
use Symfony\Component\Validator\Constraints\NotBlank; | |
class GroupForm extends AbstractType | |
{ | |
/** @var \Doctrine\ORM\EntityManager $em */ | |
protected $em; | |
/** | |
* @param mixed $em | |
*/ | |
public function setEm($em) | |
{ | |
$this->em = $em; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getEm() | |
{ | |
return $this->em; | |
} | |
public function getDepartments(){ //todo::use data transformers | |
$result = []; | |
$departments = $this->em->getRepository('Application\Departments\Entity\Department')->findAll(); | |
foreach($departments as $department){ | |
$result[$department->getId()] = $department->getName(); | |
} | |
return $result; | |
} | |
/** | |
* @return array | |
*/ | |
public function getGroupleades(){ | |
$result = []; | |
$groupleades = $this->em->getRepository('Application\Groupleades\Entity\Grouplead')->findAll(); | |
foreach($groupleades as $grouplead){ | |
$result[$grouplead->getUser()->getId()] = $grouplead->getUser()->getFullName(); | |
} | |
return $result; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$transformer = new NumberToGroupleadTansformer(); | |
$builder | |
->add('name', 'text', array( | |
'attr' => array('placeholder' => 'name'), | |
'constraints' => array( | |
new NotBlank([ | |
'message' => 'First name should not be blank !' | |
]), | |
new Length([ | |
'min' => 3 | |
]) | |
))) | |
->add('department', 'choice', array( //todo::should be an entity type | |
'choices' => $this->getDepartments(), | |
// 'preferred_choices' => array(2), | |
))->add('groupleades', 'choice', array( | |
// 'mapped' => false, | |
'choices' => $this->getGroupleades() | |
)) | |
// ->add('groupleades', 'collection', array('type' => new GroupleadForm($this->em))) | |
->addEventListener( | |
FormEvents::POST_SUBMIT, | |
array($this, 'onPostSubmit') | |
); | |
/* ->addEventListener( | |
FormEvents::POST_SET_DATA, | |
array($this, 'onPreSetData') | |
);*/ | |
} | |
/** | |
* @param FormEvent $event | |
*/ | |
public function onPostSubmit(FormEvent $event) | |
{ | |
/** @var \Application\Groups\Entity\Group $group */ | |
$group = $event->getData(); | |
$group->setDepartment($this->em | |
->getRepository('Application\Departments\Entity\Department') //todo::can be in pre/insert/update | |
->find($group->getDepartment())); | |
$user = $group->getGroupleades(); //terrible | |
$user = $this->em | |
->getRepository('Application\Users\Entity\User') | |
->find($user); | |
$grouplead = new Grouplead(); | |
$grouplead->setUser($user); | |
$grouplead->setGroup($group); | |
$group->setGroupleades(new ArrayCollection()); //todo::hardcode ! | |
$group->addGrouplead($grouplead); | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(['data_class' => 'Application\Groups\Entity\Group']); | |
// $resolver->setRequired(['groupleades','department']); | |
} | |
public function getName() | |
{ | |
return 'group'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment