2007-03-18 07:10:54 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once("reports_students.inc.php"); /* $report_students_fields */
|
2007-03-18 21:48:36 +00:00
|
|
|
require_once("reports_judges.inc.php"); /* $report_students_fields */
|
|
|
|
require_once("reports_awards.inc.php"); /* $report_students_fields */
|
2007-03-18 19:59:02 +00:00
|
|
|
require_once('../lpdf.php');
|
|
|
|
require_once('../lcsv.php');
|
2007-03-18 07:10:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
$options = array();
|
|
|
|
$options['type'] = array( 'desc' => 'Report Format',
|
|
|
|
'values' => array('pdf'=>'PDF', 'csv'=>'CSV')
|
|
|
|
);
|
|
|
|
$options['group_new_page'] = array( 'desc' => 'Start each new grouping on a new page',
|
|
|
|
'values' => array('no'=>'No', 'yes'=>'Yes')
|
|
|
|
);
|
|
|
|
$options['allow_multiline'] = array( 'desc' => 'Allow table rows to span multiple lines',
|
|
|
|
'values' => array('no'=>'No', 'yes'=>'Yes')
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Viceroy Grand Avery rows? w x h" per page
|
|
|
|
& Toy
|
|
|
|
LRP 130 99180 5960 3 2 5/8 x 1 30
|
|
|
|
LRP 120 99189 5961 2 4 x 1 20
|
|
|
|
LRP 114 99179 5959 7 4 x 1 1/2 14
|
|
|
|
LRP 214 99190 5962 7 4 x 1 1/3 14
|
|
|
|
LRP 110 99181 5963 5 4 x 2 10
|
|
|
|
LRP 106 99763 5964 3 4 x 3 1/3 6
|
|
|
|
LRP 100 99764 5965 1 8 1/2 x 11 1
|
|
|
|
LRP 180 99765 5967 4 1 3/4 x 1/2 80 */
|
|
|
|
|
|
|
|
|
|
|
|
/* FIXME: put these in a databse */
|
|
|
|
$stock = array();
|
|
|
|
$stock['5964'] = array('name' => 'Avery 5964, G&T 99763',
|
|
|
|
'page_width' => 8 + 1/2,
|
|
|
|
'page_height' => 11,
|
|
|
|
'label_width' => 4,
|
|
|
|
'x_spacing' => 3/16,
|
|
|
|
'cols' => 2,
|
|
|
|
'label_height' => 3 + 1/3,
|
|
|
|
'y_spacing' => 0,
|
|
|
|
'rows' => 3,
|
|
|
|
);
|
|
|
|
|
|
|
|
$labels[] = array( 'name' => 'GVRSF Project Labels',
|
|
|
|
'options' => array('fair_header' => true),
|
|
|
|
'items' => array(
|
|
|
|
array( 'field' => 'pn',
|
|
|
|
'x' => 'center',
|
|
|
|
'y' => 'center',
|
|
|
|
'h' => 50 ),
|
|
|
|
array( 'field' => 'bothnames',
|
|
|
|
'x' => 'center',
|
|
|
|
'y' => 80,
|
|
|
|
'h' => 10 ),
|
|
|
|
array( 'field' => 'categorydivision',
|
|
|
|
'x' => 'center',
|
|
|
|
'y' => 90,
|
|
|
|
'h' => 10 ),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* Add more types to the report format */
|
|
|
|
foreach($stock as $n=>$v) {
|
|
|
|
$options['type']['values'][$n] = $v['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$allow_options = array_keys($options);
|
|
|
|
|
|
|
|
|
|
|
|
function report_load_field($report, $type)
|
|
|
|
{
|
2007-03-18 19:59:02 +00:00
|
|
|
global $allow_options, $report_students_fields, $report_judges_fields, $report_awards_fields;
|
2007-03-18 07:10:54 +00:00
|
|
|
|
|
|
|
$allow_fields = array();
|
|
|
|
|
|
|
|
switch($report['type']) {
|
2007-03-18 19:59:02 +00:00
|
|
|
case 'student': $allow_fields = array_keys($report_students_fields); break;
|
|
|
|
case 'judge': $allow_fields = array_keys($report_judges_fields); break;
|
|
|
|
case 'award': $allow_fields = array_keys($report_awards_fields); break;
|
2007-03-18 07:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$ret = array();
|
|
|
|
$q = mysql_query("SELECT * FROM reports_items
|
|
|
|
WHERE reports_id='{$report['id']}'
|
|
|
|
AND type='$type'
|
|
|
|
ORDER BY `order`");
|
|
|
|
print(mysql_error());
|
|
|
|
|
|
|
|
if(mysql_num_rows($q) == 0) return $ret;
|
|
|
|
|
|
|
|
while($a = mysql_fetch_assoc($q)) {
|
|
|
|
$f = $a['field'];
|
|
|
|
if($type == 'option') {
|
|
|
|
if(!in_array($f, $allow_options)) {
|
|
|
|
print("Type[$type] Field[$f] not allowed.\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$ret[$f] = $a['value'];
|
|
|
|
} else {
|
|
|
|
if(!in_array($f, $allow_fields)) {
|
|
|
|
print("Type[$type] Field[$f] not allowed.\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$ret[] = $a['field'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function report_save_field($report, $type)
|
|
|
|
{
|
2007-03-18 21:48:36 +00:00
|
|
|
global $allow_options;
|
|
|
|
global $report_students_fields, $report_judges_fields, $report_awards_fields;
|
2007-03-18 07:10:54 +00:00
|
|
|
|
|
|
|
switch($report['type']) {
|
2007-03-18 19:59:02 +00:00
|
|
|
case 'student': $allow_fields = array_keys($report_students_fields); break;
|
|
|
|
case 'judge': $allow_fields = array_keys($report_judges_fields); break;
|
|
|
|
case 'award': $allow_fields = array_keys($report_awards_fields); break;
|
2007-03-18 07:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* First delete all existing fields */
|
|
|
|
mysql_query("DELETE FROM reports_items
|
|
|
|
WHERE `reports_id`='{$report['id']}'
|
|
|
|
AND `type`='$type'");
|
|
|
|
/* Now add new ones */
|
|
|
|
|
|
|
|
if(count($report[$type]) == 0) return;
|
|
|
|
|
|
|
|
$q = '';
|
|
|
|
$x = 0;
|
|
|
|
foreach($report[$type] as $k=>$v) {
|
|
|
|
if($type == 'option') {
|
|
|
|
$field = $k;
|
|
|
|
$val = $v;
|
|
|
|
} else {
|
|
|
|
$field = $v;
|
|
|
|
$val = '';
|
|
|
|
}
|
|
|
|
if($q != '') $q .= ',';
|
|
|
|
$q .= "({$report['id']}, '$field', '$type', '$val', $x)";
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
mysql_query("INSERT INTO reports_items(`reports_id`,`field`,`type`,`value`,`order`)
|
|
|
|
VALUES $q;");
|
|
|
|
echo mysql_error();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function report_load($report_id)
|
|
|
|
{
|
|
|
|
$report = array();
|
|
|
|
|
|
|
|
$q = mysql_query("SELECT * FROM reports WHERE id='$report_id'");
|
|
|
|
$r = mysql_fetch_assoc($q);
|
|
|
|
$report['name'] = $r['name'];
|
|
|
|
$report['id'] = $r['id'];
|
|
|
|
$report['desc'] = $r['desc'];
|
|
|
|
$report['creator'] = $r['creator'];
|
|
|
|
$report['type'] = $r['type'];
|
|
|
|
|
|
|
|
/* Get the data */
|
|
|
|
$report['col'] = report_load_field($report, 'col');
|
|
|
|
$report['group'] = report_load_field($report, 'group');
|
|
|
|
$report['sort'] = report_load_field($report, 'sort');
|
|
|
|
$report['distinct'] = report_load_field($report, 'distinct');
|
|
|
|
$report['option'] = report_load_field($report, 'option');
|
|
|
|
|
|
|
|
return $report;
|
|
|
|
}
|
|
|
|
|
|
|
|
function report_save($report)
|
|
|
|
{
|
|
|
|
if($report['id'] == 0) {
|
|
|
|
/* New report */
|
|
|
|
mysql_query("INSERT INTO reports (`id`) VALUES ('')");
|
|
|
|
$report['id'] = mysql_insert_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql_query("UPDATE reports SET
|
|
|
|
`name`='".mysql_escape_string($report['name'])."',
|
|
|
|
`desc`='".mysql_escape_string($report['desc'])."',
|
|
|
|
`creator`='".mysql_escape_string($report['creator'])."',
|
|
|
|
`type`='".mysql_escape_string($report['type'])."'
|
|
|
|
WHERE `id`={$report['id']}");
|
|
|
|
|
|
|
|
report_save_field($report, 'col');
|
|
|
|
report_save_field($report, 'group');
|
|
|
|
report_save_field($report, 'sort');
|
|
|
|
report_save_field($report, 'distinct');
|
|
|
|
report_save_field($report, 'option');
|
|
|
|
return $report['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
function report_load_all()
|
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
$q = mysql_query("SELECT * FROM reports ORDER BY `name`");
|
|
|
|
|
|
|
|
while($r = mysql_fetch_assoc($q)) {
|
|
|
|
$report = array();
|
|
|
|
$report['name'] = $r['name'];
|
|
|
|
$report['id'] = $r['id'];
|
|
|
|
$report['desc'] = $r['desc'];
|
|
|
|
$report['creator'] = $r['creator'];
|
|
|
|
$report['type'] = $r['type'];
|
|
|
|
$ret[] = $report;
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function report_gen_by_id($report_id, $format=NULL)
|
|
|
|
{
|
2007-03-18 07:19:00 +00:00
|
|
|
global $options;
|
2007-03-18 07:10:54 +00:00
|
|
|
$report = report_load($report_id);
|
|
|
|
if($format != NULL) {
|
|
|
|
$keys = array_keys($options['type']['values']);
|
|
|
|
if(in_array($format, $keys)) {
|
|
|
|
$report['option']['type'] = $format;
|
|
|
|
} else {
|
|
|
|
print("type [$format] not permitted\n");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return report_gen($report);
|
|
|
|
}
|
|
|
|
|
|
|
|
function report_delete($report_id)
|
|
|
|
{
|
|
|
|
$r = intval($report_id);
|
|
|
|
mysql_query("DELETE FROM reports WHERE `id`=$r");
|
|
|
|
mysql_query("DELETE FROM reports_items WHERE `reports_id`=$r");
|
|
|
|
}
|
|
|
|
|
2007-03-18 19:59:02 +00:00
|
|
|
function report_gen($report)
|
|
|
|
{
|
|
|
|
global $config, $report_students_fields, $report_judges_fields, $report_awards_fields;
|
|
|
|
|
|
|
|
//print_r($report);
|
|
|
|
switch($report['type']) {
|
|
|
|
case 'student': $fields = $report_students_fields; break;
|
|
|
|
case 'judge': $fields = $report_judges_fields; break;
|
|
|
|
case 'award': $fields = $report_awards_fields; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gen_mode = '';
|
|
|
|
$fieldname = array();
|
|
|
|
$thead = array();
|
|
|
|
|
|
|
|
$table['header']=array();
|
|
|
|
$table['widths']=array();
|
|
|
|
$table['dataalign']=array();
|
|
|
|
$table['option']=array();
|
|
|
|
|
|
|
|
if($report['option']['type']=='csv') {
|
|
|
|
$rep=new lcsv(i18n($report_name));
|
|
|
|
$gen_mode = 'table';
|
|
|
|
|
|
|
|
} else if($report['option']['type']=='label') {
|
|
|
|
/* Label */
|
|
|
|
$label_stock = $stock($report['option']['stock']);
|
|
|
|
$rep=new lpdf( i18n($config['fairname']),
|
|
|
|
i18n($report_name),
|
|
|
|
$_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif");
|
|
|
|
$rep->newPage($label_stock['pw'], $label_stock['ph']);
|
|
|
|
$rep->setFontSize(11);
|
|
|
|
$rep->setLabelDimensions($label_stock['w'], $label_stock['h'],
|
|
|
|
$label_stock['xs'], $label_stock['ys']);
|
|
|
|
$gen_mode = 'label';
|
|
|
|
} else {
|
|
|
|
$rep=new lpdf( i18n($config['fairname']),
|
|
|
|
i18n($report['name']),
|
|
|
|
$_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif");
|
|
|
|
$rep->newPage();
|
|
|
|
$rep->setFontSize(11);
|
|
|
|
$gen_mode = 'table';
|
|
|
|
if($report['option']['allow_multiline'] == 'yes')
|
|
|
|
$table['option']['allow_multiline'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sel = array();
|
|
|
|
$x=0;
|
|
|
|
$group_by = array();
|
|
|
|
/* Select columns to display */
|
|
|
|
foreach($report['col'] as $f) {
|
|
|
|
$table['header'][] = i18n($fields[$f]['header']);
|
|
|
|
$table['widths'][] = $fields[$f]['width'];
|
|
|
|
$table['dataalign'][] = 'left';
|
|
|
|
$sel[] = "{$fields[$f]['table']} AS C$x";
|
|
|
|
$fieldname[$f] = "C$x";
|
|
|
|
if(is_array($fields[$f]['group_by']))
|
|
|
|
$group_by = array_merge($group_by, $fields[$f]['group_by']);
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
/* We also want to select any column groupings, but we won't display them */
|
|
|
|
$x=0;
|
|
|
|
foreach($report['group'] as $f) {
|
|
|
|
if(isset($fieldname[$f])) continue;
|
|
|
|
$sel[] = "{$fields[$f]['table']} AS G$x";
|
|
|
|
$fieldname[$f] = "G$x";
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$x=0;
|
|
|
|
foreach($report['sort'] as $f) {
|
|
|
|
if(isset($fieldname[$f])) continue;
|
|
|
|
$sel[] = "{$fields[$f]['table']} AS S$x";
|
|
|
|
$fieldname[$f] = "S$x";
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$x=0;
|
|
|
|
foreach($report['distinct'] as $f) {
|
|
|
|
if(isset($fieldname[$f])) continue;
|
|
|
|
$sel[] = "{$fields[$f]['table']} AS D$x";
|
|
|
|
$fieldname[$f] = "D$x";
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
$sel = implode(",", $sel);
|
|
|
|
|
|
|
|
$order = array();
|
|
|
|
|
|
|
|
/* Setup the order: groups, then sort order */
|
|
|
|
foreach($report['group'] as $f) {
|
|
|
|
if(isset($fields[$f]['table_sort'])) {
|
|
|
|
$order[] = $fields[$f]['table_sort'];
|
|
|
|
} else {
|
|
|
|
$order[] = $fieldname[$f];//ields[$f]['table'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach($report['sort'] as $f) {
|
|
|
|
if(isset($fields[$f]['table_sort'])) {
|
|
|
|
$order[] = $fields[$f]['table_sort'];
|
|
|
|
} else {
|
|
|
|
$order[] = $fieldname[$f];//$fields[$f]['table'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$order = implode(",", $order);
|
|
|
|
|
|
|
|
if(!isset($report['year'])) {
|
|
|
|
$report['year'] = $config['FAIRYEAR'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($report['distinct'])) {
|
|
|
|
foreach($report['distinct'] as $f) {
|
|
|
|
$group_by[] = $fieldname[$f];
|
|
|
|
}
|
|
|
|
}
|
2007-03-18 21:48:36 +00:00
|
|
|
|
|
|
|
if(count($group_by)) {
|
2007-03-18 19:59:02 +00:00
|
|
|
$group_query = "GROUP BY ".implode(",", $group_by);
|
|
|
|
} else {
|
|
|
|
$group_query = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$q = '';
|
|
|
|
switch($report['type']) {
|
|
|
|
case 'student': $q = report_students_fromwhere($report); break;
|
|
|
|
case 'judge': $q = report_judges_fromwhere($report); break;
|
|
|
|
case 'award': $q = report_awards_fromwhere($report); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$q = "SELECT $sel $q $group_query ORDER BY $order";
|
|
|
|
|
|
|
|
// print("$q");
|
|
|
|
|
|
|
|
$r = mysql_query($q);
|
|
|
|
echo mysql_error();
|
|
|
|
|
|
|
|
$ncols = count($report['col']);
|
|
|
|
$n_groups = count($report['group']);
|
|
|
|
$last_group_data = array();
|
|
|
|
|
|
|
|
while($i = mysql_fetch_assoc($r)) {
|
|
|
|
|
|
|
|
if($n_groups > 0) {
|
|
|
|
$group_change = false;
|
|
|
|
for($x=0; $x<$n_groups; $x++) {
|
|
|
|
if($last_group_data["G$x"] != $i["G$x"]) {
|
|
|
|
$group_change = true;
|
|
|
|
}
|
|
|
|
$last_group_data["G$x"] = $i["G$x"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if($group_change) {
|
|
|
|
/* Dump the last table */
|
|
|
|
if(count($table['data'])) {
|
|
|
|
// print_r($table);
|
|
|
|
$rep->addTable($table);
|
|
|
|
$rep->nextLine();
|
|
|
|
$table['data'] = array();
|
|
|
|
/* Start a new page AFTER a table is
|
|
|
|
* dumped, so the first page doesn't
|
|
|
|
* end up blank */
|
|
|
|
if($report['option']['group_new_page'] == 'yes') {
|
|
|
|
$rep->newPage();
|
|
|
|
} else {
|
|
|
|
$rep->hr();
|
|
|
|
$rep->vspace(-0.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct a new header */
|
|
|
|
$h = implode(" -- ", $last_group_data);
|
|
|
|
$rep->heading($h);
|
|
|
|
$rep->nextLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
$x=0;
|
|
|
|
foreach($report['col'] as $c) {
|
|
|
|
if(is_array($fields[$c]['value_map'])) {
|
|
|
|
$v = $fields[$c]['value_map'][$i["C$x"]];
|
|
|
|
} else {
|
|
|
|
$v = $i["C$x"];
|
|
|
|
}
|
|
|
|
if($gen_mode == 'table') {
|
|
|
|
$data[] = $v;
|
|
|
|
} else if($gen_mode == 'label') {
|
|
|
|
$d = $label_data[$c];
|
|
|
|
/* Label text: x%, y%, width%, height%, text */
|
|
|
|
$this->addLabelText2($d['x'], $d['y'], $d['w'],
|
|
|
|
$d['h'], $v);
|
|
|
|
}
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
$table['data'][] = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($table['data'])) {
|
|
|
|
$rep->addTable($table);
|
|
|
|
}
|
|
|
|
$rep->output();
|
|
|
|
}
|
|
|
|
|
2007-03-18 07:10:54 +00:00
|
|
|
?>
|