<?php
/**
 * FormField Type for Form
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use VisualM\FormBundle\Doctrine\FieldType\FormFieldType as FieldType;

/**
 * FormField Type for Form
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class FormFieldType extends AbstractType
{
    /**
     * {@inhertDoc@}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('volgorde', 'hidden');

        $builder->add('titel', 'text', array('label' => 'form_field.titel', 'trim' => true));
        $builder->add('referentie', 'text', array('label' => 'form_field.referentie', 'trim' => true));

        $builder->add('type', 'choice', array(
            'label' => 'form_field.type.label',
            'choices' => array(                
                FieldType::TEXT => 'form_field.type.text',
                FieldType::NUMBER => 'form_field.type.number',
                FieldType::PHONE => 'form_field.type.phone',
                FieldType::EMAIL => 'form_field.type.email',
                FieldType::DATE => 'form_field.type.date',
                FieldType::TEXTAREA => 'form_field.type.textarea',
                FieldType::SELECT => 'form_field.type.select',
                FieldType::SELECTLIST => 'form_field.type.selectlist',
                FieldType::CHECKBOXLIST => 'form_field.type.checkboxlist',
                FieldType::OPTIONLIST => 'form_field.type.optionlist',
                FieldType::HIDDEN => 'form_field.type.hidden',
            ),
            'empty_value' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('verplicht', 'choice', array(
            'label' => 'form_field.verplicht',
            'choices' => array(false => 'boolean.no', true => 'boolean.yes'),
            'empty_value' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('rijen', 'choice', array(
            'label' => 'form_field.rijen',
            'choices' => array(
                '2' => '2',
                '4' => '4',
                '6' => '6',
                '8' => '8'
            ),
            'empty_value' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('regex', 'text', array('label' => 'form_field.regex', 'trim' => true, 'required' => false));
        $builder->add('waarden', 'textarea', array('label' => 'form_field.waarden', 'required' => false, 'attr' => [ 'rows' => 4 ]));

    }


    /**
     * {@inhertDoc@}
     */
    public function getName()
    {
        return 'visualm_form_field';
    }
    
    /**
     * {@inhertDoc@}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VisualM\FormBundle\Entity\FormField',
            'translation_domain' => 'forms',
            'validation_groups' => function(FormInterface $form) {
                $data = $form->getData();
                if ($data->getId() === null) {
                    return array('form_field', 'Default', 'add', 'form_field_add');
                } else {
                    return array('form_field', 'Default', 'edit', 'form_field_edit');
                }
            }
        ));
    }

}