<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: new_infusion_admin.php
| CVS Version: 1.00
| Author: INSERT NAME HERE
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once "../../maincore.php";
require_once THEMES."templates/admin_header.php";

include INFUSIONS."auto_avatar_resizer_infusion/infusion_db.php";



// Check if locale file is available matching the current site locale setting.
if (file_exists(INFUSIONS."auto_avatar_resizer_infusion/locale/".$settings['locale'].".php")) {
	// Load the locale file matching the current site locale setting.
	include INFUSIONS."auto_avatar_resizer_infusion/locale/".$settings['locale'].".php";
} else {
	// Load the infusion's default locale file.
	include INFUSIONS."auto_avatar_resizer_infusion/locale/English.php";
}
require_once "cnf.php";

// FUNCTIONS
/**
* Prints a list/menu containing the registered user groups
* @name printGroupSelects
* @param integer [$selected] Wich group should be selected
* @param boolean [$enablePublic] The recipe the image should be used with.
* @param string [$itemName] The name of the list/menu in the form.
* @return boolean If the user should be able to edit / delete the recipe, it returns true
* @author XtraLars <xtralars@baluba.org>
*/
function printGroupSelects($selected, $enablePublic, $itemName) {
	// Code from forum-administration...
	$post_groups = getusergroups(); 
	$opts = "";
	echo "<select name=\"".$itemName."\" class=\"textbox\" style=\"width:200px\">";
	while(list($key, $user_group) = each($post_groups)){
		if ($user_group['0'] != "0" || $enablePublic) {
			echo "<!-- selected = ".$selected.", user_group = ".$user_group['0']." -->";
			$sel = ($selected == $user_group['0'] ? " selected" : "");
			$opts .= "<option value='".$user_group['0']."'".$sel.">".$user_group['1']."</option>\n";
		}
		//}
	}
	echo $opts;
	echo "</select>";
} 
function printFormatSelects($selected) {
	$formats = array(IMAGETYPE_GIF => "GIF", IMAGETYPE_PNG => "PNG", IMAGETYPE_JPEG => "JPG");
	echo "<select name=\"lstFormat\" class=\"textbox\" style=\"width:200px\">";
	foreach ($formats as $id => $name) {
		if ($id == $selected) {
			echo "<option value=\"".$id."\" selected=\"selected\">".$name."</option>";
		} else {
			echo "<option value=\"".$id."\">".$name."</option>";
		}
	}
	echo "</select>";
}
function printAdminForm() {
	global $locale, $resize_settings;
	opentable($locale['rsze300']);
		echo "<form action=\"auto_avatar_resizer_admin.php?action=settings\" method=\"post\"><center><table width=\"650\" cellpadding=\"0\" cellspacing=\"2\" border=\"0\">";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze301']."</td><td>";
		printGroupSelects($resize_settings['group_id'], false, 'lstAccessGroup');
		echo "</td></tr>";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze303']."</td><td>"
			."<input type=\"text\" name=\"txtFileSize\" value=\"".$resize_settings['maxFileSize']."\" class=\"textbox\" maxlength=\"10\" style=\"width:200px\" /></td></tr>";
		echo "<tr><td colspan=\"2\" align=\"center\"><strong>".$locale['rsze312']."</strong></td></tr>";
		echo "<tr><td></td><td><input type=\"submit\" name=\"btnSubmit\" value=\"".$locale['rsze102']."\" class=\"button\" /></td></tr>";
		echo "</table></center></form>";
	closetable();
	tablebreak();
}
function printTemplateList() {
	global $db_prefix, $locale;
	$sql = "SELECT * FROM ".$db_prefix."auto_avatar_resizer_templates";
	$result = dbquery($sql);
	$altrow = 2;
	
	opentable($locale['rsze310']);
		echo "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" class=\"tbl-border\" >\n";
		echo "<tr>"
			." <td class=\"tbl".$altrow."\"><strong>".$locale['rsze305']."</strong></td>"
			." <td class=\"tbl".$altrow."\"><strong>".$locale['rsze306']."</strong></td>"
			." <td class=\"tbl".$altrow."\" align=\"center\"><strong>".$locale['rsze307']."</strong></td>"
			." <td class=\"tbl".$altrow."\" align=\"center\"><strong>".$locale['rsze308']."</strong></td>"
			." <td class=\"tbl".$altrow."\" align=\"center\"><strong>".$locale['rsze309']."</strong></td>"
			." <td class=\"tbl".$altrow."\" align=\"right\"><strong>".$locale['rsze104']."</strong></td>"
			."</tr>";
		while($data = dbarray($result)) {
			if ($altrow == 2) { $altrow = 1; } else { $altrow = 2; }
			switch ((int) $data['tem_format']) {
				case IMAGETYPE_GIF:
					$type = "GIF";
					break;
				case IMAGETYPE_PNG:
					$type = "PNG";
					break;
				case IMAGETYPE_JPEG:
					$type = "JPG";
					break;
				default:
					$type = "&nbsp;";
					break;
			}

			echo "<tr>"
				."<td class=\"tbl".$altrow."\">".$data['tem_name']."</td>"
				."<td class=\"tbl".$altrow."\">".$data['tem_description']."</td>"
				."<td class=\"tbl".$altrow."\" align=\"center\">".$data['tem_maxX']."</td>"
				."<td class=\"tbl".$altrow."\" align=\"center\">".$data['tem_maxY']."</td>"
				."<td class=\"tbl".$altrow."\" align=\"center\">".$type."</td>"
				."<td class=\"tbl".$altrow."\" align=\"right\">"
				."<a href=\"auto_avatar_resizer_admin.php?action=edit&amp;temid=".$data['tem_id']."\">".$locale['rsze100']."</a> - "
				."<a href=\"auto_avatar_resizer_admin.php?action=delete&amp;temid=".$data['tem_id']."\">".$locale['rsze103']."</a>"
				."</td></tr>";
		}
		echo "</table>";
	closetable();
	tablebreak();
}
function printTemplateForm($id, $name, $descr, $maxW, $maxH, $format, $action, $error) {
	global $locale;
	if ($action == "add") {
		$button = $locale['rsze101'];
		$header = $locale['rsze304'];
	} else if ($action == "edited") {
		$button = $locale['rsze102'];
		$header = $locale['rsze311'];
	}
	opentable($header);
		echo "<form action=\"auto_avatar_resizer_admin.php?action=".$action."\" method=\"post\"><center><table width=\"650\" cellpadding=\"0\" cellspacing=\"2\" border=\"0\">";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze305']." : </td><td>"
			."<input type=\"text\" name=\"txtName\" value=\"".$name."\" class=\"textbox\" maxlength=\"35\" style=\"width:200px\" /></td></tr>";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze306']." : </td><td>"
			."<input type=\"text\" name=\"txtDescr\" value=\"".$descr."\" class=\"textbox\" style=\"width:200px\" /></td></tr>";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze307']." : </td><td>"
			."<input type=\"text\" name=\"txtW\" value=\"".$maxW."\" class=\"textbox\" maxlength=\"4\" style=\"width:200px\" /></td></tr>";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze308']." : </td><td>"
			."<input type=\"text\" name=\"txtH\" value=\"".$maxH."\" class=\"textbox\" maxlength=\"4\" style=\"width:200px\" /></td></tr>";
		echo "<tr><td width=\"300\" align=\"right\" >".$locale['rsze309']." : </td><td>";
		printFormatSelects($format);
		echo "</td></tr>";
		echo "<tr><td><strong>".$error."</strong></td><td>"
			."<input type=\"hidden\" name=\"hidTID\" value=\"".$id."\" />"
			."<input type=\"submit\" name=\"btnSubmit\" value=\"".$button."\" class=\"button\" /></td></tr>";
		echo "</table></center></form>";
	closetable();
	tablebreak();
}
function resetHeader() {
	header("Location:auto_avatar_resizer_admin.php");
}
// START
$resize_settings = dbarray(dbquery("SELECT * FROM ".$db_prefix."auto_avatar_resizer_settings"));
if (isset($_GET['action'])) {
	$action = trim(stripinput($_GET['action']));
	switch ($action) {
		case "edit":
			printAdminForm();
			$id = isNum($_GET['temid']) ? (int) $_GET['temid'] : -1;
			if ($id != -1) {
				$sql = "SELECT * FROM ".$db_prefix."auto_avatar_resizer_templates WHERE tem_id=".$id."";
				$result = dbquery($sql);
				if (dbrows($result) == 1) {
					$data = dbarray($result);
					printTemplateForm($data['tem_id'], $data['tem_name'], $data['tem_description'], $data['tem_maxX'], $data['tem_maxY'], $data['tem_format'], "edited", "");
				} else {
					printTemplateForm("", "", "", "", "", "", "add", "");
				}
			}
			printTemplateList();
			break;
		case "edited":
			$id = isNum($_POST['hidTID']) ? (int) $_POST['hidTID'] : -1;
			$format = isNum($_POST['lstFormat']) ? (int) $_POST['lstFormat'] : IMAGETYPE_PNG;
			$name = trim(stripinput($_POST['txtName']));
			$descr = trim(stripinput($_POST['txtDescr']));
			$maxW = isNum($_POST['txtW']) ? (int) $_POST['txtW'] : -1;
			$maxH = isNum($_POST['txtH']) ? (int) $_POST['txtH'] : -1;
			$error = "";
			if ($id == -1) {
				$error .= "<br />".$locale['rsze958'];
			}
			if ($maxW < 1) {
				$error .= "<br />".$locale['rsze954'];
				$maxW = "";
			}
			if ($maxH < 1) {
				$error .= "<br />".$locale['rsze955'];
				$maxH = "";
			}
			if ($name == "") {
				$error .= "<br />".$locale['rsze956'];
			}
			if ($descr == "") {
				$error .= "<br />".$locale['rsze957'];
			}
			if ($error == "") {
				$sql = "UPDATE ".$db_prefix."auto_avatar_resizer_templates SET tem_name='".$name."', tem_description='".$descr."', tem_maxX=".$maxW.", tem_maxY=".$maxH.", tem_format=".$format." WHERE tem_id=".$id;
				dbquery($sql);
				resetHeader();
			} else {
				printAdminForm();
				printTemplateForm("", $name, $descr, $maxW, $maxH, $format, "edited", $error);
				printTemplateList();
			}
			break;
		case "delete":
			$id = isNum($_GET['temid']) ? (int) $_GET['temid'] : -1;
			if ($id != -1) {
				$sql = "DELETE FROM ".$db_prefix."auto_avatar_resizer_templates WHERE tem_id=".$id;
				dbquery($sql);
			}
			resetHeader();
			break;
		case "add":
			$format = isNum($_POST['lstFormat']) ? (int) $_POST['lstFormat'] : IMAGETYPE_PNG;
			$name = trim(stripinput($_POST['txtName']));
			$descr = trim(stripinput($_POST['txtDescr']));
			$maxW = isNum($_POST['txtW']) ? (int) $_POST['txtW'] : -1;
			$maxH = isNum($_POST['txtH']) ? (int) $_POST['txtH'] : -1;
			$error = "";
			if ($id == -1) {
				$error .= "<br />".$locale['rsze958'];
			}
			if ($maxW < 1) {
				$error .= "<br />".$locale['rsze954'];
				$maxW = "";
			}
			if ($maxH < 1) {
				$error .= "<br />".$locale['rsze955'];
				$maxH = "";
			}
			if ($name == "") {
				$error .= "<br />".$locale['rsze956'];
			}
			if ($descr == "") {
				$error .= "<br />".$locale['rsze957'];
			}
			if ($error == "") {
				$sql = "INSERT INTO ".$db_prefix."auto_avatar_resizer_templates (tem_name, tem_description, tem_maxX, tem_maxY, tem_format)"
					  ." VALUES ('".$name."', '".$descr."', ".$maxW.", ".$maxH.", ".$format.")";
				$result = dbquery($sql);
				resetHeader();
			} else {
				printAdminForm();
				printTemplateForm("", $name, $descr, $maxW, $maxH, $format, "add", $error);
				printTemplateList();
			}


			break;
		case "settings":
			$group = isNum($_POST['lstAccessGroup']) ? (int) $_POST['lstAccessGroup'] : 101;
			$buffer = isNum($_POST['txtBuffer']) ? (int) $_POST['txtBuffer'] : 5;
			$maxFS = isNum($_POST['txtFileSize']) ? (int) $_POST['txtFileSize'] : 3000000;
			$sql = "UPDATE ".$db_prefix."auto_avatar_resizer_settings SET group_id=$group, buffer_length=$buffer, maxFileSize=$maxFS";
			$result = dbquery($sql);
			$resize_settings = dbarray(dbquery("SELECT * FROM ".$db_prefix."auto_avatar_resizer_settings"));
			resetHeader();
			break;
		default:
			break;
	}
} else {
	// don't put resetHeader() here!!!
	printAdminForm();
	printTemplateForm("", "", "", "", "", "", "add", "");
	printTemplateList();

}
require_once THEMES."templates/footer.php";
?>