<?php

// -----------------------------------------------------------------------
// This file is part of AROUNDMe
//
// Copyright (C) 2003 - 2007 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301, USA.
// -----------------------------------------------------------------------

$menu_section = "me";

if (!isset($_SESSION['md5'])) {
	header("Location: index.php?http_referer=" . urlencode($_SERVER['REQUEST_URI']) . "&" . strip_tags(SID));
	exit;
}


require_once ("class/File.class.php");
$file = new File($db, $core_config['file']);

if (isset($core_config['module'])) {
	$file->core_config_module = $core_config['module'];
}

$file->user_id = $_SESSION['user_id'];
$file->module_id = 0; // core

$user->user_id = $_SESSION['user_id'];
			
if (isset($_POST['update_identity'])) {
	// insert or update identity
	if (empty($_POST['identity_name'])) {
		$GLOBALS['am_error_log'][] = array('identity_name');
	}

	if(empty($_POST['identity_nic'])) {
		$GLOBALS['am_error_log'][] = array('identity_nic');
	}

	if (empty($GLOBALS['am_error_log'])) {
		
		$user->updateUserIdentity($_POST);
		
		if (!empty($user->identity_id)) {
			// delete and insert any custom fields
			if (!empty($_POST['custom'])) {
				$user->setUserIdentityCustomFields($_POST['custom']);
			}
		}
		
		// set identity types
		if (!empty($_POST['identity_type'])) {
			$query = "
				SELECT *  
				FROM " . $db->prefix . "_user_identity
				WHERE 
				identity_type=" . $_POST['identity_type'] . " AND 
				user_id=" . $_SESSION['user_id']
			;
			
			$result = $db->Execute($query);
			
			$rec = array();
			$rec['identity_type'] = "null";
		
			$query = $db->GetUpdateSQL($result, $rec, false, get_magic_quotes_gpc());
			$result = $db->Execute($query);
			
			$query = "
				SELECT *  
				FROM " . $db->prefix . "_user_identity
				WHERE 
				identity_id=" . $_POST['identity_id']
			;
			
			$result = $db->Execute($query);
			
			$rec = array();
			$rec['identity_type'] = $_POST['identity_type'];
			
			if ($rec['identity_type'] == $core_config['user']['identity']['type']['registered']) {
				$rec['identity_allow_usage'] = 1;
			}
		
			$query = $db->GetUpdateSQL($result, $rec, false, get_magic_quotes_gpc());
			$result = $db->Execute($query);
		}
	
	}
}

// select identity -----------------------------
if (!empty($_REQUEST['identity_id'])) {
	$output_identity = $user->selUserIdentity(null, $_REQUEST['identity_id'], 1);
	
	if (isset($output_identity['user_id']) && $output_identity['user_id'] == $_SESSION['user_id']) {
		$body->set('identity', $output_identity);
		
		$images = $file->selFiles("images", "t2");
		$body->set("images", $images);
	}
	else {
		unset($output_identity, $_REQUEST['identity_id']);
	}
}

$user->file = $file;
$user->file->no_image = $core_config['file']['image']['thumb'][1]['no_image'];
$user->order_by = "i.identity_type desc";
$output_identities = $user->selUserIdentities($_SESSION['user_id'], 1);

$body->set('identities', $output_identities);

$body->set('identity_config', $core_config['user']['identity']['type']);


?>