2006-08-01 19:43:15 +00:00
|
|
|
<?
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
?>
|
|
|
|
<?
|
|
|
|
|
2006-08-11 18:53:10 +00:00
|
|
|
function config_editor_load($category, $year)
|
2006-08-01 19:43:15 +00:00
|
|
|
{
|
2006-08-11 18:53:10 +00:00
|
|
|
$query = "SELECT * FROM config WHERE year='$year' AND category='$category' ORDER BY ord";
|
2006-08-01 19:43:15 +00:00
|
|
|
$q = mysql_query($query);
|
|
|
|
print(mysql_error());
|
|
|
|
|
|
|
|
$var = array();
|
|
|
|
while($r=mysql_fetch_object($q)) {
|
|
|
|
$var[$r->var]['val'] = $r->val;
|
|
|
|
$var[$r->var]['desc'] = $r->description;
|
2006-08-11 18:53:10 +00:00
|
|
|
$var[$r->var]['category'] = $r->category;
|
|
|
|
$var[$r->var]['ord'] = $r->ord;
|
2007-01-20 22:23:44 +00:00
|
|
|
$var[$r->var]['type'] = $r->type;
|
|
|
|
$var[$r->var]['type_values'] = $r->type_values;
|
2006-08-01 19:43:15 +00:00
|
|
|
}
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
function config_editor_parse_from_http_headers($array_name)
|
|
|
|
{
|
|
|
|
$ans = array();
|
|
|
|
if(!is_array($_POST[$array_name])) return $ans;
|
|
|
|
|
|
|
|
$keys = array_keys($_POST[$array_name]);
|
|
|
|
foreach($keys as $id) {
|
|
|
|
$ans[$id] = stripslashes($_POST[$array_name][$id]);
|
|
|
|
}
|
|
|
|
return $ans;
|
|
|
|
}
|
|
|
|
|
2006-09-03 21:36:28 +00:00
|
|
|
function config_editor_require_vars($category, $year, $varlist)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
foreach($varlist as $v) {
|
|
|
|
if(isset($config[$v])) continue;
|
|
|
|
|
|
|
|
/* FInd var with year = -1 */
|
|
|
|
$q = mysql_query("SELECT * FROM config WHERE ".
|
|
|
|
"var='$v' AND year='-1'");
|
|
|
|
if(mysql_num_rows($q) != 1) {
|
|
|
|
/* Insert a dummy */
|
|
|
|
mysql_query("INSERT INTO `config` (`var`, `val`,
|
|
|
|
`description`, `category`, `year`, `ord`)
|
|
|
|
VALUES ('$v', '', '',
|
|
|
|
'$category', $year, 99999)");
|
|
|
|
} else {
|
|
|
|
$r = mysql_fetch_object($q);
|
|
|
|
mysql_query("INSERT INTO `config` (`var`, `val`,
|
|
|
|
`description`, `category`, `year`, `ord`)
|
|
|
|
VALUES ('$v', '{$r->val}', '{$r->description}',
|
|
|
|
'$category', '$year', {$r->ord})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-01 19:43:15 +00:00
|
|
|
/* A complete question editor. Just call it with the
|
|
|
|
* section you want to edit, a year, the array_name to use for
|
|
|
|
* POSTing and GETting the questions (so you can put more than one
|
|
|
|
* edtior on a single page), and give it $_SERVER['PHP_SELF'], because
|
|
|
|
* php_self inside this function is this file.
|
|
|
|
* FUTURE WORK: it would be nice to hide the order, and just implement
|
|
|
|
* a bunch of up/down arrows, and dynamically compute the order for
|
|
|
|
* all elements */
|
2006-08-11 18:53:10 +00:00
|
|
|
function config_editor($category, $year, $array_name, $self)
|
2006-08-01 19:43:15 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if($_POST['action']=="update") {
|
|
|
|
|
|
|
|
$var = config_editor_parse_from_http_headers($array_name);
|
|
|
|
$varkeys = array_keys($var);
|
|
|
|
foreach($varkeys as $k) {
|
|
|
|
$val = mysql_escape_string(stripslashes($var[$k]));
|
|
|
|
$v = mysql_escape_string(stripslashes($k));
|
2006-12-06 17:58:40 +00:00
|
|
|
mysql_query("UPDATE config SET val=\"$val\" WHERE var=\"$v\" AND `year`='$year'");
|
2006-08-01 19:43:15 +00:00
|
|
|
print mysql_error();
|
|
|
|
// echo "Saving {$v} = $val<br>";
|
|
|
|
}
|
|
|
|
echo happy(i18n("Configuration Updated"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load questions, then handle up and down, because with up and down we
|
|
|
|
* have to modify 2 questions to maintain the order */
|
2006-08-11 18:53:10 +00:00
|
|
|
$var = config_editor_load($category, $year);
|
2006-08-01 19:43:15 +00:00
|
|
|
|
|
|
|
echo "<form method=\"post\" action=\"$self\">";
|
|
|
|
|
2006-08-11 18:53:10 +00:00
|
|
|
echo "<table cellpadding=\"3\">";
|
2006-08-01 19:43:15 +00:00
|
|
|
|
|
|
|
$varkeys = array_keys($var);
|
2006-08-11 18:53:10 +00:00
|
|
|
//compute the optimal input size to use
|
|
|
|
$biggest=0;
|
|
|
|
foreach($varkeys as $k) {
|
|
|
|
if(strlen($var[$k]['val'])>$biggest)
|
|
|
|
$biggest=strlen($var[$k]['val']);
|
|
|
|
}
|
|
|
|
if($biggest>30) $size=30;
|
|
|
|
else $size=$biggest+1;
|
|
|
|
|
|
|
|
|
2006-08-01 19:43:15 +00:00
|
|
|
foreach($varkeys as $k) {
|
|
|
|
print("<tr>");
|
|
|
|
print("<td>{$var[$k]['desc']}</td>");
|
|
|
|
print("<td>");
|
2007-01-20 22:23:44 +00:00
|
|
|
|
|
|
|
$val = htmlspecialchars($var[$k]['val']);
|
|
|
|
$name = "${array_name}[$k]";
|
|
|
|
|
|
|
|
switch($var[$k]['type']) {
|
|
|
|
case "yesno":
|
|
|
|
print("<select name=\"$name\">");
|
|
|
|
$sel = ($val == 'yes') ? 'selected=selected' : '';
|
|
|
|
print("<option $sel value=\"yes\">Yes</option>");
|
|
|
|
$sel = ($val == 'no') ? 'selected=selected' : '';
|
|
|
|
print("<option $sel value=\"no\">No</option>");
|
|
|
|
print("</select>");
|
|
|
|
break;
|
|
|
|
case "enum":
|
|
|
|
$values = $var[$k]['type_values'];
|
|
|
|
/* Split values */
|
|
|
|
/* The PERL regex here matches any string of the form
|
|
|
|
* key=val| , where the = and 'val' and '|' are
|
|
|
|
* optional. val is allowed to contain spaces. Using
|
|
|
|
* preg_match_all runs this regex multiple times, and
|
|
|
|
* creates arrays for each subpattern that matches.
|
|
|
|
* For example, "aa=Aye|bb=Bee Bee|cc|dd=Dee"
|
|
|
|
* Would construct the following Array of Arrays:
|
|
|
|
* Array ( [0] => Array ( [0] => "aa=Aye|",
|
|
|
|
[1] => "bb=Bee Bee|",
|
|
|
|
[2] => "cc|",
|
|
|
|
[3] => "dd=Dee" ),
|
|
|
|
[1] => Array ( [0] => "aa",
|
|
|
|
[1] => "bb",
|
|
|
|
[2] => "cc",
|
|
|
|
[3] => "dd" ),
|
|
|
|
[2] => Array ( [0] => "Aye",
|
|
|
|
[1] => "Bee Bee",
|
|
|
|
[2] => "",
|
|
|
|
[3] => "Dee" ) )
|
|
|
|
* neat eh? :) We use [1] and [2] to form the keys and
|
|
|
|
* values that we show the user */
|
|
|
|
|
|
|
|
preg_match_all("/([^\|=]+)(?:=([^\|]+))?\|?/", $values, $regs);
|
|
|
|
// print_r($regs);
|
|
|
|
print("<select name=\"$name\">");
|
|
|
|
for($x=0; $x<count($regs[1]); $x++) {
|
|
|
|
$e_key = trim($regs[1][$x]);
|
|
|
|
$e_val = trim($regs[2][$x]);
|
|
|
|
if($e_val == "") $e_val = $e_key;
|
|
|
|
|
|
|
|
$sel = ($val == $e_key) ? 'selected=selected' : '';
|
|
|
|
print("<option $sel value=\"$e_key\">$e_val</option>");
|
|
|
|
}
|
|
|
|
print("</select>");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print("<input size=\"$size\" type=\"text\" name=\"$name\" value=\"$val\">\n");
|
|
|
|
break;
|
|
|
|
}
|
2006-08-01 19:43:15 +00:00
|
|
|
echo "</td></tr>";
|
|
|
|
}
|
|
|
|
print("</table>");
|
2006-08-11 18:53:10 +00:00
|
|
|
print("<input type=\"hidden\" name=\"category\" value=\"$category\" >\n");
|
|
|
|
print("<input type=\"hidden\" name=\"action\" value=\"update\" >\n");
|
2006-08-01 19:43:15 +00:00
|
|
|
print("<input type=\"submit\" value=\"".i18n("Save Configuration")."\" />\n");
|
|
|
|
|
|
|
|
echo "</form>";
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|