Actions

API16

JForm/filter

From Joomla! Documentation

< API16:JForm
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Method to recursively filter data for form fields.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

filter($data, $limit=null)
Parameter Name Default Value Description
$data $data The data to filter.
$limit null $limit An optional group to limit the filtering to.

Returns

array An array of filtered data.

Defined in

libraries/joomla/form/form.php

Importing

jimport( 'joomla.form.form' );

Source Body

public function filter($data, $limit = null)
{
        // Initialise variables.
        $return = array();
 
        // The data must be an object or array.
        if (!is_object($data) && !is_array($data)) {
                return false;
        }
 
        // Get some system objects.
        $config = JFactory::getConfig();
        $user   = JFactory::getUser();
 
        // Convert objects to arrays.
        if (is_object($data)) {
                // Handle a JRegistry/JParameter object.
                if ($data instanceof JRegistry) {
                        $data = $data->toArray();
                }
                // Handle a JObject.
                elseif ($data instanceof JObject) {
                        $data = $data->getProperties();
                }
                // Handle other types of objects.
                else {
                        $data = (array)$data;
                }
        }
 
        // Static input filters for specific settings.
        static $noHtmlFilter;
        static $safeHtmlFilter;
 
        // Get the safe HTML filter if not set.
        if (is_null($safeHtmlFilter)) {
                $safeHtmlFilter = &JFilterInput::getInstance(null, null, 1, 1);
        }
 
        // Get the no HTML filter if not set.
        if (is_null($noHtmlFilter)) {
                $noHtmlFilter = &JFilterInput::getInstance(/* $tags, $attr, $tag_method, $attr_method, $xss_auto */);
        }
 
        foreach($this->_fieldsets as $group => $fieldset)
        {
                if(isset($fieldset['parent']))
                {
                        $this->_groups[$fieldset['parent']] = array_merge($this->_groups[$fieldset['parent']], $this->_groups[$group]);
                }
        }
 
        // Iterate through the groups.
        foreach ($this->_groups as $group => $fields) {
                $array = $this->_fieldsets[$group]['array'];
                if ($array === true) {
                        if(isset($this->_fieldsets[$group]['parent'])) {
                                $groupControl = $this->_fieldsets[$group]['parent'];
                        } else {
                                $groupControl = $group;
                        }
                } else {
                        $groupControl = $array;
                }
                // Filter if no group is specified or if the group matches the current group.
                if ($limit === null || ($limit !== null && $group === $limit)) {
                        // If the group name matches the name of a group in the data and the value is not scalar, recurse.
                        if (isset($data[$groupControl]) && !is_scalar($data[$groupControl]) && !is_resource($data[$groupControl]))
                        {
                                if (isset($return[$groupControl])) {
                                        $return[$groupControl] = array_merge($return[$groupControl], $this->filter($data[$groupControl], $group));
                                } else {
                                        $return[$groupControl] = $this->filter($data[$groupControl], $group);
                                }
                        } else {
                                // Filter the fields.
                                foreach ($fields as $name => $field)
                                {
                                        // Get the field information.
                                        $filter = (string)$field->attributes()->filter;
 
                                        // Check for a value to filter.
                                        if (isset($data[$name])) {
                                                // Handle the different filter options.
                                                switch (strtoupper($filter)) {
                                                        case 'RULES':
                                                                $return[$name] = array();
                                                                foreach ((array) $data[$name] as $action => $ids) {
                                                                        // Build the rules array.
                                                                        $return[$name][$action] = array();
                                                                        foreach ($ids as $id => $p) {
                                                                                if ($p !== '') {
                                                                                        $return[$name][$action][$id] = ($p == '1' || $p == 'true') ? true : false;
                                                                                }
                                                                        }
                                                                }
                                                                break;
 
                                                        case 'UNSET':
                                                                // Do nothing.
                                                                break;
 
                                                        case 'RAW':
                                                                // No Filter.
                                                                $return[$name] = $data[$name];
                                                                break;
 
                                                        case 'SAFEHTML':
                                                                // Filter safe HTML.
                                                                $return[$name] = $safeHtmlFilter->clean($data[$name], 'string');
                                                                break;
 
                                                        case 'SERVER_UTC':
                                                                // Convert a date to UTC based on the server timezone offset.
                                                                if (intval($data[$name])) {
                                                                        $offset = $config->getValue('config.offset');
 
                                                                        $date   = JFactory::getDate($data[$name], $offset);
                                                                        $return[$name] = $date->toMySQL();
                                                                } else {
                                                                        $db = &JFactory::getDbo();
                                                                        $return[$name]= $db->getNullDate();
                                                                }
                                                                break;
 
                                                        case 'USER_UTC':
                                                                // Convert a date to UTC based on the user timezone offset.
                                                                if (intval($data[$name])) {
                                                                        $offset = $user->getParam('timezone', $config->getValue('config.offset'));
 
                                                                        $date = JFactory::getDate($data[$name], $offset);
                                                                        $return[$name] = $date->toMySQL();
                                                                }
                                                                break;
 
                                                        default:
                                                                // Check for a callback filter.
                                                                if (strpos($filter, '::') !== false && is_callable(explode('::', $filter))) {
                                                                        // Filter using the callback method.
                                                                        $return[$name] = call_user_func(explode('::', $filter), $data[$name]);
                                                                } else if (function_exists($filter)) {
                                                                        // Filter using the callback function.
                                                                        $return[$name] = call_user_func($filter, $data[$name]);
                                                                } else {
                                                                        // Filter using JFilterInput. All HTML code is filtered by default.
                                                                        $return[$name] = $noHtmlFilter->clean($data[$name], $filter);
                                                                }
                                                                break;
                                                }
                                        }
                                }
                        }
                }
        }
 
        return $return;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />