Thursday 18 June 2015

PHP: Generate log messages and save to a custom file.

We all know that PHP save errors in php_errors.log file.

But, that file contains a lot of data.

If we want to log our application data, we need to save it to a custom location.

We can use two parameters in the error_log function to achieve this.

http://php.net/manual/en/function.error-log.php

We can do it using:

error_log(print_r($v, TRUE), 3, '/var/tmp/errors.log');

Where,

print_r($v, TRUE) : logs $v (array/string/object) to log file.
3: Put log message to custom log file specified in the third parameter.
'/var/tmp/errors.log': Custom log file (This path is for Linux, we can specify other depending upon OS).

OR, you can use file_put_contents()

file_put_contents('/var/tmp/e.log', print_r($v, true), FILE_APPEND);

Where:

'/var/tmp/errors.log': Custom log file (This path is for Linux, we can specify other depending upon OS).
print_r($v, TRUE) : logs $v (array/string/object) to log file.
FILE_APPEND: Constant parameter specifying whether to append to the file if it exists, if file does not exist, new file will be created.

How to check the version of Yii.

To check version of Yii we are using, use the following code:

Yii::getVersion()

Tuesday 9 June 2015

Yii: jQuery UI widget dialog with ok and close buttons.

Yii: jQuery UI widget dialog with ok and close buttons.

<?php
$this->beginWidget('system.zii.widgets.jui.CJuiDialog',
array(
'id'=>'dialog'.$this->id,
// additional javascript options for the dialog plugin
'options'=>array(
'title'=>$this->id.' Dialog box 1',
'width'=> '800px',
'height' => '600',
'position' => 'top',
'top' => '50px',
'modal' => true,
'buttons' => array(
"Ok"=>'js:function(){alert("xxx")}',
'Close'=>'js:function() {$(this).dialog("close");}'
),
'autoOpen'=>false,
)
)
);
?>

Reference:

Yii 1.1: Customize admin grid view/update/delete buttons.

In admin.php, change the following:


'class' => 'CButtonColumn',
  'template' => '{update}{delete}',
    'updateButtonUrl' =>'Yii::app()->createUrl("/customer/editmember1",array("id" => $data->primaryKey))',
    'updateButtonImageUrl'=>Yii::app()->request->baseUrl.'/images/edit.jpg',
    deleteButtonUrl' =>'Yii::app()->createUrl("/customer/delete",array("id" => $data->primaryKey))',
    'deleteButtonImageUrl'=>Yii::app()->request->baseUrl.'/images/delete.jpg', 
),

Reference:

Yii: Check roles based access on every page.

in Yii, if you want to restrict users from accessing only actions assigned to them.

Install modules:


Then assign permissions to the specific user roles.

Now, in Main Controller (/protected/components/controller.php),

Add following filter

public function filterAccessControl($filterChain) {
$controller = Yii::app()->controller->id;
$action = Yii::app()->controller->action->id;
//The RBAC admin module I'm using creates entries for operations as, e.g. Post.Create
// You may need to change this to match whatever entry format you have
// in your AuthItem table
$operation = ucfirst($controller) . '.' . ucfirst($action);
Yii::log('Checking auth for user: ' . Yii::app()->user->id. ' to operation: ' . $operation, 'info');
if (Yii::app()->user->checkAccess($operation)) {
Yii::log('User authorised', 'info');
$filterChain->run();
return true;
}
else {
Yii::log('Unauthorised user!!!!!', 'info');
throw new CHttpException(401, 'You are not authorized to perform this action.');
return false;
}
}

If a user visits a page which he does not have permission, following error will be displayed:

Error 401

You are not authorized to perform this action.



Monday 8 June 2015

Yii 1.1: get current model name.

in Yii, if you are adding custom fields to forms, you need to add model name to the input fields like this:

<input name="ModelName[fieldname]"/>

here, you have access to $model, but, from this, you cannot get the model name.

Simply use PHP's get_class() method to do this:

$ModelName = get_class($model);

So, the final code should be:

<input name="<?php echo $ModelName;?>[fieldname]"/>

Yii 1.1: Add jQuery UI datepicker to multiple input date fields.

In the following way, you can add jQuery UI datepicker to your Yii site.

$this->widget('zii.widgets.jui.CJuiDatePicker',array(
    'name'=>'publishDate',
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
));
You can even add date picker to more than one elements.
For example, you have two date fields with ids:
joining_date and increment_date
Then you can add these two dates in array like:
$dateFields = array('joining_date', 'increment_date');
if (! empty($dateFields)) {
// See here, you can add run time fields.
$dateFieldIds = implode(',#', $dateFields);
$this->widget('zii.widgets.jui.CJuiDatePicker',array(
  'name' => $dateFieldIds, 
  'options'=>array('showAnim'=>'fold',),
  'htmlOptions'=>array('style'=>'height:20px; display:none;'),
 ));
}

Reference:

http://www.yiiframework.com/doc/api/1.1/CJuiDatePicker

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...