commit 3c69d1dd72a3089eb373fc715bab628daf614499 Author: Brian Read Date: Fri Sep 5 17:35:28 2025 +0200 Upload files to "/" diff --git a/Example1.pm b/Example1.pm new file mode 100644 index 0000000..12eac9d --- /dev/null +++ b/Example1.pm @@ -0,0 +1,321 @@ +package SrvMngr::Controller::Example1; +# +# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-09-05 16:05:03 +# Remember that each route must be unique (else they just overwrite each other). +# you cannot have get and post on the same name and url. +# +#---------------------------------------------------------------------- +# heading : Network +# description : Example 1 +# navigation : 2000 400 +# +# name : example1, method : get, url : /example1, ctlact : Example1#main +# name : example1u, method : post, url : /example1u, ctlact : Example1#do_update +# name : example1d, method : get, url : /example1d, ctlact : Example1#do_display +# +# routes : end +# +# Documentation: https://wiki.contribs.org/Example1 +#---------------------------------------------------------------------- + +# +# Scheme of things: +# +# TBA!! + +use strict; +use warnings; +use Mojo::Base 'Mojolicious::Controller'; + +use constant FALSE => 0; +use constant TRUE => 1; + +use Locale::gettext; +use SrvMngr::I18N; +use SrvMngr qw(theme_list init_session); + +use Data::Dumper; + +use esmith::util; +use esmith::util::network; +use esmith::ConfigDB::UTF8; +use esmith::AccountsDB; +use esmith::NetworksDB::UTF8; +use esmith::HostsDB; +use esmith::DomainsDB::UTF8; + +our $cdb; +our $adb; +our $ndb; +our $hdb; +our $ddb; + +our %ex1_data; + +require '/usr/share/smanager/lib/SrvMngr/Controller/Example1-Custom.pm'; #The code that is to be added by the developer + +sub main { +# +# Initial entry - route is "/" +# +#set initial panel +#for initial panel: + #Specifiy panel to enter + #load up _data hash with DB fields + #load up stash with pointer(s) to control fields hash(= get-)) + #and a pointer to the prefix_data hash +#render initial panel + + my $c = shift; + $c->app->log->info( $c->log_req ); + + #The most common ones - you might want to delete some of these if they are not used. + $cdb = esmith::ConfigDB::UTF8->open() || die("Couldn't open config db"); + $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db"); + $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db"); + $hdb = esmith::HostsDB::UTF8->open() || die("Couldn't open Hosts db"); + $ddb = esmith::DomainsDB::UTF8->open() || die("Couldn't open Domains db"); + + %ex1_data = (); + my $title = $c->l('ex1_Example_1'); + my $modul = ''; + + $ex1_data{'trt'} = 'TABLE'; + + #Load any DB entries into the _data area so as they are preset in the form + # which DB - this only really works if the initial panel is a PARAMS type panel and not a TABLE + my $db = $cdb; #pickup local or global db or Default to config + + + $c->do_display($ex1_data{'trt'}); + +} + +# Post request with params - submit from the form +sub do_update { +# +# Return after submit pushed on panel (this is a post) - route is "/u" +# parameters in the params hash. +# +#load up all params into prefix_data hash: +#By panel (series of if statements - only one executed): + #call validate-PANEL() - return ret = ok or error message + +#if validation not ok: + #render back to current panel with error message in stash +#otherwise: + #By panel (series of if statements - only one executed): + #do whatever is required: call perform-PANEL() - return 'ok' or Error Message + #call signal-event for any global actions specified (check it exists - error and continue?) + #if action smeserver--update exists + #signal_event smeserver--update + #call signal-event for any specific actions for thids panel (check it exists first - error and continue) + #set success in stash + #if no "nextpanel" entry: + #set firstpanel + #else + #set nextpanel + #call render + + my $c = shift; + $c->app->log->info($c->log_req); + my $modul = ''; + + #The most common ones - you might want to delete some of these if they are not used. + $cdb = esmith::ConfigDB::UTF8->open() || die("Couldn't open config db"); + $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db"); + $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db"); + $hdb = esmith::HostsDB::UTF8->open() || die("Couldn't open Hosts db"); + $ddb = esmith::DomainsDB::UTF8->open() || die("Couldn't open Domains db"); + + my $title = $c->l('ex1_Example_1'); + + # Accessing all POST/GET parameters + my $params = $c->req->params->to_hash; + + # Get number of POST parameters + #my $num_params = keys scaler %$params; + + #Params are available in the hash "params" - copy to the prefix_data hash + #while (my ($key, $value) = each %{$c->req->params->to_hash}) { + # $ex1_data{$key} = $value; + #} + + # the value of trt will tell you which panel has returned + my $trt = $c->param('trt') || 'TABLE'; #hidden control on every form. + my $ret = 'ok'; + + #Validate the parameters in a custom sub one for each panel (although only one of these will be executed) + my $thispanel; + + if ($trt eq 'PARAMS'){ + #Validate form parameters for panel PARAMS + $ret = $c->validate_PARAMS(\%ex1_data); + $thispanel = 'PARAMS'; + } + + if ($trt eq 'TABLE'){ + #Validate form parameters for panel TABLE + $ret = $c->validate_TABLE(\%ex1_data); + $thispanel = 'TABLE'; + } + + if ($ret ne 'ok'){ + $c->stash(error => $c->l($ret)); + $c->do_display($thispanel); + } else { + #Do whatever is needed, including writing values to the DB + + + if ($trt eq 'PARAMS'){ + #do whatever is required ... + $ret = $c->perform_PARAMS(\%ex1_data); + if ($ret ne 'ok') { + # return to the panel with error message + $c->stash(error => $c->l($ret)); + $c->stash( + title => $title, + modul => $modul, + ex1_data => \%ex1_data + ); + $c->render(template => "example1"); + return + } else { + $c->stash( success => $c->l('ex1_PARAMS_panel_action_was_successful')); #A bit bland - edit it in the lex file + } + } + + if ($trt eq 'TABLE'){ + #do whatever is required ... + $ret = $c->perform_TABLE(\%ex1_data); + if ($ret ne 'ok') { + # return to the panel with error message + $c->stash(error => $c->l($ret)); + $c->stash( + title => $title, + modul => $modul, + ex1_data => \%ex1_data + ); + $c->render(template => "example1"); + return + } else { + $c->stash( success => $c->l('ex1_TABLE_panel_action_was_successful')); #A bit bland - edit it in the lex file + } + } + + # and call any signal-events needed + #TBD + # Setup shared data and call panel + if ('none' eq 'none') { + $ex1_data{'trt'} = 'TABLE'; + } else { + $ex1_data{'trt'} = 'none'; + } + $c->do_display($ex1_data{'trt'}); + } +} + +sub do_display { +# +# Return after link clicked in table (this is a get) - route is "/d" +# Expects ?trt=PANEL&selected="TableRowName" plus any other required +# +# OR it maybe a post from the main panel to add a new record +# +#load up all supplied params into prefix_data hash +#call get-selected-PANEL() - returns hash of all relevent parameters +#load up returned hash into prefix_data +#render - to called panel + + my ($c,$trt) = @_; + $c->app->log->info($c->log_req); + + #The most common ones - you might want to delete some of these if they are not used. + $cdb = esmith::ConfigDB::UTF8->open() || die("Couldn't open config db"); + $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db"); + $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db"); + $hdb = esmith::HostsDB::UTF8->open() || die("Couldn't open Hosts db"); + $ddb = esmith::DomainsDB::UTF8->open() || die("Couldn't open Domains db"); + + my $title = $c->l('ex1_Example_1'); + my $modul = ''; + + # Accessing all parameters + my $params = $c->req->params->to_hash; + + # Get number of parameters + my $num_params = keys %$params; + + #Tag as Post or Get (ie. create new entry or edit existing one + my $is_new_record = ($c->req->method() eq 'POST'); + + #Params are available in the hash "params" - copy to the prefix_data hash + #while (my ($key, $value) = each %{$c->req->params->to_hash}) { + # $ex1_data{$key} = $value; + #} + + # the value of trt will tell you which panel has returned + if (! $trt){ + $trt = $c->param('trt') || 'TABLE'; #Indicates where to go now + } + + # Now add in the params from the selected row from the table + + my %selectedrow; + + if ($trt eq 'PARAMS'){ + #Validate Get selected row (if applicable) PARAMS + %selectedrow = $c->get_selected_PARAMS($ex1_data{'Selected'},$is_new_record); + } + + if ($trt eq 'TABLE'){ + #Validate Get selected row (if applicable) TABLE + %selectedrow = $c->get_selected_TABLE($ex1_data{'Selected'},$is_new_record); + } + + + #Copy in the selected row params to the prefix_data hash to pass to the panel + while (my ($key, $value) = each %selectedrow){ + $ex1_data{$key} = $value; + } + # Where to go now + $ex1_data{'trt'} = $trt; + + # Set up other shared data according to the panel to go to + + if ($trt eq 'PARAMS'){ + # pickup any other contents needed and load them into hash shared with panel + my %returned_hash; + # subroutine returns a hash directly + %returned_hash = $c->get_data_for_panel_PARAMS(); + # Copy each key-value pair from the returned hash to the prefix data hash + while (my ($key, $value) = each %returned_hash) { + $ex1_data{$key} = $value; + } + } + + if ($trt eq 'TABLE'){ + # pickup any other contents needed and load them into hash shared with panel + my %returned_hash; + # subroutine returns a hash directly + %returned_hash = $c->get_data_for_panel_TABLE(); + # Copy each key-value pair from the returned hash to the prefix data hash + while (my ($key, $value) = each %returned_hash) { + $ex1_data{$key} = $value; + } + } + + + # and table control fields + $c->stash(ibays=>$c->get_ibays()); + + + # Data for panel + $c->stash( + title => $title, + modul => $modul, + ex1_data => \%ex1_data + ); + $c->render(template => "example1"); +} +1; \ No newline at end of file diff --git a/_ex1_PARAMS.html.ep b/_ex1_PARAMS.html.ep new file mode 100644 index 0000000..2a43884 --- /dev/null +++ b/_ex1_PARAMS.html.ep @@ -0,0 +1,50 @@ +%# +%# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-09-05 16:05:03 +%# +
+%# + % if (config->{debug} == 1) { +
+			%= dumper $ex1_data
+		
+ % } + % my $btn = l('ex1_SAVE'); + % $c->param(Selected => undef); #This may need deleting for a params panel - only needed for a table + + %= form_for "example1u" => (method => 'POST') => begin + % param 'trt' => $ex1_data->{trt} unless param 'trt'; + %= hidden_field 'trt' => $ex1_data->{trt} + %# Inputs etc in here. + +

