<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// Copyright (C) 2003, 2005 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., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

include "inc/header_group.inc.php";
require_once($c_node['dir']['class_path'] . 'SocialNetwork.class.php');


//ACS check
if (!$group_acs->checkPermission($c_acs['arr_acs']['resource']['group_membership'], $c_acs['arr_acs']['privilege']['allow_access'])) { //resourece_id, Priv_id
	header("Location: g_group.php?group_id=" . $output_group['group_id']."&amp;".SID);
	exit;
}


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');
require_once($languageArrayPath . 'arr_group.lang.php');


//we add the new members
if (isset($_POST['invite_friends']) && isset($data)) {
	$ins_arr = $data['group_invite'];
	
	$group->node_id = $c_node['node']['node_id'];
	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: g_membership.php?group_id=" . $group_id . "&" . 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($user_id, $db, $c_node['node']['node_id']);
	$sn->relation_type_ids = $c_node['user']['relation_type'];
	$sn->status_ids = $c_node['user']['status'];
	$sn->date = $date;
	$sn->user = $user;

	//get social network without member id's
	
	$friends = $sn->getFriends($me['user_id']);
	
	//remove member from friends
	if (!empty($friends)) {
		foreach($friends as $key => $f):
			foreach($member_ids as $key1 => $m):
				if ($f == $m) {
				    unset($friends[$key]);
				}
			endforeach;
		endforeach;
		
		if (!empty($friends)) {
			$user->user_ids = $friends;
			$non_member_friends = $user->getProfiles();
		}
	}
	
	$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);
}

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";

?>
