Table display working - added custom controller file
This commit is contained in:
@@ -13,6 +13,38 @@ package SrvMngr::Controller::${PackageName};
|
||||
# Documentation: https://wiki.contribs.org/{PackageName}
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
#
|
||||
# Scheme of things:
|
||||
#
|
||||
#main:
|
||||
##
|
||||
## Initial entry
|
||||
##
|
||||
#set initial panel
|
||||
#for initial panel:
|
||||
# load up _data hash with DB fields
|
||||
# load up stash with pointer(s) to control fields hash(= get-))
|
||||
#render initial panel
|
||||
#
|
||||
#do_display
|
||||
##
|
||||
## Return after submit pushed on panel
|
||||
##
|
||||
#load up all params into local hash
|
||||
# by panel:
|
||||
# by param:
|
||||
# validate param (return ret = ok or error message) - call validate-
|
||||
# break out on error
|
||||
#if validation not ok
|
||||
# render back to current panel with error message in stash
|
||||
#otherwise
|
||||
# By panel:
|
||||
# Copy back to DB any that have changed (how to easily tell if it has changed?)
|
||||
# do whatever is required (inc signal_event smeserver-<whatever>-update?)
|
||||
# set success
|
||||
# call main?
|
||||
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
@@ -41,6 +73,8 @@ our $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
|
||||
our $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
|
||||
our $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
|
||||
|
||||
require '/usr/share/smanager/lib/SrvMngr/Controller/${PackageName}-Custom.pm'; #The code that is to be added by the developer
|
||||
|
||||
sub main {
|
||||
|
||||
my $c = shift;
|
||||
@@ -50,15 +84,18 @@ sub main {
|
||||
my $title = $c->l("${prefix}_${MenuDescription}");
|
||||
my $modul = '';
|
||||
|
||||
$$${prefix}_data{trt} = '${firstPanel}';
|
||||
$$${prefix}_data{'trt'} = '${firstPanel}';
|
||||
|
||||
#Load any DB entries into the <prefix>_data area so as they are preset in the form
|
||||
# which DB
|
||||
my $db = $$${db | 'cdb'}; #Default to config
|
||||
<tal:block tal:repeat="dbentry dbentries">
|
||||
$$${prefix}_data{${dbentry}} = $db->prop('${dbentry}') || ${dbdefault}
|
||||
my $db = $$${controldb | db | 'cdb'}; #pickup local or global db or Default to config
|
||||
<tal:block tal:repeat="dbentry dbentries">$$${prefix}_data{${dbentry}} = $db->prop('${dbentry}') || ${dbdefault};
|
||||
</tal:block>
|
||||
|
||||
# and table control fields
|
||||
<tal:block tal:repeat="tablecontrol tablecontrols"> $c->stash(${tablecontrol}=>get_${tablecontrol}());
|
||||
</tal:block>
|
||||
|
||||
$c->stash(
|
||||
title => $title,
|
||||
modul => $modul,
|
||||
@@ -82,8 +119,10 @@ sub do_update {
|
||||
|
||||
#Params are available in the hash "params"
|
||||
# you may use:
|
||||
my @result;
|
||||
foreach my $key (keys %params) {
|
||||
my $value = $params{$key};
|
||||
push @result, { $key => $value };
|
||||
$c->app->log->debug("$key: $value");
|
||||
}
|
||||
|
||||
@@ -91,37 +130,33 @@ sub do_update {
|
||||
my $trt = $c->param('trt') || '${firstPanel}' ; #hidden control on every form.
|
||||
my $ret = 'ok';
|
||||
#Validate the parameters accordingly
|
||||
<tal:block tal:repeat="condition conditions">
|
||||
if ($trt eq '${condition}'){
|
||||
#Validate form parameters for panel ${condition}
|
||||
# and set $ret = $c->l('Error message') if invalid'
|
||||
# otherwise set $ret to "ok"
|
||||
if ($ret ne "ok"){
|
||||
$c->stash(error => $c->l($ret))
|
||||
} else {
|
||||
#Do whatever is needed, including writing values to the DB
|
||||
my $db = $$${db | 'cdb'}; #Default to config
|
||||
<tal:block tal:repeat="dbentry dbentries">
|
||||
my $thispanel;
|
||||
<tal:block tal:repeat="panel panels">
|
||||
if ($trt eq '${panel}'){
|
||||
#Validate form parameters for panel ${panel}
|
||||
$ret = validate_${panel}();
|
||||
$thispanel = '${panel}';
|
||||
}
|
||||
</tal:block>
|
||||
if ($ret eq "ok") {
|
||||
#Do whatever is needed, including writing values to the DB
|
||||
my $db = $$${controldb | db | 'cdb'}; #pickup local or global db or Default to config
|
||||
<tal:block tal:repeat="dbentry dbentries">
|
||||
$db->set_prop('${dbentry}'=> param{'${dbentry}'}; #Copy back into db
|
||||
$$${prefix}_data{${dbentry}} = $db->prop('${dbentry}'); #Copy out into stash
|
||||
</tal:block>
|
||||
|
||||
} $c->stash( success => $c->l('ok message'))
|
||||
}
|
||||
</tal:block>
|
||||
# set ${prefix}_data{trt} = <route>;
|
||||
$c->stash(
|
||||
title => $title,
|
||||
${prefix}_data => \%${prefix}_data
|
||||
# Extra data in here - repeat for each stash data entry needed for panels
|
||||
);
|
||||
|
||||
$c->render("${PackageName}")
|
||||
</tal:block>
|
||||
# anything else here...
|
||||
$c->stash( success => $c->l("$thispanel successfull message"));
|
||||
$c->stash(
|
||||
title => $title,
|
||||
${prefix}_data => \%${prefix}_data
|
||||
# Extra data in here - repeat for each stash data entry needed for panels
|
||||
);
|
||||
${prefix}_data{'trt'} = '${firstPanel}';
|
||||
$c->render("${PackageName}");
|
||||
}
|
||||
}
|
||||
|
||||
# get routines for the stash contents here.
|
||||
|
||||
|
||||
1;
|
||||
|
||||
|
||||
|
44
Templates/custom.pm.tem
Normal file
44
Templates/custom.pm.tem
Normal file
@@ -0,0 +1,44 @@
|
||||
#
|
||||
# Routines to be editted by the developer to provide validation for parameters
|
||||
# and provison of the control data for table(s)
|
||||
#
|
||||
#$cdb=$main::cdb;
|
||||
#$adb=$main::adb;
|
||||
#$ndb=$main::ndb;
|
||||
#$hdb=$main::hdb;
|
||||
#$ddb=$main::ddb;
|
||||
|
||||
use esmith::util;
|
||||
use esmith::HostsDB;
|
||||
use esmith::AccountsDB;
|
||||
use esmith::NetworksDB;
|
||||
use esmith::HostsDB;
|
||||
use esmith::DomainsDB;
|
||||
|
||||
|
||||
#The most common ones
|
||||
our $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
|
||||
our $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
|
||||
our $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
|
||||
our $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
|
||||
our $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
|
||||
|
||||
# Validation routines - parameters for each panel
|
||||
<tal:block tal:repeat="panel panels">
|
||||
sub validate_${panel} {
|
||||
$ret = 'ok';
|
||||
return $ret;
|
||||
}
|
||||
</tal:block>
|
||||
|
||||
# Get control data for tables(s)
|
||||
<tal:block tal:repeat="tablecontrol tablecontrols">
|
||||
sub get_${tablecontrol} {
|
||||
return []
|
||||
}
|
||||
</tal:block>
|
||||
1;
|
||||
|
||||
|
||||
|
||||
|
@@ -1,10 +0,0 @@
|
||||
{
|
||||
Text:"\n<p>\n<span class=label>\n%=l('${Label}'), class => 'label'\n</span><span class=data>\n%= ${Value}, class => 'data'\n</span>\n</p>",
|
||||
Selection:"\n\n<p><span class=label>\n%=l('${Label}')\n</span><span class=data>\n% my @${Name}_options = ${Value};\n% param '${Name}' => $$${prefix}_data->{${Name}} unless param '${Name}';\n%= select_field '${Name}' => @${Name}_options, class => 'input'\n<br></span></p>",
|
||||
Textarea:"\n\n<span class=label>\n%=l('${Label}')\n</span><span class=data>\n% param '${Name}' => $$${prefix}_data->{${Name}} unless param '${Name}';\n%= text_area '${Name}', cols=>50, rows=>15\n</span><br>",
|
||||
Date:"\n\n<span class=label>\n%=$%=l('${Label}')\n</span><span class=data>\n%=date_field '${Name}' =>$$${Name}\n</span><br>",
|
||||
Textinput:"\n\n<p><span class=label>\n%=l('${Label}')\n</span><span class=data>\n% param '${Name}' => $$${prefix}_data->{${Name}} unless param '${Name}';\n%= text_field '${Name}', size => '60', class => 'input'\n<br></span></p>",
|
||||
SubHeader:"\n<h2>${value}</h2>",
|
||||
Paragraph:"\n<p>${value}</p>",
|
||||
Submit:"\n<span class='data'>\n%= submit_button l('${value}'), class => 'action'\n</span>"
|
||||
}
|
@@ -101,14 +101,14 @@
|
||||
<table class="sme-border TableSort">
|
||||
<thead>
|
||||
<tr><tal:block tal:repeat="ColHead TopHeadings">
|
||||
<td>${ColHead}</td></tal:block>
|
||||
<th class='sme-border'>${ColHead}</th></tal:block>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
% my $control_data = $self->stash('${Control}');
|
||||
% my $control_data = $self->stash('${TableControl}');
|
||||
% foreach my $row (@$control_data) {
|
||||
<tr><tal:block tal:repeat="ColContent Columns">
|
||||
<td><%=$row->${ColContent}%></td></tal:block>
|
||||
<td class='sme-border'><%=$row->{${ColContent}}%></td></tal:block>
|
||||
</tr>
|
||||
%}
|
||||
</tbody>
|
||||
|
Reference in New Issue
Block a user