Add basic timeset handling for CF.

agranig/1_0_subfix
Andreas Granig 12 years ago
parent 82f7c08ae4
commit bcc9b83bf9

@ -10,6 +10,7 @@ use NGCP::Panel::Form::SubscriberCFTSimple;
use NGCP::Panel::Form::SubscriberCFAdvanced; use NGCP::Panel::Form::SubscriberCFAdvanced;
use NGCP::Panel::Form::SubscriberCFTAdvanced; use NGCP::Panel::Form::SubscriberCFTAdvanced;
use NGCP::Panel::Form::DestinationSet; use NGCP::Panel::Form::DestinationSet;
use NGCP::Panel::Form::TimeSet;
use UUID; use UUID;
use Data::Printer; 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->uri_for_action('/subscriber/preferences_callforward_destinationset',
[$c->req->captures->[0]], $cf_type, [$c->req->captures->[0]], $cf_type,
), ),
# 'cf_actions.edit_time_sets' => 'cf_actions.edit_time_sets' =>
# $c->uri_for_action('/subscriber/preferences_callforward_timeset', $c->uri_for_action('/subscriber/preferences_callforward_timeset',
# [$c->req->captures->[0]], [$c->req->captures->[0]], $cf_type,
# ), ),
}, },
back_uri => $c->uri_for_action('/subscriber/preferences_callforward_advanced', back_uri => $c->uri_for_action('/subscriber/preferences_callforward_advanced',
[$c->req->captures->[0]], $cf_type, 'advanced'), [$c->req->captures->[0]], $cf_type, 'advanced'),
@ -820,6 +821,165 @@ sub preferences_callforward_destinationset_delete :Chained('preferences_callforw
return; 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) { sub preferences_callforward_delete :Chained('base') :PathPart('preferences/callforward/delete') :Args(1) {
my ($self, $c, $cf_type) = @_; my ($self, $c, $cf_type) = @_;

@ -11,15 +11,18 @@ sub build_options {
my $active_time_set = $form->ctx->stash->{cf_active_time_set}; my $active_time_set = $form->ctx->stash->{cf_active_time_set};
my $time_sets = $form->ctx->stash->{cf_time_sets}; my $time_sets = $form->ctx->stash->{cf_time_sets};
my @all = ({label => '<always>', value => undef}); my @all;
return \@all unless($time_sets); return \@all unless($time_sets);
push @all, { label => '', value => undef}
unless($active_time_set);
foreach my $set($time_sets->all) { foreach my $set($time_sets->all) {
my $entry = {}; my $entry = {};
$entry->{label} = $set->name; $entry->{label} = $set->name;
$entry->{value} = $set->id; $entry->{value} = $set->id;
if($active_time_set && if($active_time_set &&
$set->id == $active_time_set->id) { $set->id == $active_time_set->id) {
$entry->{active} = 1; $entry->{selected} = 1;
} }
push @all, $entry; push @all, $entry;
} }

@ -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:

@ -93,7 +93,7 @@
[% jdx = jdx + 1 %] [% jdx = jdx + 1 %]
[% END -%] [% END -%]
[% FOR p IN maps.periods -%] [% FOR p IN maps.periods -%]
[% jdx %]: [% p.year %] [% p.month %] [% p.mday %] [% p.wday %] [% p.hour %] [% p.minute %]<br/> [% p.year %] [% p.month %] [% p.mday %] [% p.wday %] [% p.hour %] [% p.minute %]<br/>
[% jdx = jdx + 1 %] [% jdx = jdx + 1 %]
[% END -%] [% END -%]
[% WHILE jdx < destinations.${idx} -%] [% WHILE jdx < destinations.${idx} -%]
@ -184,6 +184,52 @@
[% END -%] [% END -%]
</div> </div>
[%
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);
-%]
<div class="modal-body">
[% IF cf_sets -%]
<table class="table table-bordered table-striped table-highlight table-hover">
<thead>
<tr>
<th>Name</th>
<th>Values</th>
<th></th>
</tr>
</thead>
<tbody>
[% FOREACH set IN cf_sets -%]
<tr class="sw_action_row">
<td>[% set.name %]</td>
<td>
[% FOREACH p IN set.periods -%]
[% p.year %] [% p.month %] [% p.mday %] [% p.wday %] [% p.hour %] [% p.minute %]<br/>
[% END -%]
</td>
<td class="ngcp-actions-column">
<div class="sw_actions pull-right">
<a class="btn btn-small btn-primary" href="[% c.uri_for_action('/subscriber/preferences_callforward_timeset_edit', [ c.req.captures.0, set.id ], cf_type) %]">
<i class="icon-edit"></i> Edit
</a>
<a class="btn btn-small btn-secondary" data-confirm="Delete" href="[% c.uri_for_action('/subscriber/preferences_callforward_timeset_delete', [ c.req.captures.0, set.id ], cf_type) %]">
<i class="icon-trash"></i> Delete
</a>
</div>
</td>
</tr>
[% END -%]
</tbody>
</table>
[% END -%]
</div>
[% [%
modal_footer(); modal_footer();
modal_script(m.close_target = close_target ? close_target : c.uri_for_action('/subscriber/preferences', [c.req.captures.0])); modal_script(m.close_target = close_target ? close_target : c.uri_for_action('/subscriber/preferences', [c.req.captures.0]));

Loading…
Cancel
Save