Wednesday, March 14, 2012

How to add multi-language site in yii framework

To make multi language in your yii framework you have to implements some rules. You can use the session variables to make the translation between your page. With the the documentation you can look at here.

I have to implements this to make the translation page. You can follow the instruction in that articles that not almost I discuss in this page.

But when I finish implement that step, I can make my own translation page on the protected/messages/<your language id>/<your category>.php files..

For the example :

../protected/messages/id/translation.php

The example of the translation page that I use like this
<?php
/**
 * Bahasa Indonesia Yang digunakan untuk menerjemahkan
 */
return array(
  //------NAVIGASI MENU------------//
  'Home'=>'Beranda',
  'About'=>'Tentang',
  'Contact'=>'Kontak',
  'Login'=>'Masuk',
  'Logout'=>'Keluar',
  
  
  
  //-----------ISI KONTEN DEPAN------------------//
  'What are You Looking For?'=>'Apa Yang Anda Cari?',
  'Sell Your Products'=>'Mau Menjual Barang',
  'Products'=>'Barang',
  'Services'=>'Jasa'
  
  );
?>

Oke when you finish that I use my native languages (Indonesian=ID) and than I can use that for my translation in my code using this example in your view files.



echo Yii::t('translation','Home');

Tuesday, March 13, 2012

Alternative Way to make Dropdown menu in yii

Maybe you are a newbie and want to make dropdown in yii just like me before. Oke to make dropdown menu in yii is simple. You can use this solution to make your Dropdown list..
Maybe you can check my post before here

From there you can use this in two ways of using dropdown list in yii framework

The first one is just like my post before :

<div class="row">
  <?php echo $form->dropDownList($model,'country_id',
   CHtml::listData(Country::model()->findAll(), 'id', 'country_name'),
   array('empty'=>'Select Country'))?>
 </div>

And the other option is using CHtml::dropDownList like this suppose you have to make your view class with the Users model

<div class="row">
  <?php echo CHtml::dropDownList('Users[country_id'],'',
   CHtml::listData(Country::model()->findAll(), 'id', 'country_name'),
   array('empty'=>'Select Country'))?>
 </div>
That's the other option that you can choose..

Thursday, March 8, 2012

How to make CGridview with dropdown filter

To get the filter of your CGridView you have to add filter parameters in your CGridview call. This filters can be an instance of the object active records. If you want to add some filtering of your model with dropdown menu you can see the example below :


$this->widget('zii.widgets.grid.CGridView',array(
   'dataProvider'=>$model->search(),
   'id'=>'risk-id',
   'filter'=>$model,
   'columns'=>array(
   array(
      'name'=>'No',
      'type'=>'raw',
      'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1',//this for the auto page number of cgridview
      'filter'=>''//without filtering 
   ),
   array(
      'name'=>'name',
      'type'=>'raw',
      'value'=>'Chtml::link(Chtml::encode($data["name"]),array("risk/view","id"=>$data["risk_id"]))',
      'filter'=>CHtml::listData(Risk::model()->findAll(
                  array(
                   'select'=>array('name'),
                   'distinct'=>true
                   
                  )),"name","name")//this is the focus of your code
      
   ),
   array(
      'name'=>'date_identified',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->date_identified)'
   ),
   array(
      'name'=>'description',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->description)'
   ),
   array(
      'name'=>'type',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->type)',
      
   ),
   array(
      'name'=>'link',
      'type'=>'raw',
      'value'=>'$data->link'
   ),
   

)

));

YOu can see that in the field of name, You can put the dropdown box to your filtering attribut. In this example I use the data of name in my risk table and get the distinct value of it.


You can also make your spesific column to exclude the filtering by passing parameter filter to null object or '' object.

And the result you can see here :




Wednesday, March 7, 2012

Using CMaskField in your yii framework

In order to supply the format of your textfield sometimes you should have to implement the specific format of your textfield. It can be solved by using the existing widget in yii framework.

You can look at this http://www.yiiframework.com/doc/api/1.1/CMaskedTextField to make CMaskedTextField that give you the alternative way to give preformatted textfield.


Oke let's take look the code :


<?php
$this->widget('CMaskedTextField', array(
'model' => $model,
'attribute' => 'aluguel',
'mask' => '(999) 999-9999? x99999',
'htmlOptions' => array('size' => 6)
));
?>
This is with the regex types
<?php
$this->widget('CMaskedTextField', array(
'model' => $model,
'attribute' => 'aluguel',
'mask' => '9.999,99',
'charMap' => array('.'=>'[\.]' , ','=>'[,]'),
'htmlOptions' => array('size' => 6)));
?>
And the final code you can see below the result here is:


.The following mask definitions are predefined:
  • a - Represents an alpha character (A-Z,a-z)
  • 9 - Represents a numeric character (0-9)
  • * - Represents an alphanumeric character (A-Z,a-z,0-9)


Tuesday, March 6, 2012

Insert widget to another widget in Yii

