Opensource, CMS, PHP, MySql, Drupal, Frameworks

Wednesday, December 29, 2010

Get content type using node id

if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
}

Monday, September 27, 2010

Setup Drupal multisite feature in Local machine

Setup Drupal's multisite feature in Local machine.

For that we have to follow the following three steps.


1. Add alias to our Apache configuration file

2. Redirecting all requests to index.php

3. Creating our settings.php


Add alias to our Apache configuration file

We want requests for the 3 subdirectories(emea, la, uk) to go to the same Drupal instance.We can do this using Apache's Alias functionality.

DocumentRoot “/xampp/htdocs/drupal”

Alias /emea C:/xampp/htdocs/drupal

Alias /la C:/xampp/htdocs/drupal

Alias /uk C:/xampp/htdocs/drupal

I'm supposing here Drupal's codebase is hosted in C:/xampp/htdocs/drupal on your machine


Redirecting all requests to index.php

Now that we're serving all requests from one codebase we have to redirect all requests to index.php. This needs to be done since all Drupal requests are served from one endpoint, called index.php. So we have to do the below changes in the .htaccess file in the same order as mentioned below.

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} ^/emea/(.*)$

RewriteRule ^(.*)$ /emea/index.php?q=$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} ^/la/(.*)$

RewriteRule ^(.*)$ /la/index.php?q=$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} ^/emea/(.*)$

RewriteRule ^(.*)$ /uk/index.php?q=$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !=/favicon.ico

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Creating our settings.php

We can now have different settings (database, ...) for each site by creating a different settings file for each site. Place the settings.php for each file under a directory called after the domain name and subdirectory. In our example we would have the following setting files:

C:/xampp/htdocs/drupal/sites/default/settings.php -Base site Settings.php

C:/xampp/htdocs/drupal/sites/localhost.emea/settings.php -EMEA site Settings.php

C:/xampp/htdocs/drupal/sites/localhost.la/settings.php -LA site Settings.php

C:/xampp/htdocs/drupal/sites/localhost.uk/settings.php -UK site Settings.php

Now, start installing the site by using the below URLS

1. http://localhost – Base site

2. http://localhost/emea – EMEA site

3. http://localhost/la – LA site

4. http://localhost/uk – UK site

Enjoy the Drupal's multisite feature in the subdirectories.

Friday, September 24, 2010

MySQL Command-Line

shell> mysql –help

Connect to Server

shell> mysql -h host -u user -p

Enter password: ********

Disconnect from the Server

mysql> QUIT

On Unix, you can also disconnect by pressing Control-D.


Create Database

mysql> CREATE DATABASE menagerie;

Creating a database does not select it for use; you must do that explicitly. To make menagerie the current database, use this command:

mysql> USE menagerie

Database changed

Use a CREATE TABLE statement to specify the layout of your table:

mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),

-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

Once you have created a table, SHOW TABLES should produce some output:

mysql> SHOW TABLES;


To verify that your table was created the way you expected, use a DESCRIBE statement:

mysql> DESCRIBE pet;

After creating your table, you need to populate it. The LOAD DATA and INSERT statements are useful for this.


To load the text file pet.txt into the pet table, use this statement:

mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;


If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:

mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet

-> LINES TERMINATED BY '\r\n';


mysql> SELECT VERSION(), CURRENT_DATE;

Keywords may be entered in any lettercase. The following queries are equivalent:

mysql> SELECT VERSION(), CURRENT_DATE;

mysql> select version(), current_date;

mysql> SeLeCt vErSiOn(), current_DATE;

It demonstrates that you can use mysql as a simple calculator:

mysql> SELECT SIN(PI()/4), (4+1)*5;

mysql> SELECT VERSION(); SELECT NOW();


Here is a simple multiple-line statement:

mysql> SELECT

-> USER()

-> ,

-> CURRENT_DATE;


If you decide you do not want to execute a command that you are in the process of entering, cancel it by typing \c:

mysql> SELECT

-> USER()

-> \c

The simplest form of SELECT retrieves everything from a table:

mysql> SELECT * FROM pet;

mysql> SELECT DATABASE();

mysql> SHOW TABLES;

mysql> DESCRIBE pet;

mysql> SELECT USER()

Thursday, August 19, 2010

Useful functions[drupal]

drupal_parse_info_file($filename)

system_settings_form($form)

