Kanagu
Kanagu

Reputation: 606

Extending Yii framework CHtml helper class with our custom class

I have to encode user entered data into utf-8, especially if it is numeric encode, So I decided to use the following code snippet for encoding and then store it into database

$convmap = array ( 0x80, 0xffff, 0, 0xffff );
$str = htmlentities ( mb_encode_numericentity( $str, $convmap, 'UTF-8' ),ENT_QUOTES,"UTF-8",false );

But Yii default encode uses only CHtml::encode() which uses only htmlspecialchars() function

So I have decided to extend CHtml class for overriding the encode function, but I can't figure out how to do it in Yii.. Suggest a good way to extend the Yii helper classes...

Upvotes: 2

Views: 2138

Answers (2)

Stu
Stu

Reputation: 4150

you could always edit the CHtml class itself in /framework/web/helpers/CHtml.php, adding an extra param to the encode method? for example;

public static function encode($text,$extraEncode=false)
{
    if($extraEncode===true)
    {
        $convmap = array ( 0x80, 0xffff, 0, 0xffff );
        return htmlentities ( mb_encode_numericentity( $text, $convmap, 'UTF-8' ),ENT_QUOTES,"UTF-8",false );
    } else {
        return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
    }
}

obv, edit to taste.

Then you can call it like CHtml::encode($myString,true)

Upvotes: 1

SuVeRa
SuVeRa

Reputation: 2904

you can't do it fully, This class is tightly integrated in the framework. ( i.e. used in widgets, activaeform's etc... ),

one thing you can do is ... extend it and create your class and use it in your entire application. Preprocess your model data before sending it to views/widgets.

Upvotes: 1

Related Questions