<commit_before><?php

namespace Oro\Bundle\DashboardBundle\Model;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;

use Oro\Bundle\DashboardBundle\Form\Type\WidgetItemsChoiceType;
use Oro\Bundle\DashboardBundle\Entity\Widget;
use Oro\Bundle\DashboardBundle\Provider\ConfigValueProvider;
use Oro\Bundle\SecurityBundle\SecurityFacade;

use Oro\Component\Config\Resolver\ResolverInterface;

class WidgetConfigs
{
    /** @var ConfigProvider */
    protected $configProvider;

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

    /** @var ResolverInterface */
    protected $resolver;

    /** @var EntityManagerInterface */
    protected $entityManager;

    /** @var Request|null */
    protected $request;

    /** @var ConfigValueProvider */
    protected $valueProvider;

    /** @var TranslatorInterface */
    protected $translator;

    /**
     * @param ConfigProvider         $configProvider
     * @param SecurityFacade         $securityFacade
     * @param ResolverInterface      $resolver
     * @param EntityManagerInterface $entityManager
     * @param ConfigValueProvider    $valueProvider
     * @param TranslatorInterface    $translator
     */
    public function __construct(
        ConfigProvider $configProvider,
        SecurityFacade $securityFacade,
        ResolverInterface $resolver,
        EntityManagerInterface $entityManager,
        ConfigValueProvider $valueProvider,
        TranslatorInterface $translator
    ) {
        $this->configProvider = $configProvider;
        $this->securityFacade = $securityFacade;
        $this->resolver       = $resolver;
        $this->entityManager  = $entityManager;
        $this->valueProvider  = $valueProvider;
        $this->translator  = $translator;
    }

    /**
     * @param Request $request
     */
    public function setRequest(Request $request = null)
    {
        $this->request = $request;
    }

    /**
     * Returns widget attributes with attribute name converted to use in widget's TWIG template
     *
     * @param string $widgetName The name of widget
     *
     * @return array
     */
    public function getWidgetAttributesForTwig($widgetName)
    {
        $result = [
            'widgetName' => $widgetName
        ];

        $widget = $this->configProvider->getWidgetConfig($widgetName);
        unset($widget['route']);
        unset($widget['route_parameters']);
        unset($widget['acl']);
        unset($widget['items']);

        $options = $widget['configuration'];
        foreach ($options as $name => $config) {
            $widget['configuration'][$name]['value'] = $this->valueProvider->getViewValue(
                $config['type'],
                $this->getWidgetOptions()->get($name)
            );
        }

        foreach ($widget as $key => $val) {
            $attrName = 'widget';
            foreach (explode('_', str_replace('-', '_', $key)) as $keyPart) {
                $attrName .= ucfirst($keyPart);
            }
            $result[$attrName] = $val;
        }

        return $result;
    }

    /**
     * Returns filtered list of widget configuration
     * based on applicable flags and acl
     *
     * @return array
     */
    public function getWidgetConfigs()
    {
        return $this->filterWidgets($this->configProvider->getWidgetConfigs());
    }

    /**
     * Returns a list of items for the given widget
     *
     * @param string $widgetName The name of widget
     *
     * @return array
     */
    public function getWidgetItems($widgetName)
    {
        $widgetConfig = $this->configProvider->getWidgetConfig($widgetName);

        $items = isset($widgetConfig['items']) ? $widgetConfig['items'] : [];
        $items = $this->filterWidgets($items);

        return $items;
    }

    /**
     * @param $widgetName
     * @param $widgetId
     * @return array
     */
    public function getWidgetItemsData($widgetName, $widgetId)
    {
        $widgetConfig  = $this->configProvider->getWidgetConfig($widgetName);
        $widgetOptions = $this->getWidgetOptions($widgetId);

        $items = isset($widgetConfig['data_items']) ? $widgetConfig['data_items'] : [];
        $items = $this->filterWidgets($items, true, $widgetOptions->get('subWidgets', []));

        foreach ($items as $itemName => $config) {
            $items[$itemName]['value'] = $this->resolver->resolve(
                [$config['data_provider']],
                ['widgetOptions' => $widgetOptions]
            )[0];
        }

        return $items;
    }

