science-ation/tours.class.php

171 lines
4.3 KiB
PHP

<?
/* Just the fields in the tours table, we use this twice */
$tours_fields = array('name' => 'Tour Name',
'num' => 'Tour Number',
'description' => 'Description',
'capacity' => 'Capacity',
'grade_min' => 'Minimum Grade',
'grade_max' => 'Maximum Grade',
'year' => 'Year',
'contact' => 'Contact',
'location' => 'Location');
class tours
{
/* Static members for the table editor */
static function tableEditorSetup($editor)
{
global $tours_fields;
global $config;
/*
* Setup the table editor with the fields we want to display
* when displaying a list of tours, and also the type of each
* field where required
*/
$l = array(
'num' => 'Tour Number',
'name' => 'Tour Name',
'capacity' => 'Capacity',
'grade_min' => 'Minimum Grade',
'grade_max' => 'Maximum Grade',
'year' => 'Year',
);
/*
* Most of these should be moved to the base class, as they
* will be the same for all person groups
*/
$editor->setTable('tours');
$editor->setRecordType('Tour');
$editor->setListFields($l);
$editor->setPrimaryKey('id');
$editor->setEditFields($tours_fields);
$editor->setFieldOptions('year', array(
array('key' => 'NULL', 'val' => 'Inactive'),
array('key' => $config['FAIRYEAR'], 'val' => $config['FAIRYEAR'])
));
// print_r($e);
print ("<br>\n");
/* Build an array of grades */
$gradechoices = array();
for ($g = $config['mingrade']; $g <= $config['maxgrade']; $g++) {
$gradechoices[] = array('key' => $g, 'val' => "Grade $g");
}
$editor->setFieldOptions('grade_min', $gradechoices);
$editor->setFieldInputType('grade_min', 'select');
$editor->setFieldOptions('grade_max', $gradechoices);
$editor->setFieldInputType('grade_max', 'select');
}
/* Functions for $this */
function __construct($tour_id = NULL)
{
if ($tour_id == NULL) {
$this->id = FALSE;
} else {
$this->id = $tour_id;
}
}
function tableEditorLoad()
{
global $config, $pdo;
$id = $this->id;
// print("Loading Judge ID $id\n");
$q = $pdo->prepare("SELECT\ttours.*
FROM \ttours
WHERE \ttours.id=?");
$q->execute([$id]);
show_pdo_errors_if_any($pdo);
/*
* We assume that the field names in the array we want to return
* are the same as those in the database, so we'll turn the entire
* query into a single associative array
*/
$j = $q->fetch(PDO::FETCH_ASSOC);
return $j;
}
function tableEditorSave($data)
{
/*
* If $this->id == false, then we need to INSERT a new record.
* if it's a number, then we want an UPDATE statement
*/
global $tours_fields;
global $config, $pdo;
$query = '';
/* Construct an insert query if we have to */
if ($this->id == false) {
// $query = "INSERT INTO tours (id, name, num, description, contact, location) VALUES ('', '', '', '', '', '')";
$query = 'INSERT INTO tours (year, name, num, description, capacity, grade_min, grade_max, contact, location) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
$stmt = $pdo->prepare($query);
$stmt->execute([
str_replace("'", ' ', $data['year']),
str_replace("'", ' ', $data['name']),
str_replace("'", ' ', $data['num']),
str_replace("'", ' ', $data['description']),
str_replace("'", ' ', $data['capacity']),
str_replace("'", ' ', $data['grade_min']),
str_replace("'", ' ', $data['grade_max']),
str_replace("'", ' ', $data['contact']),
str_replace("'", ' ', $data['location'])
]);
$this->id = $pdo->lastInsertId();
} else {
/* Give it a proper year when saving */
/* Now just update the record */
$query = 'UPDATE `tours` SET ';
foreach ($tours_fields AS $f => $n) {
$n = $data[$f];
$query .= "`$f`=$n,";
}
// rip off the last comma
$query = substr($query, 0, -1);
$query .= ' WHERE id=?';
// echo $query;
$stmt = $pdo->prepare($query);
$stmt->execute([$this->id]);
}
}
function tableEditorDelete()
{
global $config, $pdo;
$id = $this->id;
try {
$stmt = $pdo->prepare('DELETE FROM tours_choice WHERE tour_id=? AND year=?');
$stmt->execute([$id, $config['FAIRYEAR']]);
$stmt = $pdo->prepare('DELETE FROM tours WHERE id=? AND year=?');
$stmt->execute([$id, $config['FAIRYEAR']]);
echo happy(i18n("Successfully removed tour from this year's fair"));
} catch (PDOException $exception) {
echo error(i18n("Failed to remove tour from this year's fair"));
error_log($exception);
}
}
};
?>