ExtJS 4: Parsing Filters From the FilterFeature Using PHP

I wrote this method a few weeks ago and though I’d share it. It parses ExtJS filters sent in by the FilterFeature.

<?php
function parseExtJSFilters() {
    if(!isset($_GET['filter') { // No filter passed in
        return FALSE;
    }
    $filters = json_decode($_GET['filter']); // Decode the filter
    if($filters == NULL) { // If we couldn't decode the filter
        return FALSE;
    }
    $whereClauses = array(); // Stores whereClauses
    foreach($filters as $filter) {
        switch($filter->type) {
            case 'boolean':
                // Convert value for DB
                $filter->value = ($filter->value === TRUE) ? '1' : '0';
                $whereClauses[] = "$filter->field = $filter->value" ;
                break;
            case 'date':
                // Enclose data in quotes
                $filter->value = "'$filter->value'";
            case 'numeric':
                switch($filter->comparison) {
                    case 'lt': // Less Than
                        $whereClauses[] = "$filter->field > $filter->value";
                        break;
                    case 'gt': // Greather Than
                        $whereClauses[] = "$filter->field > $filter->value";
                        break;
                    case 'eq': // Equal To
                        $whereClauses[] = "$filter->field = $filter->value";
                        break;
                }
                break;
            case 'list':
                $listItems = array();
                foreach($filter->value as $value) {
                    $listItems[] = "'$value'";
                }
                $whereClauses[] = "$filter->field IN(" .
                    implode(',', $listItems) . ')';
                break;
            case 'string':
            default: // Assume string
                $whereClauses[] = "(
                    $filter->field LIKE '{$filter->value}%' OR
                    $filter->field LIKE '%{$filter->value}' OR
                    $filter->field LIKE '%{$filter->value}%' OR
                    $filter->field = '{$filter->value}'
                )";
                break;
        }
    }
    if(count($whereClauses) > 0) {
        return implode(' AND ', $whereClauses);
    }
    return FALSE;
}
?>

You can view examples of using the FilterFeature in ExtJS 4 on the ExtJS 4 examples page for grid filtering.

Update: The source code for this snippet is available here.