<commit_before><?php
/**
 * IndexViewer.php
 *
 * @since 26/06/15
 * @author gseidel
 */

namespace Enhavo\Bundle\AppBundle\Viewer\Viewer;

use Enhavo\Bundle\AppBundle\Action\ActionManager;
use Enhavo\Bundle\AppBundle\Batch\BatchManager;
use Enhavo\Bundle\AppBundle\Controller\RequestConfiguration;
use Enhavo\Bundle\AppBundle\Filter\FilterManager;
use Enhavo\Bundle\AppBundle\Column\ColumnManager;
use Enhavo\Bundle\AppBundle\Viewer\ViewerUtil;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactory;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IndexViewer extends AppViewer
{
    /**
     * @var ActionManager
     */
    private $actionManager;

    /**
     * @var BatchManager
     */
    private $batchManager;

    /**
     * @var FilterManager
     */
    private $filterManager;

    /**
     * @var ColumnManager
     */
    private $columnManager;


    public function __construct(
        RequestConfigurationFactory $requestConfigurationFactory,
        ViewerUtil $util,
        ActionManager $actionManager,
        BatchManager $batchManager,
        FilterManager $filterManager,
        ColumnManager $columnManager
    ) {
        parent::__construct($requestConfigurationFactory, $util);
        $this->actionManager = $actionManager;
        $this->batchManager = $batchManager;
        $this->filterManager = $filterManager;
        $this->columnManager = $columnManager;
    }

    public function getType()
    {
        return 'index';
    }

    protected function buildTemplateParameters(ParameterBag $parameters, RequestConfiguration $requestConfiguration, array $options)
    {
        parent::buildTemplateParameters($parameters, $requestConfiguration, $options);

        $actions = $this->mergeConfigArray([
            $this->createActions($options),
            $options['actions'],
            $this->getViewerOption('actions', $requestConfiguration)
        ]);

        $tableRoute = $this->mergeConfig([
            $this->getTableRoute($options),
            $options['table_route'],
            $this->getViewerOption('table_route', $requestConfiguration)
        ]);

        $tableConfiguration = $this->util->createConfigurationFromRoute($tableRoute);

        $filterData = $tableConfiguration->getFilters();
        $columnData = $this->getViewerOption('columns', $tableConfiguration);

        $batchRoute = $this->mergeConfig([
            $this->getBatchRoute($options),
            $options['batch_route'],
            $this->getViewerOption('batch_route', $requestConfiguration)
        ]);

        $batchConfiguration = $this->util->createConfigurationFromRoute($batchRoute);
        $batchData = $batchConfiguration->getBatches();

        /** @var Request $request */
        $request = $requestConfiguration->getRequest();

        $viewerOptions = $requestConfiguration->getViewerOptions();
        if(isset($viewerOptions['translationDomain'])) {
            $this->addTranslationDomain($columnData, $viewerOptions['translationDomain']);
        }

        $grid = [
            'tableRoute' => $tableRoute,
            'batchRoute' => $batchRoute,
            'page' => $request->get('page', 1),
            'batches' => $this->batchManager->createBatchesViewData($batchData),
            'columns' => $this->columnManager->createColumnsViewData($columnData),
            'filters' => $this->filterManager->createFiltersViewData($filterData),
            'pagination' => 100,
            'paginationSteps' => [
                5, 10, 50, 100, 500
            ],
        ];
        
        $parameters->set('data', [
            'grid' => $grid,
            'actions' => $this->actionManager->createActionsViewData($actions),
            'view_id' => $request->get('view_id', null),
        ]);

        return;
    }

    private function addTranslationDomain(&$configuration, $translationDomain)
    {
        foreach($configuration as &$config) {
            if(!isset($config['translation_domain'])) {
                $config['translation_domain'] = $translationDomain;
            }
        }
    }

    private function getTableRoute($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];
        return sprintf('%s_%s_table', $metadata->getApplicationName(), $this->getUnderscoreName($metadata));
    }

    private function getBatchRoute($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];
        return sprintf('%s_%s_batch', $metadata->getApplicationName(), $this->getUnderscoreName($metadata));
    }

    private function createActions($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];

        $default = [
            'create' => [
                'type' => 'create',
                'route' => sprintf('%s_%s_create', $metadata->getApplicationName(), $this->getUnderscoreName($metadata)),
            ]
        ];

        return $default;
    }

    public function configureOptions(OptionsResolver $optionsResolver)
    {
        parent::configureOptions($optionsResolver);

        $optionsResolver->setDefaults([
            'javascripts' => [
                'enhavo/index'
            ],
            'stylesheets' => [
                'enhavo/index'
            ],
            'actions' => [],
            'table_route' => null,
            'batch_route' => null
        ]);
    }
}<commit_msg>open and close edit views<commit_after><?php
/**
 * IndexViewer.php
 *
 * @since 26/06/15
 * @author gseidel
 */

namespace Enhavo\Bundle\AppBundle\Viewer\Viewer;

