<commit_before><?php

namespace Oro\Bundle\UIBundle\Twig;

use Symfony\Component\HttpFoundation\Request;

use Twig_Environment;

class WidgetExtension extends \Twig_Extension
{
    const EXTENSION_NAME = 'oro_widget';

    /**
     * Protect extension from infinite loop
     *
     * @var bool
     */
    protected $rendered = [];

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

    /**
     * Returns a list of functions to add to the existing list.
     *
     * @return array An array of functions
     */
    public function getFunctions()
    {
        return [
            'oro_widget_render' => new \Twig_Function_Method(
                $this,
                'render',
                [
                    'is_safe'           => ['html'],
                    'needs_environment' => true
                ]
            )
        ];
    }

    /**
     * Renders a widget.
     *
     * @param \Twig_Environment $environment
     * @param array             $options
     *
     * @throws \InvalidArgumentException
     * @return string
     */
    public function render(Twig_Environment $environment, array $options = [])
    {
        $optionsHash = md5(json_encode($options));

        if (!empty($this->rendered[$optionsHash])) {
            return '';
        }

        $this->rendered[$optionsHash] = true;

        if (!array_key_exists('url', $options)) {
            throw new \InvalidArgumentException('Option url is required');
        }

        if (!array_key_exists('widgetType', $options)) {
            throw new \InvalidArgumentException('Option widgetType is required');
        } else {
            $widgetType = $options['widgetType'];
            unset($options['widgetType']);
        }

        if (!isset($options['wid'])) {
            $options['wid'] = $this->getUniqueIdentifier();
        }

        $elementId = 'widget-container-' . $options['wid'];

        if (!array_key_exists('elementFirst', $options)) {
            $options['elementFirst'] = true;
        }

        if ($options['elementFirst']) {
            $options['el'] = '#' . $elementId . ' .widget-content:first';
        } else {
            $options['container'] = '#' . $elementId;
        }

        $options['url'] = $this->getUrlWithContainer(
            $options['url'],
            $widgetType,
            $options['wid'],
            $options['state']['id']
        );

        if ($this->request) {
            $options['url'] = $this->addRequestParameters($options['url']);
        }

        return $environment->render(
            'OroUIBundle::widget_loader.html.twig',
            [
                'elementId'  => $elementId,
                'options'    => $options,
                'widgetType' => $widgetType,
            ]
        );
    }

    /**
     * @param string $url
     * @param string $widgetType
     * @param string $wid
     * @return string
     */
    protected function getUrlWithContainer($url, $widgetType, $wid, $id)<commit_msg>BAP-7689: Dashboard widget configuration<commit_after><?php

namespace Oro\Bundle\UIBundle\Twig;

use Symfony\Component\HttpFoundation\Request;

use Twig_Environment;

class WidgetExtension extends \Twig_Extension
{
    const EXTENSION_NAME = 'oro_widget';

    /**
     * Protect extension from infinite loop
     *
     * @var bool
     */
    protected $rendered = [];

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

    /**
     * Returns a list of functions to add to the existing list.
     *
     * @return array An array of functions
     */
    public function getFunctions()
    {
        return [
            'oro_widget_render' => new \Twig_Function_Method(
                $this,
                'render',
                [
                    'is_safe'           => ['html'],
                    'needs_environment' => true
                ]
            )
        ];
    }

    /**
     * Renders a widget.
     *
     * @param \Twig_Environment $environment
     * @param array             $options
     *
     * @throws \InvalidArgumentException
     * @return string
     */
    public function render(Twig_Environment $environment, array $options = [])
    {
        $optionsHash = md5(json_encode($options));

        if (!empty($this->rendered[$optionsHash])) {
            return '';
        }

        $this->rendered[$optionsHash] = true;

        if (!array_key_exists('url', $options)) {
            throw new \InvalidArgumentException('Option url is required');
        }

        if (!array_key_exists('widgetType', $options)) {
            throw new \InvalidArgumentException('Option widgetType is required');
        } else {
            $widgetType = $options['widgetType'];
            unset($options['widgetType']);
        }

        if (!isset($options['wid'])) {
            $options['wid'] = $this->getUniqueIdentifier();
        }

        $elementId = 'widget-container-' . $options['wid'];

        if (!array_key_exists('elementFirst', $options)) {
            $options['elementFirst'] = true;
        }

        if ($options['elementFirst']) {
            $options['el'] = '#' . $elementId . ' .widget-content:first';
        } else {
            $options['container'] = '#' . $elementId;
        }

        $options['url'] = $this->getUrlWithContainer(
            $options['url'],
            $widgetType,
            $options['wid'],
            isset($options['state']) && isset($options['state']['id']) ? $options['state']['id'] : null
        );

        if ($this->request) {
            $options['url'] = $this->addRequestParameters($options['url']);
        }

        return $environment->render(
            'OroUIBundle::widget_loader.html.twig',
            [
                'elementId'  => $elementId,
                'options'    => $options,
                'widgetType' => $widgetType,
            ]
        );
    }

    /**
     * @param string $url
     * @param string $widgetType
     * @param string $wid
     * @return string
     */
    protected function getUrlWithContainer($url, $widgetType, $wid, $id)
{'fraction_non_alphanumeric': 0.14052986671054796, 'fraction_numerical': 0.0009873292743129835, 'mean_word_length': 1.7715458276333789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '7480191', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1621, 'n_words': 327}