<?php
/**
 * Add media to gedcom file
 *
 * This file allows the user to maintain a seperate table
 * of media files and associate them with individuals in the gedcom
 * and then add these records later.
 * Requires MySQL mode.
 *
 * $Id: addmedia.php,v 1.12 2004/12/03 18:19:09 meliza Exp $
 *
 * 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
 * @package PhpGedView
 * @subpackage Tools
 */
 
/**
 * load config file
 */
require("config.php");
require($PGV_BASE_DIRECTORY.$factsfile["english"]);
if (file_exists($PGV_BASE_DIRECTORY . $factsfile[$LANGUAGE])) require $PGV_BASE_DIRECTORY . $factsfile[$LANGUAGE];

if (empty($ged)) $ged = $GEDCOM;
$GEDCOM = $ged;
require("gdbi_functions.php");

/**
 * get the list of media from the database
 *
 * searches the media table of the database for media items that should be added to the currently
 * active GEDCOM.
 *
 * The medialist that is returned contains the following elements:
 * - $media["ID"] the unique id of this media item in the table
 * - $media["GEDFILE"] the gedcom file the media item should be added to
 * - $media["FILE"] the filename of the media item
 * - $media["INDI"] the gedcom record the media item should be linked to
 * - $media["FORM"] the format of the item (ie JPG, PDF, etc)
 * - $media["_PRIM"] if the media item is the primary image that should appear on charts
 * - $media["TITL"] a title for the item
 * - $media["ORDER"] the order of the item in the gedcom record, this is the order they will be listed on the page
 *
 * @return array a media list array
 */
function get_new_media_list() {
	global $medialist, $GEDCOM;
	global $TBLPREFIX;

	$medialist = array();

	$sql = "SELECT * FROM ".$TBLPREFIX."media WHERE m_gedfile='$GEDCOM' ORDER BY m_indi, m_order";
	$res =& dbquery($sql);
	$ct = $res->numRows();
	while($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)){
		$media = array();
		$media["ID"] = $row["m_id"];
		$media["GEDFILE"] = $row["m_gedfile"];
		$media["FILE"] = $row["m_file"];
		$media["INDI"] = $row["m_indi"];
		$media["FORM"] = $row["m_ext"];
		$media["_PRIM"] = $row["m_ind"];
		$media["TITL"] = $row["m_titl"];
		$media["ORDER"] = $row["m_order"];
		$medialist[] = $media;
	}
	return $medialist;
}


print_simple_header($pgv_lang["add_media_tool"]);
print "\n\t<div class=\"center\"><h2>".$pgv_lang["add_media_tool"]."</h2></div>\n\t";
print $pgv_lang["add_media_descr"]." <b>$ged</b>.<br /><br />";

$sql = "SHOW TABLES";
$res =& dbquery($sql);
$has_media = false;
while($table =& $res->fetchRow()) {
	if ($table[0]==$TBLPREFIX."media") $has_media = true;
}

if (!$has_media) {
	$sql = "CREATE TABLE ".$TBLPREFIX."media (
	m_id int(11) NOT NULL auto_increment,
	m_indi varchar(15) NOT NULL,
	m_ext char(3) NOT NULL,
	m_ind char(1) NOT NULL,
	m_titl varchar(255) NULL,
	m_order int(11) NOT NULL,
	m_file varchar(255) NOT NULL,
	m_gedfile varchar(255) NOT NULL,
	PRIMARY KEY(m_id))";
	$res =& dbquery($sql);
	if ($res) print "Successfully created <i>media</i> table.";
}
else {
	$ssql = "DESCRIBE ".$TBLPREFIX."media";
	$sres =& dbquery($ssql);
	$has_id=false;
	while($field =& $sres->fetchRow()) {
		if ($field[0]=="m_id") $has_id = true;
	}
	if (!$has_id) {
		$sql = "alter table ".$TBLPREFIX."media drop primary key";
		$res =& dbquery($sql);
		$sql = "alter table ".$TBLPREFIX."media ADD COLUMN m_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST";
		$res =& dbquery($sql);
		$sql = "alter table ".$TBLPREFIX."media drop COLUMN m_indiname";
		$res =& dbquery($sql);
		$sql = "alter table ".$TBLPREFIX."media CHANGE COLUMN m_file m_gedfile varchar(255) NOT NULL";
		$res =& dbquery($sql);
		$sql = "alter table ".$TBLPREFIX."media CHANGE COLUMN m_name m_file varchar(255) NOT NULL AFTER m_order";
		$res =& dbquery($sql);
		$res =& dbquery($sql);
		//$sql = "alter table ".$TBLPREFIX."media ADD PRIMARY KEY(m_id)";
		//$res =& dbquery($sql);
		if ($res) print $pgv_lang["media_table_created"];
	}
}

if (empty($action)) $action="showmedia";

if (!isset($m_indi)) $m_name="";
if (!isset($m_ext)) $m_ext="";
if (!isset($m_ind)) $m_ind="";
if (!isset($m_titl)) $m_titl="";
if (!isset($m_order)) $m_order="";
if (!isset($m_file)) $m_file="";

if ($action=="newentry") {
	$sql = "INSERT INTO ".$TBLPREFIX."media VALUES(NULL, '".addslashes($m_indi)."','".addslashes($m_ext)."','".addslashes($m_ind)."','".addslashes($m_titl)."','".addslashes($m_order)."','".addslashes($m_file)."','".addslashes($m_gedfile)."')";
	$res =& dbquery($sql);
	$action = "showmedia";
}
if ($action=="delete") {
	$sql = "DELETE FROM ".$TBLPREFIX."media WHERE m_id=$m_id";
	$res =& dbquery($sql);
	$action = "showmedia";
}

