diff --git a/lib/NGCP/Panel/Controller/Subscriber.pm b/lib/NGCP/Panel/Controller/Subscriber.pm index 4167d1ab11..d194656379 100644 --- a/lib/NGCP/Panel/Controller/Subscriber.pm +++ b/lib/NGCP/Panel/Controller/Subscriber.pm @@ -10,6 +10,7 @@ use NGCP::Panel::Form::SubscriberCFTSimple; use NGCP::Panel::Form::SubscriberCFAdvanced; use NGCP::Panel::Form::SubscriberCFTAdvanced; use NGCP::Panel::Form::DestinationSet; +use NGCP::Panel::Form::TimeSet; use UUID; use Data::Printer; @@ -556,10 +557,10 @@ sub preferences_callforward_advanced :Chained('base') :PathPart('preferences/cal $c->uri_for_action('/subscriber/preferences_callforward_destinationset', [$c->req->captures->[0]], $cf_type, ), -# 'cf_actions.edit_time_sets' => -# $c->uri_for_action('/subscriber/preferences_callforward_timeset', -# [$c->req->captures->[0]], -# ), + 'cf_actions.edit_time_sets' => + $c->uri_for_action('/subscriber/preferences_callforward_timeset', + [$c->req->captures->[0]], $cf_type, + ), }, back_uri => $c->uri_for_action('/subscriber/preferences_callforward_advanced', [$c->req->captures->[0]], $cf_type, 'advanced'), @@ -820,6 +821,165 @@ sub preferences_callforward_destinationset_delete :Chained('preferences_callforw return; } +sub preferences_callforward_timeset :Chained('base') :PathPart('preferences/timeset') :Args(1) { + my ($self, $c, $cf_type) = @_; + + my $prov_subscriber = $c->stash->{subscriber}->provisioning_voip_subscriber; + + my @sets; + if($prov_subscriber->voip_cf_time_sets) { + foreach my $set($prov_subscriber->voip_cf_time_sets->all) { + if($set->voip_cf_periods) { + my @periods = (); + foreach my $period($set->voip_cf_periods->all) { + my %cols = $period->get_columns; + push @periods, \%cols; + } + push @sets, { name => $set->name, id => $set->id, periods => \@periods}; + } + } + } + $c->stash->{cf_sets} = \@sets; + + my $cf_form = undef; + + $self->load_preference_list($c); + $c->stash(template => 'subscriber/preferences.tt'); + $c->stash( + edit_timeset_flag => 1, + cf_description => "Time Sets", + cf_form => $cf_form, + close_target => $c->uri_for_action('/subscriber/preferences_callforward_advanced', + [$c->req->captures->[0]], $cf_type, 'advanced'), + cf_type => $cf_type, + ); +} + +sub preferences_callforward_timeset_base :Chained('base') :PathPart('preferences/timeset') :CaptureArgs(1) { + my ($self, $c, $set_id) = @_; + + $c->stash(time_set => $c->stash->{subscriber} + ->provisioning_voip_subscriber + ->voip_cf_time_sets + ->find($set_id)); + + $self->load_preference_list($c); + $c->stash(template => 'subscriber/preferences.tt'); +} + +sub preferences_callforward_timeset_edit :Chained('preferences_callforward_timeset_base') :PathPart('edit') :Args(1) { + my ($self, $c, $cf_type) = @_; + + my $form = NGCP::Panel::Form::TimeSet->new; + + my $posted = ($c->request->method eq 'POST'); + + my $set = $c->stash->{time_set}; + my $params; + unless($posted) { + $params->{name} = $set->name; + my @periods; + for my $period($set->voip_cf_periods->all) { + push @periods, { + year => $period->year, + month => $period->month, + mday => $period->mday, + wday => $period->wday, + hour => $period->hour, + minute => $period->minute, + id => $period->id, + }; + } + $params->{period} = \@periods; + } + + $form->process( + params => $posted ? $c->req->params : $params + ); + + if($posted && $form->validated) { + try { + my $schema = $c->model('DB'); + $schema->txn_do(sub { + my @fields = $form->field('period')->fields; + unless(@fields) { + foreach my $mapping($set->voip_cf_mappings) { + $mapping->update({ time_set_id => undef }); + } + $set->delete; + + $c->response->redirect( + $c->uri_for_action('/subscriber/preferences_callforward_timeset', + [$c->req->captures->[0]], $cf_type) + ); + return; + } + if($form->field('name')->value ne $set->name) { + $set->update({name => $form->field('name')->value}); + } + foreach my $period($set->voip_cf_periods->all) { + $period->delete; + } + foreach my $period($form->field('period')->fields) { + $set->voip_cf_periods->create({ + year => $period->field('year')->value, + month => $period->field('month')->value, + mday => $period->field('mday')->value, + wday => $period->field('wday')->value, + hour => $period->field('hour')->value, + minute => $period->field('minute')->value, + }); + } + $c->response->redirect( + $c->uri_for_action('/subscriber/preferences_callforward_timeset', + [$c->req->captures->[0]], $cf_type) + ); + return; + }); + } catch($e) { + $c->log->error("failed to update time set: $e"); + $c->response->redirect( + $c->uri_for_action('/subscriber/preferences_callforward_timeset', + [$c->req->captures->[0]], $cf_type) + ); + return; + } + } + + $c->stash( + edit_cf_flag => 1, + cf_description => "Time Set", + cf_form => $form, + close_target => $c->uri_for_action('/subscriber/preferences_callforward_timeset', + [$c->req->captures->[0]], $cf_type), + ); + +} + +sub preferences_callforward_timeset_delete :Chained('preferences_callforward_timeset_base') :PathPart('delete') :Args(1) { + my ($self, $c, $cf_type) = @_; + + my $set = $c->stash->{time_set}; + + try { + my $schema = $c->model('DB'); + $schema->txn_do(sub { + foreach my $map($set->voip_cf_mappings->all) { + $map->update({ destination_set_id => undef }); + } + $set->delete; + }); + } catch($e) { + $c->log->error("failed to delete time set: $e"); + } + + $c->response->redirect( + $c->uri_for_action('/subscriber/preferences_callforward_timeset', + [$c->req->captures->[0]], $cf_type) + ); + return; +} + sub preferences_callforward_delete :Chained('base') :PathPart('preferences/callforward/delete') :Args(1) { my ($self, $c, $cf_type) = @_; diff --git a/lib/NGCP/Panel/Field/SubscriberTimeSet.pm b/lib/NGCP/Panel/Field/SubscriberTimeSet.pm index d56a2c7bcb..270d16e051 100644 --- a/lib/NGCP/Panel/Field/SubscriberTimeSet.pm +++ b/lib/NGCP/Panel/Field/SubscriberTimeSet.pm @@ -11,15 +11,18 @@ sub build_options { my $active_time_set = $form->ctx->stash->{cf_active_time_set}; my $time_sets = $form->ctx->stash->{cf_time_sets}; - my @all = ({label => '', value => undef}); + my @all; return \@all unless($time_sets); + push @all, { label => '', value => undef} + unless($active_time_set); foreach my $set($time_sets->all) { my $entry = {}; $entry->{label} = $set->name; $entry->{value} = $set->id; + if($active_time_set && $set->id == $active_time_set->id) { - $entry->{active} = 1; + $entry->{selected} = 1; } push @all, $entry; } diff --git a/lib/NGCP/Panel/Form/TimeSet.pm b/lib/NGCP/Panel/Form/TimeSet.pm new file mode 100644 index 0000000000..628b2086cc --- /dev/null +++ b/lib/NGCP/Panel/Form/TimeSet.pm @@ -0,0 +1,123 @@ +package NGCP::Panel::Form::TimeSet; +use HTML::FormHandler::Moose; +use HTML::FormHandler::Widget::Block::Bootstrap; +use Moose::Util::TypeConstraints; +extends 'HTML::FormHandler'; + +with 'NGCP::Panel::Render::RepeatableJs'; + +has '+widget_wrapper' => (default => 'Bootstrap'); + +has_field 'submitid' => ( + type => 'Hidden', +); + +has_field 'name' => ( + type => 'Text', + label => 'Name', + wrapper_class => [qw/hfh-rep-field/], + required => 1, +); + +has_field 'period' => ( + type => 'Repeatable', + setup_for_js => 1, + do_wrapper => 1, + do_label => 0, + tags => { + controls_div => 1, + }, + wrapper_class => [qw/hfh-rep/], +); + +has_field 'period.id' => ( + type => 'Hidden', +); +has_field 'period.year' => ( + type => 'Text', + label => 'Year', + wrapper_class => [qw/hfh-rep-field/], +); +has_field 'period.month' => ( + type => 'Text', + label => 'Month', + wrapper_class => [qw/hfh-rep-field/], +); +has_field 'period.mday' => ( + type => 'Text', + label => 'Day of Month', + wrapper_class => [qw/hfh-rep-field/], +); +has_field 'period.wday' => ( + type => 'Text', + label => 'Day of Week', + wrapper_class => [qw/hfh-rep-field/], +); +has_field 'period.hour' => ( + type => 'Text', + label => 'Hour', + wrapper_class => [qw/hfh-rep-field/], +); +has_field 'period.minute' => ( + type => 'Text', + label => 'Minute', + wrapper_class => [qw/hfh-rep-field/], +); + +has_field 'period.rm' => ( + type => 'RmElement', + value => 'Remove', + element_class => [qw/btn btn-primary pull-right/], +# tags => { +# "data-confirm" => "Delete", +# }, +); + + +has_field 'period_add' => ( + type => 'AddElement', + repeatable => 'period', + value => 'Add another period', + element_class => [qw/btn btn-primary pull-right/], +); + +has_block 'fields' => ( + tag => 'div', + class => [qw(modal-body)], + render_list => [qw(submitid name period period_add)], +); + +has_field 'save' => ( + type => 'Submit', + do_label => 0, + value => 'Save', + element_class => [qw(btn btn-primary)], +); + +has_block 'actions' => ( + tag => 'div', + class => [qw(modal-footer)], + render_list => [qw(save)], +); + +sub build_render_list { + return [qw(fields actions)]; +} + +sub build_form_element_class { + return [qw(form-horizontal)]; +} + +#sub validate_destination { +# my ($self, $field) = @_; +# +# # TODO: proper SIP URI check! +# if($field->value !~ /^sip:.+\@.+$/) { +# my $err_msg = 'Destination must be a valid SIP URI in format "sip:user@domain"'; +# $field->add_error($err_msg); +# } +#} + +1; + +# vim: set tabstop=4 expandtab: diff --git a/share/templates/subscriber/preferences.tt b/share/templates/subscriber/preferences.tt index 01022aea96..ab6b1e3dfa 100644 --- a/share/templates/subscriber/preferences.tt +++ b/share/templates/subscriber/preferences.tt @@ -93,7 +93,7 @@ [% jdx = jdx + 1 %] [% END -%] [% FOR p IN maps.periods -%] - [% jdx %]: [% p.year %] [% p.month %] [% p.mday %] [% p.wday %] [% p.hour %] [% p.minute %]
+ [% p.year %] [% p.month %] [% p.mday %] [% p.wday %] [% p.hour %] [% p.minute %]
[% jdx = jdx + 1 %] [% END -%] [% WHILE jdx < destinations.${idx} -%] @@ -184,6 +184,52 @@ [% END -%] +[% + modal_footer(); + modal_script(m.close_target = close_target ? close_target : c.uri_for_action('/subscriber/preferences', [c.req.captures.0])); +-%] +[% ELSIF edit_timeset_flag -%] +[% + PROCESS "helpers/modal.tt"; + modal_header(m.name = cf_description); +-%] + + + [% modal_footer(); modal_script(m.close_target = close_target ? close_target : c.uri_for_action('/subscriber/preferences', [c.req.captures.0]));