Showing posts with label Yii. Show all posts
Showing posts with label Yii. Show all posts

Thursday, February 28, 2013

How easly format dates in Yii

Very useful tools in Yii are CDateTimeParser and CDateFormatter
Let's assume we have date and time formatted like this: "28/02/2018 14:10"
It might be output from MySql for example.

How to convert it to "28 feb 2018, 14:10" without any hassle?
<?php
$unixtime=CDateTimeParser::parse($model->time,'dd/MM/yyyy hh:mm');
echo Yii::app()->dateFormatter->format('dd MMM yyyy, hh:mm',$unixtime); 
?>
More:
CDateFormatter
CDateTimeParser

Saturday, February 16, 2013

How to cut all link tags using HtmlPurifier in Yii

To filter output it's better to use HtmlPurifier,  Yii has wrapper for this class, CHtmlPurifier. HtmlPurifier it's very power tool to filter output text. For instance, this code will keep all other tags, but remove link tag:

<?php 
$purifier = new CHtmlPurifier();
$purifier->options = array(
   'HTML.ForbiddenElements' => array('a'),
);
echo $purifier->purify($text);
?>
Check out:
http://htmlpurifier.org/live/configdoc/plain.html#HTML.ForbiddenAttributes

See more HtmlPurifier options at http://htmlpurifier.org/live/configdoc/plain.html

Friday, February 15, 2013

How to set number of page buttons in Yii CLinkPager for CActiveDataProvider

Sometimes, for mobile versions for example, in Yii's CActiveDataProvider is needed to reduce standard page buttons number (it's 10). The maxButtonCount that located in CLinkPager will do it. But how to get to this variable from CActiveDataProvider?

In the view:
$this->widget('zii.widgets.CListView',array(
      'dataProvider'=>$dataProvider,
      'pager' => array(
               'maxButtonCount'=>Yii::app()->controller->isMobile?4:10,
                ),
));
If you're using YiiBooster dont' forget to add to the pager:
    'class'          => 'bootstrap.widgets.TbPager',

Tuesday, February 12, 2013

How to show different CAPTCHA on every new attempt in Yii::CCaptcha

To show CAPTCHA after N unsuccessful attempts read this:
http://www.yiiframework.com/wiki/339/show-captcha-after-n-unsuccessfull-attempts

Simple CAPTCHA implementation in Russian:
http://yiiframework.ru/doc/cookbook/ru/form.captcha
Hope they will translate it soon to english

If you want to show different CAPTCHA on every user's new attempt to post/login/etc.. simply add
'testLimit'=>'1', in actions();


public function actions(){
return array(
   'captcha'=>array(
'class'=>'CCaptchaAction',
'testLimit'=>'1',
   ),
);
   }

Monday, February 11, 2013

How to change domain name in Yii's createAbsoluteUrl function?


When you face a problem to change domain name in Yii::app()->createAbsoluteUrl() function, just use
Yii::app()->getRequest()->setHostInfo('http://example.com');