    /**
     * Returns a list of options for widget with id $widgetId or current widget if $widgetId is not specified
     *
     * @param int|null $widgetId
     *
     * @return WidgetOptionBag
     */
    public function getWidgetOptions($widgetId = null)
    {
        if (!$this->request) {
            return new WidgetOptionBag();
        }

        if (!$widgetId) {
            $widgetId = $this->request->query->get('_widgetId', null);
        }

        if (!$widgetId) {
            return new WidgetOptionBag();
        }

        $widget       = $this->findWidget($widgetId);
        $widgetConfig = $this->configProvider->getWidgetConfig($widget->getName());
        $options      = $widget->getOptions();

        foreach ($widgetConfig['configuration'] as $name => $config) {
            $value          = isset($options[$name]) ? $options[$name] : null;
            $options[$name] = $this->valueProvider->getConvertedValue(
                $widgetConfig,
                $config['type'],
                $value,
                $config,
                $options
            );
        }

        return new WidgetOptionBag($options);
    }

    /**
     * @param Widget $widget
     * @return array
     */
    public function getFormValues(Widget $widget)
    {
        $options      = $widget->getOptions();
        $widgetConfig = $this->configProvider->getWidgetConfig($widget->getName());

        foreach ($widgetConfig['configuration'] as $name => $config) {
            $value          = isset($options[$name]) ? $options[$name] : null;
            $options[$name] = $this->valueProvider->getFormValue($config['type'], $config, $value);
        }

        $options = $this->loadDefaultValue($options, $widgetConfig);

        return $options;
    }

    /**
     * @param $options
     * @param $widgetConfig
     *
     * @return mixed
     */
    protected function loadDefaultValue($options, $widgetConfig)
    {
        if (!$options['title']) {
            $options['title'] = [
                'title'      => $this->translator->trans($widgetConfig['label']),
                'useDefault' => true
            ];
        }

        return $options;
    }

    /**<commit_msg>BAP-7999: Incorrect align of controls on widget settings pop-up - fix show default value<commit_after><?php

namespace Oro\Bundle\DashboardBundle\Model;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;

use Oro\Bundle\DashboardBundle\Form\Type\WidgetItemsChoiceType;
use Oro\Bundle\DashboardBundle\Entity\Widget;
use Oro\Bundle\DashboardBundle\Provider\ConfigValueProvider;
use Oro\Bundle\SecurityBundle\SecurityFacade;

use Oro\Component\Config\Resolver\ResolverInterface;

class WidgetConfigs
{
    /** @var ConfigProvider */
    protected $configProvider;

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

    /** @var ResolverInterface */
    protected $resolver;

    /** @var EntityManagerInterface */
    protected $entityManager;

    /** @var Request|null */
    protected $request;

    /** @var ConfigValueProvider */
    protected $valueProvider;

    /** @var TranslatorInterface */
    protected $translator;

    /**
     * @param ConfigProvider         $configProvider
     * @param SecurityFacade         $securityFacade
     * @param ResolverInterface      $resolver
     * @param EntityManagerInterface $entityManager
     * @param ConfigValueProvider    $valueProvider
     * @param TranslatorInterface    $translator
     */
    public function __construct(
        ConfigProvider $configProvider,
        SecurityFacade $securityFacade,
        ResolverInterface $resolver,
        EntityManagerInterface $entityManager,
        ConfigValueProvider $valueProvider,
        TranslatorInterface $translator
    ) {
        $this->configProvider = $configProvider;
        $this->securityFacade = $securityFacade;
        $this->resolver = $resolver;
        $this->entityManager = $entityManager;
        $this->valueProvider = $valueProvider;
        $this->translator = $translator;
    }

    /**
     * @param Request $request
     */
    public function setRequest(Request $request = null)
    {
        $this->request = $request;
    }

