<?php

/*
 *	W-AGORA 4.0
 *	-----------
 *	$Id: editfields.php,v 1.19 2005/12/31 09:43:34 mdruilhe Exp $
 *	Usage:		Edit variables to be inserted into database
 *	Author:		Marc Druilhe <mdruilhe@w-agora.com>
 */

	require ('init_admin.inc');

	function get_default_forumtables () {
		global $db, $site, $cfg_dir, $ext;

		$tables = array();
		$db->query ("SELECT bn_db, bn_name FROM $site");
		while ($db->next_record() ) {
			$cfg_file = "$cfg_dir/". $db->f("bn_name") . ".$ext";
			if (file_exists("$cfg_file")) {
				$bn_fields = '';
				include ("$cfg_file");
				if (empty($bn_fields) ) {
					$tables[] = $db->f("bn_db");
				}
			}
		}
		return $tables;
	}


# Only a site administrator can edit layout
# -----------------------------------------
	if ($site=="agora") {
		if ($auth->level < ROOT) {
			msgForm (ERROR_NO_ROOT, "admin_site.$ext?site=$site&force_login=1", "", "_top");
			echo "</body></html>";
			exit;
		}
		$cfg_file = $site_cfg_file;
	} elseif ($auth->level < ADMIN) {
		msgForm (ERROR_NO_ADMIN, "admin_site.$ext?site=$site&force_login=1", "", "_top");
		echo "</body></html>";
		exit;
		if (($editsite==1) || ($editdefault==1) ) {
			$cfg_file = $site_cfg_file;
		}
	}

	require ("$site_cfg_file");
	require ("$cfg_file");

	$page_info = '';
	if ($editdefault==1) {
		$var_file = "$bn_dir_default/fields.$ext";
		$editing_default=1;
		$page_info = WARNING_EDITING_DEFAULT;
	} elseif (empty ($bn_fields) ) {
		$var_file = "$bn_dir_default/fields.$ext";
		$editing_default=1;
		$page_info = WARNING_EDITING_DEFAULT;
		if (!empty($bn) ) {
			$page_info .= "<br />" . sprintf(LABEL_SPECIFIC_TEMPLATE, "editconf.$ext?site=$site&bn=$bn&mode=advanced#include");
		}
	} else {
		$var_file="$bn_dir/$bn_fields";
	}

	$page_title = LABEL_FILE_EDITING . " '$var_file'";

