Wednesday, September 26, 2012

Securely Storing Files on your server

Here are several ways to securely store files on your server

1. Store files in your web directory and secure them using .htaccess.

2. Or store the files in a directory that isn't web-accessible but is readable by the user PHP runs as.

3. If you are using Apache you can use htaccess to password protect directories. (http://www.javascriptkit.com/howto/htaccess3.shtml)

Tuesday, September 18, 2012

How to Customize cGridView Data Column

I wanted to display the building information in a cGridView table with a owner that has a foreign key with building ID.  Here is how you can customize cGridView Data column. 

In Controller, you can add a function

protected function gridDataBuildingInfo($data,$raw) {
        // ... generate the output for the column

        // Params:
        // $data ... the current row data  
        // $row ... the row index   
        $model=Building::model()->findByPk($data->building_id);
        return  $model->name;  
    }


/* Another example without the need to connect with the database */
protected function gridSeasonName($data,$row)
        {
            switch ($data->Season) {
                case 1:
                    return "Winter";
                    break;
                case 2:
                    return "Fall";
                    break;
                case 3:
                    return "Summer";
                    break;
                case 4:
                    return "Spring";
                    break;
            }
        }


And in the views/owner/admin.php, 
you can modify CGridView as below:

 $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'owner-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'name',
        'phone',
        'email',
        array('name'=>'church_id', 'value'=>array($this,'gridDataBuildingInfo')),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));  



Here is a link to yiiframework.com wiki about customizing complex data columns in cGridView.

http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/


YII: How to create a dropdown list with database information

I need to create a dropdown list from a database table data.

Here is an example that I created for displaying school quarter information.

You can use this in the /view folder _form.php file.
e.g. /view/season/_form.php 

<div class="row">
        <?php  /*  Display label */ 

                    echo $form->labelEx($model,'QuarterID'); ?>
                <?php   

                        /* Retrieve data - select QuarterID, Year, Season; ordered by QuarterID DESC */
                        $Qmodels = Quarter::model()->findAll(array('select'=>'QuarterID, Year, Season','order'=>'QuarterID DESC'));
                        $data = array();

                        /* Used array to rename the season name to something more clear */
                        $season    = array(1=>'Winter','Fall','Summer','Spring');
                       
                        foreach ($Qmodels as $Qmodel)
                            $data[$Qmodel->QuarterID] = $Qmodel->Year . ' '. $season[$Qmodel->Season];   ?>
                <?php 

                      /* Display "Select Quarter" by default */
                      echo $form->dropDownList($model,'QuarterID',$data, array('empty'=>'Select Quarter')) ;?>
        <?php echo $form->error($model,'QuarterID'); ?>
    </div>



Here is an another example from yiiframework.com.  It is a simple and good example.
http://www.yiiframework.com/forum/index.php/topic/601-how-to-populate-a-dropdown-list-with-database-data/

 $qAlbums=Album::model()->findAll($criteria);

                

                $albums = array();

                foreach($qAlbums as $p)

                {

                        $albums[$p->AlId] = $p->AlDescr;

                }

                return $albums;



and then give $albums as parameter to 
 
$form->dropDownList($model, 'attribute', $albums);