use Enhavo\Bundle\AppBundle\Action\ActionManager;
use Enhavo\Bundle\AppBundle\Batch\BatchManager;
use Enhavo\Bundle\AppBundle\Controller\RequestConfiguration;
use Enhavo\Bundle\AppBundle\Filter\FilterManager;
use Enhavo\Bundle\AppBundle\Column\ColumnManager;
use Enhavo\Bundle\AppBundle\Viewer\ViewerUtil;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactory;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IndexViewer extends AppViewer
{
    /**
     * @var ActionManager
     */
    private $actionManager;

    /**
     * @var BatchManager
     */
    private $batchManager;

    /**
     * @var FilterManager
     */
    private $filterManager;

    /**
     * @var ColumnManager
     */
    private $columnManager;


    public function __construct(
        RequestConfigurationFactory $requestConfigurationFactory,
        ViewerUtil $util,
        ActionManager $actionManager,
        BatchManager $batchManager,
        FilterManager $filterManager,
        ColumnManager $columnManager
    ) {
        parent::__construct($requestConfigurationFactory, $util);
        $this->actionManager = $actionManager;
        $this->batchManager = $batchManager;
        $this->filterManager = $filterManager;
        $this->columnManager = $columnManager;
    }

    public function getType()
    {
        return 'index';
    }

    protected function buildTemplateParameters(ParameterBag $parameters, RequestConfiguration $requestConfiguration, array $options)
    {
        parent::buildTemplateParameters($parameters, $requestConfiguration, $options);

        $actions = $this->mergeConfigArray([
            $this->createActions($options),
            $options['actions'],
            $this->getViewerOption('actions', $requestConfiguration)
        ]);

        $tableRoute = $this->mergeConfig([
            $this->getTableRoute($options),
            $options['table_route'],
            $this->getViewerOption('table_route', $requestConfiguration)
        ]);

        $tableConfiguration = $this->util->createConfigurationFromRoute($tableRoute);

        $filterData = $tableConfiguration->getFilters();
        $columnData = $this->getViewerOption('columns', $tableConfiguration);

        $batchRoute = $this->mergeConfig([
            $this->getBatchRoute($options),
            $options['batch_route'],
            $this->getViewerOption('batch_route', $requestConfiguration)
        ]);

        $updateRoute = $this->mergeConfig([
            $this->geUpdateRoute($options),
            $options['update_route'],
            $this->getViewerOption('update_route', $requestConfiguration)
        ]);

        $batchConfiguration = $this->util->createConfigurationFromRoute($batchRoute);
        $batchData = $batchConfiguration->getBatches();

        /** @var Request $request */
        $request = $requestConfiguration->getRequest();

        $viewerOptions = $requestConfiguration->getViewerOptions();
        if(isset($viewerOptions['translationDomain'])) {
            $this->addTranslationDomain($columnData, $viewerOptions['translationDomain']);
        }

        $grid = [
            'tableRoute' => $tableRoute,
            'batchRoute' => $batchRoute,
            'updateRoute' => $updateRoute,
            'page' => $request->get('page', 1),
            'batches' => $this->batchManager->createBatchesViewData($batchData),
            'columns' => $this->columnManager->createColumnsViewData($columnData),
            'filters' => $this->filterManager->createFiltersViewData($filterData),
            'pagination' => 100,
            'paginationSteps' => [
                5, 10, 50, 100, 500
            ],
        ];
        
        $parameters->set('data', [
            'grid' => $grid,
            'actions' => $this->actionManager->createActionsViewData($actions),
            'view_id' => $request->get('view_id', null),
        ]);

        return;
    }

    private function addTranslationDomain(&$configuration, $translationDomain)
    {
        foreach($configuration as &$config) {
            if(!isset($config['translation_domain'])) {
                $config['translation_domain'] = $translationDomain;
            }
        }
    }

    private function getTableRoute($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];
        return sprintf('%s_%s_table', $metadata->getApplicationName(), $this->getUnderscoreName($metadata));
    }

    private function getBatchRoute($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];
        return sprintf('%s_%s_batch', $metadata->getApplicationName(), $this->getUnderscoreName($metadata));
    }

    private function geUpdateRoute($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];
        return sprintf('%s_%s_update', $metadata->getApplicationName(), $this->getUnderscoreName($metadata));
    }

    private function createActions($options)
    {
        /** @var MetadataInterface $metadata */
        $metadata = $options['metadata'];

        $default = [
            'create' => [
                'type' => 'create',
                'route' => sprintf('%s_%s_create', $metadata->getApplicationName(), $this->getUnderscoreName($metadata)),
            ]
        ];

        return $default;
    }

    public function configureOptions(OptionsResolver $optionsResolver)
    {
        parent::configureOptions($optionsResolver);

        $optionsResolver->setDefaults([
            'javascripts' => [
                'enhavo/index'
            ],
            'stylesheets' => [
                'enhavo/index'
            ],
            'actions' => [],
            'table_route' => null,
            'batch_route' => null,
            'update_route' => null
        ]);
    }
}
{'fraction_non_alphanumeric': 0.13280853224987305, 'fraction_numerical': 0.0035551041137633316, 'mean_word_length': 2.6342663795755152, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 130, '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': '3370008', 'n_tokens_mistral': 3139, 'n_tokens_neox': 3042, 'n_words': 482}