<%=l('ex1_Example_Contrib')%>

+ +

<%=l('ex1_Manage_Ibay_settings:')%>

+ +

+ %=l('ex1_These_parameters_will_be_effective') +

+ +

+ %=l('ex1_Information_Bay_name') + + % param 'IbayName' => $ex1_data->{IbayName} unless param 'IbayName'; + %= text_field 'IbayName', size => '50', class => 'textinput IbayName' , pattern=>'.*' , placeholder=>'IbayName', title =>'Pattern regex mismatch', id => 'IbayName_text' +

+ +

+ %=l('ex1_Share_owner_Group') + + % my @ShareOwnerGrp_options = [['Write = admin, Read = group' => 'All'], ['Write = group, Read = everyone' => 'Read:All-Write:Grp']]; + % param 'ShareOwnerGrp' => $ex1_data->{ShareOwnerGrp} unless param 'ShareOwnerGrp'; + %= select_field 'ShareOwnerGrp' => @ShareOwnerGrp_options, class => 'input', id => 'ShareOwnerGrp_select' +

+ + %# .... + %# Probably finally by a submit. + %= submit_button $btn, class => 'action' + %end +
\ No newline at end of file diff --git a/_ex1_TABLE.html.ep b/_ex1_TABLE.html.ep new file mode 100644 index 0000000..79151e0 --- /dev/null +++ b/_ex1_TABLE.html.ep @@ -0,0 +1,54 @@ +%# +%# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-09-05 16:05:03 +%# +
+%# + % if (config->{debug} == 1) { +
+			%= dumper $ex1_data
+		
+ % } + % my $btn = l('ex1_SAVE'); + % $c->param(Selected => undef); #This may need deleting for a params panel - only needed for a table + + %= form_for "example1u" => (method => 'POST') => begin + % param 'trt' => $ex1_data->{trt} unless param 'trt'; + %= hidden_field 'trt' => $ex1_data->{trt} + %# Inputs etc in here. + +

