#!/usr/bin/perl
# ConfReg:  Conference participant form, list of participants
# (c) NTREM, 2005-2009
# Vaclav Stepan, Vit Zyka
#
# 2007-11-23 derived from admin/people.cgi
# 2007-10-13 all types of participants with semaphore flag
# 2008-10-15 VZ blue puntik
# 2008-10-22 VZ script copyright header
# 2008-12-23 VZ typo: instractions
# 2009-03-31 VZ confirn -> confirm
# 2009-09-20 VZ rename: people.cgi -> participants.cgi; buttOK focus
# 2009-09-21 VZ set_local to lib_util
# 2009-09-21 VZ locales

use locale;
use POSIX qw(locale_h);

use CGI qw(-utf8 :standard);
#use CGI::Carp qw/fatalsToBrowser/;
use DBI;

require "./lib/server_conf.pl"; # Server specific settings
require "./lib/defaults.pl"; # Default settings
require "./lib/lib_log.pl"; # Audit/log functions
require "./lib/lib_util.pl"; # Utility functions

set_locale('cs_CZ.utf8');

my $debug = 0;

#----------------------------------------------------------------------
sub print_participants {
  # my $debug=1;
  my $filter = shift; # Typicky WHERE podminka pro omezeni vypisu
  my $sql;            # tmp
  my @fields = qw/id pretitle fname lname posttitle salutation von modus/;
  my %table_row;      # Radky tabulky, kvuli trideni
  my $key;            # Docasna polozka pro tridici klic

  push(@fields, @dorg_fields);
  push(@fields, qw/email pass stav/);

  print "FILTER IS: ".$filter.p() if($debug>0);

  # Pripravime si dotaz
  $sql = 'SELECT '.join(',',@fields)." FROM participants ".$filter." ORDER BY id;";
  print "SQL: $sql".p if ($debug>0);
  $sth = $dbh->prepare($sql);
  $sth->execute();

  # A jedeme radek po radku a plnime si je do hashe s klicem a kodem
  $i = 0;
  while( $row = $sth->fetchrow_hashref('NAME_lc') )
    {
      $i++;
      # Typ
      $items = td(TypeParticPuntik($$row{'stav'},$$row{'id'},$$row{'modus'}));
      # Jmeno
      $items .= '<td>';
#      $items .= $$row{'salutation'}." " if ($$row{'salutation'});
#      $items .= $$row{'pretitle'}." " if ($$row{'pretitle'});
      $items .= $$row{'von'}." " if ($$row{'von'});
      $items .= $$row{'lname'} if ($$row{'lname'});
      $items .= ',&nbsp;'.$$row{'fname'} if ($$row{'fname'});
#      $items .= ", ".$$row{'posttitle'} if ($$row{'posttitle'});
#      $items .= ' ('.$$row{'stav'}.')</td>';
      $items .= '</td>';
      for my $f (@org_fields_reg_list)
	{
	  $items .= pit($$row{$f});
	}
#      $items .= pit($$row{'email'});

      # Pripravime si klic a vlozime to do hashe, abychom to mohli tridit
      $key = $$row{'lname'}.' '.$$row{'fname'}.' '.$$row{'id'};
      $table_row{$key} = $items;
    }

  # Tisk tabulky
  print
    table({-class=>'max'},
	  TR({-class=>'max'},
	     td({-style=>'width:25%'},ImgPuntik(1).'&nbsp;Pending'),
	     td({-style=>'width:25%'},ImgPuntik(4).'&nbsp;Registered'),
	     td({-style=>'width:25%'},ImgPuntik(5).'&nbsp;Confirmed'),
	     td({-style=>'width:25%'},'&nbsp;'),
	    ),
	 ),
    '<table border="0" cellpadding="2" cellspacing="0" frame="below" width="100%">'."\n",
    TR(
       th({-class=>'left inverse'},['&nbsp;','&nbsp;','Participant','Institution','City','Country']),
      );
  # Tohle zajistuje vytisteni v poradi danem locale nastavenim
  $i = 0;
  foreach $key (sort keys(%table_row))
    {
      $i++;
      print
	TR({-class=>($i%2==0?'gray':'')},
	   pit("$i."),
	   $table_row{$key},
	  );
    }
  print "</table>\n";
}


#======================================================================
$jscript = "window.onload=function(){document.getElementById('buttOK').focus()};";
hlavicka('white', $jscript);

$dbh = connect_to_db();

print h2('List of participants');
print
  p('Do you want to attend the conference? Can not you find your name here? '.
    'Please, register yourself in the <a href="form_participant.cgi">participant register form</a> '.
    'and then follow the instructions in a confirmation email.');

#print_participants("WHERE stav='C'");
#print_participants("");
print_participants("WHERE country<>'Russia'");

$dbh->disconnect()
  or warn("Disconnection from $DSN failed: $DBI::errstr\n");

print
  hr,
  start_form(-action=>'edit_participant.cgi'), "\n",
  hidden('e', ''.param('email')), "\n",
  hidden('t', ''.param('pass')), "\n",
  (param('a') ? hidden('a', ''.param('a'))."\n" : ""),
  submit(-name=>'buttOK',-id=>'buttOK',-value=>'OK',-class=>'mainbtn'),"\n",
  end_form,
  konec();

# end of reg_partic_list.cgi
