<commit_before><?php
/**
 * Created By <NAME>
 * Date: 08/01/2014
 * Time: 17:28
 */

namespace Pirastru\FormBuilderBundle\Block;

use Sonata\BlockBundle\Block\BlockContextInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 *
 * @author     <NAME>
 */
class FormBuilderBlockService extends BaseBlockService
{
    protected $formBuilderAdmin;


    /**
     * @param string             $name
     * @param EngineInterface    $templating
     * @param ContainerInterface $container
     */
    public function __construct($name, EngineInterface $templating, ContainerInterface $container)
    {
        parent::__construct($name, $templating);

        $this->container    = $container;
    }


    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'Form Builder Drag&Drop';
    }



    /**
     * {@inheritdoc}
     */
    public function setDefaultSettings(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults ( array (
            'template' => 'PirastruFormBuilderBundle:Block:block_form_builder.html.twig',
            'formBuilderId' => null,
        ) );
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
            'keys' => array(
                array($this->getFieldFormBuilder($formMapper), null, array()),
            )
        ));
    }


    /**
     * @return mixed
     */
    public function getFormBuilderAdmin()
    {
        if (!$this->formBuilderAdmin) {
            $this->formBuilderAdmin = $this->container->get('pirastru_form_builder.admin');
        }

        return $this->formBuilderAdmin;
    }


    /**
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
     *
     * @return \Symfony\Component\Form\FormBuilder
     */
    protected function getFieldFormBuilder(FormMapper $formMapper)
    {
        // simulate an association ...
        $fieldDescription = $this->getFormBuilderAdmin()->getModelManager()->getNewFieldDescriptionInstance($this->formBuilderAdmin->getClass(), 'form_builder');
        $fieldDescription->setAssociationAdmin($this->getFormBuilderAdmin());
        $fieldDescription->setAdmin($formMapper->getAdmin());
        $fieldDescription->setOption('edit', 'list');
        $fieldDescription->setAssociationMapping(array(
            'fieldName' => 'form_builder',
            'type'      => \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_ONE
        ));

        return $formMapper->create('FormBuilderId', 'sonata_type_model', array(
            'sonata_field_description' => $fieldDescription,
            'label'                    => "Form Builder",
            'class'                    => $this->getFormBuilderAdmin()->getClass(),
            'model_manager'            => $this->getFormBuilderAdmin()->getModelManager()<commit_msg>a type on Block Service<commit_after><?php
/**
 * Created By <NAME>
 * Date: 08/01/2014
 * Time: 17:28
 */

namespace Pirastru\FormBuilderBundle\Block;

use Sonata\BlockBundle\Block\BlockContextInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 *
 * @author     <NAME>
 */
class FormBuilderBlockService extends BaseBlockService
{
    protected $formBuilderAdmin;


    /**
     * @param string             $name
     * @param EngineInterface    $templating
     * @param ContainerInterface $container
     */
    public function __construct($name, EngineInterface $templating, ContainerInterface $container)
    {
        parent::__construct($name, $templating);

        $this->container    = $container;
    }


    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'Form Builder Drag&Drop';
    }



    /**
     * {@inheritdoc}
     */
    public function setDefaultSettings(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults ( array (
            'template' => 'PirastruFormBuilderBundle:Block:block_form_builder.html.twig',
            'formBuilderId' => null,
        ) );
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
            'keys' => array(
                array($this->getFieldFormBuilder($formMapper), null, array()),
            )
        ));
    }


    /**
     * @return mixed
     */
    public function getFormBuilderAdmin()
    {
        if (!$this->formBuilderAdmin) {
            $this->formBuilderAdmin = $this->container->get('pirastru_form_builder.admin');
        }

        return $this->formBuilderAdmin;
    }


    /**
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
     *
     * @return \Symfony\Component\Form\FormBuilder
     */
    protected function getFieldFormBuilder(FormMapper $formMapper)
    {
        // simulate an association ...
        $fieldDescription = $this->getFormBuilderAdmin()->getModelManager()->getNewFieldDescriptionInstance($this->formBuilderAdmin->getClass(), 'form_builder');
        $fieldDescription->setAssociationAdmin($this->getFormBuilderAdmin());
        $fieldDescription->setAdmin($formMapper->getAdmin());
        $fieldDescription->setOption('edit', 'list');
        $fieldDescription->setAssociationMapping(array(
            'fieldName' => 'form_builder',
            'type'      => \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_ONE
        ));

        return $formMapper->create('formBuilderId', 'sonata_type_model', array(
            'sonata_field_description' => $fieldDescription,
            'label'                    => "Form Builder",
            'class'                    => $this->getFormBuilderAdmin()->getClass(),
            'model_manager'            => $this->getFormBuilderAdmin()->getModelManager()
{'fraction_non_alphanumeric': 0.12560532687651332, 'fraction_numerical': 0.0036319612590799033, 'mean_word_length': 2.809221902017291, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1343325', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1776, 'n_words': 279}