<?php

// -----------------------------------------------------------------------
// This file is part of AROUNDMe
// 
// Copyright (C) 2003 - 2006 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.
// -----------------------------------------------------------------------

$group_id = $_REQUEST['group_id'];
include "inc/header_group.inc.php";
require_once('class/SocialNetwork.class.php');




if (isset($_REQUEST['role_id']) && $_REQUEST['role_id'] > 0) {
	$role_id = $_REQUEST['role_id'];
	$group->role_id = $role_id;
}

//append language setup
require_once($languageArrayPath . 'arr_user.lang.php');


//we add the new members
if (isset($_POST['invite']) && isset($data)) {
	$ins_arr = $data['group_invite'];
	
	unset($group_acs->role_id);
	$role_default = $group_acs->getGroupRole(1);
	$group->member_status_id = $c_node['group']['member_status']['pending'];
	
	$rownums = count($ins_arr);
	
	for ($i=0; $i < $rownums; $i++) {
		$group->user_id = $ins_arr[$i];
		//get their profile
		foreach($data['group_invite_profile'] as $key => $r):
			if ($ins_arr[$i] == $key) {
				$group->insertMembership($r, $role_default['role_id']);
				break;
			}
		endforeach;
		
	}
	
	if (empty($GLOBALS['am_error_log'])) {
		header("Location: grp_membership.php?group_id=" . $group_id . "&" . strip_tags(SID));
		exit;
	}
	
}


if (isset($_REQUEST['invite_members'])) {
	//get member ids
	$output_members = $group->getMembers();
	if (!empty($output_members)) {
		foreach($output_members as $key => $i):
			$member_ids[] = $i['user_id'];
		endforeach;
	}
	
	//setup social network
	$sn = new Socialnetwork($me['user_id'], $db);
	$sn->relation_type_ids = $c_node['user']['relation_type'];
	$sn->status_ids = $c_node['user']['status'];

	//get social network without member id's
	
	$non_member_relations = $me['relations'];
	
	if (!empty($me['relations'])) {
		foreach($me['relations'] as $key => $i):
			foreach($member_ids as $key1 => $m):
				if ($i['user_id_relation'] == $m) {
					unset($non_member_relations[$key]);
				}
			endforeach;
		
			$user->user_ids[] = $i['user_id_relation'];
		endforeach;
	}
	else {
		$user->user_ids[] = 0;
	}
		
	$non_member_relations = $user->selProfiles();
	
	$display = "invite_members";
}
else {
	$output_members = $group->getMembers();
	
	
	if (isset($output_members)) {
		//get the status of each member
		foreach($output_members as $key => $i):
			$output_members[$key]['online_status'] = $user->getStatus($i['active_time']);
		endforeach;
	}
}


//get count of each memberships role
$group_roles = $group_acs->getGroupRoles();


//move to template
if (!empty($group_roles)) {
	$body->set('group_roles', $group_roles);
}

if (!empty($output_group)) {
	$body->set('group', $output_group);
	$tpl->set('group', $output_group);
}

if (!empty($output_members)) {
	$body->set('members', $output_members);
}

if (!empty($non_member_friends)) {
	$body->set('non_member_friends', $non_member_friends);
}

if (isset($display)) {
	$body->set('display', $display);
}

if (isset($role_id)) {
	$body->set('search_role_id', $role_id);
}

$template_pageLink = $lang['txt_membership'];
include "inc/footer.inc.php";

?>
