<?php

namespace AppBundle\Form;

use Doctrine\ORM\EntityRepository;
use FOS\UserBundle\Util\LegacyFormHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PersonType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('locked', null, array('label' => 'Kilitle'))
            ->add('email', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\EmailType'),
                array('label' => 'E-Posta Adresi', 'translation_domain' => 'FOSUserBundle'))
            ->add('username', null, array('label' => 'Kullanıcı Adı', 'translation_domain' => 'FOSUserBundle'))
            ->add('plainPassword', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\RepeatedType'), array(
                'type' => LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'),
                'options' => array('translation_domain' => 'FOSUserBundle'),
                'first_options' => array('label' => 'Şifre'),
                'second_options' => array('label' => 'Şifre (Tekrar)'),
                'invalid_message' => 'Şifreniz uyuşmamaktadır',
            ))
            ->add('name', null, array('label' => 'Ad'))
            ->add('surname', null, array('label' => 'Soyad'))
            ->add('telephone', null, array('label' => 'Telefon'))
            ->add('address', null, array('label' => 'Adres'))
            ->add('roles', EntityType::class, array(
                'label' => 'Roller (Birden fazla seçmek için Ctrl tuşuna basıp seçiniz)',
                'class' => 'AppBundle:PersonRole',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->orderBy('u.name', 'ASC');
                },
                'choice_label' => 'name',
                'multiple' => true,
            ));
        /*veya asagidaki gibi ozel kod yazilabilir. expanded ozelligi acilmamalidir.
           ->add('roles', EntityType::class, array(
               // query choices from this entity
               'class' => 'AppBundle:PersonRole',

               // use the User.username property as the visible option string
               'choice_label' => 'name',

               // used to render a select box, check boxes or radios
               'multiple' => true,
               // 'expanded' => true,
           ));
           */
    }

    protected function buildChoices()
    {
        $choices = [];
        $table2Repository = $this->getDoctrine()->getRepository('AppBundle:PersonRole');
        $table2Objects = $table2Repository->findAll();

        foreach ($table2Objects as $table2Obj) {
            $choices[$table2Obj->getId()] = $table2Obj->getName();
        }

        return $choices;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Person'
        ));
    }
}
