<?php
/*=================================================
	Project: phpGedView
	File: index_edit.php
	Author: John Finlay
	Comments:
		MyGedView page allows a logged in user the abilty
		to keep bookmarks, see a list of upcoming events, etc.

	Change Log:
		11/6/03 - File Created

	phpGedView: Genealogy Viewer
    Copyright (C) 2002 to 2003  John Finlay and Others

    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 of the License, 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

===================================================*/
# $Id: index_edit.php,v 1.18 2004/12/01 18:06:01 yalnifj Exp $

require("config.php");
require("functions_blocks.php");

if (!isset($action)) $action="";
if (!isset($command)) $command="user";
if (!isset($main)) $main=array();
if (!isset($right)) $right=array();
if (!isset($setdefault)) $setdefault=false;

//-- make sure that they have user status before they can use this page
//-- otherwise have them login again
$uname = getUserName();
if (empty($uname) || empty($name)) {
	print_simple_header("");
	print $pgv_lang["access_denied"];
	print_simple_footer();
	exit;
}
else $user = getUser($uname);

if (!userIsAdmin($uname)) $setdefault=false;

//-- get the blocks list
if ($command=="user") {
	$ublocks = getBlocks($uname);
	if ((count($ublocks["main"])==0)||($action=="reset")) {
		$ublocks["main"] = array();
		$ublocks["main"][] = "print_todays_events";
		$ublocks["main"][] = "print_user_messages";
		$ublocks["main"][] = "print_user_favorites";
	}
	if ((count($ublocks["right"])==0)||($action=="reset")) {
		$ublocks["right"] = array();
		$ublocks["right"][] = "print_welcome_block";
		$ublocks["right"][] = "print_random_media";
		$ublocks["right"][] = "print_upcoming_events";
		$ublocks["right"][] = "print_logged_in_users";
	}
}
else {
	$ublocks = getBlocks($GEDCOM);
	if ((count($ublocks["main"])==0)||($action=="reset")) {
		$ublocks["main"] = array();
		$ublocks["main"][] = "print_gedcom_stats";
		$ublocks["main"][] = "print_gedcom_news";
		$ublocks["main"][] = "print_gedcom_favorites";
	}
	if ((count($ublocks["right"])==0)||($action=="reset")) {
		$ublocks["right"] = array();
		$ublocks["right"][] = "print_gedcom_block";
		$ublocks["right"][] = "print_random_media";
		$ublocks["right"][] = "print_todays_events";
		$ublocks["right"][] = "print_logged_in_users";
	}
}
/* - sample blocks array
$ublocks = array();
$ublocks["main"] = array();
$ublocks["right"] = array();
$ublocks["main"][] = "print_todays_events";
$ublocks["main"][] = "print_user_messages";
$ublocks["main"][] = "print_user_favorites";
$ublocks["right"][] = "print_welcome_block";
$ublocks["right"][] = "print_random_media";
$ublocks["right"][] = "print_upcoming_events";
$ublocks["right"][] = "print_logged_in_users";
*/

if ($command=="user") print_simple_header($pgv_lang["mygedview"]);
else print_simple_header($GEDCOMS[$GEDCOM]["title"]);

if ($action=="update") {
	if (is_array($main)) {
		$ublocks["main"] = array();
		foreach($main as $b) {
			$ublocks["main"][] = $b;
		}
	}

	if (is_array($right)) {
		$ublocks["right"] = array();
		foreach($right as $b) {
			$ublocks["right"][] = $b;
		}
	}

	setBlocks($name, $ublocks, $setdefault);
}

?>
<script language="JavaScript" type="text/javascript">
<!--
/**
 * Add Block JavaScript function
 *
 * This function adds an option from the add select list to the section select list
 * @author John Finlay
 * @param String section_name the name of the select to add the option to
 * @param String add_name the name of the select to get the option from
 */
function add_block(section_name, add_name) {
	section_select = document.getElementById(section_name);
	add_select = document.getElementById(add_name);
	if ((section_select) && (add_select)) {
		if (add_select.selectedIndex == 0) return false;
		add_option = add_select.options[add_select.selectedIndex];
		section_select.options[section_select.length] = new Option(add_option.text, add_option.value);
	}
}

/**
 * Romove Block JavaScript function
 *
 * This function removes the selected option from the given select list
 * @author John Finlay
 * @param String section_name the name of the select to remove the selected option from
 */
function remove_block(section_name) {
	section_select = document.getElementById(section_name);
	if (section_select) {
		if (section_select.selectedIndex == -1) return false;
		section_select.options[section_select.selectedIndex] = null;
	}
}

/**
 * Move Up Block JavaScript function
 *
 * This function moves the selected option up in the given select list
 * @author John Finlay
 * @param String section_name the name of the select to move the options
 */
