<commit_before><?php

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\ClassificationBundle\Block\Service;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\ClassificationBundle\Model\CollectionInterface;
use Sonata\ClassificationBundle\Model\CollectionManagerInterface;
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
use Sonata\CoreBundle\Model\Metadata;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractCollectionsBlockService extends AbstractClassificationBlockService
{
    /**
     * @var CollectionManagerInterface
     */
    private $collectionManager;

    /**
     * @var AdminInterface
     */
    private $collectionAdmin;

    /**
     * @param string                     $name
     * @param EngineInterface            $templating
     * @param ContextManagerInterface    $contextManager
     * @param CollectionManagerInterface $collectionManager
     * @param AdminInterface             $collectionAdmin
     */
    public function __construct($name, EngineInterface $templating, ContextManagerInterface $contextManager, CollectionManagerInterface $collectionManager, AdminInterface $collectionAdmin)
    {
        parent::__construct($name, $templating, $contextManager);

        $this->collectionManager = $collectionManager;
        $this->collectionAdmin = $collectionAdmin;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        $collection = $this->getCollection($blockContext->getSetting('collectionId'), $blockContext->getSetting('collection'));
        $collections = $this->contextManager->findBy(array(
            'enabled' => true,
            'context' => $blockContext->getSetting('context'),
        ));

        return $this->renderResponse($blockContext->getTemplate(), array(
            'context' => $blockContext,
            'settings' => $blockContext->getSettings(),
            'block' => $blockContext->getBlock(),
            'collection' => $collection,
            'collections' => $collections,
        ), $response);
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $adminField = $this->getFormAdminType($formMapper, $this->collectionAdmin, 'collectionId', 'collection', array(
            'label' => 'form.label_collection',
        ), array(
            'translation_domain' => 'SonataClassificationBundle',
            'link_parameters' => array(
                array(
                    'context' => $block->getSetting('context'),
                ),
            ),
        ));

        $formMapper->add('settings', ImmutableArrayType::class, array(
            'keys' => array(
                array('title', TextType::class, array(
                    'label' => 'form.label_title',
                    'required' => false,
                )),
                array('context', ChoiceType::class, array(
                    'label' => 'form.label_context',
                    'required' => false,
                    'choices' => $this->getContextChoices(),
                )),
                array($adminField, null, array()),
            ),
            'translation_domain' => 'SonataClassificationBundle',
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function configureSettings(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'title' => 'Collections',
            'collection' => false,
            'collectionId' => null,
            'context' => null,
            'template' => 'SonataClassificationBundle:Block:base_block_collections.html.twig',
        ));
    }

    /**
     * {@inheritdoc}<commit_msg>Make form field types compatible with Symfony 2.3<commit_after><?php

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\ClassificationBundle\Block\Service;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\ClassificationBundle\Model\CollectionInterface;
use Sonata\ClassificationBundle\Model\CollectionManagerInterface;
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
use Sonata\CoreBundle\Model\Metadata;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractCollectionsBlockService extends AbstractClassificationBlockService
{
    /**
     * @var CollectionManagerInterface
     */
    private $collectionManager;

    /**
     * @var AdminInterface
     */
    private $collectionAdmin;

    /**
     * @param string                     $name
     * @param EngineInterface            $templating
     * @param ContextManagerInterface    $contextManager
     * @param CollectionManagerInterface $collectionManager
     * @param AdminInterface             $collectionAdmin
     */
    public function __construct($name, EngineInterface $templating, ContextManagerInterface $contextManager, CollectionManagerInterface $collectionManager, AdminInterface $collectionAdmin)
    {
        parent::__construct($name, $templating, $contextManager);

        $this->collectionManager = $collectionManager;
        $this->collectionAdmin = $collectionAdmin;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        $collection = $this->getCollection($blockContext->getSetting('collectionId'), $blockContext->getSetting('collection'));
        $collections = $this->contextManager->findBy(array(
            'enabled' => true,
            'context' => $blockContext->getSetting('context'),
        ));

        return $this->renderResponse($blockContext->getTemplate(), array(
            'context' => $blockContext,
            'settings' => $blockContext->getSettings(),
            'block' => $blockContext->getBlock(),
            'collection' => $collection,
            'collections' => $collections,
        ), $response);
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $adminField = $this->getFormAdminType($formMapper, $this->collectionAdmin, 'collectionId', 'collection', array(
            'label' => 'form.label_collection',
        ), array(
            'translation_domain' => 'SonataClassificationBundle',
            'link_parameters' => array(
                array(
                    'context' => $block->getSetting('context'),
                ),
            ),
        ));

        $formMapper->add(
            'settings',
            // NEXT_MAJOR: remove when dropping Symfony <2.8 support
            method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
                ? 'Sonata\CoreBundle\Form\Type\ImmutableArrayType'
                : 'sonata_type_immutable_array',
            array(
                'keys' => array(
                    array('title',
                        // NEXT_MAJOR: remove when dropping Symfony <2.8 support
                        method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
                            ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
                            : 'text',
                        array(
                            'label' => 'form.label_title',
                            'required' => false,
                        )
                    ),
                    array('context',
                        // NEXT_MAJOR: remove when dropping Symfony <2.8 support
                        method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
                            ? 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
                            : 'choice',
                        array(
                            'label' => 'form.label_context',
                            'required' => false,
                            'choices' => $this->getContextChoices(),
                        )
                    ),
                    array($adminField, null, array()),
                ),
                'translation_domain' => 'SonataClassificationBundle',
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function configureSettings(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'title' => 'Collections',
            'collection' => false,
            'collectionId' => null,
            'context' => null,
            'template' => 'SonataClassificationBundle:Block:base_block_collections.html.twig',
        ));
    }

    /**
     * {@inheritdoc}
{'fraction_non_alphanumeric': 0.11689922480620155, 'fraction_numerical': 0.0008268733850129199, 'mean_word_length': 2.2933968686181077, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 97, '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': '3742213', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2379, 'n_words': 447}