Friday, May 30, 2014

Yii: Step by Step Create Custom Component

Setup Component:

1. Create new class file in protected/components/Beauty.php
2. Start with the following code in Beauty.php

<?php
class Beauty extends CApplicationComponent
{
     public function makeup(){
        $colorbox = 'blue';
        return $colorbox;
     }
}
?>

3. Go to protected/config/main.php and add the following line under components:

'components'=>array(
   'beauty' = array (
            'class' => 'application.components.Beauty';
              ),
),


How to use it in controller:

1. In controller file:

<?php
  class FashionController extends Controller{
        //call beauty component;
       $beauty = Yii::app()->beauty;
       $color = $beauty->makeup();
       
        // output is blue;
        echo $color
  }
?>

Thursday, May 29, 2014

Yii: Step by Step Working with CFile Extension

CFile extension is commonly used for filesystem objects (files or directories) manipulate. The following step will show you how to config till how to use it.

Setup
1. Click on -> CFILE and download the folder.
2. Unzip and copy the folder in to protected/extensions path
3. Rename the folder to file
4. Add the following code into protected/config/main.php

'components'=>array(
    'file' => array(
        'class' => 'application.extensions.file.CFile',
     ),
 ),

How to Use
// yourfile
$yourfile = 'sample.txt';

// Call extension into $cfile variable
$cfile = Yii::app()->file;

// Check file exist
$cfile->set($yourfile)->exists;

// Create file
$cfile->set($yourfile)->create();

// Append Content
$cfile->set($yourfile)->setContents('treat me a coffee for this tutorial!');

// Get Content from file
$cfile->set($yourfile)->getContents();

// Delete File
$cfile->set($yourfile)->delete();


Others available method as follow:
  • Create
  • CreateDir
  • Purge
  • Contents (get; set for files, append possible; contents filters for directories)
  • Copy
  • Rename/Move
  • Send/Download ('x-sendfile' header support)
  • Delete
The properties of file you can check as follow:
  • exists
  • isdir
  • isfile
  • isempty
  • isuploaded
  • readable
  • writeable
  • realpath
  • basename (setter available)
  • filename (setter available)
  • dirname
  • extension (setter available)
  • mimeType
  • timeModified
  • size
  • owner (setter available)
  • group (setter available)
  • permissions (setter available)

Wednesday, May 28, 2014

Yii: Step By Step Create Yiic Console Command Line

This is interesting part in Yii that I like, we can create our own yiic console application to run our function.
http://www.yiiframework.com/doc/guide/1.1/en/topics.console#creating-commands

Step 1: Create Command Class File in path protected/command/shell/commands
class Commands extends CConsoleCommand  {  
    public function actionIndex() {
        echo "Possible action : getMember(), ...\n";
    }
    public function actionMember($id="", $status = "") {
       // action activities
    }  
}

Step 2: Set configuration in /protected/config/console.php
'commandMap' => array(
      'foo' => array(
          'class' => 'application.commands.shell.Commands',
      ),
  ),

Step 3: Test your console command

From command prompt, go to directory protected/ and run yiic. You will notice new command that you have created available on the list. To use your command, just type yiic foo --bar=123 --status=active


Jquery Looping Cause "long running scripts"

I have to load array variable into selection box (selectBoxIt plugin). First attempt was using $.each and it cause overhead in jquery, therefore I got error "long running scripts". It has been solved by replace $.each loop method with regular for loop method.

Solution:
$.ajax({
    url: '/GetBar',
    type: 'GET',
    dataType: 'json',
    async: true,
    success: function(data) {
      var selectBox = $("select#Bar").data("selectBox-selectBoxIt");
      var bar = [];
       for (var i=0;i<data.length;i++) {
         bar[i] = {value:data[i]['mdm_id'], text:data[i]['bar']}
       }
       selectBox.add(bar);
    }
  });

But there are more studies about performance among $.each and for loop in jquery, will check out when i free. (http://stackoverflow.com/questions/14808144/each-vs-each-vs-for-loop-in-jquery)

Monday, May 19, 2014

SelectBoxIt Change Text When Option Value Is Not Unique

Big thanks to my colleague, I resolved the error in SelectBoxIt.

This problem happened when option value is not unique, and it did not display the correct text after we select the option. The following is how i work it:

First, set option attribute to selected when we make change of the option.

$('#member_profession').change( function() {
      var selectedOption = $('#member_profession option:selected').text();
      $("#member_profession").find('option').removeAttr("selected");
      $("#member_profession option").filter(function(index) { return $(this).text() === selectedOption; }).attr('selected', 'selected');
  });

Then, we have to bind changed event to selectbox:

  $('#member_profession').bind({
    "changed" : function(ev,obj) {
      var selectedOption = $('#member_profession option:selected').text();
      var selectBox = $("#member_profession").data("selectBox-selectBoxIt");
      selectBox._setText(selectBox.dropdownText, selectedOption);
    }
  });

Taaadaaa.. Hope it is helpful and also good for future reference too. ^^

References Link:
http://gregfranko.com/jquery.selectBoxIt.js/#OptionsAPI