PHP

Executing PHP in command line

Technologies: 

There are times when one needs to execute PHP in command like, for example trying the date command output or a one line execution. For trivial tasks, executing the same at the command line without the need for writing and executing at a file level is a great tool for time saving.

The argument to be used is : -r

Example:

php -r '
$a = range(0,2);
foreach($a as $b) {
   echo "entry: $b\n";
}

The output is:
entry: 0
entry: 1
entry: 2

Another example:
php -r '
> echo date("d-m-Y");
> echo "\n";
> '
14-01-2013

Utility Methods

Technologies: 

function_exists: function_exists — Return TRUE if the given function has been defined

Syntax:

bool function_exists ( string $function_name )

Example:

<?php
if (function_exists('payroll_getcomponents')) {
    echo(
"payroll_getcomponents functions is available");
}else{
    echo(
"payroll_getcomponents functions is NOT available");
}
?>

profile2_load_by_user: Loads the profile data of a user. Takes user id as input

Syntax:

profile2_load_by_user($account, $type_name = NULL)

Example:
<?php

How to Set Filters for a View in Drupal 7 programmatically

Technologies: 

Accessing a view programmatically

<?php
$view = views_get_view('view_name');
$view->init();
$view->set_display('default'); // or display id like page_1, block_1
$view->set_arguments(array($ARGS));
//display_name = 'default' or 'page' and so on
//filter_name is the name of the filter
//debug($view->display['display_name']->handler->options['filters']['filter_name']);
$view->display['default']->handler->options['filters']['date_filter']['value']['min']='2012-05-01';
$view->display['default']->handler->options['filters']['date_filter']['value']['max']='2012-06-30';

Inspecting Objects: Simple Trick

Technologies: 

How often have you felt the need to display a PHP Object in a readable manner? We are all very familiar with the use of the print_r() function. However, print_r() gives a difficult to read output and we end up matching braces to figure out the exact structure of complex objects. Here is a small trick which you can use to see the print_r() output in a 'Display for Humans' format.

echo '<pre>';
print_r($object);
echo '</pre>';

Let's see an example(borrowed from PHP.net):

<?php

Creating a Drupal 7 Node Programmatically


<?php
/**
* Basic Node Creation Example for Drupal 7
*
* This example:
* - Assumes a standard Drupal 7 installation
* - Does not verify that the field values are correct
*/
$body_text = 'This is the body text I want entered with the node.';

$node = new stdClass();
$node->type = 'article';
node_object_prepare($node); //Creates default settings for the node of a type
$node->title = 'Node Created Programmatically on ' . date('c');
$node->language = LANGUAGE_NONE; //String constant value for "und"

/* If u have body text do the following below

Dates using PHP

Technologies: 

Time and again date parsing issue comes up and I find myself looking all over the net for finding ways to do the same. This post is created to compile all such ways as I encounter:

Parsing Dates

If your date format is: yyyy-mm-dd, you can simply use strtotime and date function to get the desired date format

$data[] = date( 'd-m-Y', strtotime('2012-03-21')); //get the format as 21-03-2012

Get Dates Using Strotime
strtotime can recognize certain keywords.
<?php
echo strtotime('now');
echo strtotime('+4 days');

How to check if an image is present in the site or not

Technologies: 

You might want to display an image if the image is present or a template image if it does not exist. There are various ways of doing the same from client side using JavaScript or from server end.

The below example provides another way to check if the image file exists, if so display the image in HTML generated or display the template HTML in Drupal
<?php
if(file_exists($relative_file_path_in_server)){
$image_url= " < img src=".$base_url."/".$relative_file_path_in_server.">";
}else{

PHP Tuning

Technologies: 

For development site Change the php.ini file setting for apache:
display_errors=On so that PHP errors can be known
and change the setting
error_report = E_ALL && ~E_NOTICE (Report all errors except notice type)

Display different home page based on User Roles

Technologies: 

Use Panels!
Create layout as you like and set the panel as the home page of the site in the Site Information section. Add content in the panels with view permissions for specific user role. This would ensure that a user would be able to view specific content based on their role.
Take care to ensure that a user does not have 2 roles :)