<?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.
// -----------------------------------------------------------------------

$f_path = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])."..")."..")."/aroundme/components/core/language/1/barnraiser_01/common.lang.php";
$file = file($f_path);
$roles = array();
$keys = array();

for($i = 0; $i < count($file); $i++){
	if (strstr($file[$i], "lang['role']")) {
		$temp = explode('"', $file[$i]);
		$roles[] = $temp[1];
		$keys[] = $i;
	}
}

if (isset($_POST['add_role'])) {

	if (isset($_POST['role']) && $_POST['role'] != '') {
		$lines = array();
		//$lines[] = '// added by installer at '.date('Y-m-d')."\n";
		for ($i=0; $i < count($roles); $i++) {
			$lines[] = "$" . "lang['role'][" . ($i+1) . "] = \"" . $roles[$i] . "\";\n";
		}
		$lines[]= "$" . "lang['role'][" . ($i+1) . "] = \"" . $_POST['role'] . "\";\n";
	
		unset($file[count($file)-1]);
		foreach($keys as $k) {
			unset($file[$k]);
		}	
		
		$new_file = array_merge($file, $lines);
		$new_file[] = "?>";
		
		$content = "";
		foreach($new_file as $n) {
			$content .= $n;
		}
		
		if(!$f = fopen($f_path, "w+")) {
			echo $lang['error_fail_openfile'] . " " . $f_path;
		}
		
		if (!fwrite($f, $content)) {
			echo $lang['error_fail_writefile'] . " " . $f_path;
		}
		fclose($f);
		unset($roles);
		
		foreach($new_file as $line) {

			if (strstr($line, "lang['role']")) {
				$temp = explode('"', $line);
				$roles[] = $temp[1];
			}
		}
	}
}

for($i=0; $i < count($roles); $i++) {

	if (isset($_POST['del_role_'.$i])) {
		unset($file[$keys[$i]]);
		
		$content = "";
		foreach($file as $n) {
			$content .= $n;
		}
		
		if(!$f = fopen($f_path, "w+")) {
			echo $lang['error_fail_openfile'] . " " . $f_path;
		}
		
		if (!fwrite($f, $content)) {
			echo $lang['error_fail_writefile'] . " " . $f_path;
		}
		fclose($f);
		
		unset($roles);
		foreach($file as $line) {

			if (strstr($line, "lang['role']")) {
				$temp = explode('"', $line);
				$roles[] = $temp[1];
			}
		}
	}
}

for($i=0; $i < count($roles); $i++) {
	
	if (isset($_POST['update_role_'.$i])) {
			
		if(!$f = fopen($f_path, "w+")) {
			echo $lang['error_fail_openfile']. " ".$f_path;
		}
			
		$file[$keys[$i]] = "$"."lang['role'][".$i."] = \"".$_POST['role_'.$i]."\";\n";

		foreach($file as $line) {
			fputs($f, $line);
		}
			
		fclose($f);
			
		unset($roles);
		foreach($file as $line) {

			if (strstr($line, "lang['role']")) {
				$temp = explode('"', $line);
				$roles[] = $temp[1];
			}
		}
	}
}


function am_check_roles($arr) {
	GLOBAL $lang;
	if (isset($arr['role_0']) && $arr['role_0'] != '') {
	
		write_config($arr['default_role'], "core_config['role']['invite_default']");
		return true;
	}
	echo $lang['error_too_few_roles'];
	return false;
}

?>

	<form method="post" action="install.php">
	<input type="hidden" name="step" value="7" />	

	<p><?php echo $lang['txt_step_7']; ?></p>
	
	<?php if (isset($_POST['frm_send'])) { ?>	
		<div class="message">
			<?php if (am_check_roles($_POST)) { header("location: install.php?step=8"); exit; }?>	
		</div>
	<?php } ?>	
	
	<table width="100%">
		<?php for($i=0; $i < count($roles); $i++) { ?>
			<tr>
				<td><?php echo $lang['txt_step_7_role_role']; ?> <?php echo ($i+1); ?></td>
				<td><input type="text" name="role_<?php echo $i; ?>" value="<?php echo $roles[$i];?>"/></td>
				<td align="right"><input type="submit" name="del_role_<?php echo $i; ?>" value="<?php echo $lang['frm_step_7_delete']; ?>" /></td>
				<td align="right"><input type="submit" name="update_role_<?php echo $i; ?>" value="<?php echo $lang['frm_step_7_update']; ?>" /></td>
			</tr>
		<?php } ?>
	
		<tr>
			<td><?php echo $lang['txt_step_7_role_defaultrole']; ?></td>
			<td>
				<select name="default_role">
					<?php for($i=0; $i < count($roles); $i++) { ?>
						<option value="<?php echo ($i+1); ?>" <?php if (isset($_POST['default_role']) && $_POST['default_role'] == ($i+1)) echo "selected='selected'"; ?>><?php echo $roles[$i]; ?></option>
					<?php } ?>			
				</select>
			</td>
			<td></td>
			<td></td>	
		</tr>
	
		<tr>
			<td><?php echo $lang['txt_step_7_role_addrole']; ?></td>
			<td><input type="text" name="role"/></td>
			<td></td>
			<td align="right"><input type="submit" name="add_role" value="<?php echo $lang['frm_step_7_add']; ?>" /></td>	
		</tr>
	
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td><input type="submit" name="frm_send" value="<?php echo $lang['frm_step_7_next_step']; ?>"/></td>
		</tr>	
	</table>
	</form>