Implement the fundraising goals editor on the setup screen

This commit is contained in:
james 2009-10-02 20:47:04 +00:00
parent ce9446d66a
commit abd5f1fcca

View File

@ -3,8 +3,7 @@
This file is part of the 'Science Fair In A Box' project This file is part of the 'Science Fair In A Box' project
SFIAB Website: http://www.sfiab.ca SFIAB Website: http://www.sfiab.ca
Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org> Copyright (C) 2009 James Grant <james@lightbox.org>
Copyright (C) 2008 James Grant <james@lightbox.org>
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public modify it under the terms of the GNU General Public
@ -51,22 +50,44 @@
echo "<div id=\"level_$r->id\">\n"; echo "<div id=\"level_$r->id\">\n";
echo "<form id=\"level_form_$r->id\" onsubmit=\"return level_save($r->id)\">\n"; echo "<form id=\"level_form_$r->id\" onsubmit=\"return level_save($r->id)\">\n";
echo "<input type=\"hidden\" name=\"id\" value=\"$r->id\">\n"; echo "<input type=\"hidden\" name=\"id\" value=\"$r->id\">\n";
echo i18n("Level Name").": <input type=\"text\" name=\"level\" value=\"$r->level\"><br />"; echo "<table style=\"width: 100%;\">";
echo i18n("Value Range").": \$<input size=\"5\" type=\"text\" name=\"min\" value=\"$r->min\"> to \$<input size=\"5\" type=\"text\" name=\"max\" value=\"$r->max\"><br />\n"; echo "<tr><td>";
echo i18n("Level Name").":</td><td><input type=\"text\" size=\"40\" name=\"level\" value=\"$r->level\"></td></tr>\n";
echo "<tr><td>";
echo i18n("Value Range").":</td><td>\$<input size=\"5\" type=\"text\" name=\"min\" value=\"$r->min\"> to \$<input size=\"5\" type=\"text\" name=\"max\" value=\"$r->max\"><br />\n";
echo "</td></tr>\n";
echo "<tr><td colspan=\"2\">";
echo i18n("Description/Benefits").":<br /><textarea name=\"description\" rows=\"4\" style=\"width: 100%;\">".htmlspecialchars($r->description)."</textarea>"; echo i18n("Description/Benefits").":<br /><textarea name=\"description\" rows=\"4\" style=\"width: 100%;\">".htmlspecialchars($r->description)."</textarea>";
echo "<input type=\"submit\" value=\"".i18n("Save")."\" >"; echo "</td></tr>\n";
echo "</form>\n"; echo "</table>\n";
echo "<table style=\"width: 100%;\"><tr><td style=\"width: 50%; text-align: center;\">";
echo "<input type=\"submit\" value=\"".i18n("Save Level")."\" >";
echo "</td><td style=\"width: 50%; text-align: right;\">";
echo "<input type=\"button\" value=\"".i18n("Delete Level")."\" onclick=\"return level_delete($r->id)\" >";
echo "</td></tr></table>\n";
echo "</form>";
echo "</div>\n"; echo "</div>\n";
$x++;
} }
echo "<h3><a href=\"#\">Create New Level</a></h3>\n"; echo "<h3><a href=\"#\">Create New Level</a></h3>\n";
echo "<div id=\"level_new\">\n"; echo "<div id=\"level_new\">\n";
echo "<form id=\"level_form\" onsubmit=\"return level_save()\">\n"; echo "<form id=\"level_form\" onsubmit=\"return level_save()\">\n";
echo i18n("Level Name").": <input type=\"text\" name=\"level\"><br />";
echo i18n("Value Range").": \$<input size=\"5\" type=\"text\" name=\"min\"> to \$<input size=\"5\" type=\"text\" name=\"max\"><br />\n"; echo "<table style=\"width: 100%;\">";
echo "<tr><td>";
echo i18n("Level Name").":</td><td><input type=\"text\" size=\"40\" name=\"level\"></td></tr>\n";
echo "<tr><td>";
echo i18n("Value Range").":</td><td>\$<input size=\"5\" type=\"text\" name=\"min\"> to \$<input size=\"5\" type=\"text\" name=\"max\"><br />\n";
echo "</td></tr>\n";
echo "<tr><td colspan=\"2\">";
echo i18n("Description/Benefits").":<br /><textarea name=\"description\" rows=\"4\" style=\"width: 100%;\"></textarea>"; echo i18n("Description/Benefits").":<br /><textarea name=\"description\" rows=\"4\" style=\"width: 100%;\"></textarea>";
echo "<input type=\"submit\" value=\"".i18n("Save")."\">"; echo "</td></tr>\n";
echo "</table>\n";
echo "<table style=\"width: 100%;\"><tr><td style=\"width: 50%; text-align: center;\">";
echo "<input type=\"submit\" value=\"".i18n("Create Level")."\">";
echo "</td><td style=\"width: 50%; text-align: right;\">";
echo "</td></tr></table>\n";
echo "</form>\n"; echo "</form>\n";
echo "</div>\n"; echo "</div>\n";
@ -76,14 +97,76 @@
break; break;
case "goals": case "goals":
$q=mysql_query("SELECT * FROM fundraising_goals WHERE fiscalyear='{$config['FISCALYEAR']}' ORDER BY name");
echo "<div id=\"goalaccordion\" style=\"width: 75%;\">\n";
while($r=mysql_fetch_object($q)) {
echo "<h3><a href=\"#\">$r->name (".format_money($r->budget,false).") Deadline: ".format_date($r->deadline)."</a></h3>\n";
echo "<div id=\"goal_$r->id\">\n";
echo "<form id=\"goal_form_$r->id\" onsubmit=\"return goal_save($r->id)\">\n";
echo "<input type=\"hidden\" name=\"id\" value=\"$r->id\">\n";
echo "<table style=\"width: 100%;\">";
echo "<tr><td>";
echo i18n("Goal Name").":</td><td><input type=\"text\" size=\"40\" name=\"name\" value=\"$r->name\"></td></tr>\n";
echo "<tr><td>";
echo i18n("Budget Amount").":</td><td>\$<input size=\"5\" type=\"text\" name=\"budget\" value=\"$r->budget\"></td></tr>";
echo "<tr><td>";
echo i18n("Deadline").":</td><td><input size=\"9\" type=\"text\" name=\"deadline\" value=\"$r->deadline\"></td></tr>";
echo "<tr><td colspan=\"2\">";
echo i18n("Description").":<br /><textarea name=\"description\" rows=\"4\" style=\"width: 100%;\">".htmlspecialchars($r->description)."</textarea>";
echo "</td></tr>\n";
echo "</table>\n";
echo "<table style=\"width: 100%;\"><tr><td style=\"width: 50%; text-align: center;\">";
echo "<input type=\"submit\" value=\"".i18n("Save Goal")."\" >";
echo "</td><td style=\"width: 50%; text-align: right;\">";
echo "<input type=\"button\" value=\"".i18n("Delete Goal")."\" onclick=\"return goal_delete($r->id)\" >";
echo "</td></tr></table>\n";
echo "</form>";
echo "</div>\n";
}
echo "<h3><a href=\"#\">Create New Goal</a></h3>\n";
echo "<div id=\"goal_new\">\n";
echo "<form id=\"goal_form\" onsubmit=\"return goal_save()\">\n";
echo "<table style=\"width: 100%;\">";
echo "<tr><td>";
echo i18n("Goal Name").":</td><td><input type=\"text\" size=\"40\" name=\"name\"></td></tr>\n";
echo "<tr><td>";
echo i18n("Budget Amount").":</td><td>\$<input size=\"5\" type=\"text\" name=\"budget\"></td></tr>";
echo "<tr><td>";
echo i18n("Deadline").":</td><td><input size=\"9\" type=\"text\" name=\"deadline\"></td></tr>";
echo "<tr><td colspan=\"2\">";
echo i18n("Description").":<br /><textarea name=\"description\" rows=\"4\" style=\"width: 100%;\"></textarea>";
echo "</td></tr>\n";
echo "</table>\n";
echo "<table style=\"width: 100%;\"><tr><td style=\"width: 50%; text-align: center;\">";
echo "<input type=\"submit\" value=\"".i18n("Create Goal")."\">";
echo "</td><td style=\"width: 50%; text-align: right;\">";
echo "</td></tr></table>\n";
echo "</form>\n";
echo "</div>\n";
echo "</div>\n";
exit;
break; break;
} }
switch($_GET['action']) { switch($_GET['action']) {
case "level_save": case "level_save":
$id=$_POST['id']; $id=$_POST['id'];
if($id) { if(! ($_POST['level'] && $_POST['min'] && $_POST['max'])) {
error_("Level name, minimum and maximum value range are required");
exit;
}
if($_POST['min']>=$_POST['max']) {
error_("Value range minimum must be smaller than range maximum");
exit;
}
if($id) {
mysql_query("UPDATE fundraising_donor_levels SET mysql_query("UPDATE fundraising_donor_levels SET
min='".mysql_real_escape_string($_POST['min'])."', min='".mysql_real_escape_string($_POST['min'])."',
max='".mysql_real_escape_string($_POST['max'])."', max='".mysql_real_escape_string($_POST['max'])."',
@ -94,21 +177,82 @@
happy_("Level Saved"); happy_("Level Saved");
} }
else { else {
if($_POST['level'] && $_POST['min'] && $_POST['max']) { mysql_query("INSERT INTO fundraising_donor_levels (`level`,`min`,`max`,`description`,`fiscalyear`) VALUES (
mysql_query("INSERT INTO fundraising_donor_levels (`level`,`min`,`max`,`description`,`fiscalyear`) VALUES ( '".mysql_real_escape_string($_POST['level'])."',
'".mysql_real_escape_string($_POST['level'])."', '".mysql_real_escape_string($_POST['min'])."',
'".mysql_real_escape_string($_POST['min'])."', '".mysql_real_escape_string($_POST['max'])."',
'".mysql_real_escape_string($_POST['max'])."', '".mysql_real_escape_string($_POST['description'])."',
'".mysql_real_escape_string($_POST['description'])."', '{$config['FISCALYEAR']}')");
'{$config['FISCALYEAR']}')"); happy_("Level Created");
happy_("Level Created");
}
else {
error_("Level name, minimum and maximum value range are required");
}
} }
exit; exit;
break; break;
case "level_delete":
$id=$_POST['id'];
mysql_query("DELETE FROM fundraising_donor_levels WHERE id='$id' AND fiscalyear='{$config['FISCALYEAR']}'");
happy_("Level Deleted");
exit;
break;
case "goal_save":
$id=$_POST['id'];
if(! ($_POST['name'] && $_POST['budget'])) {
error_("Goal name and budget are required");
exit;
}
if($id) {
mysql_query("UPDATE fundraising_goals SET
budget='".mysql_real_escape_string($_POST['budget'])."',
deadline='".mysql_real_escape_string($_POST['deadline'])."',
name='".mysql_real_escape_string($_POST['name'])."',
description='".mysql_real_escape_string($_POST['description'])."'
WHERE id='$id' AND fiscalyear='{$config['FISCALYEAR']}'
");
happy_("Goal Saved");
}
else {
$goal=strtolower($_POST['name']);
$goal=ereg_replace("[^a-z]","",$goal);
$q=mysql_query("SELECT * FROM fundraising_goals WHERE goal='$goal' AND fiscalyear='{$config['FISCALYEAR']}'");
echo mysql_error();
if(mysql_num_rows($q)) {
error_("The automatically generated goal key (%1) generated from (%2) is not unique. Please try a different Goal Name",array($goal,$_POST['name']));
exit;
}
mysql_query("INSERT INTO fundraising_goals (`goal`,`name`,`budget`,`deadline`,`description`,`fiscalyear`) VALUES (
'".mysql_real_escape_string($goal)."',
'".mysql_real_escape_string($_POST['name'])."',
'".mysql_real_escape_string($_POST['budget'])."',
'".mysql_real_escape_string($_POST['deadline'])."',
'".mysql_real_escape_string($_POST['description'])."',
'{$config['FISCALYEAR']}')");
happy_("Goal Created");
}
exit;
break;
case "goal_delete":
$id=$_POST['id'];
//they cant delete system ones
$q=mysql_query("SELECT * FROM fundraising_goals WHERE id='$id' AND fiscalyear='{$config['FISCALYEAR']}'");
if(!$r=mysql_fetch_object($q)) {
error_("Invalid goal to delete");
exit;
}
if($r->system=="yes") {
error_("Fundraising goals created automatically and used by the system cannot be deleted");
exit;
}
$q=mysql_query("SELECT * FROM fundraising_donations WHERE fundraising_goal='$r->goal' AND fiscalyear='{$config['FISCALYEAR']}'");
if(mysql_num_rows($q)) {
error_("This goal already has donations assigned to it, it cannot be deleted");
exit;
}
mysql_query("DELETE FROM fundraising_goals WHERE id='$id' AND fiscalyear='{$config['FISCALYEAR']}'");
happy_("Goal Deleted");
exit;
break;
} }
@ -158,10 +302,40 @@ function level_save(id) {
return false; return false;
} }
function update_goals() { function level_delete(id) {
$("#editor_tab_levels").load("fundraising_setup.php?gettab=goals"); if(confirmClick('Are you sure you want to delete this fundraising level?')) {
var f=$("#level_form_"+id);
$("#debug").load("fundraising_setup.php?action=level_delete",f.serializeArray(), function() { update_levels(); });
}
return false;
} }
function update_goals() {
$("#editor_tab_goals").load("fundraising_setup.php?gettab=goals",null,
function() {
$("#goalaccordion").accordion();
$("[name=deadline]").datepicker({ dateFormat: 'yy-mm-dd'});
}
);
}
function goal_save(id) {
if(id) var f=$("#goal_form_"+id);
else var f=$("#goal_form");
$("#debug").load("fundraising_setup.php?action=goal_save",f.serializeArray(), function() { update_goals(); });
return false;
}
function goal_delete(id) {
if(confirmClick('Are you sure you want to delete this fundraising goal?')) {
var f=$("#goal_form_"+id);
$("#debug").load("fundraising_setup.php?action=goal_delete",f.serializeArray(), function() { update_goals(); });
}
return false;
}
</script> </script>
<div id="setup" style="width: 780px;"> <div id="setup" style="width: 780px;">