#!/usr/bin/perl use strict; use warnings; use JSON; use Data::Dumper; use CGI::Lazy; #use GlobalTest; #our $q = CGI::Lazy->new('/var/cache/apache2/cgi-conf/lazydemo.conf'); our $q = CGI::Lazy->new({ tmplDir => "/var/templates/lazydemo", buildDir => "/var/build", jsDir => "/lazydocs/js", cssDir => '/lazydocs/css', imgDir => '/lazydocs/images', # dbhVar => "DBH", plugins => { mod_perl => { # PerlHandler => "ModPerl::PerlRun" PerlHandler => "ModPerl::Registry", saveOnCleanup => "1", }, dbh => { dbDatasource => "dbi:mysql:lazydemo:localhost", dbUser => "luser", dbPasswd => "letmein", dbArgs => {"RaiseError" => 1}, }, # session => { # sessionTable => "SessionData", # sessionCookie => "frobnostication", # saveOnDestroy => "1", # expires => "+15m" # }, } }); our $stuff = $q->widget->composite({ id => 'stuff', type => 'parentChild', relationship => { parent => { id => 'parent', # searchLike => '%?%', }, children => { child => { parentKey => 'id', childKey => 'invoiceid', }, }, }, members => [ { class => 'dataset', id => 'parent', type => 'single', multiType => 'list', flagColor => 'blue', containerId => 'stuff', template => "parent.tmpl", multipleTemplate => 'parentMultiple.tmpl', searchLike => '%?%', lookups => { countryLookup => { sql => 'select ID, country from countryCodeLookup', preload => 1, orderby => ['ID'], output => 'hash', primarykey => 'ID', }, }, recordset => { table => 'invoice', mysqlAuto => 1, fieldlist => [ {name => 'id', hidden => 1}, {name => 'merchant', label => 'Merchant', multi => 1}, {name => 'batch', label => 'Bat', multi => 1}, {name => 'post_date', label => 'Post Date', multi => 1}, {name => 'cardnum', label => 'Card Number', multi => 1}, {name => 'tailno', label => 'Tail Number',}, {name => 'authCode', label => 'Auth Code',}, {name => 'icao', label => 'ICAO',}, {name => 'invoicenum', label => 'Invoice Number', multi => 1}, {name => 'invtotal', label => 'Invoice Total', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}, }, {name => 'trandate', label => 'Invoice Date', }, {name => 'country', label => 'Country',}, {name => 'countryname', label => '', displayOnly => 1, noLabel => 1,}, {name => 'card_type', hidden => 1,}, ], basewhere => '', orderby => 'id', primarykey => 'id', insertdefaults => { card_type => {#field name # sql => 'select description from prodCodeLookup where ID = 1', value => 'frobnitz', # handle => $id, }, }, }, }, { class => 'dataset', id => 'child', type => 'multi', template => "child.tmpl", headings => 'childHDR.tmpl', # nodelete => 1, lookups => { prodcodeLookup => { sql => 'select ID, description from prodCodeLookup', preload => 1, orderby => ['ID'], output => 'hash', primarykey => 'ID', }, }, recordset => { table => 'detail', fieldlist => [ {name => 'detail.ID', hidden => 1}, {name => 'invoiceid', hidden => 1}, { name => 'prodCode', label => 'Prod Code', webcontrol => { type => 'select', sql => ['select description, id from prodCodeLookup'], }, # validator => {rules => ['/\d+/'], msg => 'number only, and is required'} }, {name => 'quantity', label => 'Quantity', validator => {rules => ['/\d+/'], msg => 'number only, and is required'}, outputMask => "%.1f", }, {name => 'unitPrice', label => 'Unit Price' , validator => {rules => ['/\d+/'], msg => 'number only, and is required'}, inputMask => "%.1f", }, {name => 'productGross', label => 'Product Gross' , validator => {rules => ['/\d+/'], msg => 'number only, and is required'}}, {name => 'prodCodeLookup.description', label => 'Product Description', readOnly => 1 }, ], where => '', joins => [ {type => 'inner', table => 'prodCodeLookup', field1 => 'prodCode', field2 => 'prodCodeLookup.ID',}, ], orderby => 'detail.ID', primarykey => 'detail.ID', }, }, ] }); #my $b = $q->template->boilerplate($stuff)->buildTemplates; my %nav = ( dbwrite => \&dbwrite, ); if ($q->param('nav')) { $nav{$q->param('nav')}->(); } elsif ($q->param('POSTDATA') || $q->param('keywords')) { ajaxHandler(); } else { display('blank'); } #---------------------------------------------------------------------------------------- sub ajaxHandler { my $incoming = from_json($q->param('POSTDATA') || $q->param('keywords')); if ($incoming->{delete}) { doFullDelete($incoming); return; } print $q->header, $stuff->ajaxSelect(incoming => $incoming); return; } #---------------------------------------------------------------------------------------- sub dbwrite { # $q->util->debug->eparam; $stuff->dbwrite; display('blank'); } #---------------------------------------------------------------------------------------- sub display { my $mode = shift; print $q->header, # $q->start_html({-style => {src => $q->css->file('lazydemo.css')}}), $q->start_html({-style => {src => $q->css->file('lazydemo.css')}}), $q->javascript->modules($stuff); #javascript functions needed by widget #header section print $q->template('header.tmpl')->process({ mainTitle => 'CGI::Lazy Demo', secondaryTitle => 'Composite', versionTitle => $q->lazyversion, messageTitle => 'Nik rocks', }); #help block section print $q->template('helpBlock.tmpl')->process({ helpMessage1 => ' ', helpMessage2 => ' ', }); #composite widget section print $q->start_form({-name => 'mainform'}), $q->hidden({-name => 'nav', -value => 'dbwrite'}); print $stuff->display(mode => $mode); print $q->javascript->load('lazydemo.js'); print $q->template('submit.tmpl')->process({url => $q->url}); print $q->end_form; print $q->template('footer.tmpl')->process({version => $q->lazyversion}); return; } #---------------------------------------------------------------------------------------- sub doFullDelete { my $incoming = shift; my $invoiceid = $incoming->{delete}->{invoiceid}; $q->db->do('delete from detail where invoiceid = ?', $invoiceid); $q->db->do('delete from invoice where invoiceid = ?', $invoiceid); my $parent = $stuff->members->{parent}; my $child = $stuff->members->{child}; print $q->header, $stuff->ajaxBlank; return; }