<commit_before><?php

namespace Oro\Bundle\GridBundle\Filter\ORM\Flexible;

use Doctrine\Common\Persistence\ObjectRepository;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Oro\Bundle\FlexibleEntityBundle\Entity\Repository\FlexibleEntityRepository;
use Oro\Bundle\GridBundle\Datagrid\ORM\ProxyQuery;
use Oro\Bundle\FlexibleEntityBundle\Entity\Attribute;
use Oro\Bundle\FlexibleEntityBundle\Entity\AttributeOption;

class FlexibleOptionsFilter extends AbstractFlexibleFilter
{
    /**
     * {@inheritdoc}
     */
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $value)
    {
        if (!$value || !is_array($value) || !array_key_exists('value', $value) || null === $value['value']) {
            return;
        }

        $value['value'] = trim($value['value']);

        if (strlen($value['value']) == 0) {
            return;
        }

        /** @var $proxyQuery ProxyQuery */
        $queryBuilder = $proxyQuery->getQueryBuilder();

        /** @var $entityRepository FlexibleEntityRepository */
        $entityRepository = $this->flexibleManager->getFlexibleRepository();
        $entityRepository->applyFilterByOptionAttribute($queryBuilder, $field, $value['value']);
    }

    /**
     * {@inheritdoc}
     */
    public function getDefaultOptions()
    {
        return array();
    }

    /**
     * {@inheritdoc}
     */
    public function getRenderSettings()
    {
        return array('oro_grid_type_filter_flexible_options', array(
            'label'         => $this->getLabel(),
            'field_options' => array('choices' => $this->getFieldChoices()),
        ));
    }

    /**
     * @return array
     * @throws \LogicException
     */
    protected function getFieldChoices()
    {
        $filedName = $this->getOption('field_name');

        /** @var $attributeRepository ObjectRepository */
        $attributeRepository = $this->flexibleManager->getAttributeRepository();
        /** @var $attribute Attribute */
        $attribute = $attributeRepository->findOneBy(array('code' => $filedName));
        if (!$attribute) {
            throw new \LogicException('There is no flexible attribute with name ' . $filedName . '.');
        }

        /** @var $optionsRepository ObjectRepository */
        $optionsRepository = $this->flexibleManager->getAttributeOptionRepository();
        $options = $optionsRepository->findBy(
            array('attribute' => $attribute)
        );

        $choices = array();
        /** @var $option AttributeOption */
        foreach ($options as $option) {
            $optionValue = $option->getOptionValue();
            if ($optionValue) {
                $choices[$option->getId()] = $optionValue->getValue();
            }
        }
<commit_msg>BAP-111: Implement basic filter functionality - fixed choices select params<commit_after><?php

namespace Oro\Bundle\GridBundle\Filter\ORM\Flexible;

use Doctrine\Common\Persistence\ObjectRepository;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Oro\Bundle\FlexibleEntityBundle\Entity\Repository\FlexibleEntityRepository;
use Oro\Bundle\GridBundle\Datagrid\ORM\ProxyQuery;
use Oro\Bundle\FlexibleEntityBundle\Entity\Attribute;
use Oro\Bundle\FlexibleEntityBundle\Entity\AttributeOption;

class FlexibleOptionsFilter extends AbstractFlexibleFilter
{
    /**
     * {@inheritdoc}
     */
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $value)
    {
        if (!$value || !is_array($value) || !array_key_exists('value', $value) || null === $value['value']) {
            return;
        }

        $value['value'] = trim($value['value']);

        if (strlen($value['value']) == 0) {
            return;
        }

        /** @var $proxyQuery ProxyQuery */
        $queryBuilder = $proxyQuery->getQueryBuilder();

        /** @var $entityRepository FlexibleEntityRepository */
        $entityRepository = $this->flexibleManager->getFlexibleRepository();
        $entityRepository->applyFilterByOptionAttribute($queryBuilder, $field, $value['value']);
    }

    /**
     * {@inheritdoc}
     */
    public function getDefaultOptions()
    {
        return array();
    }

    /**
     * {@inheritdoc}
     */
    public function getRenderSettings()
    {
        return array('oro_grid_type_filter_flexible_options', array(
            'label'         => $this->getLabel(),
            'field_options' => array('choices' => $this->getFieldChoices()),
        ));
    }

    /**
     * @return array
     * @throws \LogicException
     */
    protected function getFieldChoices()
    {
        $filedName = $this->getOption('field_name');

        /** @var $attributeRepository ObjectRepository */
        $attributeRepository = $this->flexibleManager->getAttributeRepository();
        /** @var $attribute Attribute */
        $attribute = $attributeRepository->findOneBy(
            array('entityType' => $this->flexibleManager->getFlexibleName(), 'code' => $filedName)
        );
        if (!$attribute) {
            throw new \LogicException('There is no flexible attribute with name ' . $filedName . '.');
        }

        /** @var $optionsRepository ObjectRepository */
        $optionsRepository = $this->flexibleManager->getAttributeOptionRepository();
        $options = $optionsRepository->findBy(
            array('attribute' => $attribute)
        );

        $choices = array();
        /** @var $option AttributeOption */
        foreach ($options as $option) {
            $optionValue = $option->getOptionValue();
            if ($optionValue) {
                $choices[$option->getId()] = $optionValue->getValue();
            }
        }

{'fraction_non_alphanumeric': 0.14048418448489133, 'fraction_numerical': 0.0008835483300936561, 'mean_word_length': 2.716349310571241, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '2357879', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1506, 'n_words': 267}