node_get_types($op = 'types', $node = NULL, $reset = FALSE)

drupal_get_path($type, $name)

drupal_get_form($form_id)

variable_set($name, $value)
variable_get($name, $default)
variable_del($name)

MENU_NORMAL_ITEM
MENU_CALLBACK
MENU_SUGGESTED_ITEM
MENU_LOCAL_TASK
MENU_DEFAULT_LOCAL_TASK

Wednesday, August 18, 2010

module annotate(_nodeapi)[drupal]

annotate.info
; $Id$
name = Annotate
description = Allows users to annotate nodes.
core = 6.x
package = Drupal

annotate.module
'Annotation Settings',
'description' => 'Change how annotations behave',
'page callback' => 'drupal_get_form',
'page arguments' => array('annotate_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'annotate.admin.inc',
);
$items['admin/annotate'] = array(
'title' => 'Node Annotation',
'description' => 'Adjust node annotation options.',
'position' => 'right',
'weight' => -5,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module','system'),
);
return $items;
}
function annotate_nodeapi(&$node, $op, $teaser, $page){
global $user;
switch($op){
case 'view':
if($user->uid == 0 !$page ){
break;
}
$type_to_annotate = variable_get('annotate_node_types',array('page'));
if(!$type_to_annotate[$node->type]){
break;
}
$result = db_query('SELECT note FROM {annotations} WHERE nid = %d AND uid = %d', $node->nid, $user->uid);
$node->annotate = db_result($result);
$node->content['annotation_form'] = array(
'#value' => drupal_get_form('annotate_entry_form',$node),
'#weignt' => 10,
);
break;
case 'delete':
db_query('DELETE FROM {annotations} WHERE nid = %d', $node->nid);
break;
}
}
function annotate_entry_form($form_state, $node){
$form['annotate'] = array(
'#type' => 'fieldset',
'#title' => t('Anotations'),
);
$form['annotate']['note'] = array(
'#type' => 'textarea',
'#title' => t('Notes'),
'#default_value' => isset($node->annotate) ? $node->annotate : '',
'#description' => t('Make your personal annotations about this content here. Only you (and the site administrator) will be able to see them.')
);
$form['annotate']['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['annotate']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
function annotate_entry_form_submit($form,$form_state){
global $user;
$note = $form_state['values']['note'];
$nid = $form_state['values']['nid'];
db_query('DELETE FROM {annotations} WHERE nid = %d AND uid = %d', $nid, $user->uid);
db_query("INSERT INTO {annotations} (nid, uid, note, created) VALUES (%d, %d, '%s', %d)", $nid, $user->uid, $note, time());
drupal_set_message('Your annotation have been saved.');
}


annotate.admin.inc
'checkboxes',
'#title' => t('Users may annotate these content types'),
'#options' => $options,
'#default_value' => variable_get('annotate_node_types',array('page')),
'#description' => t('A text field will be available on these content types tomake user-specific notes.'),
);
$form['annotation_deletion'] = array(
'#type' => 'radios',
'#title' => t('Annotation will be deleted.'),
'#description' => t('Select a method for deleting annotations.'),
'#options' => array(
t('Never'),
t('Randomly'),
t('After 30 days')
),
'#default_value' => variable_get('annotation_deletion',0) // Default to never
);
$form['annotate_limit_per_node'] = array(
'#type' => 'textfield',
'#title' => t('Annotations per node'),
'#description' => t('Enter the maximum number of annotations allowed per node (0 for no limit).'),
'#default_value' => variable_get('annotate_limit_per_node',1),
'#size' => 3,
);
 
return system_settings_form($form);
}
function annotate_admin_settings_validate($form, $form_state){
dpm($form_state);
$limit = $form_state['values']['annotate_limit_per_node'];
if(!is_numeric($limit)){
form_set_error('annotate_limit_per_node',t('Please enter a number.'));
}
}
 

annotate.install
t('Stores node annotations that users write.'),
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {node}.nid to which the annotation applies.'),
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {user}.uid of the user who created the annotation.'),
),
'note' => array(
'description' => t('The text of the annotation.'),
'type' => 'text',
'not null' => TRUE,
'size' => 'big'
),
'created' => array(
'description' => t('A Unix timestamp indicating when the annotation was created.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0
),
),
'primary key' => array(
'nid', 'uid'
),
);
return $schema;
}