# Prevent the user to edit a file outside w-agora
# -----------------------------------------------
	if (! (ereg("$bn_dir_forums", $var_file) || ereg("$tmpl_dir", $var_file))) {
		msgForm ("access restricted to $var_file", $WA_SELF, 'back');
		exit;
	}

	if (isset($_POST['undo'])) {
		if (file_exists ("$var_file.bak.$ext")) {
			copy ("$var_file.bak.$ext", "$var_file");
		}
	} elseif (isset($_POST['save']) || isset($_GET['save']) ) {

		if (empty($var_file)) {
			msgForm (ERROR_NO_CURRENT_FILE, "$WA_SELF&site=$site", 'back');
			exit;
		}

# delete field
# ------------
		if (isset ($_POST['delfield']) ) {
			$delfield = $_POST['delfield'];
		} elseif (isset ($_GET['delfield']) ) {
			$delfield = $_GET['delfield'];
		}

		if (!empty($delfield)) {
		    include "$var_file";
			if (isset($bn_var_size[$delfield])) unset ($bn_var_size[$delfield]);
			if (isset($list_var[$delfield])) unset ($list_var[$delfield]);
			if (isset($bn_bind_var[$delfield])) unset ($bn_bind_var[$delfield]);
			if (isset($bn_html_var[$delfield])) unset ($bn_html_var[$delfield]);
			if (isset($bn_cookie_var[$delfield])) unset ($bn_cookie_var[$delfield]);
		}

# start writing file
# ------------------
		$new_contents = "<"."?php\n";
		$new_contents .= "unset (\$db_var);\n";

# User variables
# --------------
		$new_contents .= "\n# " . LABEL_USER_VARIABLES . "\n";
		$i = 0;			// db_var index
		$db_count = 0;	// new count index
		while (is_array($db_var) && ($i < count ($db_var)) ) {
			$var = $db_var[$i];
			if ($var != $delfield ) {
				$sz = $bn_var_size[$var];
				$new_contents .= "\$db_var[$db_count]=\"$var\";\n";
				$new_contents .= "\$bn_var_size[\"$var\"]=\"$sz\";\n";
				$db_count++;
			}
			$i++;
		}

# Add new field
# -------------
		if (!empty ($_POST['new_var']) ) {
			if (empty ($new_size) ) {
				msgForm (ERROR_UNDEFINED_VARSIZE, $WA_SELF, 'back');
				exit;
			}


			include "$var_file";
		}

		if (!empty ($new_var)) {

			$new_contents .= "\$db_var[$i]=\"$new_var\";\n";
			$new_contents .= "\$bn_var_size[$new_var]=\"$new_size\";\n";

			$db->setHaltOnError('report');
			if ($editing_default==1) {
				// Gets all forums that are using this "fields description"
			    $update_forums = get_default_forumtables();
				reset ($update_forums);
				while (list (,$table) = each ($update_forums)) {
					$db->addField($table, $new_var, $new_size);
				}
			} elseif (!empty($bn)) {
				$f = $db->getForum($site, $bn);
				if (is_array($f) ) {
				    $db->addField($f['bn_db'], $new_var, $new_size);
				}
			}

			$bn_var_size[$new_var] = $new_size;
			if (isset($new_list_var)) {
				$list_var[$new_var] = 1;
			}
			if (isset($new_bind_var)) {
				$bn_bind_var[$new_var] = 1;
			}
			if (isset($new_html_var)) {
				$bn_html_var[$new_var] = 1;
			}
			if (isset($new_cookie_var)) {
				$bn_cookie_var[$new_var] = 1;
			}
		}

		$new_contents .= "\n# " . LABEL_LISTED_VARIABLES . "\n";
		while (is_array($list_var) && (list($var,$val) = each($list_var)) ) {
			$val = (empty($val)) ? '0' : '1';
			$new_contents .= "\$list_var[\"$var\"] = $val;\n";
		}

		if (is_array($bn_bind_var)) {
			$new_contents .= "\n# ". LABEL_REQUIRED_VARIABLES. "\n";
			reset($bn_bind_var);
			while (list($var,$val) = each($bn_bind_var)) {
				$val = (empty($val)) ? '0' : '1';
				$new_contents .= "\$bn_bind_var[\"$var\"] = $val;\n";
			}
		}

		if (is_array($bn_html_var)) {
			$new_contents .= "\n# " . LABEL_VARIABLES_ACCEPT_HTML . "\n";
			reset($bn_html_var);
			while (list($var,$val) = each($bn_html_var)) {
				$val = (empty($val)) ? '0' : '1';
				$new_contents .= "\$bn_html_var[\"$var\"] = $val;\n";
			}
		}

		if (is_array($bn_cookie_var)) {
			$new_contents .= "\n# " . LABEL_VARIABLES_COOKIE. "\n";
			reset($bn_cookie_var);
			while (list($var,$val) = each($bn_cookie_var)) {
				$val = (empty($val)) ? '0' : '1';
				$new_contents .= "\$bn_cookie_var[\"$var\"] = $val;\n";
			}
		}

		$new_contents .= "?".">\n";

# Make a backup
# -------------
		copy ("$var_file", "$var_file.bak.$ext");

# and write new contents
# ----------------------
		$fd = fopen ($var_file, "w");
		if (!$fd) {
			msgForm (str_replace("{FILE}", $var_file, ERROR_OPEN_FILE), $WA_SELF, 'back');
			exit;
		}
		fputs ($fd, $new_contents);
		fclose ($fd);

	}

	if (file_exists ($var_file)) {
		include "$var_file";
		$form_title = '<em>' . LABEL_FILE_EDITING . ": $var_file </em>";
	} else {
		$form_title = "<em>" . LABEL_NEW_FILE_EDITING . " $var_file </em>";
		if (file_exists ("$bn_dir_default/fields.$ext")) {
			copy ("$bn_dir_default/fields.$ext", "$var_file");
			include "$var_file";
		}
	}

	display_header ($page_title);
?>
<script language="JavaScript" type="text/javascript">

function deleteField(n){
  f = document.forms[0];
  ok=window.confirm("<?php echo LABEL_CONFIRM_DELETE_FIELD?>: "+n);
  if(ok){
	f.delfield.value = n;
    f.submit();
  }
}

function addField(){
  f = document.forms[0];
  if (f.new_var.value.length==0) {
    alert ("<?php echo ERROR_UNDEFINED_VARNAME?>");
	f.new_var.focus();
	return false;
  }
  for (i = 0; i < f.elements.length; i++) {
    if ( (f.elements[i].name == 'bn_var_size['+f.new_var.value+']') ) {
      alert ("<?php echo ERROR_VARNAME_ALREADY_DEFINED?>");
	  f.new_var.focus();
	  return false;
    }
  }
  if (f.new_size.value.length==0) {
    alert ("<?php echo ERROR_UNDEFINED_VARSIZE?>");
	f.new_size.focus();
	return false;
  }

  f.submit();
}
</script>
<?php
	echo $page_info;
	beginForm();
	table_header ($form_title);
	hiddenField ('var_file', $var_file);
	hiddenField ('editsite', $editsite);
	hiddenField ('editdefault', $editdefault);
	hiddenField ('delfield', '');
	hiddenField ('save', '1');