For this time I want to share how I accomplish inserting widget in yii to another widget..

You can take a look of this code of using widget in yii :

  $this->widget('ext.xxx.yyy',array(...)); 
It executing echo statement in yii so if you want to insert widget to another widget you have to get the result of this string.

To get this way you have to save the result before and then insert those string to another widget using ob_start(). 


Oke let's take a look of example how we can insert cjuislider inside of cdetailview..

the widget is as below :


<?php
//save the result of this result of string to slider variable
ob_start();
$this->widget('zii.widgets.jui.CJuiSliderInput', array(
    'name'=>'my_slider',
    'value'=>0,
    'event'=>'change',
    'options'=>array(
        'min'=>0,
        'max'=>100,
        'slide'=>'js:function(event,ui){$("#amount").text(ui.value);}',
),
    'htmlOptions'=>array(
        'style'=>'width:200px; float:left;'
),
));
echo '<div id="amount"></div>';
$slider=ob_get_contents();
ob_end_clean();

?>
<?php $this->widget('zii.widgets.CDetailView', array(
 'data'=>$model,
 'id'=>'hor-minimalist-b',
 'attributes'=>array(
  'project_id',
  'title',
  array(
         'label'=>'Project proposal',
         'type'=>'raw',
         'value'=>empty($model->project_proposal)?"":CHtml::link($model->project_proposal,Yii::app()->homeUrl.'/../upload/'.$model->project_proposal),
  ),
  array(
         'label'=>'Progress',
         'type'=>'raw',
         'value'=>$slider,//this variable that's get from the code above
  ),
  'start_time_actual',
  'end_time_actual',
//   'id_FK',
  'scope_name'
 ),
));
?>

Oke that's simple way how to insert widget to another widget in yii If you have another good solution just share yours..

Monday, March 5, 2012

Add paging index in your CListView

If you are using CListView to populate your data you can customize your view to show the automatic number of your CListView..

you can just add $index variable to your _view in CListView


<div class="view" >
 <i><b><?php echo CHtml::encode($data->getAttributeLabel('Project title')); ?>:</b></i>
 <span class="info">
 <?php echo $index+1;?>//this is the variable in CListView
 </span>
 <h3><?php echo CHtml::link(CHtml::encode($data->title), array('view', 'id'=>$data->project_id)); ?></h3>

In your index file in your view you can see like this :


<h1>Projects</h1>

<?php $this->widget('zii.widgets.CListView', array(
 'dataProvider'=>$dataProvider,
 'itemView'=>'_view',
)); ?>

Make page numbering in CGridview yii framework

This time I want to share how we can easily modify CGridView based on the table shown to add automatic numbering of CGridView.

Let's take a look how we can set up this with little modification :
See the example below :


$this->widget('zii.widgets.grid.CGridView',array(
   'dataProvider'=>$provider,
   'id'=>'components-id',
   'columns'=>array(
     array(
      'name'=>'No',
      'type'=>'raw',
      'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1'//this is the numbering of your cgridview
      ),
     array(
      'name'=>'Name',
      'type'=>'raw',
      'value'=>'Chtml::link(Chtml::encode($data->name),array("risk/view","id"=>$data->risk_id))'
      ),
     array(
      'name'=>'Date Identified',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->date_identified)'
      ),
     array(
      'name'=>'description',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->description)'
      ),
     
    )
   
 ));

Voila now we can see an automatic number in your CGridView..

Friday, March 2, 2012

Make ajax request in yii framework

To make ajax request in yii framework is simple to use. For the usual  ajax request from jquery we can use this pattern like this..

$.ajax({
    url:"http://localhost/request.php",//this is the request page of ajax
    data:{params1:param1,param2:param2},//data for throwing the expected url
    type:"POST",//you can also use GET method
    dataType:"html",// you can also specify for the result for json or xml
    success:function(response){
         $('#result_selector').html(response);
    },
    error:function(){
         alert('Failed request data from ajax page");
    }

But in yii the ajax function has been integrated to the yii native function. There are many choices for you to make ajax call through button, link or other components.. From the documentation we can take a look of this part.

ajax()Generates the JavaScript that initiates an AJAX request.CHtml
ajaxButton()Generates a push button that can initiate AJAX requests.CHtml
ajaxSubmitButton()Generates a push button that can submit the current form in POST method.CHtml
That's the list of function supported by yii function. you can use that to populate the ajax request just use that function.

Oke let's take a look below code for the example

<?php
       echo CHtml::ajaxButton(
            'Submit request',
            array('test/function'),
            array('data'=>array('test'=>$model->id)),
            array('update'=>'#update_selector')//this is the update selector of yours $('#update_selector').load(url);
        );
?>
And then don't forget if you want to make an ajax call you should have to use renderPatial so the whole contents is not included.
public function actionFunction() {
 if(Yii::app()->request->isAjaxRequest){
  $this->renderPartial("_form",array('model'=>$model),false,true);
  Yii::app()->end();

 }
}
That's the easy look of yii function..