<%=l('ex1_NFS_Share_Contrib')%>

+ +

<%=l('ex1_Manage_NFS_Ibay_settings:')%>

+ +
+ + + + + + + + + + % my $control_data = $c->stash('ibays'); + % foreach my $row (@$control_data) { + + + + + + + %} + +
<%=l('ex1_Name')%><%=l('ex1_Description')%><%=l('ex1_Status')%><%=l('ex1_Action')%>
<%=$c->render_to_string(inline=>$row->{'Name'})%><%=$c->render_to_string(inline=>$row->{'Description'})%><%=$c->render_to_string(inline=>$row->{'flag'})%><%=$c->render_to_string(inline=>$row->{'Modify'})%>
+ + + %# .... + %# Probably finally by a submit. + %= submit_button $btn, class => 'action' + %end +
\ No newline at end of file diff --git a/example1.html.ep b/example1.html.ep new file mode 100644 index 0000000..88a0408 --- /dev/null +++ b/example1.html.ep @@ -0,0 +1,56 @@ +%# +%# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-09-05 16:05:03 +%# +% layout 'default', title => "Sme server 2 - Example 1", share_dir => './'; +%# css specific to this panel: +% content_for 'module' => begin +%= stylesheet '/css/example1.css' +%= javascript '/js/example1.js' +
+ + % if (config->{debug} == 1) { +
+		%= dumper $c->current_route
+		%= dumper $ex1_data->{trt}
+	
+ % } + +

