Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Change to using separate filter block
Browse files Browse the repository at this point in the history
This block ensures the field is properly URL-encoded when filtering. It
properly accounts for URL paths that have query strings and multiple
path segments too.
  • Loading branch information
Matthew Gamble committed Nov 16, 2015
1 parent 62a07b7 commit 8ae31cb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category BL
* @package BL_CustomGrid
* @copyright Copyright (c) 2015 Benoît Leulliette <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

class BL_CustomGrid_Block_Widget_Grid_Column_Filter_Text_Urldecoded extends BL_CustomGrid_Block_Widget_Grid_Column_Filter_Text
{
/**
* Overridden to strip domain from filter value, as it does not make sense
*
* @return string
*/
public function getValue()
{
$parsedUrl = parse_url(parent::getValue());
return (isset($parsedUrl["path"]) ? $parsedUrl["path"] : "") .
(isset($parsedUrl["query"]) ? "?" . $parsedUrl["query"] : "") .
(isset($parsedUrl["fragment"]) ? "#" . $parsedUrl["fragment"] : "");
}

/**
* @return string
*/
public function getUrlencodedValue()
{
$parsedUrl = parse_url($this->getValue());
$urlPath = "";
if (isset($parsedUrl["path"])) {
$urlPath .= implode("/", array_map("rawurlencode", explode("/", $parsedUrl["path"])));
}
if (isset($parsedUrl["query"])) {
// This urlencodes all the query parameters properly for us
parse_str($parsedUrl["query"], $parsedQuery);
$urlPath .= "?" . http_build_query($parsedQuery);
}
if (isset($parsedUrl["fragment"])) {
$urlPath .= "#" . urlencode($parsedUrl["fragment"]);
}
return $urlPath;
}

/**
* Return the collection condition(s) usable to filter on the given value with the LIKE function. Overridden to
* URL-encode the filter value.
*
* @param string $value Filter value
* @param string $filterMode Filter mode
* @param bool $isNegative Whether negative filter is enabled
* @return array
*/
public function getLikeCondition($value, $filterMode, $isNegative)
{
return parent::getLikeCondition($this->getUrlencodedValue(), $filterMode, $isNegative);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,9 @@ public function getForcedBlockValues(
Mage_Core_Model_Store $store
) {
return array(
'filter_mode' => BL_CustomGrid_Block_Widget_Grid_Column_Filter_Text::MODE_INSIDE_LIKE,
'filter' => 'customgrid/widget_grid_column_filter_text_urldecoded',
'renderer' => 'customgrid/widget_grid_column_renderer_text_urldecoded',
'filter_condition_callback' => array($this, 'addFilterToGridCollection'),
);
}

/**
* @param Mage_Core_Model_Resource_Url_Rewrite_Collection $collection
* @param Mage_Adminhtml_Block_Widget_Grid_Column $columnBlock
*/
public function addFilterToGridCollection($collection, Mage_Adminhtml_Block_Widget_Grid_Column $columnBlock)
{
$filter = $columnBlock->getFilter();
$value = $filter->getValue();
$filter->setValue(urlencode($value));
$collection->addFieldToFilter($this->getDuplicatedFieldName(), $filter->getCondition());
$filter->setValue($value);
}
}

0 comments on commit 8ae31cb

Please sign in to comment.