if ($action=="showmedia") {
	$medialist = get_new_media_list();
	if (count($medialist)>0) {
		print "<table class=\"list_table\">\n";
		print "<tr><td class=\"list_label\">".$pgv_lang["delete"]."</td><td class=\"list_label\">".$pgv_lang["title"]."</td><td class=\"list_label\">".$pgv_lang["gedcomid"]."</td>\n";
		print "<td class=\"list_label\">".$factarray["FILE"]."</td><td class=\"list_label\">".$pgv_lang["highlighted"]."</td><td class=\"list_label\">order</td><td class=\"list_label\">gedcom</td></tr>\n";
		foreach($medialist as $media) {
			print "<tr>";
			print "<td class=\"list_value\"><a href=\"addmedia.php?action=delete&m_id=".$media["ID"]."\">delete</a></td>";
			print "<td class=\"list_value\">".$media["TITL"]."</td>";
			print "<td class=\"list_value\">";
			print_list_person($media["INDI"], array(get_person_name($media["INDI"]), $GEDCOM));
			print "</td>";
			print "<td class=\"list_value\">".$media["FILE"]."</td>";
			print "<td class=\"list_value\">".$media["_PRIM"]."</td>";
			print "<td class=\"list_value\">".$media["ORDER"]."</td>";
			print "<td class=\"list_value\">".$media["GEDFILE"]."</td>";
			print "</tr>\n";
		}
		print "</table>\n";
	}
	?>
	<script language="JavaScript" type="text/javascript">
	<!--
	var pasteto;
	function open_find(textbox) {
		pasteto = textbox;
		findwin = window.open('findid.php', '', 'left=50,top=50,width=450,height=450,resizable=1,scrollbars=1');
	}
	function paste_id(value) {
		pasteto.value=value;
	}
	function findMedia(field) {
		pasteto = field;
		window.open('findmedia.php', '', 'top=55,left=55,width=500,height=500,scrollbars=1,resizeable=1');
		return false;
	}
	//-->
	</script>
	<?php
	//-- add a table and form to easily add new values to the table
	print "<form method=\"post\" name=\"newmedia\" action=\"addmedia.php\">\n";
	if (count($medialist)>0) print "<input type=\"button\" value=\"".str_replace("#GEDCOM#", $GEDCOM, $pgv_lang["click_to_add_media"])."\" onclick=\"window.location='addmedia.php?action=addmedia&amp;ged=$GEDCOM';\" /><br /><br />\n";
	print "<input type=\"hidden\" name=\"action\" value=\"newentry\" />\n";
	print "<input type=\"hidden\" name=\"ged\" value=\"$GEDCOM\" />\n";
	print "<table class=\"facts_table\">\n";
	print "<tr><td class=\"facts_label\">".$pgv_lang["gedcomid"]."</td><td class=\"facts_value\"><input type=\"text\" size=\"5\" name=\"m_indi\" />";
	if (check_for_import($GEDCOM)) print "<font size=\"1\"> <a href=\"javascript:open_find(document.newmedia.m_indi);\">".$pgv_lang["find_id"]."</a></font>";
	print "</td></tr>\n";
	print "<tr><td class=\"facts_label\">".$factarray["FILE"]."</td><td class=\"facts_value\"><input type=\"text\" name=\"m_file\" />";
	print "<font size=\"1\"> <a href=\"#\" onclick=\"return findMedia(document.newmedia.m_file);\">".$pgv_lang["find_media"]."</a></font>\n";
	print "</td></tr>\n";
	print "<tr><td class=\"facts_label\">".$pgv_lang["extension"]."</td><td class=\"facts_value\"><input type=\"text\" name=\"m_ext\" value=\"jpg\" /></td></tr>\n";
	print "<tr><td class=\"facts_label\">".$pgv_lang["highlighted"]."</td><td class=\"facts_value\"><select name=\"m_ind\" /><option value=\"Y\">".$pgv_lang["yes"]."</option><option value=\"N\">".$pgv_lang["no"]."</option></select></td></tr>\n";
	print "<tr><td class=\"facts_label\">".$pgv_lang["title"]."</td><td class=\"facts_value\"><input type=\"text\" name=\"m_titl\" /></td></tr>\n";
	print "<tr><td class=\"facts_label\">".$pgv_lang["order"]."</td><td class=\"facts_value\"><input type=\"text\" name=\"m_order\" value=\"0\" /></td></tr>\n";
	print "<tr><td class=\"facts_label\">".$pgv_lang["gedcom_file"]."</td><td class=\"facts_value\"><select name=\"m_gedfile\">";
	foreach($GEDCOMS as $ged=>$gedarray) {
		print "<option value=\"$ged\"";
		if ($ged==$GEDCOM) print " selected=\"selected\"";
		print ">".PrintReady($gedarray["title"])."</option>\n";
	}
	print "</select></td></tr>\n";
	print "</table>\n";
	print "<input type=\"submit\" value=\"".$pgv_lang["add_media_button"]."\" />\n";
	print "</form>\n";
}

if ($action=="addmedia") {
	$medialist = get_new_media_list();
	foreach($medialist as $media) {
		$indirec = find_record_in_file($media["INDI"]);
		$indirec .= "\r\n1 OBJE";
		$indirec .= "\r\n2 FILE ".$media["FILE"];
		$indirec .= "\r\n2 TITL ".$media["TITL"];
		$indirec .= "\r\n2 _PRIM ".$media["_PRIM"];
		$indirec .= "\r\n2 FORM ".$media["FORM"];
		replace_gedrec($media["INDI"], $indirec);
	}
	print "<p>".$pgv_lang["adds_completed"]."</p>\n";
}
print "<p><a href=\"#\" onclick=\"window.close();\">".$pgv_lang["close_window"]."</a></p>\n";

print_simple_footer();
?>