<?php

namespace VGM\WOBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

class NDepartmentType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', null, array('label' => 'Name', 'required' => true))
            ->add('description', 'textarea', array('label' => 'Description', 'required' => false))
            ->add('supervisors', 'entity', array(
                'class' => 'VGMWOBundle:Worker',
                'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('w')
                            ->where('w.supervisor = :sup')
                            ->setParameter('sup', true)
                            ->orderBy('w.name', 'ASC')
                            ->addOrderBy('w.lastname', 'ASC');
                    },
                'expanded' => true,
                'multiple' => true,
                'required' => true))
            ->add('units', 'entity', array(
                'class' => 'VGMWOBundle:CUnit',
                'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('cu')
                            ->orderBy('cu.level', 'ASC');
                    },
                'expanded' => true,
                'multiple' => true,
                'required' => true));
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VGM\WOBundle\Entity\NDepartment'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'vgm_wobundle_ndepartment';
    }
}
