Site building and maintenance

Blog. Yii. Tips for Yii. Chapter 14

Search

Tips for Yii. Chapter 14.

  1. CHtml::listData usage.

    There is a query:

    1. $ids = array(18, 19, 20, 21);  
    2. $sql = 'SELECT id_object, COUNT(id) as count FROM {{images}} WHERE id_object IN ('.implode(','$ids).') GROUP BY id_object';  
    3. $res = Yii::app()->db->createCommand($sql)->queryAll();  

    The result of the query running will be the following array:

    1. Array  
    2. (  
    3.     [0] => Array  
    4.         (  
    5.             [id_object] => 18  
    6.             [count] => 3  
    7.         )  
    8.   
    9.     [1] => Array  
    10.         (  
    11.             [id_object] => 19  
    12.             [count] => 3  
    13.         )  
    14.   
    15.     [2] => Array  
    16.         (  
    17.             [id_object] => 20  
    18.             [count] => 4  
    19.         )  
    20.   
    21.     [3] => Array  
    22.         (  
    23.             [id_object] => 21  
    24.             [count] => 2  
    25.         )  
    26. )  

    Task: to return the array. The keys of this array are "id_object" and key values is "count".

    You can certainly use such construct's as foreach / for / while ...

    But Yii framework is good because of its "magic" methods.

    So all you need is to insert a line of the code:

    1. return CHtml::listData($res'id_object''count');  

    The result is an array, which we need:

    1. Array  
    2. (  
    3.     [18] => 3  
    4.     [19] => 3  
    5.     [20] => 4  
    6.     [21] => 2  
    7. )  

     

  2. Display of the validation errors in the block, which you can close.

    Practical application: the mobile version of the website/ responsive design, where the viewable size of the screen is limited. That's why user can hide them after reading the description of errors. So we can bring an overview of the "useful" part of the site back.

    If the project uses Yii BootStrap, so you need to write this:

    1. <?php  
    2. if (CHtml::errorSummary($model)) {  
    3.     Yii::app()->user->setFlash('error', CHtml::errorSummary($model));  
    4.     $this->widget('bootstrap.widgets.TbAlert');  
    5. }  
    6. ?>  

    Instead of:

    1. <?php echo $form->errorSummary($model); ?>  

    It helps to close the error list using this sign "x".

    If the project doesn't use Yii BootStrap, we can add the needed functionality ourselves:

    Instead of:

    1. <?php echo $form->errorSummary($model); ?>  

    write this:

    1. <?php if (CHtml::errorSummary($model)) :?>    
    2.     <div class="error-block">    
    3.         <a class="close" href="javascript:void(0);">×</a>    
    4.     
    5.         <?php echo CHtml::errorSummary($model);?>    
    6.     </div>    
    7.     
    8.     <?php    
    9.     $css=<<<EOT    
    10.     .error-block, .error-block .errorSummary { background-color: #DE6D6B !important; color: #FFFFFF; }    
    11.     .error-block .close { float: right; font-size: 22px;  position: relative;  right: 9px;  text-decoration: none; top: 1px;}    
    12. EOT;    
    13.     Yii::app()->clientScript->registerCss('inline.css'$css);    
    14.     
    15.     Yii::app()->clientScript->registerScript('close-action' 
    16.         $('.error-block .close').click( function(){ $(this).parent().fadeOut(500); });  
    17.     ", CClientScript::POS_END);    
    18.     ?>    
    19. <?php endif;?>  

     

  3. CListView. Passing of optional parameters to the view.

    There is the call CListView in the code:

    1. $this->widget('zii.widgets.CListView'array(  
    2.     'dataProvider' => $dataProvider,  
    3.     'itemView' => '_view',  
    4.     'itemsTagName' => 'ol',  
    5.     'itemsCssClass' => 'advertising-blocks',  
    6.     'sortableAttributes' => array(  
    7.         'position',  
    8.         'type',  
    9.         'active',  
    10.     ),  
    11. ));  

    You need to know not only $data object in_view.php, but also the meaning of the text in the variable $text.

    We need to add viewData:

    1. 'viewData' => array('text' => 'Here will be a text'),    

    The call will look like this:

    1. $this->widget('zii.widgets.CListView'array(    
    2.     'dataProvider' => $dataProvider,    
    3.     'viewData' => array('text' => 'Here will be a text'),    
    4.     'itemView' => '_view',    
    5.     'itemsTagName' => 'ol',    
    6.     'itemsCssClass' => 'advertising-blocks',    
    7.     'sortableAttributes' => array(    
    8.         'position',    
    9.         'type',    
    10.         'active',    
    11.     ),    
    12. ));    

    Now we can display the value of the variable $text in the view.

    You can also use a "clip". Write in the controller:

    1. $this->beginClip('testClip');  
    2. echo 'Here will be a text';  
    3. $this->endClip();  

    And in _view.php we will add this code:

    1. <?php    
    2. if (isset($this->clips['testClip']) && !empty($this->clips['testClip'])) {    
    3.     echo $this->clips['testClip'];    
    4. }    
    5. ?>    

     

  4. CViewAction. Static pages in Yii.

    Let's look at the script of the business card website Open Business Card.

    Let's create the folder "staticpages" in the directory protected/views.

    We will add two files into this folder: staticpage1.php and staticpage2.php.

    The content of the file staticpage1.php is:

    1. <p>This is an example of static page #1.</p>  
    2. <p>This page can be edited in text editor.</p>  

    Here is the content of the file staticpage2.php:

    1. <p>This is an example of a static page #2.</p>  

    In the file protected/controllers/SiteController.php we will replace this code:

    1. public function actions() {  
    2.     return array(  
    3.         // captcha action renders the CAPTCHA image displayed on the contact page  
    4.         'captcha' => array(  
    5.             'class' => 'MathCCaptchaAction',  
    6.             'backColor' => 0xFFFFFF,  
    7.         ),  
    8.         // page action renders "static" pages stored under 'protected/views/site/pages'  
    9.         // They can be accessed via: index.php?r=site/page&view=FileName  
    10.         'page' => array(  
    11.             'class' => 'CViewAction',  
    12.         ),  
    13.     );  
    14. }  

    to:

    1. public function actions() {  
    2.     return array(  
    3.         // captcha action renders the CAPTCHA image displayed on the contact page  
    4.         'captcha' => array(  
    5.             'class' => 'MathCCaptchaAction',  
    6.             'backColor' => 0xFFFFFF,  
    7.         ),  
    8.         // page action renders "static" pages stored under 'protected/views/site/pages'  
    9.         // They can be accessed via: index.php?r=site/page&view=FileName  
    10.         'staticpage' => array(  
    11.             'class' => 'CViewAction',  
    12.             'basePath' => 'application.views.staticpages', # the directory where the static pages are.  
    13.             'renderAsText' => true, # otherwise PHP code will be processed, if it exists on the page.  
    14.             'defaultView' => 'staticpage1' # default page  
    15.             'layout' => '//layouts/user' # layout for static pages. If "null" is specified, the page will be without a layout  
    16.         ),  
    17.     );  
    18. }  

    Now you can call the static pages using this way:

    http://site_domain/site/staticpage/view/staticpage1 and http://site_domain/site/staticpage/view/staticpage2

    Links do not look good. Let’s fix it.

    In the file protected/config/main.php add a rule (section urlManager):

    1. 'statpage/<view:(staticpage1|staticpage2)>' => array('site/staticpage'),  

    Now static pages can be invoked so:

    http://site_domain/statpage/staticpage1 and http://site_domain/statpage/staticpage2

     

Discuss the article in the forum