<?

/*
 * This file is part of the Science-ation project
 * Science-ation Website: https://science-ation.ca
 *
 * This file was part of the 'Science Fair In A Box' project
 *
 *
 * Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
 * Copyright (C) 2005 James Grant <james@lightbox.org>
 * Copyright (C) 2024 AlgoLibre Inc. <science-ation@algolibre.io>
 *
 * 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.
 */

// THIS FILE IS NEEDED to fix a bug in the rollover script that some people might have already used
// the prizes werent properly rolled over, so if it detects that there are no prizes this year but there were
// prizes last year, it re-rolls the prizes over properly.
// it only does this if the number of awards matchces exactly (aka hasnt been modified yet since the rollover)
// it is safe to include this script at any point, since it does all the checks required.
// this file will eventually be deleted

// we know the years needed, so hardcode them in
$currentfairyear = 2007;
$newfairyear = 2008;

// first make sure they have indeed done the rollover...
if ($config['FAIRYEAR'] == 2008) {
	// make sure the number of awards are identical (aka they havent added any new ones)
	$nq1 = $pdo->prepare("SELECT * FROM award_awards WHERE year=?");
	$nq1->execute([$newfairyear]);
	$nq2 = $pdo->prepare("SELECT * FROM award_awards WHERE year=?");
	$nq2->execute([$currentfairyear]);
	if ($nq1->rowCount() == $nq2->rowcount()) {
		$npq1 = $pdo->prepare("SELECT * FROM award_prizes WHERE year?");
		$npq1->execute([$newfairyear]);
		$npq2 = $pdo->prepare("SELECT * FROM award_prizes WHERE year=?");
		$npq2->execute([$currentfairyear]);

		if ($npq2->rowCount() > 0 && $npq1->rowCount() == 0) {
			echo '<br />';
			echo notice(i18n('A BUG WAS IDENTIFIED IN YOUR PREVIOUS YEAR ROLLOVER WHICH CAUSED AWARD PRIZES TO NOT BE ROLLED OVER PROPERLY.  THEY ARE NOW BEING RE-ROLLED OVER WITH THE PROPER PRIZE INFORMATION.  THIS WILL ONLY HAPPEN ONCE.')) . '<br />';
			$stmt = $pdo->prepare("DELETE FROM award_awards WHERE year=?");
			$stmt->execute([$newfairyear]);
			$stmt = $pdo->prepare("DELETE FROM award_prizes WHERE year=?");
			$stmt->execute([$newfairyear]);
			$stmt = $pdo->prepare("DELETE FROM award_contacts WHERE year=?");
			$stmt->execute([$newfairyear]);
			$stmt = $pdo->prepare("DELETE FROM award_types WHERE year=?");
			$stmt->execute([$newfairyear]);
			$stmt = $pdo->prepare("DELETE FROM award_awards_projectcategories WHERE year=?");
			$stmt->execute([$newfairyear]);
			$stmt = $pdo->prepare("DELETE FROM award_awards_projectdivisions WHERE year=?");
			$stmt->execute([$newfairyear]);

			echo i18n('Rolling awards') . '<br />';
			// awards
			$q = $pdo->prepare("SELECT * FROM award_awards WHERE year=?");
			$q->execute([$currentfairyear]);
			show_pdo_errors_if_any($pdo);
			while ($r = $q->fetch(PDO::FETCH_OBJ)) {
				$stmt = $pdo->prepare("INSERT INTO award_awards (award_sponsors_id,award_types_id,name,criteria,presenter,`order`,year,excludefromac,cwsfaward) VALUES (
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?)");
				$stmt->execute([$r->award_sponsors_id,$r->award_types_i ,$r->name,$r->criteria,$r->presenter,$r->order,$newfairyear,$r->excludefromac,$r->cwsfaward ]);
				$award_awards_id = $pdo->lastInsertId();

				$q2 = $pdo->prepare("SELECT * FROM award_awards_projectcategories WHERE year=? AND award_awards_id=?");
				$q2->execute([$currentfairyear,$r->id]);
				show_pdo_errors_if_any($pdo);
				while ($r2 = $q2->fetch(PDO::FETCH_OBJ)) {
					$stmt = $pdo->prepare("INSERT INTO award_awards_projectcategories (award_awards_id,projectcategories_id,year) VALUES (
				?,
				?,
				?)");
					$stmt->execute([$award_awards_id,$r2->projectcategories_id,$newfairyear]);
				}

				$q2 = $pdo->prepare("SELECT * FROM award_awards_projectdivisions WHERE year=? AND award_awards_id=?");
				$q2->execute([$currentfairyear,$r->id]);
				show_pdo_errors_if_any($pdo);
				while ($r2 = $q2->fetch(PDO::FETCH_OBJ)) {
					$stmt = $pdo->prepare("INSERT INTO award_awards_projectdivisions (award_awards_id,projectdivisions_id,year) VALUES (
				?,
				?,
				?");
					$stmt->execute([$award_awards_id,$r2->projectdivisions_id,$newfairyear]);
				}

				echo i18n('&nbsp; Rolling award prizes') . '<br />';
				$q2 = $pdo->prepare("SELECT * FROM award_prizes WHERE year=? AND award_awards_id=?");
				$q2->execute([$currentfairyear,$r->id]);
				show_pdo_errors_if_any($pdo);
				while ($r2 = $q2->fetch(PDO::FETCH_OBJ)) {
					$stmt = $pdo->prepare("INSERT INTO award_prizes (award_awards_id,cash,scholarship,`value`,prize,number,`order`,year,excludefromac) VALUES (
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?)");
				}
			}
			$q2->execute([$award_awards_id,$r2->cash,$r2->scholarship,$r2->value,$r2->prize,$r2->number,$r2->order,$newfairyear,$r2->excludefromac]);
			echo i18n('Rolling award contacts') . '<br />';
			// award contacts
			$q = $pdo->prepare("SELECT * FROM award_contacts WHERE year=?");
			$q->execute([$currentfairyear]);
			show_pdo_errors_if_any($pdo);
			while ($r = $q->fetch(PDO::FETCH_OBJ))
				$stmt = $pdo->prepare("INSERT INTO award_contacts (award_sponsors_id,salutation,firstname,lastname,position,email,phonehome,phonework,phonecell,fax,notes,year) VALUES (
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?)");

			$stmt->execute([$r->award_sponsors_id,$r->salutation,$r->firstname,$r->lastname,$r->position,$r->email,$r->phonehome,$r->phonework,$r->phonecell,$r->fax,$r->notes,$newfairyear]);
			echo i18n('Rolling award types') . '<br />';
			// award types
			$q = $pdo->prepare("SELECT * FROM award_types WHERE year=?");
			$q->execute([$currentfairyear]);
			show_pdo_errors_if_any($pdo);
			while ($r = $q->fetch(PDO::FETCH_OBJ))
				$stmt = $pdo->prepare("INSERT INTO award_types (id,type,`order`,year) VALUES (
				?,
				?,
				?,
				?)");
			$stmt->execute([$r->id,$r->type,$r->order,$newfairyear]);
		}
	}
}
?>