function move_up_block(section_name) {
	section_select = document.getElementById(section_name);
	if (section_select) {
		if (section_select.selectedIndex <= 0) return false;
		index = section_select.selectedIndex;
		temp = new Option(section_select.options[index-1].text, section_select.options[index-1].value);
		section_select.options[index-1] = new Option(section_select.options[index].text, section_select.options[index].value);
		section_select.options[index] = temp;
		section_select.selectedIndex = index-1;
	}
}

/**
 * Move Down Block JavaScript function
 *
 * This function moves the selected option down in the given select list
 * @author John Finlay
 * @param String section_name the name of the select to move the options
 */
function move_down_block(section_name) {
	section_select = document.getElementById(section_name);
	if (section_select) {
		if (section_select.selectedIndex < 0) return false;
		if (section_select.selectedIndex >= section_select.length-1) return false;
		index = section_select.selectedIndex;
		temp = new Option(section_select.options[index+1].text, section_select.options[index+1].value);
		section_select.options[index+1] = new Option(section_select.options[index].text, section_select.options[index].value);
		section_select.options[index] = temp;
		section_select.selectedIndex = index+1;
	}
}



/**
 * Move Block from one column to the other JavaScript function
 *
 * This function moves the selected option down in the given select list
 * @author KosherJava
 * @param String add_to_column the name of the select to move the option to
 * @param String remove_from_column the name of the select to remove the option from
 */
function move_left_right_block(add_to_column, remove_from_column) {
	section_select = document.getElementById(remove_from_column);
	add_select = document.getElementById(add_to_column);
	if ((section_select) && (add_select)) {
		add_option = add_select.options[add_select.selectedIndex];
		section_select.options[section_select.length] = new Option(add_option.text, add_option.value);
		add_select.options[add_select.selectedIndex] = null; //remove from list
	}
}

/**
 * Select Options JavaScript function
 *
 * This function selects all the options in the multiple select lists
 * @author John Finlay
 */
function select_options() {
	section_select = document.getElementById('main_select');
	if (section_select) {
		for(i=0; i<section_select.length; i++) {
			section_select.options[i].selected=true;
		}
	}
	section_select = document.getElementById('right_select');
	if (section_select) {
		for(i=0; i<section_select.length; i++) {
			section_select.options[i].selected=true;
		}
	}
	return true;
}

<?php
print "var block_descr = new Array();\n";
foreach($PGV_BLOCKS as $b=>$block) {
	print "block_descr['$b'] = '".preg_replace("/'/", "\\'", $block["descr"])."';\n";
}
?>

/**
 * Show Block Description JavaScript function
 *
 * This function shows a description for the selected option
 * @author John Finlay
 * @param String add_name the name of the select to get the option from
 */
function show_description(add_name) {
	add_select = document.getElementById(add_name);
	instruct = document.getElementById('instructions');
	if (add_select && instruct) {
		if (add_select.selectedIndex == 0) instruct.innerHTML = "";
		else instruct.innerHTML = block_descr[add_select.options[add_select.selectedIndex].value];
	}
}

function parentrefresh() {
	window.opener.refreshpage();
	window.close();
}
//-->
</script>
<?php
if ($command=="user") print "<b>".str2upper($pgv_lang["customize_page"])."</b><br />";
else print "<b>".str2upper($pgv_lang["customize_gedcom_page"])."</b><br />";
print $pgv_lang["portal_config_intructions"];

print "\n<form method=\"post\" action=\"index_edit.php\" onsubmit=\"return select_options();\">\n<input type=\"hidden\" name=\"command\" value=\"$command\" />\n<input type=\"hidden\" name=\"action\" value=\"update\" />\n<input type=\"hidden\" name=\"name\" value=\"$name\" />\n";
print "<table border=\"1\">\n";
print "<tr><td valign=\"top\" class=\"list_value\">\n";
print "<b>".$pgv_lang["main_section"]."</b>";
print "</td>\n";
print "<td class=\"list_value\">";
print "<b>".$pgv_lang["right_section"]."</b>";
print "</td></tr>\n";
print "<tr><td valign=\"top\">\n";
print "<table><tr><td>";
print "<select multiple=\"multiple\" style=\"text-align:left;\" id=\"main_select\" name=\"main[]\" size=\"10\">\n";
//-- start of main content section
foreach($ublocks["main"] as $block) {
	if ($MULTI_MEDIA) print "<option value=\"$block\">".$PGV_BLOCKS[$block]["name"]."</option>\n";
	else if (!$MULTI_MEDIA && $block != "print_random_media") print "<option value=\"$block\">".$PGV_BLOCKS[$block]["name"]."</option>\n";
}
//-- end of main content section
print "</select>\n";
print "</td><td>";
print "<input type=\"button\" class=\"details1\" onclick=\"move_up_block('main_select');\" value=\"".$pgv_lang["move_up"]."\" />\n";
print "<input type=\"button\" class=\"details1\" onclick=\"move_down_block('main_select');\" value=\"".$pgv_lang["move_down"]."\" />\n";
print "<input type=\"button\" class=\"details1\" onclick=\"move_left_right_block('main_select', 'right_select');\" value=\"".$pgv_lang["move_right"]."\" />\n";
print "<input type=\"button\" class=\"details1\" onclick=\"remove_block('main_select');\" value=\"".$pgv_lang["remove"]."\" />\n";

