#!/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 => 'selectableDataset', relationship => { parent => { id => 'parent', # searchLike => '%?%', }, children => { child => [ { parentParam => 'id', childField => 'invoiceid', }, ], }, }, members => [ { class => 'controller', id => 'parent', containerId => 'stuff', template => "parentController.tmpl", controls => [ { name => 'id', label => 'invoice ID', type => 'select', sql => ['select id, id from invoice'], required => 1, }, ], }, { 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('submitSelectable.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; }