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 92e692c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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()
{
$value = parent::getValue();
if ($this->getColumn()->getIsUrlPath()) {
$parsedUrl = parse_url($value);
return (isset($parsedUrl["path"]) ? $parsedUrl["path"] : "") .
(isset($parsedUrl["query"]) ? "?" . $parsedUrl["query"] : "") .
(isset($parsedUrl["fragment"]) ? "#" . $parsedUrl["fragment"] : "");
} else {
return $value;
}
}

/**
* @return string
*/
public function getUrlencodedValue()
{
$value = $this->getValue();
if ($this->getColumn()->getIsUrlPath()) {
$parsedUrl = parse_url($value);
$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;
} else {
return urlencode($value);
}
}

/**
* 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);
}
}
45 changes: 31 additions & 14 deletions app/code/community/BL/CustomGrid/Model/Custom/Column/Urldecoded.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

class BL_CustomGrid_Model_Custom_Column_Urldecoded extends BL_CustomGrid_Model_Custom_Column_Simple_Duplicate
{
/**
* Prepare config data
*
* @return BL_CustomGrid_Model_Custom_Column_Abstract
*/
protected function _prepareConfig()
{
$helper = $this->_getBaseHelper();

$this->addCustomizationParam(
"is_url_path",
array(
"label" => $helper->__("Is URL Path"),
"group" => $helper->__("Filtering"),
"description" => $helper->__("Whether or not to filter out non-URL-path related parts of filter values automatically.
Also ensures URL paths are encoded properly when filtering."),
"type" => "select",
"source_model" => "adminhtml/system_config_source_yesno",
"value" => 0,
),
10
);

return parent::_prepareConfig();
}

/**
* Return forced grid column block values
* (you can check BL_CustomGrid_Model_Custom_Column_Abstract::getBlockValues() for the priorities
Expand All @@ -23,22 +49,13 @@ public function getForcedBlockValues(
array $params,
Mage_Core_Model_Store $store
) {
$isUrlPath = $this->_extractBoolParam($params, "is_url_path");

return array(
'filter_mode' => BL_CustomGrid_Block_Widget_Grid_Column_Filter_Text::MODE_INSIDE_LIKE,
'filter' => 'customgrid/widget_grid_column_filter_text_urldecoded',
'is_url_path' => $isUrlPath,
'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);
}
}
3 changes: 3 additions & 0 deletions app/code/community/BL/CustomGrid/etc/customgrid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,23 @@
<id_path_urldecoded model="customgrid/custom_column_urldecoded" module="customgrid">
<name>ID Path (URL Decoded)</name>
<group>Duplicated</group>
<allow_customization>1</allow_customization>
<config_params>
<duplicated_field_name>id_path</duplicated_field_name>
</config_params>
</id_path_urldecoded>
<request_path_urldecoded model="customgrid/custom_column_urldecoded" module="customgrid">
<name>Request Path (URL Decoded)</name>
<group>Duplicated</group>
<allow_customization>1</allow_customization>
<config_params>
<duplicated_field_name>request_path</duplicated_field_name>
</config_params>
</request_path_urldecoded>
<target_path_urldecoded model="customgrid/custom_column_urldecoded" module="customgrid">
<name>Target Path (URL Decoded)</name>
<group>Duplicated</group>
<allow_customization>1</allow_customization>
<config_params>
<duplicated_field_name>target_path</duplicated_field_name>
</config_params>
Expand Down

0 comments on commit 92e692c

Please sign in to comment.