Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

chrism01's scratchpad

by chrism01 (Friar)
on Feb 05, 2008 at 04:03 UTC ( [id://666195]=scratchpad: print w/replies, xml ) Need Help??

#!/usr/bin/perl -w #********************************************************************* +********* # # Program : asset_mgr.pl # # Desc : Top level menu program to run the Asset Mgr software. # Each DB table has it's own perl code file to manage it' +s # data. These are instantiated as pm (Perl module) files, # one per table. # # Params : See get_cmd_line_params() # #********************************************************************* +********* # # Amendment History # ----------------- # # Date Author Description # ----------- ---------- -------------------------------------- +--------- # 04-Feb 2008 C Moden Orig version # #********************************************************************* +********* use locale; # Ensure correct charset for eg 'uc()' use CGI::FormBuilder; # build GUI forms use Asset_Mgr; # custom/local AM module use adsl_routers; # routines to deal with adsl_routers use strict; # Enforce declarations # Get cfg params get_cmd_line_params(); # Connect to DB db_connect(); # run the code asset_mgr_ctrl(); #********************************************************************* +********* # # Function : asset_mgr_ctrl # # Description : Main processing loop. # Setup a menu for user's to choose which asset type t +o # 'maintain' and call the appropriate module. # # Params : none # # Returns : none # #********************************************************************* +********* sub asset_mgr_ctrl { my ( $tmp, @asset_type_opts, # list of asset types for drop down list $asset_cnt, # count of asset id recs ie id exists ? $form, # formbuilder object $fields # hashref of form fields: name=>value ); # Create form layout ... $form = CGI::FormBuilder->new( name => 'asset_mgr', title => 'Asset Manager', text => "Select Asset Type", method => 'post', sticky => 0, header => 1, submit => 'AM', fields => [qw(action asset_type asset_id)], required => [qw(action asset_type)] ); $form->field(name => 'action', options => [qw(CREATE UPDATE)], type => 'select', selectname => 0, value => "CREATE" ); $form->field(name => 'asset_type', options => \@asset_type_opts, type => 'select', selectname => 0, value => "$asset_type_opts[0]" ); # Check form $fields = $form->field; if( $form->submitted eq "AM") { if( $fields->{'action'} eq 'UPDATE' ) { # Check they've specified an extant asset id $fields->{'asset_id'} = uc($fields->{'asset_id'}); #DEBUG log_msg("type $fields->{'asset_type'} id $fields->{'asset_id'}"); $asset_cnt = db_check_asset_id($fields->{'asset_type'}, $fields->{'asset_id'}); if( !$asset_cnt ) { # Invalid; show form and allow corrections $cfg::err_flag = 0; print $form->render(sticky => 1); } elsif( $form->validate ) { # Call pm $tmp ='ADSL'; adsl_routers_ctrl($fields->{'action'}, $fields->{'asset_id'}); log_msg("adsl done"); } else { # Invalid; show form and allow corrections $cfg::err_flag = 0; print $form->render(sticky => 1); } } else { # Print form print $form->render; } } elsif( $tmp eq "ADSL" ) { log_msg("call do it"); adsl_routers_ctrl("DOIT",""); } else { # Print form print $form->render; } } ################### NEW FILE STARTS HERE #################3 #!/usr/bin/perl -w #********************************************************************* +********* # # Program : adsl_routers_maint.pm # # Desc : Insert/Update/Delete recs from the adsl_routers table i +n # the Asset Mgr DB. # Also, update ZABBIX system. ???? # # Params : See get_cmd_line_params() # #********************************************************************* +********* # # Amendment History # ----------------- # # Date Author Description # ----------- ---------- -------------------------------------- +--------- # 25-Jan 2008 C Moden Orig version # #********************************************************************* +********* package adsl_routers; use locale; # Ensure correct charset for eg 'uc()' use CGI::FormBuilder; # build GUI forms use Asset_Mgr; # custom/local AM module use strict; # Enforce declarations use vars qw(@EXPORT); require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( adsl_routers_ctrl ); #********************************************************************* +********* # # Function : adsl_routers_ctrl # # Description : Main processing loop. # Having got the new settings, insert or update the # adsl_routers table as appropriate. # Note that we never delete a box, we just set it's st +atus # to 'DECOMMISSIONED'. # # Params : $_[0] action CREATE/UPDATE # $_[1] asset id (if UPDATE) # # Returns : none # #********************************************************************* +********* sub adsl_routers_ctrl { my ( $param_action, # create/update from prev form $param_asset_id, # asset to update from prev form $form, # formbuilder object $fields, # hashref of form fields: name=>value $asset_vals, # hashref to DB row for specified asset id %tidy_fields # data for SQL processing ); # Create the form & display it $param_action = $_[0]; $param_asset_id = $_[1]; log_msg("adsl code: $param_action $param_asset_id"); if( uc($param_action) eq 'CREATE' ) { # CREATE # Create form layout ... $form = CGI::FormBuilder->new( name => 'adsl_routers', title => 'ADSL Routers Maintenance', text => "Create New ADSL Router", method => 'post', sticky => 0, header => 1, fields => [qw(ip_addr fqdn alias operating_system location purch_date purch_amt install_date end_of_life make model makers_serial makers_desc notes)], required => [qw(operating_system location purch_date purch_amt install_date end_of_life make model makers_serial makers_desc)], validate => { ip_addr => 'IPV4', fqdn => 'DOMAIN', purch_date => 'EUDATE', purch_amt => '/^[+-]?\d+(\.\d{1,2})?$/', install_date => 'EUDATE', end_of_life => 'EUDATE'} ); $form->field(name => 'purch_date', comment => '(DD/MM/YYYY)'); $form->field(name => 'install_date', comment => '(DD/MM/YYYY)'); $form->field(name => 'end_of_life', comment => '(DD/MM/YYYY)'); } elsif( uc($param_action) eq 'UPDATE' ) { # for updates, get current DB rec values if( defined($param_asset_id) ) { $asset_vals = db_get_adsl_routers($param_asset_id); } # Create form layout ... $form = CGI::FormBuilder->new( name => 'adsl_routers', title => 'ADSL Routers Maintenance', text => "Update Information for $param_asset_id", method => 'post', sticky => 0, header => 1, submit => 'ADSL', fields => [qw(ip_addr fqdn alias operating_system location old_status new_status purch_date purch_amt install_date commission_date end_of_life make model makers_serial makers_desc notes)], required => [qw(operating_system location new_status purch_date purch_amt install_date end_of_life make model makers_serial makers_desc)], validate => { ip_addr => 'IPV4', fqdn => 'DOMAIN', purch_date => 'EUDATE', purch_amt => '/^[+-]?\d+(\.\d{1,2})?$/', install_date => 'EUDATE', commission_date => 'EUDATE', end_of_life => 'EUDATE'} ); $form->field(name => 'asset_id', value => "$param_asset_id", type => 'hidden'); # for db update $form->field(name => 'old_status', type => 'hidden', sticky => 0 ); # for db update $form->field(name => 'new_status', comment => "(Current: $asset_vals->{'old_stat +us'})", options => \@Asset_Mgr::status_opts, type => 'select', selectname => 0, value => "$asset_vals->{'old_status'}" ); $form->field(name => 'purch_date', comment => '(DD/MM/YYYY)'); $form->field(name => 'install_date', comment => '(DD/MM/YYYY)'); $form->field(name => 'commission_date', comment => '(DD/MM/YYYY)'); $form->field(name => 'end_of_life', comment => '(DD/MM/YYYY)'); } log_msg("form defined"); # Check form if( $form->submitted ) { #DEBUG log_msg("submitted"); if( $form->validate ) { #DEBUG log_msg("validated"); # More data munging $fields = $form->field; %tidy_fields = do_data_tidy($param_action, %{$fields}); # Amend Database $cfg::err_flag = 0; db_start_txn() if( !$cfg::err_flag ); update_database(%tidy_fields) if( !$cfg::err_flag ); db_commit_txn() if( !$cfg::err_flag ); if( !$cfg::err_flag ) { print $form->confirm(sticky => 1,header => 1); } else { #DEBUG log_msg("mytrap"); # Invalid; show form and allow corrections # $form->field(name => 'new_status', # message => "Uknown change requested" # ); # $form->validate(new_status => 'NONE'); $cfg::err_flag = 0; print $form->render(sticky => 1, debug =>2); } } else { # Invalid; show form and allow corrections $cfg::err_flag = 0; print $form->render(sticky => 1); } } else { log_msg("not yet submitted"); # Clear flag & render (appropriate) form version $cfg::err_flag = 0; if( uc($param_action) eq 'CREATE' ) { # CREATE: # Print empty form for next part addition. print $form->render; } else { log_msg("render update"); # UPDATE: # Print form filled with values for selected asset_id print $form->render(values => $asset_vals); } } }
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found