print "</td></tr></table>\n";
print "</td><td valign=\"top\">\n";
print "<table><tr><td>";
print "<select multiple=\"multiple\" style=\"text-align:left;\" id=\"right_select\" name=\"right[]\" size=\"10\">\n";
//-- start of blocks section
foreach($ublocks["right"] as $block) {
	if ($MULTI_MEDIA) print "<option value=\"$block\">".$PGV_BLOCKS[$block]["name"]."</option>\n";
	else if (!$MULTI_MEDIA && $block != "print_random_media") print "<option value=\"$block\">".$PGV_BLOCKS[$block]["name"]."</option>\n";
}
//-- end of blocks section
print "</select>\n";
print "</td><td>";
print "<input type=\"button\" class=\"details1\" onclick=\"move_up_block('right_select');\" value=\"".$pgv_lang["move_up"]."\" />\n";
print "<input type=\"button\" class=\"details1\" onclick=\"move_down_block('right_select');\" value=\"".$pgv_lang["move_down"]."\" />\n";
print "<input type=\"button\" class=\"details1\" onclick=\"move_left_right_block('right_select', 'main_select');\" value=\"".$pgv_lang["move_left"]."\" />\n";
print "<input type=\"button\" class=\"details1\" onclick=\"remove_block('right_select');\" value=\"".$pgv_lang["remove"]."\" />\n";
print "</td></tr></table>\n";
print "</td></tr>\n";
print "<tr><td>\n";
print "<select style=\"text-align:left;\" id=\"main_add\" name=\"main_add\" onchange=\"show_description('main_add');\">\n";
print "<option value=\"\">".$pgv_lang["add_main_block"]."</option>\n";
foreach($PGV_BLOCKS as $b=>$BLOCK) {
	if (!isset($BLOCK["type"])) $BLOCK["type"]=$command;
	if ($MULTI_MEDIA) {
		if ($BLOCK["type"]==$command) print "<option value=\"$b\">".$BLOCK["name"]."</option>\n";
	}
	else if (!$MULTI_MEDIA && $b != "print_random_media") {
		if ($BLOCK["type"]==$command) print "<option value=\"$b\">".$BLOCK["name"]."</option>\n";
	}
}
print "</select>\n";
print "<input type=\"button\" class=\"details1\" onclick=\"add_block('main_select', 'main_add');\" value=\"".$pgv_lang["add"]."\" />\n";
print "</td><td>\n";
print "<select style=\"text-align:left;\" id=\"right_add\" name=\"right_add\" onchange=\"show_description('right_add');\">\n";
print "<option value=\"\">".$pgv_lang["add_right_block"]."</option>\n";
foreach($PGV_BLOCKS as $b=>$BLOCK) {
	if (!isset($BLOCK["type"])) $BLOCK["type"]=$command;
	if ($MULTI_MEDIA) {
		if ($BLOCK["type"]==$command) print "<option value=\"$b\">".$BLOCK["name"]."</option>\n";
	}
	else if (!$MULTI_MEDIA && $b != "print_random_media") {
		if ($BLOCK["type"]==$command) print "<option value=\"$b\">".$BLOCK["name"]."</option>\n";
	}
}
print "</select>\n";
print "<input type=\"button\" class=\"details1\" onclick=\"add_block('right_select', 'right_add');\" value=\"".$pgv_lang["add"]."\" />\n";
print "</td></tr>\n";
print "<tr><td colspan=\"2\">\n";
print "<div id=\"instructions\"></div>\n";
if ((userIsAdmin($uname))&&($command=='user')) {
	print $pgv_lang["use_blocks_for_default"]."<input type=\"checkbox\" name=\"setdefault\" value=\"1\" /><br />\n";
}
print "<input type=\"submit\" value=\"".$pgv_lang["save"]."\" />\n";
print "<input type=\"button\" value=\"".$pgv_lang["reset_default_blocks"]."\" onclick=\"window.location='index_edit.php?command=$command&amp;action=reset&amp;name=".preg_replace("/'/", "\'", $name)."';\" />\n";
print "</td></tr>\n";
print "</table>\n";
print "</form>\n";
print "<br /><br /><br /><center>";
print "<a href=\"#\" onclick=\"parentrefresh();\">".$pgv_lang["close_window"]."</a></center>";
print_simple_footer();
?>