<commit_before><?php

namespace Oro\Bundle\TagBundle\Grid;

use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;
use Oro\Bundle\EntityBundle\ORM\EntityClassResolver;
use Oro\Bundle\EntityBundle\Tools\EntityRoutingHelper;
use Oro\Bundle\TagBundle\Entity\TagManager;
use Oro\Bundle\TagBundle\Helper\TaggableHelper;
use Oro\Bundle\SecurityBundle\SecurityFacade;

class TagsExtension extends AbstractTagsExtension
{
    /** @var TaggableHelper */
    protected $taggableHelper;

    /** @var EntityRoutingHelper */
    protected $entityRoutingHelper;

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

    /**
     * @param TagManager          $tagManager
     * @param EntityClassResolver $resolver
     * @param TaggableHelper      $helper
     * @param EntityRoutingHelper $entityRoutingHelper
     * @param SecurityFacade      $securityFacade
     */
    public function __construct(
        TagManager $tagManager,
        EntityClassResolver $resolver,
        TaggableHelper $helper,
        EntityRoutingHelper $entityRoutingHelper,
        SecurityFacade $securityFacade
    ) {
        parent::__construct($tagManager, $resolver);

        $this->taggableHelper      = $helper;
        $this->entityRoutingHelper = $entityRoutingHelper;
        $this->securityFacade      = $securityFacade;
    }

    /**
     * {@inheritdoc}
     */
    public function isApplicable(DatagridConfiguration $config)
    {
        $className = $this->getEntityClassName($config);

        return
            !$this->isReportOrSegmentGrid($config) &&
            $className &&
            $this->taggableHelper->isTaggable($className) &&
            null !== $config->offsetGetByPath(self::PROPERTY_ID_PATH) &&
            null !== $this->securityFacade->getToken() &&
            $this->securityFacade->isGranted('oro_tag_view');
    }

    /**
     * {@inheritdoc}
     */
    public function processConfigs(DatagridConfiguration $config)
    {
        $columns = $config->offsetGetByPath('[columns]', []);
        $column  = [self::COLUMN_NAME => $this->getColumnDefinition($config)];
        $config->offsetSetByPath('[columns]', array_merge($columns, $column));

        // do not add tag filter if $filters are empty(case when they are disabled).
        $filters = $config->offsetGetByPath(self::GRID_FILTERS_PATH, []);<commit_msg>CBORO-225: Dotmailer watchdog import status break export - fix tag extension exception during marketing list segment build: add disable param for extension<commit_after><?php

namespace Oro\Bundle\TagBundle\Grid;

use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;
use Oro\Bundle\EntityBundle\ORM\EntityClassResolver;
use Oro\Bundle\EntityBundle\Tools\EntityRoutingHelper;
use Oro\Bundle\TagBundle\Entity\TagManager;
use Oro\Bundle\TagBundle\Helper\TaggableHelper;
use Oro\Bundle\SecurityBundle\SecurityFacade;

class TagsExtension extends AbstractTagsExtension
{
    const ROOT_PARAM = '_tags';
    const DISABLED_PARAM   = '_disabled';

    /** @var TaggableHelper */
    protected $taggableHelper;

    /** @var EntityRoutingHelper */
    protected $entityRoutingHelper;

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

    /**
     * @param TagManager          $tagManager
     * @param EntityClassResolver $resolver
     * @param TaggableHelper      $helper
     * @param EntityRoutingHelper $entityRoutingHelper
     * @param SecurityFacade      $securityFacade
     */
    public function __construct(
        TagManager $tagManager,
        EntityClassResolver $resolver,
        TaggableHelper $helper,
        EntityRoutingHelper $entityRoutingHelper,
        SecurityFacade $securityFacade
    ) {
        parent::__construct($tagManager, $resolver);

        $this->taggableHelper      = $helper;
        $this->entityRoutingHelper = $entityRoutingHelper;
        $this->securityFacade      = $securityFacade;
    }

    /**
     * {@inheritdoc}
     */
    public function isApplicable(DatagridConfiguration $config)
    {
        $tagParameters = $this->getParameters()->get(self::ROOT_PARAM);
        $isDisabled = $tagParameters && !empty($tagParameters[self::DISABLED_PARAM]);

        return !$isDisabled
            && !$this->isReportOrSegmentGrid($config)
            && $this->isGridRootEntityTaggable($config)
            && null !== $config->offsetGetByPath(self::PROPERTY_ID_PATH)
            && $this->isAccessGranted();
    }

    /**
     * @param DatagridConfiguration $configuration
     *
     * @return bool
     */
    protected function isGridRootEntityTaggable(DatagridConfiguration $configuration)
    {
        $className = $this->getEntityClassName($configuration);
        return $className && $this->taggableHelper->isTaggable($className);
    }

    /**
     * @return bool
     */
    protected function isAccessGranted()
    {
        return null !== $this->securityFacade->getToken() && $this->securityFacade->isGranted('oro_tag_view');
    }

    /**
     * {@inheritdoc}
     */
    public function processConfigs(DatagridConfiguration $config)
    {
        $columns = $config->offsetGetByPath('[columns]', []);
        $column  = [self::COLUMN_NAME => $this->getColumnDefinition($config)];
        $config->offsetSetByPath('[columns]', array_merge($columns, $column));

        // do not add tag filter if $filters are empty(case when they are disabled).
        $filters = $config->offsetGetByPath(self::GRID_FILTERS_PATH, []);
{'fraction_non_alphanumeric': 0.11697370774458953, 'fraction_numerical': 0.0005365766410302271, 'mean_word_length': 3.2460136674259683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '126993', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1576, 'n_words': 276}