MT#5879 Separated Model DB actions. Time spent with FormHandler and Catalyst::Adaptor.

ipeshinskaya/InvoiceTemplate5
Irina Peshinskaya 12 years ago committed by Victor Seva
parent a1cf4235ef
commit 468420909d

@ -17,6 +17,8 @@ use NGCP::Panel::Form::Customer::PbxFieldDevice;
use NGCP::Panel::Form::Customer::PbxFieldDeviceEdit;
use NGCP::Panel::Form::Customer::PbxFieldDeviceSync;
use NGCP::Panel::Form::Customer::InvoiceTemplate;
use NGCP::Panel::Model::DB::InvoiceTemplate;
use NGCP::Panel::Utils::Message;
use NGCP::Panel::Utils::Navigation;
use NGCP::Panel::Utils::DateTime;
@ -834,44 +836,66 @@ sub calls :Chained('base') :PathPart('calls') :Args(0) {
#$c->stash(template => 'customer/calls.tt');
# $c->stash(contract => $contract_rs->first);
}
sub loglong{
my ($str) = @_;
use Data::Dumper;
open(my $log,">>/tmp/irka.log");
print $log Dumper($str);
close $log;
}
sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
my ($self, $c, $in);
($self,$c,@$in{qw/tt_type tt_viewmode tt_sourcestate/}) = @_;
my ($self, $c) = @_;
#$c->log->debug($c->model('DB'));
#return;
#my $db = NGCP::Panel::Model::DB::InvoiceTemplate->new();
#$c->log->debug($db);
#my $contract_id = $in->{contract_id} = ;
no warnings 'uninitialized';
my($validator,$db,$in);
#input
(undef,undef,@$in{qw/contract_id tt_type tt_viewmode tt_sourcestate tt_id/}) = ( $c->stash->{contract}->id, @_ );
$in->{tt_string} = $c->request->body_parameters->{template} || '';
#die();
#$c->view('SVG');
#handle request
# my $tt_viewmode //= '';
# my $tt_state //= 'saved';
# my $tt_type //= 'svg';
my $invoicetemplate = $c->request->body_parameters->{template} || '';
#output
my $tt_string;
#input checking & simple preprocessing
$validator = NGCP::Panel::Form::Customer::InvoiceTemplate->new;
# $form->schema( $c->model('DB::InvoiceTemplate')->schema );
#to common form package ? removing is necessary due to FormHandler param presence evaluation - it is based on key presence, not on defined/not defined value
foreach ( keys %$in) { if(!( defined $in->{$_} )){ delete $in->{$_}; } };
#storage
#pass scheme here is ugly, and should be moved somehow to DB::Base
$db = NGCP::Panel::Model::DB::InvoiceTemplate->new( schema => $c->model('DB') );
#$c->log->debug("1.invoicetemplate is empty=".($invoicetemplate?0:1).";viewbox=".($invoicetemplate !~/^<svg.*?viewbox.*?>/is ).";\n");
$c->log->debug("1.invoicetemplate is empty=".($invoicetemplate?0:1).";viewbox=".($invoicetemplate !~/^<svg.*?viewbox.*?>/is ).";\n");
my $form = NGCP::Panel::Form::Customer::InvoiceTemplate->new;
$form->process(
#really, we don't need a form here at all
#just use as already implemented fields checking and defaults applying
$validator->setup_form(
posted => 1,
params => $in,
action => $c->uri_for_action("/customer/calls_svg", [$c->stash->{contract}->id]),
);
$form->validate();
if(!$invoicetemplate){
#getCustomerActiveInvoiceTemplateFromDB.
$c->log->debug("validated=".$validator->validated.";\n");
my $in_validated = $validator->fif;
#dirty hack
$in = $in_validated;
#really this is for code for field default in validator. The question is how to pass DB model to validator (formhandler) - ideologically correctly?
$tt_string = $in->{tt_string};
if(!$tt_string){
#here we also may be better should contact model, not DB directly. Will return to this separation later
#at the end - we can figure out rather basic controller behaviour
$tt_string = $db->getCustomerInvoiceTemplate( %$in );
}
if(!$invoicetemplate){
if(!$tt_string){
#getDefault
NGCP::Panel::Utils::InvoiceTemplate::getDefaultInvoiceTemplate( c => $c, result => \$tt_string );
try{
NGCP::Panel::Utils::InvoiceTemplate::getDefault( c => $c, invoicetemplate => \$invoicetemplate );
#Utils... mmm - maybe model?
NGCP::Panel::Utils::InvoiceTemplate::getDefaultInvoiceTemplate( c => $c, result => \$tt_string );
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
@ -880,18 +904,18 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
);
}
}#else{
#here we have invoice content - of customer or default, so no checking of invoicetemplate is necessary
#if($invoicetemplate){
# #here we have invoice content - of customer or default, so no checking of tt_string is necessary
#if($tt_string){
#sub candidate, preSaveCustomTemplate
#if it is presaving, making it for default isn't necessary, default should contain it
#but while let it be here
##moved to correct place - to js, generating svg
##### $c->log->debug("3.invoicetemplate is empty=".($invoicetemplate?0:1).";viewbox=".($invoicetemplate !~/^<svg.*?viewbox.*?>/is).";\n");
##### if( $invoicetemplate !~/^<svg.*?viewbox.*?>/is ){
##### (my ($width)) = ($invoicetemplate =~/^<svg.*?width.*?(\d+).*?>/is );
##### $c->log->debug("3.tt_string is empty=".($tt_string?0:1).";viewbox=".($tt_string !~/^<svg.*?viewbox.*?>/is).";\n");
##### if( $tt_string !~/^<svg.*?viewbox.*?>/is ){
##### (my ($width)) = ($tt_string =~/^<svg.*?width.*?(\d+).*?>/is );
##### $c->log->debug("width=$width;\n");
##### (my ($height)) = ($invoicetemplate =~/^<svg.*?height.*?(\d+).*?>/is );
##### my($replaced) = $invoicetemplate =~s/^<svg(.*?(?:width.*?height|height.*width).*?\d+.*? )/<svg $1 viewBox="0 0 $width $height" /i;
##### (my ($height)) = ($tt_string =~/^<svg.*?height.*?(\d+).*?>/is );
##### my($replaced) = $tt_string =~s/^<svg(.*?(?:width.*?height|height.*width).*?\d+.*? )/<svg $1 viewBox="0 0 $width $height" /i;
##### $c->log->debug("replaced=$replaced;\n");
##### #storeToDB
##### }
@ -899,25 +923,27 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
#prepare response
$c->response->content_type('image/svg+xml');
$c->log->debug("tt_viewmode=".$in->{tt_viewmode}.";\n");
#$c->log->debug("tt_viewmode=".$in->{tt_viewmode}.";\n");
$c->log->debug("tt_viewmode=;\n");
if($in->{tt_viewmode} eq 'raw'){
#$c->stash->{VIEW_NO_TT_PROCESS} = 1;
$c->response->body($invoicetemplate);
$c->response->body($tt_string);
return;
}else{
my $contacts = $c->model('DB')->resultset('contacts')->search({ id => $c->stash->{contract}->id });
my $contacts = $c->model('DB')->resultset('contacts')->search({ id => $in->{contract_id} });
#some preprocessing should be done only before showing. So, there will be:
#preSaveCustomTemplate prerpocessing
#preShowCustomTemplate prerpocessing
{
#preShowInvoice
$invoicetemplate =~s/(?:{\s*)?<!--{|}-->(?:\s*})?//gs;
#also to model
$tt_string =~s/(?:{\s*)?<!--{|}-->(?:\s*})?//gs;
}
$c->stash( provider => $contacts->first );
#use irka;
#irka::loglong(\$invoicetemplate);
$c->stash( template => \$invoicetemplate );
#irka::loglong(\$tt_string);
$c->stash( template => \$tt_string );
$c->log->debug("before_detach;\n");
$c->detach($c->view('SVG'));
}

@ -4,45 +4,48 @@ use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
enum 'TemplateType' => [ qw/svg/ ];#html
enum 'TemplateType' => [ qw/svg html/ ];#html
enum 'TemplateViewMode' => [ qw/raw parsed/ ];
enum 'TemplateSourceState' => [ qw/saved previewed/ ];
no Moose::Util::TypeConstraints;
#while use only for validation, no rendering is necessary
#use HTML::FormHandler::Widget::Block::Bootstrap;
#looks as often repeated
#has '+widget_wrapper' => ( default => 'Bootstrap' );
#sub build_form_element_class { [qw/form-horizontal/] }
#Attempt to use Moose to validation
#has 'sort_order' => (
# is => 'ro',
# isa => enum([qw[ ascending descending ]]),
#);
has '+use_fields_for_input_without_param' => ( default => 1 );
has_field 'tt_type' => (
# is => 'rw',
# isa => enum([qw[ svg ]]),#html
type => 'Text',
required => 0,
#apply => [ 'enum' ],
#apply => [ { check => [qw/svg/] } ],
required => 1,
default => 'svg',
#apply => [ qw/svg/ ],
apply => [ 'TemplateType' ],
);
has_field 'tt_viewmode' => (
type => 'Text',
required => 0,
apply => [ 'TemplateViewMode' ],
#check => [ qw/raw parsed/ ],
default => 'parsed',
);
has_field 'tt_sourcestate' => (
type => 'Text',
required => 1,
default => 'saved',
apply => [ 'TemplateSourceState' ],
#check => [ qw/saved previewed/ ],
);
has_field 'tt_string' => (
type => 'Text',
#default => \&
#apply => [ { check => \&validate_tt_string } ],
required => 0,
);
sub validate_tt_string{
#here could be following: take default from file and get all variables and validate variables from customer string
};
1;
=head1 NAME
@ -51,7 +54,7 @@ NGCP::Panel::Form::InvoiceTemplate
=head1 DESCRIPTION
Form to modify a invoice template.
Form to modify an invoice template.
=head1 METHODS

@ -0,0 +1,15 @@
package NGCP::Panel::Model::DB::Base;
use base 'Catalyst::Model::Adaptor';
use NGCP::Panel::Model::DB;
use Moose;
__PACKAGE__->config(
class => __PACKAGE__ ,
args => {},
);
has 'schema' => (
is => 'rw',
isa => 'NGCP::Panel::Model::DB',
);
1;

@ -0,0 +1,30 @@
package NGCP::Panel::Model::DB::InvoiceTemplate;
use base NGCP::Panel::Model::DB::Base;
sub getCustomerInvoiceTemplate{
my $self = shift;
my (%params) = @_;
my ($contract_id,$tt_sourcestate,$tt_type) = @params{qw/contract_id tt_sourcestate/};
my $result = '';
#my $tt_record = $self->resultset('invoice_template')->search({
my $tt_record = $self->schema->resultset('invoice_template')->search({
reseller_id => $contract_id,
is_active => 1,
type => $tt_type
})->first;
#here may be base64 decoding
#here we will rely on form checking and defaults
#if('saved' eq $tt_sourcestate){
if( $tt_record ){
$result = \$tt_record->get_column( 'base64_'.$tt_sourcestate );
}
if( $result && exists $params{result} ){
${$params{result}} = $result;
}
return $result;
}
1;

@ -1,38 +1,20 @@
package NGCP::Panel::Utils::InvoiceTemplate;
use strict;
use warnings;
use Moose;
use Sipwise::Base;
use DBIx::Class::Exception;
use NGCP::Panel::Utils::DateTime;
#use NGCP::Panel::Utils::DateTime;
sub getDefault{
my %params = @_;
my $c = $params{c};
#in future kay be we will store it in Db, but now it is convenient to edit template as file
return ${$params{invoicetemplate}} = $c->view('SVG')->getTemplateContent($c, 'customer/calls_svg.tt');
}
sub getCustomerTemplate{
my %params = @_;
my $c = $params{c};
my $contract_id = $params{contract_id} || $c->stash->{contract}->id;
my $tt_state = $params{tt_state} || 'saved';
my $result;
sub getDefaultInvoiceTemplate{
my (%in) = @_;
#in future may be we will store root default in Db too, but now it is convenient to edit template as file
my $result = $in{c}->view('SVG')->getTemplateContent($in{c}, 'customer/calls_svg.tt');
#$in{c}->log->debug("result=$result;");
my $template_record = $c->model('DB')->resultset('invoice_template')->search({
reseller_id => $contract_id,
is_active => 1,
})->first;
#here may be base64 decoding
if('saved' eq $tt_state){
$result = \$template_record->get_column('base64_'.$tt_state);
if( $result && exists $in{result} ){
${$in{result}} = $result;
}
return $result;
return \$result;
}
1;

@ -38,6 +38,8 @@ sub process
}
return 1;
}
#method is necessary to apply APP specific template configurations, e.g. path, tt file extensions etc
#may be moved to main view class, but as on template_invoice experiments stage it would be ok to leave it in separated class
sub getTemplateContent{
my ( $self, $c, $template ) = @_;
if(defined $template){

Loading…
Cancel
Save