SM2Gen/Templates/controller.pm.tem

285 lines
9.0 KiB
Plaintext
Raw Normal View History

2024-04-09 11:33:50 +02:00
package SrvMngr::Controller::${PackageName};
#
2024-09-16 15:01:17 +02:00
# Generated by ${version}
#
2024-04-09 11:33:50 +02:00
#----------------------------------------------------------------------
# heading : ${MenuHeading}
# description : ${MenuDescription}
# navigation : ${MenuNavigation}
#
# name : ${lcPackageName}, method : get, url : /${lcPackageName}, ctlact : ${PackageName}#main
# name : ${lcPackageName}u, method : post, url : /${lcPackageName}u, ctlact : ${PackageName}#do_update
# name : ${lcPackageName}d, method : get, url : /${lcPackageName}d, ctlact : ${PackageName}#do_display
2024-04-09 11:33:50 +02:00
#
# routes : end
#
2024-04-28 13:03:06 +02:00
# Documentation: https://wiki.contribs.org/${PackageName}
2024-04-09 11:33:50 +02:00
#----------------------------------------------------------------------
#
# Scheme of things:
#
2024-04-28 13:03:06 +02:00
# TBA!!
2024-04-09 11:33:50 +02:00
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;
2024-04-09 11:33:50 +02:00
use esmith::AccountsDB;
use esmith::NetworksDB;
use esmith::HostsDB;
use esmith::DomainsDB;
#The most common ones
2024-04-24 17:09:23 +02:00
our $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
2024-04-09 11:33:50 +02:00
our $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
our $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
2024-04-24 17:09:23 +02:00
our $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
our $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
2024-04-09 11:33:50 +02:00
require '/usr/share/smanager/lib/SrvMngr/Controller/${PackageName}-Custom.pm'; #The code that is to be added by the developer
2024-04-09 11:33:50 +02:00
sub main {
2024-04-28 13:03:06 +02:00
#
# Initial entry - route is "/<whatever>"
#
#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
2024-04-09 11:33:50 +02:00
my $c = shift;
$c->app->log->info( $c->log_req );
my %${prefix}_data = ();
2024-05-06 11:05:43 +02:00
my $title = $c->l('${prefix}_${MenuDescription}');
2024-04-09 11:33:50 +02:00
my $modul = '';
$$${prefix}_data{'trt'} = '${firstPanel}';
2024-04-24 17:09:23 +02:00
#Load any DB entries into the <prefix>_data area so as they are preset in the form
2024-04-28 13:03:06 +02:00
# which DB - this only really works if the initial panel is a PARAMS type panel and not a TABLE
my $db = $$${controldb | db | 'cdb'}; #pickup local or global db or Default to config
<tal:block tal:repeat="dbfield dbfields">
2024-11-22 16:09:21 +01:00
$$${prefix}_data{'${dbfield}'} = $db->prop('${dbfield}') || ${dbdefault | ""} || "";
2024-04-24 17:09:23 +02:00
</tal:block>
#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_${firstPanel}();
# Copy each key-value pair from the returned hash to the prefix data hash
while (my ($key, $value) = each %returned_hash) {
$$${prefix}_data{$key} = $value;
}
2024-04-09 11:33:50 +02:00
# and table control fields
<tal:block tal:repeat="tablecontrol tablecontrols">$c->stash(${tablecontrol[0]}=>$c->get_${tablecontrol[0]}());
</tal:block>
2024-04-09 11:33:50 +02:00
$c->stash(
title => $title,
modul => $modul,
${prefix}_data => \%${prefix}_data
);
$c->render( template => "${lcPackageName}" );
2024-04-09 11:33:50 +02:00
}
2024-05-06 11:05:43 +02:00
# Post request with params - submit from the form
2024-04-09 11:33:50 +02:00
sub do_update {
2024-04-28 13:03:06 +02:00
#
# Return after submit pushed on panel (this is a post) - route is "/<whatever>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-<whatever>-update exists
#signal_event smeserver-<whatever>-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
2024-04-09 11:33:50 +02:00
my $c = shift;
$c->app->log->info($c->log_req);
2024-11-22 15:00:54 +01:00
my $modul = '';
2024-04-09 11:33:50 +02:00
my %${prefix}_data = ();
2024-05-06 11:05:43 +02:00
my $title = $c->l('${prefix}_${MenuDescription}');
2024-04-09 11:33:50 +02:00
# Accessing all POST parameters
my %params = $c->req->params->to_hash;
# Get number of POST parameters
my $num_params = keys %params;
2024-04-28 13:03:06 +02:00
#Params are available in the hash "params" - copy to the prefix_data hash
#while (my ($key, $value) = each %{$c->req->params->to_hash}) {
# $$${prefix}_data{$key} = $value;
#}
2024-04-09 11:33:50 +02:00
# the value of trt will tell you which panel has returned
2024-04-28 13:03:06 +02:00
my $trt = $c->param('trt') || '${firstPanel}'; #hidden control on every form.
2024-04-09 11:33:50 +02:00
my $ret = 'ok';
2024-04-28 13:03:06 +02:00
#Validate the parameters in a custom sub one for each panel (although only one of these will be executed)
my $thispanel;
<tal:block tal:repeat="panel panels">
if ($trt eq '${panel}'){
#Validate form parameters for panel ${panel}
2024-09-03 13:01:17 +02:00
$ret = $c->validate_${panel}(\%${prefix}_data);
$thispanel = '${panel}';
}
</tal:block>
2024-04-28 13:03:06 +02:00
if ($ret ne "ok") {
2024-11-22 16:09:21 +01:00
${prefix}_data{'trt'} = $thispanel;
2024-04-28 13:03:06 +02:00
$c->stash(error => $c->l($ret));
2024-11-22 15:00:54 +01:00
$c->stash(
title => $title,
modul => $modul,
${prefix}_data => \%${prefix}_data
);
$c->render( template => "${lcPackageName}" );
2024-04-28 13:03:06 +02:00
} else {
#Do whatever is needed, including writing values to the DB
2024-04-28 13:03:06 +02:00
<tal:block tal:repeat="panel panels">
if ($trt eq '${panel}'){
#do whatever is required ...
2024-09-03 13:01:17 +02:00
$ret = $c->perform_${panel}(\%${prefix}_data);
2024-04-28 13:03:06 +02:00
if ($ret ne "ok") {
# return to the panel with error message
$c->stash(error => $c->l($ret));
2024-11-22 16:09:21 +01:00
$c->stash(
title => $title,
modul => $modul,
${prefix}_data => \%${prefix}_data
);
$c->render(template => "${lcPackageName}");
2024-04-28 13:03:06 +02:00
} else {
$c->stash( success => $c->l('${panel} panel action was successful')); #A bit bland - edit it in the lex file
2024-04-28 13:03:06 +02:00
}
}
</tal:block>
2024-04-28 13:03:06 +02:00
# and call any signal-events needed
2024-11-22 16:09:21 +01:00
#TBD
2024-04-28 13:03:06 +02:00
# Setup shared data and call panel
if ('${nextpanel | "none"}' eq 'none') {
$$${prefix}_data{'trt'} = '${firstPanel}';
} else {
$$${prefix}_data{'trt'} = '${NextPanel | "none"}';
}
2024-11-22 16:09:21 +01:00
$c->stash(
title => $title,
modul => $modul,
${prefix}_data => \%${prefix}_data
);
$c->render( template => "${lcPackageName}" );
2024-04-28 13:03:06 +02:00
}
2024-04-09 11:33:50 +02:00
}
2024-04-28 13:03:06 +02:00
sub do_display {
#
# Return after link clicked in table (this is a get) - route is "/<whatever>d"
# Expects ?trt=PANEL&selected="TableRowName" plus any other required
#
2024-05-06 11:05:43 +02:00
# OR it maybe a post from the main panel to add a new record
#
2024-04-28 13:03:06 +02:00
#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 = shift;
$c->app->log->info($c->log_req);
my %${prefix}_data = ();
2024-05-06 11:05:43 +02:00
my $title = $c->l('${prefix}_${MenuDescription}');
2024-04-28 13:03:06 +02:00
2024-05-06 11:05:43 +02:00
# Accessing all parameters
2024-04-28 13:03:06 +02:00
my %params = $c->req->params->to_hash;
2024-05-06 11:05:43 +02:00
# Get number of parameters
2024-04-28 13:03:06 +02:00
my $num_params = keys %params;
2024-05-06 11:05:43 +02:00
#Tag as Post or Get (ie. create new entry or edit existing one
my $is_new_record = ($c->req->method() eq 'POST');
2024-04-28 13:03:06 +02:00
#Params are available in the hash "params" - copy to the prefix_data hash
#while (my ($key, $value) = each %{$c->req->params->to_hash}) {
# $$${prefix}_data{$key} = $value;
#}
2024-04-28 13:03:06 +02:00
# the value of trt will tell you which panel has returned
my $trt = $c->param('trt') || '${firstPanel}'; #Indicates where to go now
# Now add in the params from the selected row from the table
2024-05-06 11:05:43 +02:00
2024-04-28 13:03:06 +02:00
my %selectedrow;
<tal:block tal:repeat="panel panels">
if ($trt eq '${panel}'){
#Validate form parameters for panel ${panel}
2024-09-03 14:58:08 +02:00
%selectedrow = $c->get_selected_${panel}($$${prefix}_data{'Selected'},$is_new_record);
2024-04-28 13:03:06 +02:00
}
</tal:block>
#Copy in the selected row params to the prefix_data hash to pass to the panel
while (my ($key, $value) = each %selectedrow){
$$${prefix}_data{$key} = $value;
}
# Where to go now
$$${prefix}_data{'trt'} = $trt;
# Set up other shared data according to the panel to go to
<tal:block tal:repeat="panel panels">
if ($trt eq '${panel}'){
# 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_${panel}();
# Copy each key-value pair from the returned hash to the prefix data hash
while (my ($key, $value) = each %returned_hash) {
$$${prefix}_data{$key} = $value;
}
}
</tal:block>
# and table control fields
<tal:block tal:repeat="tablecontrol tablecontrols">$c->stash(${tablecontrol[0]}=>$c->get_${tablecontrol[0]}());
</tal:block>
2024-04-28 13:03:06 +02:00
# Data for panel
$c->stash(
title => $title,
${prefix}_data => \%${prefix}_data
);
2024-11-22 16:09:21 +01:00
$c->render(template => "${lcPackageName}");
2024-04-28 13:03:06 +02:00
}
1;