<?php

/* 
 *	W-AGORA 4.2
 *	-----------
 *	$Id: editlist.php,v 1.32 2006/02/24 23:06:40 mdruilhe Exp $
 *	Usage:		Configure listing of notes/forums
 *	Author:		Marc Druilhe <mdruilhe@w-agora.net>
 */

	require ("init_admin.inc");

	function display_var($var) {
		$var = ereg_replace("\\$"."([a-zA-Z0-9_]+)", "{\\1}", $var);
		return htmlSpecialChars ($var);
	}

	function get_field ($name, &$var) {
 		$fieldlen = 60;
 		if (ereg("\n", $var) || strlen($var) > $fieldlen) {
			textArea ("$name", $var, 6, $fieldlen);
		} else {
			textField ("$name", $var, $fieldlen);
		}
	}

	function display_line ($label, $name, $value='') {
		echo '<TR><TD align="right" valign="top" class="label">'. $label .'</TD><TD>';
		$editval = display_var ( (empty($value) ) ? $GLOBALS[$name] : $value);
		get_field ($name, $editval);
		echo "</TD></TR>\n";
	}


# 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;
/* set in init_admin
		if ($editsite==1) {
			$cfg_file = $site_cfg_file;	
		}
*/
	}

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

	if ($site=='agora') {
		display_header (TITLE_SITE_OUTLINE);
	} elseif ($editsite) {
		display_header (TITLE_FORUM_OUTLINE);
	} else {
		display_header (TITLE_MESSAGE_OUTLINE);
	}

	$page_info = '';
	if ($editdefault==1) {
		$var_file = "$bn_dir_default/list.$ext";
		$page_info = WARNING_EDITING_DEFAULT;
	} elseif (empty ($bn_list_vars) ) {
		if ($editsite==1) {
			$var_file="$bn_dir/site_list.$ext"; // default file for site listing
		} else {
			$var_file = "$bn_dir_default/list.$ext";
			$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_list_vars";
	}

	if (isset($save)) {
		if (empty($var_file)) {
			MsgForm (ERROR_NO_CURRENT_FILE, "$WA_SELF", "back");
			exit;
		}
		
		$conf = new Config;
		$ret = $conf->open($var_file);
		if (!$ret) {
			MsgForm (str_replace("{FILE}", $var_file, ERROR_OPEN_FILE), "$WA_SELF", "back");
			exit;
		}
		$conf->writeInt ("bn_maxthreads", (int) getPostVar('bn_maxthreads'), LABEL_MAXTHREADS);
		$conf->writeString ("bn_pad", getPostVar('bn_pad'), LABEL_PAD);
		$conf->writeInt ("bn_depth_limit", (int) getPostVar('bn_depth_limit'), LABEL_DEPTH_LIMIT);
		$conf->writeString ("bn_before_line", getPostVar('bn_before_line'), LABEL_BEFORE_LINE);
		$conf->writeString ("bn_sort", getPostVar('bn_sort'), LABEL_SORT);
		$conf->writeString ("bn_hot_color", getPostVar('bn_hot_color'), LABEL_HOT_COLOR);
		$conf->writeString ("bn_new_color", getPostVar('bn_new_color'), LABEL_NEW_COLOR);
		$conf->writeString ("bn_normal_color", getPostVar('bn_normal_color'), LABEL_NORMAL_COLOR);
		$conf->writeInt ("bn_hot_age", ( (int) getPostVar('bn_hot_age'))*3600, LABEL_HOT_AGE);
		$conf->writeInt ("bn_new_age", ( (int) getPostVar('bn_new_age'))*3600, LABEL_NEW_AGE);
		$conf->writeInt ("bn_hot_threshold", (int)getPostVar('bn_hot_threshold'), LABEL_HOT_THRESHOLD);

		if ($editsite) {
			$conf->writeString ("bn_list_header", getPostVar('bn_list_header'));
			$conf->writeString ("bn_list_category", getPostVar('bn_list_category'));
			$conf->writeString ("bn_list_odd_first_line", getPostVar('bn_list_odd_first_line'));
			$conf->writeString ("bn_list_even_first_line", getPostVar('bn_list_even_first_line'));
			$conf->writeString ("bn_list_footer", getPostVar('bn_list_footer'));
		} else {
			$conf->writeString ("bn_list_header", getPostVar('bn_list_header'));
			$conf->writeString ("bn_list_odd_first_line", getPostVar('bn_list_odd_first_line'));
			$conf->writeString ("bn_list_even_first_line", getPostVar('bn_list_even_first_line'));
			$conf->writeString ("bn_list_begin_level", getPostVar('bn_list_begin_level'));
			$conf->writeString ("bn_list_odd_line", getPostVar('bn_list_odd_line'));
			$conf->writeString ("bn_list_even_line", getPostVar('bn_list_even_line'));
			$conf->writeString ("bn_list_end_level", getPostVar('bn_list_end_level'));
			$conf->writeString ("bn_list_end_thread", getPostVar('bn_list_end_thread'));
			$conf->writeString ("bn_list_footer", getPostVar('bn_list_footer'));

			$conf->writeString ("bn_view_header", getPostVar('bn_view_header'));
			$conf->writeString ("bn_view_odd_first_line", getPostVar('bn_view_odd_first_line'));
			$conf->writeString ("bn_view_even_first_line", getPostVar('bn_view_even_first_line'));
			$conf->writeString ("bn_view_begin_level", getPostVar('bn_view_begin_level'));
			$conf->writeString ("bn_view_odd_line", getPostVar('bn_view_odd_line'));
			$conf->writeString ("bn_view_even_line", getPostVar('bn_view_even_line'));
			$conf->writeString ("bn_view_end_level", getPostVar('bn_view_end_level'));
			$conf->writeString ("bn_view_end_thread", getPostVar('bn_view_end_thread'));
			$conf->writeString ("bn_view_footer", getPostVar('bn_view_footer'));
		
			$conf->writeString ("bn_delete_header", getPostVar('bn_delete_header'));
			$conf->writeString ("bn_delete_odd_first_line", getPostVar('bn_delete_odd_first_line'));
			$conf->writeString ("bn_delete_even_first_line", getPostVar('bn_delete_even_first_line'));
			$conf->writeString ("bn_delete_begin_level", getPostVar('bn_delete_begin_level'));
			$conf->writeString ("bn_delete_odd_line", getPostVar('bn_delete_odd_line'));
			$conf->writeString ("bn_delete_even_line", getPostVar('bn_delete_even_line'));
			$conf->writeString ("bn_delete_end_level", getPostVar('bn_delete_end_level'));
			$conf->writeString ("bn_delete_end_thread", getPostVar('bn_delete_end_thread'));
			$conf->writeString ("bn_delete_footer", getPostVar('bn_delete_footer'));

			$conf->writeString ("bn_digest_header", getPostVar('bn_digest_header'));
			$conf->writeString ("bn_digest_odd_first_line", getPostVar('bn_digest_odd_first_line'));
			$conf->writeString ("bn_digest_even_first_line", getPostVar('bn_digest_even_first_line'));
			$conf->writeString ("bn_digest_begin_level", getPostVar('bn_digest_begin_level'));
			$conf->writeString ("bn_digest_odd_line", getPostVar('bn_digest_odd_line'));
			$conf->writeString ("bn_digest_even_line", getPostVar('bn_digest_even_line'));
			$conf->writeString ("bn_digest_end_level", getPostVar('bn_digest_end_level'));
			$conf->writeString ("bn_digest_end_thread", getPostVar('bn_digest_end_thread'));
			$conf->writeString ("bn_digest_footer", getPostVar('bn_digest_footer'));

		}

		$conf->writeString ("bn_search_forum_title", getPostVar('bn_search_forum_title'));
		$conf->writeString ("bn_search_header", getPostVar('bn_search_header'));
		$conf->writeString ("bn_search_odd_first_line", getPostVar('bn_search_odd_first_line'));
		$conf->writeString ("bn_search_even_first_line", getPostVar('bn_search_even_first_line'));
		$conf->writeString ("bn_search_begin_level", getPostVar('bn_search_begin_level'));
		$conf->writeString ("bn_search_odd_line", getPostVar('bn_search_odd_line'));
		$conf->writeString ("bn_search_even_line", getPostVar('bn_search_even_line'));
		$conf->writeString ("bn_search_end_level", getPostVar('bn_search_end_level'));
		$conf->writeString ("bn_search_end_thread", getPostVar('bn_search_end_thread'));
		$conf->writeString ("bn_search_footer", getPostVar('bn_search_footer'));

		$conf->close();

	} elseif (isset($undo)) {
		if (file_exists ("$var_file.bak.$ext")) {
			copy ("$var_file.bak.$ext", "$var_file");
		}
	}

	if (!$var_file) {
		echo '<H2>' . NO_CURRENT_FILE . '</H2>';
		beginForm();
		echo '<CENTER>' . LABEL_TYPE_FILENAME . ' : ';
		inputField ('var_file');
		echo '</CENTER>';
		endForm();
		exit;
	}

	if (file_exists ($var_file)) {
		include "$var_file";
	} else {
		if (file_exists ("$bn_dir_default/list.$ext")) {
			@copy ("$bn_dir_default/list.$ext", "$var_file");
			include "$var_file";
		}
	}

	echo "<center>$page_info</center><br>";
	table_header (LABEL_FILE_EDITING . " : $var_file");

	beginForm();
	hiddenField ("editsite", $editsite);
	hiddenField ('editdefault', $editdefault);

	echo "<CENTER>";

	echo '<TABLE width="100%">';
	echo '<TR><TD COLSPAN=2><h2>'. LABEL_GLOBAL_FORMAT. '</h2></TD></tr>';
	display_line (LABEL_DEFAULT_ORDER, 'bn_sort');

	echo '<TR><TD align="right" class="label">'. LABEL_BEFORE_LINE .'</TD><TD>';
	textArea ('bn_before_line', htmlspecialchars($bn_before_line), 6, 60);
	echo "</TD></TR>\n";

	if ($site!='agora') {
		echo '<TR><TD ALIGN="RIGHT" class="label">';
		echo LABEL_HOT_COLOR . '</td><TD class="label">'. getTextField ("bn_hot_color", display_var ($bn_hot_color), 10) . LABEL_MESSAGES_NEWER_THAN . getTextField ("bn_hot_age", $bn_hot_age/3600, 3) . LABEL_HOURS;
		echo '</td></tr>';
		echo '<TR><TD ALIGN="RIGHT" class="label">';
		echo LABEL_NEW_COLOR . '</td><TD class="label">'. getTextField ("bn_new_color", display_var ($bn_new_color), 10) . LABEL_MESSAGES_NEWER_THAN . getTextField ("bn_new_age", $bn_new_age/3600, 3) . LABEL_HOURS;
		echo '</td></tr>';
		display_line (LABEL_NORMAL_COLOR, 'bn_normal_color');
	}

	if ($editsite) {

		echo '</TABLE><hr noshade><TABLE width="100%">';
		echo '<TR><TD COLSPAN=2><h2>'. LABEL_FORUM_OUTLINE. '</h2></TD></tr>';
		display_line (LABEL_HEAD_LISTING, 'bn_list_header');
		display_line (LABEL_CATEGORY_LINE, 'bn_list_category');
		display_line (LABEL_FORUM_LINE_ODD, 'bn_list_odd_first_line');
		display_line (LABEL_FORUM_LINE_EVEN, 'bn_list_even_first_line');
		display_line (LABEL_BOTTOM_LISTING, 'bn_list_footer');
		echo '</TABLE>';

		echo '<hr noshade><TABLE width="100%">';
		echo '<TR><TD COLSPAN=2><h2>'. LABEL_EDIT_SEARCH_LISTING. '</h2></TD></tr>';
		display_line (LABEL_SEARCH_FORM_TITLE, 'bn_search_forum_title');
		echo '</TABLE>';

   } elseif ($site=='agora') {

		echo '</TABLE><hr noshade><TABLE width="100%">';
		// echo '<TR><TD COLSPAN=2><h2>'. LABEL_FORUM_OUTLINE. '</h2></TD></tr>';
		display_line (LABEL_HEAD_LISTING, 'bn_list_header');
		display_line (LABEL_SITE_LINE_ODD, 'bn_list_odd_first_line');
		display_line (LABEL_SITE_LINE_EVEN, 'bn_list_even_first_line');
		display_line (LABEL_BOTTOM_LISTING, 'bn_list_footer');
		echo '</TABLE>';
   } else {

		display_line (LABEL_MAXTHREADS, 'bn_maxthreads');
		display_line (LABEL_PAD, 'bn_pad');
		display_line (LABEL_DEPTH_LIMIT, 'bn_depth_limit');
		display_line (LABEL_HOT_THRESHOLD, 'bn_hot_threshold');

		echo '</TABLE><hr noshade><TABLE width="100%">';
		echo '<TR><TD COLSPAN=2><h2>'. EDIT_THREAD_LIST. '</h2></TD></tr>';
		display_line (LABEL_HEAD_LISTING, 'bn_list_header');
		display_line (LABEL_THREAD_FIRSTLINE_ODD, 'bn_list_odd_first_line');
		display_line (LABEL_THREAD_FIRSTLINE_EVEN, 'bn_list_even_first_line');

		display_line (LABEL_LEVEL_START, 'bn_list_begin_level');
		display_line (LABEL_LINE_CONTENT_ODD, 'bn_list_odd_line');
		display_line (LABEL_LINE_CONTENT_EVEN, 'bn_list_even_line');
		display_line (LABEL_LEVEL_END, 'bn_list_end_level');
		display_line (LABEL_THREAD_END, 'bn_list_end_thread');
		display_line (LABEL_LIST_END, 'bn_list_footer');
		echo '</TABLE>';

		echo '<hr noshade><TABLE width="100%">';
		echo '<TR><TD COLSPAN=2><h2>'. EDIT_THREAD_VIEW. '</h2></TD></tr>';
		display_line (LABEL_HEAD_LISTING, 'bn_view_header');
		display_line (LABEL_FIRST_THREAD, 'bn_view_even_first_line');
		display_line (LABEL_LEVEL_START, 'bn_view_begin_level');
		display_line (LABEL_LINE_CONTENT_ODD, 'bn_view_odd_line');
		display_line (LABEL_LINE_CONTENT_EVEN, 'bn_view_even_line');
		display_line (LABEL_LEVEL_END, 'bn_view_end_level');
		display_line (LABEL_LIST_END, 'bn_view_footer');
		echo '</TABLE>';

       	echo '<hr noshade><TABLE width="100%">';
		echo '<TR><TD COLSPAN=2><h2>'. EDIT_THREAD_DOWNLOAD. '</h2></TD></tr>';
		display_line (LABEL_HEAD_LISTING, 'bn_digest_header');
		display_line (LABEL_THREAD_FIRSTLINE_ODD, 'bn_digest_odd_first_line');
		display_line (LABEL_THREAD_FIRSTLINE_EVEN, 'bn_digest_even_first_line');
		display_line (LABEL_LEVEL_START, 'bn_digest_begin_level');
		display_line (LABEL_LINE_CONTENT_ODD, 'bn_digest_odd_line');
		display_line (LABEL_LINE_CONTENT_EVEN, 'bn_digest_even_line');
		display_line (LABEL_LEVEL_END, 'bn_digest_end_level');
		display_line (LABEL_THREAD_END, 'bn_digest_end_thread');
		display_line (LABEL_BOTTOM_LISTING, 'bn_digest_footer');
		echo '</TABLE>';

		echo '<hr noshade="noshade"><table width="100%">';
		echo '<TR><TD COLSPAN=2><h2>'. LABEL_EDIT_SEARCH_LISTING. '</h2></TD></tr>';
		display_line (LABEL_HEAD_LISTING, 'bn_search_header');
		display_line (LABEL_LINE_CONTENT_ODD, 'bn_search_odd_first_line');
		display_line (LABEL_LINE_CONTENT_EVEN, 'bn_search_even_first_line');
		display_line (LABEL_LIST_END, 'bn_search_footer');
		echo '</TABLE>';

   }
?>
  <HR>
  <INPUT TYPE="submit" NAME="save" VALUE="<?php echo LABEL_SAVE?>">
  <INPUT TYPE="reset" VALUE="<?php echo LABEL_RESET?>">
  <INPUT TYPE="submit" NAME="undo" VALUE="<?php echo LABEL_UNDO?>">
 </CENTER>
</FORM>
<?php table_footer(); ?>