?>
  <TABLE>
   <TR bgcolor=white>
    <TD width="25%" align="center" class="label"><?php echo LABEL_NAME ?></TD>
	<TD width="13%" align="right" class="label"><?php echo LABEL_SIZE?></TD>
	<TD width="12%" align="center" class="label"><?php echo LABEL_REQUIRED ?></TD>
	<TD width="10%" class="label"><?php echo LABEL_LISTED ?></TD>
	<TD width="20%" class="label"><?php echo LABEL_ACCEPT_HTML?></TD>
	<TD width="20%" class="label"><?php echo LABEL_SAVE_COOKIE?></TD>
	<TD width="20%" class="label"><?php echo LABEL_DELETE_FIELD?></TD>
   </TR>
<?php


# Set core variables (managed by w-agora)
# this code is here for compatibility w/ old fields def. files (bn_var is reset)
# ------------------------------------------------------------------------------

	$db_count = Count ($bn_var);
	for ($i=0; $i < $db_count; $i++) {
		$var = $bn_var[$i];
		printf ("<tr><td align='center' class=label>%s</td>", $var);
		printf ("<td align='right' class=label>%s</td>", $bn_var_size[$var]);
		printf ("<td align='center' class=label>%s</td>", ($bn_bind_var[$var] == 1) ? "X" : '&nbsp;');
		printf ("<td align='center' class=label>%s</td>", ($list_var[$var] == 1) ? "X" : '&nbsp;');
		echo "<td align='center' class=label>"; checkBox ("bn_html_var[$var]", "1", $bn_html_var[$var]); echo '</td>';
		echo "<td align='center' class=label>"; checkBox ("bn_cookie_var[$var]", "1", $bn_cookie_var[$var]); echo '</td>';
		echo "</td><td>&nbsp;</td></tr>\n";
	}
	$db_count = Count ($db_var);
	for ($i=0; $i < $db_count; $i++) {
		$var = $db_var[$i];
		$sz = $bn_var_size[$var];
		?><tr bgcolor=#d0dae6><td align='center' class=label><?php
		hiddenField ("db_var[$i]", $var);
		echo "$var";
		?></td><td align='right' class=label>
		<?php textField ("bn_var_size[$var]", $sz, 5);
		?></td><td align='center' class=label><?php
		checkBox ("bn_bind_var[$var]", "1", $bn_bind_var[$var]);
		?></td><td align='center' class=label><?php
		checkBox ("list_var[$var]", "1", $list_var[$var]);
		?></td><td align='center' class=label><?php
		checkBox ("bn_html_var[$var]", "1", $bn_html_var[$var]);
		?></td><td align='center' class=label><?php
		checkBox ("bn_cookie_var[$var]", "1", $bn_cookie_var[$var]);
		?></td><td align="center" class="label"><a href="<?php echo "$WA_SELF?var_file=$var_file&editsite=$editsite&editdefault=$editdefault&delfield=$var&save=1"?>"  onclick="deleteField('<?php echo $var?>'); return false;"><img src="<?php echo "$icon_dir/delete.gif"?>" align="absmiddle" border=0 alt="<?php printf (ALT_DELETE_FIELD, $var)?>"></A></td></tr><?php
	}
?>
   <TR bgcolor="#d0dae6">
    <TD align="center"><INPUT NAME="new_var"></TD>
    <TD align="right"><INPUT SIZE=5 NAME="new_size"></TD>
    <TD align="center"><INPUT TYPE="checkbox" NAME="new_bind_var"></TD>
    <TD align="center"><INPUT TYPE="checkbox" NAME="new_list_var"></TD>
    <TD align="center"><INPUT TYPE="checkbox" NAME="new_html_var"></TD>
    <TD align="center"><INPUT TYPE="checkbox" NAME="new_cookie_var"></TD>
	<TD align="center" class="label">
      <a href="#" onclick="addField(); return false;"><img src="<?php echo "$icon_dir/expand.gif"?>" align="absmiddle" border=0 alt="<?php echo ALT_ADD_FIELD?>"></A>
    </TD>
   </TR>
   <TR>
    <TD colspan=7 align="center">
	 <HR noshade color="black" width="90%">
<?php
	submitButton('save', LABEL_SAVE);
	resetButton ('reset', LABEL_RESET);
	submitButton ('undo', LABEL_UNDO);
	echo '</TD></TR></TABLE>';

	table_footer();
	endForm();
?>