    /**
     * Returns widget attributes with attribute name converted to use in widget's TWIG template
     *
     * @param string $widgetName The name of widget
     *
     * @return array
     */
    public function getWidgetAttributesForTwig($widgetName)
    {
        $result = [
            'widgetName' => $widgetName
        ];

        $widget = $this->configProvider->getWidgetConfig($widgetName);
        unset($widget['route']);
        unset($widget['route_parameters']);
        unset($widget['acl']);
        unset($widget['items']);

        $options = $widget['configuration'];
        foreach ($options as $name => $config) {
            $widget['configuration'][$name]['value'] = $this->valueProvider->getViewValue(
                $config['type'],
                $this->getWidgetOptions()->get($name)
            );
        }

        foreach ($widget as $key => $val) {
            $attrName = 'widget';
            foreach (explode('_', str_replace('-', '_', $key)) as $keyPart) {
                $attrName .= ucfirst($keyPart);
            }
            $result[$attrName] = $val;
        }

        return $result;
    }

    /**
     * Returns filtered list of widget configuration
     * based on applicable flags and acl
     *
     * @return array
     */
    public function getWidgetConfigs()
    {
        return $this->filterWidgets($this->configProvider->getWidgetConfigs());
    }

    /**
     * Returns a list of items for the given widget
     *
     * @param string $widgetName The name of widget
     *
     * @return array
     */
    public function getWidgetItems($widgetName)
    {
        $widgetConfig = $this->configProvider->getWidgetConfig($widgetName);

        $items = isset($widgetConfig['items']) ? $widgetConfig['items'] : [];
        $items = $this->filterWidgets($items);

        return $items;
    }

    /**
     * @param $widgetName
     * @param $widgetId
     * @return array
     */
    public function getWidgetItemsData($widgetName, $widgetId)
    {
        $widgetConfig  = $this->configProvider->getWidgetConfig($widgetName);
        $widgetOptions = $this->getWidgetOptions($widgetId);

        $items = isset($widgetConfig['data_items']) ? $widgetConfig['data_items'] : [];
        $items = $this->filterWidgets($items, true, $widgetOptions->get('subWidgets', []));

        foreach ($items as $itemName => $config) {
            $items[$itemName]['value'] = $this->resolver->resolve(
                [$config['data_provider']],
                ['widgetOptions' => $widgetOptions]
            )[0];
        }

        return $items;
    }

    /**
     * Returns a list of options for widget with id $widgetId or current widget if $widgetId is not specified
     *
     * @param int|null $widgetId
     *
     * @return WidgetOptionBag
     */
    public function getWidgetOptions($widgetId = null)
    {
        if (!$this->request) {
            return new WidgetOptionBag();
        }

        if (!$widgetId) {
            $widgetId = $this->request->query->get('_widgetId', null);
        }

        if (!$widgetId) {
            return new WidgetOptionBag();
        }

        $widget       = $this->findWidget($widgetId);
        $widgetConfig = $this->configProvider->getWidgetConfig($widget->getName());
        $options      = $widget->getOptions();

        foreach ($widgetConfig['configuration'] as $name => $config) {
            $value          = isset($options[$name]) ? $options[$name] : null;
            $options[$name] = $this->valueProvider->getConvertedValue(
                $widgetConfig,
                $config['type'],
                $value,
                $config,
                $options
            );
        }

        return new WidgetOptionBag($options);
    }

    /**
     * @param Widget $widget
     * @return array
     */
    public function getFormValues(Widget $widget)
    {
        $options      = $widget->getOptions();
        $widgetConfig = $this->configProvider->getWidgetConfig($widget->getName());

        foreach ($widgetConfig['configuration'] as $name => $config) {
            $value          = isset($options[$name]) ? $options[$name] : null;
            $options[$name] = $this->valueProvider->getFormValue($config['type'], $config, $value);
        }

        $options = $this->loadDefaultValue($options, $widgetConfig);

        return $options;
    }

    /**
     * @param $options
     * @param $widgetConfig
     *
     * @return mixed
     */
    protected function loadDefaultValue($options, $widgetConfig)
    {
        if (!$options['title']['title'] || $options['title']['useDefault']) {
            $options['title']['title'] = $this->translator->trans($widgetConfig['label']);
            $options['title']['useDefault'] = true;
        }

        return $options;
    }

    /**
{'fraction_non_alphanumeric': 0.13358138177573503, 'fraction_numerical': 0.0004377325454147516, 'mean_word_length': 2.316719090249214, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, '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': '1082836', 'n_tokens_mistral': 3708, 'n_tokens_neox': 3646, 'n_words': 748}