<commit_before><?php

namespace Oro\Bundle\DashboardBundle\Provider\Converters;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\DashboardBundle\Provider\ConfigValueConverterAbstract;
use Oro\Bundle\SecurityBundle\SecurityFacade;
use Oro\Bundle\OrganizationBundle\Provider\BusinessUnitAclProvider;

/**
 * Class WidgetBusinessUnitSelectConverter
 * @package Oro\Bundle\DashboardBundle\Provider\Converters
 */
class WidgetBusinessUnitSelectConverter extends ConfigValueConverterAbstract
{
    /** @var EntityRepository */
    protected $businessUnitRepository;

    /** @var SecurityFacade */
    protected $securityFacade;

    /** @var BusinessUnitAclProvider */
    protected $businessUnitAclProvider;

    /**
     * @param EntityRepository $businessUnitRepository
     * @param SecurityFacade $securityFacade
     * @param BusinessUnitAclProvider $businessUnitAclProvider
     */
    public function __construct(
        EntityRepository $businessUnitRepository,
        SecurityFacade $securityFacade,
        BusinessUnitAclProvider $businessUnitAclProvider
    ) {
        $this->businessUnitRepository = $businessUnitRepository;
        $this->securityFacade = $securityFacade;
        $this->businessUnitAclProvider = $businessUnitAclProvider;
    }

    /**
     * {@inheritdoc}
     */
    public function getConvertedValue(array $widgetConfig, $value = null, array $config = [], array $options = [])
    {
        if ($value === null) {
            return $this->getBusinessUnitList($config);
        }

        return parent::getConvertedValue($widgetConfig, $value, $config, $options);
    }

    /**
     * {@inheritdoc}
     */
    public function getFormValue(array $converterAttributes, $value)
    {
        if ($value === null) {
            return $this->getBusinessUnitList($converterAttributes);
        }

        return parent::getFormValue($converterAttributes, $value);
    }

    /**
     * @param mixed $value
     * @return mixed
     */
    public function getViewValue($value)
    {
        return empty($value) ? null : implode('; ', $value);
    }

    /**
     * @param array $config
     * @return array
     */
    public function getBusinessUnitList($config)
    {
        $aclClass = isset($config['aclClass']) ? $config['aclClass'] : null;
        $aclPermission = isset($config['aclPermission']) ? $config['aclPermission'] : null;
        $queryBuilder = $this->businessUnitRepository->createQueryBuilder('businessUnit');

        if ($aclClass && $aclPermission) {

            $businessUnitIds = $this
                ->businessUnitAclProvider
                ->getBusinessUnitIds($aclClass, $aclPermission);

            if (!is_array($businessUnitIds) || count($businessUnitIds) === 0) {
                $businessUnitIds = [0];
            }

            $this->applyAdditionalConditions(
                $queryBuilder,
                $businessUnitIds,
                $this->businessUnitAclProvider->getProcessedEntityAccessLevel()
            );
        }

        return $queryBuilder->getQuery()->getResult();<commit_msg>BAP-9756: List of BU doesn't displayed according ACL on the freshly added Forecast widget<commit_after><?php

namespace Oro\Bundle\DashboardBundle\Provider\Converters;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\DashboardBundle\Provider\ConfigValueConverterAbstract;
use Oro\Bundle\SecurityBundle\SecurityFacade;
use Oro\Bundle\OrganizationBundle\Provider\BusinessUnitAclProvider;

/**
 * Class WidgetBusinessUnitSelectConverter
 * @package Oro\Bundle\DashboardBundle\Provider\Converters
 */
class WidgetBusinessUnitSelectConverter extends ConfigValueConverterAbstract
{
    /** @var EntityRepository */
    protected $businessUnitRepository;

    /** @var SecurityFacade */
    protected $securityFacade;

    /** @var BusinessUnitAclProvider */
    protected $businessUnitAclProvider;

    /**
     * @param EntityRepository $businessUnitRepository
     * @param SecurityFacade $securityFacade
     * @param BusinessUnitAclProvider $businessUnitAclProvider
     */
    public function __construct(
        EntityRepository $businessUnitRepository,
        SecurityFacade $securityFacade,
        BusinessUnitAclProvider $businessUnitAclProvider
    ) {
        $this->businessUnitRepository = $businessUnitRepository;
        $this->securityFacade = $securityFacade;
        $this->businessUnitAclProvider = $businessUnitAclProvider;
    }

    /**
     * {@inheritdoc}
     */
    public function getConvertedValue(array $widgetConfig, $value = null, array $config = [], array $options = [])
    {
        if ($value === null) {
            return $this->getBusinessUnitList($config);
        }

        return parent::getConvertedValue($widgetConfig, $value, $config, $options);
    }

    /**
     * {@inheritdoc}
     */
    public function getFormValue(array $converterAttributes, $value)
    {
        if ($value === null) {
            return $this->getBusinessUnitList($converterAttributes);
        }

        return parent::getFormValue($converterAttributes, $value);
    }

    /**
     * @param mixed $value
     * @return mixed
     */
    public function getViewValue($value)
    {
        return empty($value) ? null : implode('; ', $value);
    }

    /**
     * @param array $config
     * @return array
     */
    public function getBusinessUnitList($config)
    {
        $aclClass = isset($config['aclClass']) ? $config['aclClass'] : null;
        $aclPermission = isset($config['aclPermission']) ? $config['aclPermission'] : null;
        $queryBuilder = $this->businessUnitRepository->createQueryBuilder('businessUnit');

        if ($aclClass && $aclPermission) {
            $businessUnitIds = $this
                ->businessUnitAclProvider
                ->getBusinessUnitIds($aclClass, $aclPermission);

            if (!is_array($businessUnitIds) || count($businessUnitIds) === 0) {
                $businessUnitIds = [0];
            }

            $this->applyAdditionalConditions(
                $queryBuilder,
                $businessUnitIds,
                $this->businessUnitAclProvider->getProcessedEntityAccessLevel()
            );
        }

        return $queryBuilder->getQuery()->getResult();
{'fraction_non_alphanumeric': 0.11439936356404137, 'fraction_numerical': 0.0012728719172633255, 'mean_word_length': 2.923845193508115, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '3527756', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1646, 'n_words': 298}