From 468420909d201129cf62e8c0770174caabfb78aa Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Wed, 5 Mar 2014 18:21:51 +0100 Subject: [PATCH] MT#5879 Separated Model DB actions. Time spent with FormHandler and Catalyst::Adaptor. --- lib/NGCP/Panel/Controller/Customer.pm | 104 +++++++++++------- .../Panel/Form/Customer/InvoiceTemplate.pm | 43 ++++---- lib/NGCP/Panel/Model/DB/Base.pm | 15 +++ lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm | 30 +++++ lib/NGCP/Panel/Utils/InvoiceTemplate.pm | 38 ++----- lib/NGCP/Panel/View/SVG.pm | 2 + 6 files changed, 145 insertions(+), 87 deletions(-) create mode 100644 lib/NGCP/Panel/Model/DB/Base.pm create mode 100644 lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index 1877338baf..78cadc47c1 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -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 !~/^/is ).";\n"); - $c->log->debug("1.invoicetemplate is empty=".($invoicetemplate?0:1).";viewbox=".($invoicetemplate !~/^/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 !~/^/is).";\n"); -##### if( $invoicetemplate !~/^/is ){ -##### (my ($width)) = ($invoicetemplate =~/^/is ); +##### $c->log->debug("3.tt_string is empty=".($tt_string?0:1).";viewbox=".($tt_string !~/^/is).";\n"); +##### if( $tt_string !~/^/is ){ +##### (my ($width)) = ($tt_string =~/^/is ); ##### $c->log->debug("width=$width;\n"); -##### (my ($height)) = ($invoicetemplate =~/^/is ); -##### my($replaced) = $invoicetemplate =~s/^/is ); +##### my($replaced) = $tt_string =~s/^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')); } diff --git a/lib/NGCP/Panel/Form/Customer/InvoiceTemplate.pm b/lib/NGCP/Panel/Form/Customer/InvoiceTemplate.pm index deeceea7c6..ece21676d7 100644 --- a/lib/NGCP/Panel/Form/Customer/InvoiceTemplate.pm +++ b/lib/NGCP/Panel/Form/Customer/InvoiceTemplate.pm @@ -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 diff --git a/lib/NGCP/Panel/Model/DB/Base.pm b/lib/NGCP/Panel/Model/DB/Base.pm new file mode 100644 index 0000000000..6caaabc49f --- /dev/null +++ b/lib/NGCP/Panel/Model/DB/Base.pm @@ -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; \ No newline at end of file diff --git a/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm b/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm new file mode 100644 index 0000000000..2380bfe26a --- /dev/null +++ b/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm @@ -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; \ No newline at end of file diff --git a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm index b4f4a0bee0..e7436fe570 100644 --- a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm +++ b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm @@ -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; \ No newline at end of file diff --git a/lib/NGCP/Panel/View/SVG.pm b/lib/NGCP/Panel/View/SVG.pm index 644b5f3c26..30de3f3bfc 100644 --- a/lib/NGCP/Panel/View/SVG.pm +++ b/lib/NGCP/Panel/View/SVG.pm @@ -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){