#!/usr/bin/perl -w # # index.fcgi # # Developed by Ondrej Jombik # Copyright (c) 2004 Platon SDG, http://platon.sk/ # Licensed under terms of GNU General Public License. # All rights reserved. # # Changelog: # 16/01/2004 - created # # $Platon: web-apps/call-centre-system/index.fcgi,v 1.1 2004/03/10 11:34:51 nepto Exp $ package main; $| = 1; use strict; umask 022; use lib qw( ./modules ); use FCGI; use CGI::Lite; use DBI qw(:sql_types); use Template; use Template::Plugin; use Template::Exception; use Date::Calc qw(Today); use Time::HiRes qw(gettimeofday tv_interval); use Cwd; use File::Basename qw( basename fileparse ); use Customer; use Product; use User; use Call; use Order; use vars qw ( $cgi $req $tmpl $current_user %query %cookie $runcount $runlimit $fastcgi_name ); $req = FCGI::Request(); $cgi = CGI::Lite->new(); $tmpl = Template->new( { INCLUDE_PATH => [ './templates/en', './templates' ], AUTHOR => 'Ondrej Jombik', PRE_PROCESS => 'defaults.tt2', VARIABLES => { fastcgi_script => 'index.fcgi', fastcgi_title => 'CALL CENTRE SYSTEM', image_edit => 'images/pme-change.png', image_view => 'images/pme-view.png', image_delete => 'images/pme-delete.png', image_copy => 'images/pme-copy.png' }, } ); # signal handlers {{{ local $SIG{ALRM} = sub { $req->LastCall(); }; local $SIG{SEGV} = sub { die "SIGSEGV received: $!"; }; local $SIG{TERM} = sub { $req->LastCall(); }; local $SIG{ABRT} = sub { $req->LastCall(); }; local $SIG{KILL} = sub { $req->LastCall(); }; local $SIG{XCPU} = sub { $req->LastCall(); }; local $SIG{XFSZ} = sub { $req->LastCall(); }; # }}} $runcount = 0; $runlimit = 1000; while ( ( $runcount++ < $runlimit ) && ( $req->Accept() >= 0 ) ) { %query = $cgi->parse_new_form_data(); %cookie = $cgi->parse_cookies(); foreach ( keys(%query) ) { # simplify query if they are arrays $query{$_} = $query{$_}[0] if ( ref $query{$_} ); $query{$1}{$2} = $query{$_} if ( $_ =~ /^(.+)\[(.+)\]$/ ); } foreach ( keys(%cookie) ) { # simplify cookies if they are arrays $cookie{$_} = $cookie{$_}[0] if ( ref $cookie{$_} ); } $current_user = User->new( $cookie{'SESSIONID'} ); # # Default action # if ( !defined( $query{action} ) or $query{action} eq '' ) { $query{action} = 'welcome'; } # # DB Insert/Update/Delete actions # if ( $query{action} eq 'login' && $current_user->isAnonymous() ) { my $success = $current_user->login( $query{'login'}, $query{'password'} ); $query{action} = $success ? 'welcome' : 'login_page'; } elsif ( $query{action} eq 'logout' && !$current_user->isAnonymous() ) { $current_user->logout(); $query{action} = 'welcome'; } elsif ( $query{action} eq 'customer_save' ) { customerSave(); $query{action} = 'customer_list'; } elsif ( $query{action} eq 'customer_delete' ) { customerDelete(); $query{action} = 'customer_list'; } elsif ( $query{action} eq 'product_save' ) { productSave(); $query{action} = 'product_list'; } elsif ( $query{action} eq 'product_delete' ) { productDelete(); $query{action} = 'product_list'; } elsif ( $query{action} eq 'user_save' ) { userSave(); $query{action} = 'user_list'; } elsif ( $query{action} eq 'user_delete' ) { userDelete(); $query{action} = 'user_list'; } elsif ( $query{action} eq 'call_save' ) { callSave(); $query{action} = $query{customer_calls} ? 'customer_view' : 'call_list'; } elsif ( $query{action} eq 'call_delete' ) { callDelete(); $query{action} = $query{customer_calls} ? 'customer_view' : 'call_list'; } # # Screen actions # if ( $query{action} eq 'login_page' && $current_user->isAnonymous() ) { loginPage(); } elsif ( $query{action} eq 'customer_list' ) { customerList(); } elsif ( $query{action} eq 'customer_view' ) { customerView(); } elsif ( $query{action} eq 'customer_edit' ) { customerEdit(); } elsif ( $query{action} eq 'customer_copy' ) { customerCopy(); } elsif ( $query{action} eq 'product_list' ) { productList(); } elsif ( $query{action} eq 'product_view' ) { productView(); } elsif ( $query{action} eq 'product_edit' ) { productEdit(); } elsif ( $query{action} eq 'product_copy' ) { productCopy(); } elsif ( $query{action} eq 'user_list' ) { userList(); } elsif ( $query{action} eq 'user_view' ) { userView(); } elsif ( $query{action} eq 'user_edit' ) { userEdit(); } elsif ( $query{action} eq 'user_copy' ) { userCopy(); } elsif ( $query{action} eq 'call_list' ) { callList(); } elsif ( $query{action} eq 'call_view' ) { callView(); } elsif ( $query{action} eq 'call_edit' ) { callEdit('edit'); } elsif ( $query{action} eq 'call_copy' ) { callEdit('copy'); } else { # default "welcome" screen welcome(); } $current_user->updateLastAccess(); if ( -M $ENV{SCRIPT_FILENAME} < 0 ) { # Autorestart my $seconds = -86400.0 * -M $ENV{SCRIPT_FILENAME}; warn "FastCGI MODIFIED $seconds seconds after startup, restarting ...\n"; $req->LastCall(); } $req->Finish(); undef %cookie; undef %query; } exit 0; ######################################################################### ## ## FUNCTIONS ## ######################################################################### sub welcome # {{{ { my $template_data = { current_user => $current_user->get(), query => \%query, }; $tmpl->process( 'welcome.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub loginPage # {{{ { my $template_data = { current_user => $current_user->get(), query => \%query, }; $tmpl->process( 'login_page.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} # CUSTOMERS routines {{{ sub customerSave # {{{ { Customer::set( $query{'data'}{'cid'}, $query{'data'}, $current_user->getUserID() ); } # }}} sub customerDelete # {{{ { Customer::delete( $query{'data'}{'cid'} ); } # }}} sub customerList # {{{ { my $customers = Customer::getFilteredList( $query{'filter'}, $query{'sort'} ); my $template_data = { current_user => $current_user->get(), query => \%query, customers => $customers }; $tmpl->process( 'customer_list.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub customerView # {{{ { my $cid = $query{'data'}{'cid'}; my $customer = Customer::get($cid); $query{'filter2'}{'cid'} = $cid; $query{'sort2'} ||= '-call_date'; $customer->{'calls'} = Call::getFilteredList( $query{'filter2'}, $query{'sort2'} ); my $template_data = { current_user => $current_user->get(), query => \%query, customer => $customer, customer_calls => 1 }; $tmpl->process( 'customer_view.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub customerEdit # {{{ { my $cid = $query{'data'}{'cid'}; my $customer = Customer::get($cid); my $template_data = { current_user => $current_user->get(), query => \%query, customer => $customer }; $tmpl->process( 'customer_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub customerCopy # {{{ { my $cid = $query{'data'}{'cid'}; my $customer = Customer::get($cid); $customer->{'cid'} = 0; my $template_data = { current_user => $current_user->get(), query => \%query, customer => $customer, }; $tmpl->process( 'customer_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} # }}} # PRODUCTS routines {{{ sub productSave # {{{ { Product::set( $query{'data'}{'prod_id'}, $query{'data'}, $current_user->getUserID() ); } # }}} sub productDelete # {{{ { Product::delete( $query{'data'}{'prod_id'} ); } # }}} sub productList # {{{ { my $products = Product::getFilteredList( $query{'filter'}, $query{'sort'} ); my $template_data = { current_user => $current_user->get(), query => \%query, products => $products }; $tmpl->process( 'product_list.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub productView # {{{ { my $prod_id = $query{'data'}{'prod_id'}; my $product = Product::get($prod_id); my $template_data = { current_user => $current_user->get(), query => \%query, product => $product }; $tmpl->process( 'product_view.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub productEdit # {{{ { my $prod_id = $query{'data'}{'prod_id'}; my $product = Product::get($prod_id); my $template_data = { current_user => $current_user->get(), query => \%query, product => $product }; $tmpl->process( 'product_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub productCopy # {{{ { my $prod_id = $query{'data'}{'prod_id'}; my $product = Product::get($prod_id); $product->{'prod_id'} = 0; my $template_data = { current_user => $current_user->get(), query => \%query, product => $product }; $tmpl->process( 'product_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} # }}} # USER routines {{{ sub userSave # {{{ { User::set( $query{'data'}{'uid'}, $query{'data'}, $current_user->getUserID() ); } # }}} sub userDelete # {{{ { User::delete( $query{'data'}{'uid'} ); } # }}} sub userList # {{{ { my $users = User::getFilteredList( $query{'filter'}, $query{'sort'} ); my $template_data = { current_user => $current_user->get(), query => \%query, users => $users }; $tmpl->process( 'user_list.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub userView # {{{ { my $uid = $query{'data'}{'uid'}; my $user = User::get($uid); my $template_data = { current_user => $current_user->get(), query => \%query, user => $user }; $tmpl->process( 'user_view.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub userEdit # {{{ { my $uid = $query{'data'}{'uid'}; my $user = $uid ? User::get($uid) : undef; $user->{'parents'} = User::getLoginList(); $user->{'parents'}{0} = 'N/A'; delete $user->{'parents'}{$uid}; my $template_data = { current_user => $current_user->get(), query => \%query, user => $user }; $tmpl->process( 'user_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub userCopy # {{{ { my $uid = $query{'data'}{'uid'}; my $user = User::get($uid); $user->{'uid'} = 0; my $template_data = { current_user => $current_user->get(), query => \%query, user => $user }; $tmpl->process( 'user_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} # }}} # CALL routines {{{ sub callSave # {{{ { $query{'data'}{'uid'} ||= $current_user->getUserID(); Call::set( $query{'data'}{'call_id'}, $query{'data'}, $current_user->getUserID() ); } # }}} sub callDelete # {{{ { Call::delete( $query{'data'}{'call_id'} ); } # }}} sub callList # {{{ { my ( $year, $month, $day ) = Date::Calc::Today(); $query{'filter2'}{'call_date'} = sprintf( '%04d-%02d-%02d', $year, $month, $day ) if ( !defined $query{'filter2'}{'call_date'} ); $query{'filter2'}{'uid'} = $current_user->getUserID(); $query{'sort2'} ||= '-call_date'; my $calls = Call::getFilteredList( $query{'filter2'}, $query{'sort2'} ); my $template_data = { current_user => $current_user->get(), query => \%query, calls => $calls, customer_calls => 0 }; $tmpl->process( 'call_list.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub callView # {{{ { my $customer_calls = $query{'customer_calls'} ? 1 : 0; my $call_id = $query{'data'}{'call_id'}; my $call = Call::get($call_id); my $template_data = { current_user => $current_user->get(), query => \%query, call => $call, customer_calls => $customer_calls }; $tmpl->process( 'call_view.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} sub callEdit # {{{ { my $customer_calls = $query{'customer_calls'} ? 1 : 0; my $operation = shift; my $call_id = $query{'data'}{'call_id'}; my $call = Call::get($call_id); $call->{'call_id'} = 0 if ( $operation eq 'copy' ); $call->{'logins'} = User::getLoginList(); $call->{'customers'} = Customer::getNameList(); $call->{'cid'} ||= $query{'data'}{'cid'}; $call->{'customer'} ||= $call->{'customers'}->{ $call->{'cid'} }; my $template_data = { current_user => $current_user->get(), query => \%query, call => $call, customer_calls => $customer_calls }; $tmpl->process( 'call_edit.tt2', $template_data ) or die $tmpl->error(), "\n"; } # }}} # }}} # vim: ft=perl # vim600: fdm=marker fdl=0 fdc=3