<commit_before><?php
namespace Victoire\Bundle\BlogBundle\Form;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Victoire\Bundle\CoreBundle\DataTransformer\ViewToIdTransformer;

/**
 *
 */
class ArticleType extends AbstractType
{
    private $entityManager;

    /**
     * Constructor
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * define form fields
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $viewToIdTransformer = new ViewToIdTransformer($this->entityManager);

        $builder
            ->add('name', null, array(
                    'label' => 'form.article.name.label',
                ))
            ->add('description', null, array(
                    'label' => 'form.article.description.label',
                    'required' => false))
            ->add('image', 'media', array(
                    'label' => 'form.article.image.label',
                ))
            ->add(
                $builder->create('blog', 'hidden', array(
                        'label' => 'form.article.blog.label')
                )->addModelTransformer($viewToIdTransformer)
            )
            ->add(
                'tags',
                'tags',
                array(
                    'required' => false,
                    'multiple' => true
                )
            )
            ->remove('visibleOnFront');

        $builder->get('blog')->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $data = $event->getData();
                $form = $event->getForm();
                $this->manageCategories($data, $form->getParent());
            });

        $builder->get('blog')->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $this->manageCategories($data, $form->getParent());
            });

        $articlePatterns = function(EntityRepository $repo) {
            return $repo->getInstance()->andWhere("pattern.businessEntityId = 'article'");
        };
        $builder->add('pattern', null, array(
                'label'         => 'form.view.type.pattern.label',
                'property'      => 'name',
                'required'      => true,
                'query_builder' => $articlePatterns,
            ));
    }

    protected function manageCategories($blogId, $form) {
        $categoryRepo = $this->entityManager->getRepository('Victoire\Bundle\BlogBundle\Entity\Category');

        if ($blogId) {
            $queryBuilder = $categoryRepo->getOrderedCategories($blogId)->getInstance();
        } else {
            $queryBuilder = $categoryRepo->getAll()->getInstance();
        }

        $form->add('category', 'hierarchy_tree', array(<commit_msg>the Category repo is called 2 times, so we have to clear it between two calls<commit_after><?php
namespace Victoire\Bundle\BlogBundle\Form;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Victoire\Bundle\CoreBundle\DataTransformer\ViewToIdTransformer;

/**
 *
 */
class ArticleType extends AbstractType
{
    private $entityManager;

    /**
     * Constructor
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * define form fields
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $viewToIdTransformer = new ViewToIdTransformer($this->entityManager);

        $builder
            ->add('name', null, array(
                    'label' => 'form.article.name.label',
                ))
            ->add('description', null, array(
                    'label' => 'form.article.description.label',
                    'required' => false))
            ->add('image', 'media', array(
                    'label' => 'form.article.image.label',
                ))
            ->add(
                $builder->create('blog', 'hidden', array(
                        'label' => 'form.article.blog.label')
                )->addModelTransformer($viewToIdTransformer)
            )
            ->add(
                'tags',
                'tags',
                array(
                    'required' => false,
                    'multiple' => true
                )
            )
            ->remove('visibleOnFront');

        $builder->get('blog')->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $data = $event->getData();
                $form = $event->getForm();
                $this->manageCategories($data, $form->getParent());
            });

        $builder->get('blog')->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $this->manageCategories($data, $form->getParent());
            });

        $articlePatterns = function(EntityRepository $repo) {
            return $repo->getInstance()->andWhere("pattern.businessEntityId = 'article'");
        };
        $builder->add('pattern', null, array(
                'label'         => 'form.view.type.pattern.label',
                'property'      => 'name',
                'required'      => true,
                'query_builder' => $articlePatterns,
            ));
    }

    protected function manageCategories($blogId, $form) {
        $categoryRepo = $this->entityManager->getRepository('Victoire\Bundle\BlogBundle\Entity\Category');

        if ($blogId) {
            $queryBuilder = $categoryRepo->getOrderedCategories($blogId)->getInstance();
            $categoryRepo->clearInstance();
        } else {
            $queryBuilder = $categoryRepo->getAll()->getInstance();
            $categoryRepo->clearInstance();
        }

        $form->add('category', 'hierarchy_tree', array(
{'fraction_non_alphanumeric': 0.13634996993385448, 'fraction_numerical': 0.0001503307276007216, 'mean_word_length': 1.9807347670250897, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, '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': '3296582', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1677, 'n_words': 286}