Forums / Extensions / eZ Find / ezFind filter with logical AND for categories

ezFind filter with logical AND for categories

Author Message

Ivan Švogor

Thursday 09 December 2010 8:05:37 am

As I am to lazy to write a tutorial, I will post this simply in forum form and someone will sure make use of it.

So, did you ever need to search filtering categories? - You'll say sure, It's easy, in the search from, you simply add option box that looks like this:

(categories are solved trough custom class with just text area to name the category, and it is added to master class in form of object relations)

<select name="filter[]">
 <option value="my_class(eg. article)/my_attribute(eg custom class for category):value">Value 1</option>
 <option value="my_class/my_attribute:value2">Value 2</option>
 <option value="my_class/my_attribute:value 3">Value 3</option>
 </select>

It's easy, you can also find this on forum. But what's a bit tricky is when you want to filter out multiple categories which implies logical AND. Problem is in the background of ezFind. When you pass this arguments to /content/search one template gets to work and calls functions in order to get your search result. (this template is in ezFind/design/your_design_name/templates/content/search.tpl

And here is a fetch function that parses your filter, it looks like this:

{def $filterParameters = fetch( 'ezfind', 'filterParameters' ) }

This fetch function calls to php witch does the parse.

ezFind/modules/ezfind/function_definition.php contains fetch function definitions, and if you search filterParameters, you will find that it is in ez/Find/classes/ezfmodulefunctioncollection.php.

The method we are looking for is called getFilterParameters, and the line that is a problem is this one:

list( $name, $value ) = explode( ':', $filterCond );
 $filterList[$name] = $value;

If you pass the parameters from multiple select (as is above), the key will always be the same and it will override all the values leaving you with only one filter value (the last one)

And to get around this, you can create new function or edit the current one (it is prefered to create your own fetch function to do this)

your php would look like this:

public function getFilterParametersForDocuments(){
        $http = eZHTTPTool::instance();
        $filterList = array();
        $values = '';
        if ( $http->hasGetVariable( 'filter' ) )
        {
            foreach( $http->getVariable( 'filter' ) as $filterCond )
            {

                list( $name, $value ) = explode( ':', $filterCond );
                $values .= $value.",";
            }
            $filterList[$name] = $values;

        }

        return array( 'result' => $filterList );
    }