<commit_before><?php

/*
 * This file is part of the TableBundle.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JGM\TableBundle\Twig;

use JGM\TableBundle\DependencyInjection\Service\TableStopwatchService;
use JGM\TableBundle\Table\Filter\FilterInterface;
use JGM\TableBundle\Table\Filter\OptionsResolver\FilterOptions;
use JGM\TableBundle\Table\TableException;
use JGM\TableBundle\Table\TableView;
use JGM\TableBundle\Table\Utils\UrlHelper;
use Twig_Environment;
use Twig_SimpleFunction;

/**
 * Twig extension for render the table filter views.
 * 
 * @author	<NAME> <<EMAIL>>
 * @since	1.3
 */
class FilterExtension extends AbstractTwigExtension
{
	/**
	 * Not every filter_* method, called from 
	 * twig tempalte, needs the view. There are
	 * some methods like filter_label, which accepts
	 * the filter interface only. For these methods,
	 * we'll save the tableView, which were passed on
	 * beginning (filter_begin).
	 * 
	 * @var TableView 
	 */
	protected $tableView;
	
	/**
	 * Array for mapping tables and their
	 * need for form environments.
	 * The key of the map is the table name,
	 * the value is a boolean.
	 * 
	 * @var array
	 */
	protected $filterNeedsFormEnvironment;
	
	public function __construct(UrlHelper $urlHelper, TableStopwatchService $stopwatchService)
	{
		parent::__construct($urlHelper, $stopwatchService);
		
		$this->filterNeedsFormEnvironment = array();
	}
	
	public function getName()
	{
		return 'filter';
	}

	public function getFunctions()
	{
		return array(
			new Twig_SimpleFunction ('filter', array($this, 'getFilterContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_label', array($this, 'getFilterLabelContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_widget', array($this, 'getFilterWidgetContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_row', array($this, 'getFilterRowContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_rows', array($this, 'getFilterRowsContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_begin', array($this, 'getFilterBeginContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_submit_button', array($this, 'getFilterSubmitButtonContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_reset_link', array($this, 'getFilterResetLinkContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_end', array($this, 'getFilterEndContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_url', array($this, 'getFilterUrl'))
		);
	}
	public function getFilterContent(Twig_Environment $environment, TableView $tableView)
	{
		$template = $this->loadTemplate(
			$environment, 
			$this->tableView->getFilterOption(FilterOptions::TEMPLATE)
		);
		
		return $template->renderBlock('filter', array(
				'view' => $tableView
		));
	}
	
	public function getFilterBeginContent(Twig_Environment $environment, TableView $tableView)
	{
		$this->tableView = $tableView;
		
		$template = $this->loadTemplate(
			$environment, 
			$this->tableView->getFilterOption(FilterOptions::TEMPLATE)
		);
		
		$content = $template->renderBlock('filter_begin', array(
			'needsFormEnviroment' => $this->getFilterNeedsFormEnviroment($tableView),<commit_msg>Fixed error rendering table filters with "filter" twig function<commit_after><?php

/*
 * This file is part of the TableBundle.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JGM\TableBundle\Twig;

use JGM\TableBundle\DependencyInjection\Service\TableStopwatchService;
use JGM\TableBundle\Table\Filter\FilterInterface;
use JGM\TableBundle\Table\Filter\OptionsResolver\FilterOptions;
use JGM\TableBundle\Table\TableException;
use JGM\TableBundle\Table\TableView;
use JGM\TableBundle\Table\Utils\UrlHelper;
use Twig_Environment;
use Twig_SimpleFunction;

/**
 * Twig extension for render the table filter views.
 * 
 * @author	<NAME> <<EMAIL>>
 * @since	1.3
 */
class FilterExtension extends AbstractTwigExtension
{
	/**
	 * Not every filter_* method, called from 
	 * twig template, needs the view. There are
	 * some methods like filter_label, which accepts
	 * the filter interface only. For these methods,
	 * we'll save the tableView, which were passed on
	 * beginning (filter_begin).
	 * 
	 * @var TableView 
	 */
	protected $tableView;
	
	/**
	 * Array for mapping tables and their
	 * need for form environments.
	 * The key of the map is the table name,
	 * the value is a boolean.
	 * 
	 * @var array
	 */
	protected $filterNeedsFormEnvironment;
	
	public function __construct(UrlHelper $urlHelper, TableStopwatchService $stopwatchService)
	{
		parent::__construct($urlHelper, $stopwatchService);
		
		$this->filterNeedsFormEnvironment = array();
	}
	
	public function getName()
	{
		return 'filter';
	}

	public function getFunctions()
	{
		return array(
			new Twig_SimpleFunction ('filter', array($this, 'getFilterContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_label', array($this, 'getFilterLabelContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_widget', array($this, 'getFilterWidgetContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_row', array($this, 'getFilterRowContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_rows', array($this, 'getFilterRowsContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_begin', array($this, 'getFilterBeginContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_submit_button', array($this, 'getFilterSubmitButtonContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_reset_link', array($this, 'getFilterResetLinkContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_end', array($this, 'getFilterEndContent'), array('is_safe' => array('html'), 'needs_environment' => true)),
			new Twig_SimpleFunction ('filter_url', array($this, 'getFilterUrl'))
		);
	}
	public function getFilterContent(Twig_Environment $environment, TableView $tableView)
	{
		$template = $this->loadTemplate(
			$environment, 
			$tableView->getFilterOption(FilterOptions::TEMPLATE)
		);
		
		return $template->renderBlock('filter', array(
				'view' => $tableView
		));
	}
	
	public function getFilterBeginContent(Twig_Environment $environment, TableView $tableView)
	{
		$this->tableView = $tableView;
		
		$template = $this->loadTemplate(
			$environment, 
			$this->tableView->getFilterOption(FilterOptions::TEMPLATE)
		);
		
		$content = $template->renderBlock('filter_begin', array(
			'needsFormEnviroment' => $this->getFilterNeedsFormEnviroment($tableView),
{'fraction_non_alphanumeric': 0.137546468401487, 'fraction_numerical': 0.0005310674455655868, 'mean_word_length': 5.642857142857143, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 76, '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': '322015', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2191, 'n_words': 549}