Tuesday, May 31, 2011

Post in multiple Facebook fan page in one go


Hi,

This is yet another post related to Facebook API. In this I will be demonstrating how to make a wall post in multiple Facebook page of which you are an admin or a fan. A similar functionality I had posted in one of my previous post regarding posting in friends wall. In this also the basic idea is same. The main factor here is to generate the Facebook page list for the logged in user.

First we need yo understand the FQL(Facebook Query Language). Here we will be working with two tables, "page" and "page_admin". The page table is the master table and page_admin holds the mapping between Facebook user and the pages. Here is the query that I am using to fetch the list of pages:

$query="select page_id, name, pic_small, type from page where type<>'APPLICATION' and page_id in (select page_id from page_admin where uid =" . $uid . ")";

Here $uid is the Facebook user id of the currently logged in user. Also I have added a type<>'APPLICATION' in the where clause, so that I only get pages and not my applications. In Facebook, when you create a new App, it is also treated as a page. So I had to give the above validation.

Here is how the page looks:


Explanation:

Social sharing (Facebook - Twitter - Digg - Delicious)

Hi,

Facebook Share:

For sharing a page in Facebook, it provides a specific URL to share a particular page in facebook. The parameters that are passed are basically just an URL. What facebook does is, it parses the URL and fetches the meta title and description. Now these title and description are added as the title and description of the new Facebook post. For the image of the post, Facebook selects a random image from the page that is being shared. So when you are sharing a page, make sure it has proper title and description.

Here is an example of a sharing a particular page in Facebook:

<a href="http://www.facebook.com/sharer.php?u=<?php echo urlencode("URL_TO_SHARE"); ?>&t=TITLE_OF_POST">Share with Facebook</a>

Digg Share

Similar to Facebook, Digg also provides an URL to share a particular page. The URL of that page needs to be passed as parameter to Digg.

e.g.
<a href="http://digg.com/submit?url=<?php echo urlencode("URL_TO_SHARE"); ?>&title=TITLE_OF_POST">Digg</a>

Share Delicious

Saturday, May 21, 2011

Drupal force login/logout

Hi,

This is not a very regular scenario, but I had got one, where I had to make a user auto login when they are coming to my website via a newsletter, that I had sent to the website members. The flow is, first I create various email newsletters and distribute it among my website users by mailing them. The newsletters would contain a link back to my website. Now when the users click that link, they come back to my website. They have to enter few basic details such as the password, gender, date of birth etc. When they click next they get logged in to the website.

I am using Drupal 6.20 for this development. The users that I am talking about are basically drupal users as well, so I need to login them in Drupal framework as well. It is actually simpler than I had thought. In Drupal whenever a user logs in it creates a global variable, "$user", instead of session variables to store the details of the logged in users. So all I had to do is to overwrite that variable with the user details who needs to be logged in.

This is the code that I had used:

$user_details=db_fetch_array(db_query("select * from {users} where id=<USER_ID>"));


$user_details->hostname=$_SERVER['REMOTE_ADDR'];
$user_details->timestamp=time();
$user_details->roles=array('4'=>'client');


global $user;
$user=$user_details;

Explanation:

The idea is very simple. You just need to overwrite the $user variable with that in the database. So I simply fetch all the details of the user and put it in the $user variable. But just by doing this will not do the job, I also need to assign few extra variables such as the hostname, timestamp and roles. Hostname and timestamp are pretty much easy and self explanatory. What is vital is the roles variable given as:

Wednesday, May 18, 2011

Multiple database in Drupal


Hi,

Recently I have been working with a distributed database architecture in Drupal 6, without the use of multisite. So thought of sharing in the blog. Multiple database is very important in Drupal when you have custom tables along with Drupal inbuilt tables. The best practice is to keep separate database for both. For creating multiple database in Drupal, first you need to follow the conventional Drupal installation process. One's that is done, create a seperate database and dump all the custom tables in that database.

Assume the name of the drupal database is "drupal_db" and the name of the custom database is "business_db". Now after you have successfully completed installing Drupal in your server, go to sites/default/settings.php file. There you will find the following or similiar looking lines:

$db_url = 'mysqli://root@localhost/drupal_db';
$db_prefix = 'drp_';

1st step to convert the current Drupal setup into multi database, is to replace the above two lines with the following:


$db_url['default'] = 'mysqli://root@localhost/drupal_db';
$db_prefix['default'] = 'drp_';

$db_url['business_data'] = 'mysqli://root@localhost/business_db';

Basically you just need to create an array of connections. In this, $db_url['default'] refers to the default connection if you are not explicitly mentioning from which database the data needs to be fetched. Most cases this is the drupal database. For the custom database you need to create another index of the same array with the details of that database. In this case I have named it as "business_data" in:

$db_url['business_data'] = 'mysqli://root@localhost/business_db';

Now the main part is to switch connections as needed. For that I had used db_set_active() function. Generally I have used this switching process just before and after performing any database transaction. This function takes 1 parameter, which is the array index name which I had created earlier i.e. "default" and "business_data".

e.g.

Tuesday, May 17, 2011

SVG Charts - PHP

Hi folks,

In this post I will demonstrate possibly my first product in this blog. Generating charts has been a headache for many developers. This is a very simple package to generate interactive charts without the use of flash plugin. This uses basic concepts of SVG to generate those. Unfortunately explaining the entire class file will be a little out of scope of this blog, so I have prepared a short video explaining the functionalities of the chart, But again since this is custom made and just the first version, it may not look that great. But it would definitely serve some of your purpose.

This chart comes with a feature of downloading the chart in PNG format. You can go on fiddle with the main class file to customize the chart according to your needs. Here is the small video demonstrating the use of the chart:




Download: svg_charts.zip

Thursday, May 5, 2011

Facebook style quick friend search and select


Hi,

In this post, I will discuss how to create a friend selection widget as in Facebook. When ever you are inviting your friends, or sending some application request to your friend in Facebook, you might get a friend selection widget as below:


In this I have made a similar functionality using jQuery. Here is what my screen looks like:


In the above, as you start typing the name of your friend, the list below gets filtered quickly accordingly. This is not refreshing the page nor is calling any AJAX. The whole thing is in realtime. Also you can select/unselect 1 or more friend by clicking them. And finally when you click the "Send Request" button the form gets submitted with selected friends user id. This is how I have achieved this.

Code and Explanation:

First you need to download the entire source code from here. Now here is the basic HTML: