Hi kdubya,

> I'm trying to follow the structure of CakePHP but I don't know where
> to put a utility function that I would like to call to filter certain
> fields in more than one Model.
>
> The utility function filters most HTML tags, but allows some safe ones
> like <b> etc. and replaces newlines with <br>.
>
> I want to call this utility function in the beforeSave() in more than
> one Model. So where is the proper place to put it? It's not a behavior
> (I don't think).

Sounds like a perfect candidate for a behaviour to me. It's not
applicable to all models, you wouldn't want your User or Group model
to be checked, and perhaps you have different field names in different
models that need checking, these would go in the config array.

here;s an idea:

var $actsAs = array(
        'Cleanup'=>array('body')
);

class CleanupBehavior extends ModelBehavior
{

        /**
         * Contain settings indexed by model name.
         *
         * @var array
         * @access private
         */
        var $__settings = array();

        /**
        * setups the behaviour
        *
        * @param object &$model the model that the behaviour is called from
        * @param array $settings the settings array defined in the model
        */
        function setup(&$Model, $settings = array())
        {
                if (!isset($this->__settings[$Model->alias]))
                {
                        if (!empty($settings))
                        {
                                foreach($settings as $field=>$options)
                                {
                                        
$this->__settings[$Model->alias][$field] = am($this->__default, $options);
                                }                       
                        }
                }
        }

        /**
        * beforeSave callback
        *
        */
        function beforeSave(&$Model)
        {       
                foreach($this->__settings[$Model->alias] as $field => $options)
                {
                        if(isset($Model->data[$Model->alias][$field]))
                        {
                                // clean data - obviously does nothing at the 
mo!
                                $Model->data[$Model->name][$field] = 
$Model->data[$Model->name][$field];
                        }
                        
                }
                
        return parent::beforeSave($Model);
        }
}

hth

jon

-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to