Alimozzaman

Archive for the ‘Drupal’ Category

Here is the codes to migrate user & password from drupal 6 to drupal 7.  It works fine for me.

// connect with mysql server

$mysql_server = $server; // Server name
$mysql_userName = DATABASE_USER_NAME;
$mysql_password = DATABASE_PASSWORD;
$connection = mysql_connect($mysql_server,$mysql_userName,$mysql_password);

// query

$query = “SELECT * FROM users”;
$count = 0;
$result = mysql_db_query(DATABASE_NAME, $query) or die(mysql_error() . “<br />” . $query);
while($row = mysql_fetch_array($result))
{
//if($count++ > 10) break;

if($row[uid] == 0 || $row[uid] == 1) continue;

//require_once DRUPAL_ROOT . ‘/’ . variable_get(‘password_inc’, ‘includes/password.inc’);
$new_password_hash = user_hash_password($row[pass], 11);
if($new_password_hash){
$new_password_hash = ‘U’.$new_password_hash;
}
$data = array(
‘uid’ => $row[uid],//db_next_id(db_query(‘SELECT MAX(uid) FROM {users}’)->fetchField()),
‘name’ => $row[name],
‘pass’ => $new_password_hash,
‘mail’ => $row[mail],
‘theme’ => $row[theme],
‘signature’ => $row[signature],
‘signature_format’ => $row[signature_format],
‘created’ => $row[created],
‘access’ => $row[access],
‘login’ => $row[login],
‘status’ => $row[status],
‘timezone’ => $row[timezone],
‘language’ => $row[language],
‘picture’ => !empty($row[picture])?1:0,
‘init’ => $row[init],
‘data’ => $row[data],
);

drupal_write_record(‘users’, $data);
}

Advertisements

1. To save a single image with node.

global $user;

// Build the node.

$node = new stdClass();

$node->type = $node_type;

$node->uid = $user->uid;

$node->name = $user->name;

$node->title = isset($title) ? $title : $name_place;

$node->body = isset($body) ? $body : $body_content;

// Image file path.

$image = file_directory_path().’/ example.jpg’’;

// Load up the CCK field. First parameter image field name and second parameter node type. It can be chnaged to any field name and also and node type.

$field = content_fields(‘field_image’,“page”);

// Load up the appropriate validators

$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));

// Store file path.

$files_path = filefield_widget_file_path($field);

// Create the file object, replace existing file with new file as source and dest are the same

$file = field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

// put the file into node image field.

$node->field_image = array( 0 => $file);

// Set the node’s defaults… (copied this from node and comment.module)

$node_options = variable_get(‘node_options_’. $node->type, array(‘status’, ‘promote’));

$node->status = in_array(‘status’, $node_options);

$node->promote = in_array(‘promote’, $node_options);

if (module_exists(‘comment’)) {

$node->comment = variable_get(“comment_$node->type”,   COMMENT_NODE_READ_WRITE);

}

$node = node_submit($node);

node_save($node);

2. To save multiple images with node

3.  global $user;

4.  // Build the node.

5.  $node = new stdClass();

6.  $node->type = $node_type;

7.  $node->uid = $user->uid;

8.  $node->name = $user->name;

9.  $node->title = isset($title) ? $title : $name_place;

10.$node->body = isset($body) ? $body : $body_content;

11.$files = array();

12.

13.for($i =0;$i<10;$i++){

14.

15.// Image file path.

16.$image = file_directory_path().’/example.jpg’;

17.

18.// Load up the CCK field. First parameter image field name and second parameter node type. It can be chnaged to any field name and also and node type.

19.

20.$field = content_fields(‘field_image’,“page”);

21.

22.// Load up the appropriate validators

23.$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));

24.

25.// Store file path.

26.$files_path = filefield_widget_file_path($field);

27.// Create the file object, replace existing file with new file as source and dest are the same

28.$file = field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

29.

30.$files[$i] = $file;

31.

32.}

33.

34.// put the files into node image field.

35.$node->field_image = $files;

36.

37. // Set the node’s defaults… (copied this from node and comment.module)

38.$node_options = variable_get(‘node_options_’. $node->type, array(‘status’, ‘promote’));

39.$node->status = in_array(‘status’, $node_options);

40.$node->promote = in_array(‘promote’, $node_options);

41.if (module_exists(‘comment’)) {

42. $node->comment = variable_get(“comment_$node->type”,   COMMENT_NODE_READ_WRITE);

43.    }

44.

45.$node = node_submit($node);

46.node_save($node);

Tags: