<?

/* This file may contain 2 functions, a db_update_$ver_pre() and a
 * db_update_$ver_post(). _pre() is called before the SQL patch is
 *applied, and as expected, _post() is called after.
 * 
 * These functions are called from the main db_update.php file, and included
 * once, so any global variables declared in here WILL REMAIN across both
 * calls.  meaning you can pull some stuff out of the database in _pre(), and
 * then the patch will be applied, and they it can be inserted back into the
 * database in _post(). 
 * Also note that MULTIPLE php scripts could be included if the db update is 
 * large, so global variable names probably shouldn't conflict... put the version
 * number in them*/

$update_62_committee = array();
function db_update_62_pre()
{
	global $update_62_committee;
	$q = $pdo->prepare("SELECT * FROM committees_members");
	$q->execute();
	while($r = $q->fetch(PDO::FETCH_ASSOC)) {
		$update_62_committee[] = $r;
	}
}

function db_update_62_post()
{
	global $update_62_committee;
	global $config;

	foreach($update_62_committee as $c) {
		list($fn, $ln) = split(' ', $c['name'], 2);
		$username = $c['email'];
		
		if($config['committee_password_expiry_days'] > 0) {
			$passwordexpiry = "DATE_ADD(CURDATE(),
				INTERVAL {$config['committee_password_expiry_days']} DAY)";
		} else {
			$passwordexpiry = "NULL";
		}

		$deleted = ($c['deleted'] == 'Y') ? 'yes' : 'no';
		$q = "INSERT INTO users
			(`types`,`firstname`,`lastname`,`username`,`password`,`passwordexpiry`,
			`email`,`phonehome`,`phonework`,`phonecell`,`fax`,`organization`,
			`created`,`deleted`) 
			VALUES ('committee','$fn', '$ln', '$username',
				'".$c['password']."',
				$passwordexpiry,
				'{$c['email']}',
				'{$c['phonehome']}',
				'{$c['phonework']}',
				'{$c['phonecell']}',
				'{$c['fax']}',
				'".$c['organization']."',
				NOW(),
				'$deleted')";
		$stmt = $pdo->prepare($q);
		$stmt->execute();
		echo "$q\n";
		$id = $pdo->lastInsertId();

		$access_admin = ($c['access_admin'] == 'Y') ? 'yes' : 'no';
		$access_config = ($c['access_config'] == 'Y') ? 'yes' : 'no';
		$access_super = ($c['access_super'] == 'Y') ? 'yes' : 'no';
		$displayemail = ($c['displayemail'] == 'Y') ? 'yes' : 'no';
		$q = "INSERT INTO users_committee(`users_id`,`emailprivate`,
				`ord`,`displayemail`,`access_admin`,`access_config`,
				`access_super`) VALUES (
					'$id', '{$c['emailprivate']}',
					'{$c['ord']}',
					'$displayemail',
					'$access_admin',
					'$access_config',
					'$access_super')";
		$stmt = $pdo->prepare($q);
		$stmt->execute();
		echo "$q\n";
		echo $pdo->errorInfo();
					
		/* Update committee links */
		$q = "UPDATE committees_link SET users_id='$id'
				WHERE committees_members_id='{$c['id']}'";
		$stmt = $pdo->prepare($q);
		$stmt->execute();
		echo "$q\n";

	}
}


?>
