<?

/*
 * This file is part of the 'Science Fair In A Box' project
 * SFIAB Website: http://www.sfiab.ca
 *
 * Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
 * Copyright (C) 2005 James Grant <james@lightbox.org>
 *
 * 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, version 2.
 *
 * 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
?>
<?
require ('../common.inc.php');
require_once ('../user.inc.php');
user_auth_required('committee', 'config');
if (get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GET, 'action') == 'new') {
	send_header(get_value_from_array($_GET, 'action') == 'edit' ? 'Edit Sub-Division' : 'New Sub-Division',
		array('Committee Main' => 'committee_main.php',
			'Science-ation Configuration' => 'config/index.php',
			'Project Sub-Divisions' => 'config/subdivisions.php'),
		'project_sub_divisions');
} else {
	send_header('Project Sub-Divisions',
		array('Committee Main' => 'committee_main.php',
			'Science-ation Configuration' => 'config/index.php'),
		'project_sub_divisions');
}

if (get_value_from_array($_POST, 'action') == 'edit') {
	if (get_value_from_array($_POST, 'id') && get_value_from_array($_POST, 'projectdivisions_id') && get_value_from_array($_POST, 'subdivision')) {
		$q = $pdo->prepare("SELECT id FROM projectsubdivisions WHERE id=? AND year=?");
		$q->execute([$_POST['id'],$config['FAIRYEAR']]);
		if ($q->rowCount() && $_POST['saveid'] != $_POST['id']) {
			echo error(i18n('Sub-Division ID %1 already exists', array($_POST['id'])));
		} else {
			$stmt = $pdo->prepare('UPDATE projectsubdivisions SET '
				. "id=?, "
				. "projectdivisions_id=?, "
				. "subdivision=?"
				. "WHERE id=?");
			$stmt->execute([$_POST['id'],$_POST['projectdivisions_id'],stripslashes($_POST['subdivision']),$_POST['saveid']]);
			echo happy(i18n('Sub-Division successfully saved'));
		}
	} else {
		echo error(i18n('All fields are required'));
	}
}

if (get_value_from_array($_POST, 'action') == 'new') {
	if (get_value_from_array($_POST, 'projectdivisions_id') && get_value_from_array($_POST, 'subdivision')) {
		if (!$_POST['id']) {
			$idq = $pdo->prepare('SELECT MAX(id) AS id FROM projectsubdivisions');
			$idq->execute();
			$idr = $idq->fetch(PDO::FETCH_OBJ);
			$newid = $idr->id + 1;
		} else
			$newid = $_POST['id'];

		$q = $pdo->prepare("SELECT id FROM projectsubdivisions WHERE id=? AND year=?");
		$q->execute([$newid,$config['FAIRYEAR']]);
		if ($q->rowCount()) {
			echo error(i18n('Sub-Division ID %1 already exists', array($newid)));
		} else {
			$stmt = $pdo->prepare('INSERT INTO projectsubdivisions (id,projectdivisions_id,subdivision,year) VALUES (?,?,?,?) ');
			$stmt->execute([$newid,$_POST['projectdivisions_id'],stripslashes($_POST['subdivision']),$config['FAIRYEAR']]);
			echo happy(i18n('Sub-Division successfully added'));
		}
	} else {
		echo error(i18n('All fields except ID are required'));
	}
}

if (get_value_from_array($_GET, 'action') == 'remove' && get_value_from_array($_GET, 'remove')) {
	$stmt = $pdo->prepare("DELETE FROM projectsubdivisions WHERE id=?");
	$stmt->execute([$_GET['remove']]);
	echo happy(i18n('Sub-Division successfully removed'));
}

echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';

if (!(get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GET, 'action') == 'new'))
	echo '<a href="' . $_SERVER['PHP_SELF'] . '?action=new">' . i18n('Add new sub-division') . "</a>\n";

echo '<table class="summarytable">';
echo '<tr>';
echo '<th>' . i18n('Parent Division') . "</th>\n";
echo '<th>' . i18n('ID') . "</th>\n";
echo '<th>' . i18n('Sub-Division') . "</th>\n";
echo '<th>' . i18n('Actions') . "</th>\n";
echo '</tr>';

if (get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GET, 'action') == 'new') {
	echo '<input type="hidden" name="action" value="' . get_value_from_array($_GET, 'action') . "\">\n";
	$divisionr = array();
	if (get_value_from_array($_GET, 'action') == 'edit') {
		echo '<input type="hidden" name="saveid" value="' . get_value_from_array($_GET, 'edit') . "\">\n";
		$q = $pdo->prepare("SELECT * FROM projectsubdivisions WHERE id=? AND year=?");
		$q->execute([get_value_from_array($_GET, 'edit'),$config['FAIRYEAR']]);
		$divisionr = $q->fetch(PDO::FETCH_OBJ);
		$buttontext = 'Save';
	} else if ($_GET['action'] == 'new') {
		$buttontext = 'Add';
	}
	echo '<tr>';
	echo ' <td>';
	echo '<select name="projectdivisions_id">';
	$dq = $pdo->prepare("SELECT * FROM projectdivisions WHERE year=? ORDER BY division");
	$dq->execute([$config['FAIRYEAR']]);
	while ($dr = $dq->fetch(PDO::FETCH_OBJ)) {
		if ($dr->id == $divisionr->projectdivisions_id)
			$sel = 'selected="selected"';
		else
			$sel = '';
		echo "<option $sel value=\"$dr->id\">$dr->division</option>\n";
	}
	echo '</select>';
	echo '</td>';
	echo ' <td><input type="text" size="3" name="id" value="' . get_value_from_array($divisionr, 'id', '') . '"></td>';
	echo ' <td><input type="text" size="30" name="subdivision" value="' . get_value_from_array($divisionr, 'subdivision', '') . '"></td>';
	echo ' <td><input type="submit" value="' . i18n($buttontext) . '"></td>';
	$dq->execute();
	echo '</tr>';
} else {
	$q = $pdo->prepare("SELECT projectsubdivisions.id, 
\t \t\t\tprojectsubdivisions.projectdivisions_id,
				projectsubdivisions.subdivision,
				projectdivisions.division
				FROM 
					projectsubdivisions,
					projectdivisions
				WHERE 
					projectsubdivisions.year=? 
					AND projectdivisions.year=?
					AND projectsubdivisions.projectdivisions_id=projectdivisions.id
				ORDER BY 
					division,subdivision");
	$q->execute([$config['FAIRYEAR'],$config['FAIRYEAR']]);
	show_pdo_errors_if_any($pdo);
	while ($r = $q->fetch(PDO::FETCH_OBJ)) {
		echo '<tr>';
		echo " <td>$r->division</td>";
		echo " <td>$r->id</td>";
		echo " <td>$r->subdivision</td>";
		echo ' <td>';
		echo '<a title="Edit" href="' . $_SERVER['PHP_SELF'] . "?action=edit&amp;edit=$r->id\"><img src=\"" . $config['SFIABDIRECTORY'] . '/images/16/edit.' . $config['icon_extension'] . '" border=0></a>';
		echo '&nbsp; &nbsp;';
		echo '<a title="Remove" onClick="return confirmClick(\'Are you sure you want to remove this division?\');" href="' . $_SERVER['PHP_SELF'] . "?action=remove&amp;remove=$r->id\"><img src=\"" . $config['SFIABDIRECTORY'] . '/images/16/button_cancel.' . $config['icon_extension'] . '" border=0></a>';
		echo ' </td>';
		echo '</tr>';
	}
}
echo '</table>';
if (get_value_from_array($_GET, 'action') == 'new')
	echo '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;' . i18n('Leave ID field blank to auto-assign next available ID');
echo '</form>';

send_footer();
?>