<?php

/*
 * dcprofilesmigration.module
 * From: DCForum+ User Profiles
 * To Drupal profile module.
 * Remember to search and replace "$my_dcf_dbase" with the name of the dcforum+ mysql database. 
 */
function dcprofilesmigration_menu($may_cache)
{
	global $user;
  $items = array(); 
  $admin_access = user_access('administer site configuration');
	$items[] = array('path' => 'mdata', 'title' => t('migrate profiles'),
      'callback' => '_migrate_data', 'access' => $admin_access);
	  
	  
  return $items;
}

function _migrate_data()
{
	$old = db_query("SELECT fid, name FROM {profile_fields}");
	$output_old = array();
	while($_old = db_fetch_object($old))
	{
		$output_old[$_old->fid].= $_old->name;
	}
	
	$new = db_query("SELECT * FROM $my_dcf_dbase.dcuser LIMIT 1");
	$output_new= array();
	while($_new = db_fetch_array($new))
	{
		$output_new = array_keys($_new);
	}
	$output.="<table>";

	foreach($output_new as $key=>$new)
	{
		$output.= "<tr><td><input type='checkbox' name='select[$new]' value='$key'> </td><td>".form_select($new, $key, $key, $output_old)."</td></tr>";
	}
	$output.="</table>";
	
	$output.= form_button('SQL_QUERY');
	$output = form($output);

	$jcc_usernames = db_query("SELECT username FROM $my_dcf_dbase.dcuser");
	
	$userids = array();
	while($suser = db_fetch_object($jcc_usernames))
	{
		$userids[].= db_result(db_query("SELECT uid FROM {users} WHERE name= '%s'", $suser->username));
		
	}
	
	$br = "<br/>";
	foreach($userids as $uid)
	{
		if(sizeof($_POST))
		{
			$edit = $_POST['edit'];
			$select = $_POST['select'];
			//print_r($edit);
			//print_r($select);
			foreach($select as $key=>$value)
			{
				$map[$key] = $edit[$value];
			}
			//$map = (array_intersect(($select), ($edit) ));
//			print_r($map);
			foreach($map as $key =>$fid)
			{
				if(($key != "id") && ($key != "userlevel") && ($key != "username") && ($key != "password"))
				{
					$_userq= sprintf("REPLACE INTO {profile_values} (fid, uid, value) SELECT %d, %d, %s FROM $my_dcf_dbase.dcuser WHERE id=%d \n", $fid, $uid, $key, $uid); 
					db_query($_userq);
					$userquery.= $_userq;
					
				}
			}
			$query = $userquery;
			
		}
		
		$textarea = $query;	
		//$query = $textarea;
	}
	
	$output.= form_textarea("SQL", 'sql', $textarea, 70, 15);
	

		
	print theme('page', $output);
}

function get_key($select, $edit)
{
	return $edit;
}

?>