<%=$title%>

+ + % if ( stash('modul')) { + %= $c->render_to_string(inline => stash('modul') ); + % } + + %if ($c->stash('first')) { +

+ %=$c->render_to_string(inline =>$c->l($c->stash('first'))) +

+ + %} elsif ($c->stash('success')) { +
+ %= $c->l($c->stash('success')); +
+
+ + %} elsif ($c->stash('error')) { +
+ %= $c->l($c->stash('error')); +
+
+ %} + + %#Routing to partials according to trt parameter. + %#This ought to be cascading if/then/elsif, but is easier to just stack the if/then's rather like a case statement' + + % if ($ex1_data->{trt} eq "PARAMS") { + %= include 'partials/_ex1_PARAMS' + %} + + % if ($ex1_data->{trt} eq "TABLE") { + %= include 'partials/_ex1_TABLE' + %} + + + +
+%end \ No newline at end of file diff --git a/example1_en.lex b/example1_en.lex new file mode 100644 index 0000000..52548bf --- /dev/null +++ b/example1_en.lex @@ -0,0 +1,18 @@ +# +# Generated by SM2Gen version: SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-09-05 16:05:03 +# +'ex1_Description' => 'Description', +'ex1_SAVE' => 'Save', +'ex1_Action' => 'Action', +'ex1_These_parameters_will_be_effective' => 'These parameters will be effective only if the share is enabled The share is in /home/e-smith/files/ibays//files', +'ex1_PARAMS_panel_action_was_successful' => 'PARAMS panel action was successful', +'ex1_Manage_Ibay_settings:' => 'Manage bay settings', +'ex1_Name' => 'Name', +'ex1_Example_1' => 'Example 1', +'ex1_NFS_Share_Contrib' => 'NFS Share control', +'ex1_Status' => 'Status', +'ex1_Example_Contrib' => 'Example control', +'ex1_Manage_NFS_Ibay_settings:' => 'Manage NFS bay settings', +'ex1_Information_Bay_name' => 'Information Bay name', +'ex1_TABLE_panel_action_was_successful' => 'TABLE panel action was successful', +'ex1_Share_owner_Group' => 'Share owner Group',