From d4ec2ef0427e47b1d99fb8ecd57616d6e35000a1 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Fri, 13 Sep 2013 16:10:18 +0200 Subject: [PATCH] Implement PBX field device handling. --- lib/NGCP/Panel/Controller/Customer.pm | 124 ++++++++++++++++-- .../Panel/Form/Customer/PbxFieldDevice.pm | 6 +- share/templates/customer/details.tt | 10 +- 3 files changed, 124 insertions(+), 16 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index 6a2ea6a95d..f37b6f3d32 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -151,6 +151,13 @@ sub base :Chained('list_customer') :PathPart('') :CaptureArgs(1) { $c->stash->{subscribers} = $subs->{subscribers}; $c->stash->{pbx_groups} = $subs->{pbx_groups}; + my $field_devs = [ $c->model('DB')->resultset('autoprov_field_devices')->search({ + 'provisioning_voip_subscriber.account_id' => $contract->first->id + }, { + join => 'provisioning_voip_subscriber' + })->all ]; + $c->stash(pbx_devices => $field_devs); + $c->stash(product => $product); $c->stash(balance => $balance); $c->stash(fraud => $contract->first->contract_fraud_preference); @@ -556,14 +563,12 @@ sub pbx_device_create :Chained('base') :PathPart('pbx/device/create') :Args(0) { my ($self, $c) = @_; my $posted = ($c->request->method eq 'POST'); - unless($posted) { - $c->stash->{autoprov_profile_rs} = $c->model('DB')->resultset('autoprov_profiles') - ->search({ - 'device.reseller_id' => $c->stash->{contract}->contact->reseller_id, - },{ - join => { 'config' => 'device' }, - }); - } + $c->stash->{autoprov_profile_rs} = $c->model('DB')->resultset('autoprov_profiles') + ->search({ + 'device.reseller_id' => $c->stash->{contract}->contact->reseller_id, + },{ + join => { 'config' => 'device' }, + }); my $form = NGCP::Panel::Form::Customer::PbxFieldDevice->new(ctx => $c); my $params = {}; $params = $params->merge($c->session->{created_objects}); @@ -582,7 +587,21 @@ sub pbx_device_create :Chained('base') :PathPart('pbx/device/create') :Args(0) { try { my $schema = $c->model('DB'); $schema->txn_do( sub { - + my $prov_subscriber = $schema->resultset('provisioning_voip_subscribers')->find({ + id => $form->params->{subscriber_id}, + account_id => $c->stash->{contract}->id, + }); + unless($prov_subscriber) { + NGCP::Panel::Utils::Message->error( + c => $c, + error => "invalid provisioning subscriber_id '".$form->params->{subscriber_id}. + "' for contract id '".$c->stash->{contract}->id."'", + desc => "Invalid provisioning subscriber id detected.", + ); + } + else { + $prov_subscriber->autoprov_field_devices->create($form->params); + } }); $c->flash(messages => [{type => 'success', text => 'PBX device successfully created'}]); @@ -604,6 +623,93 @@ sub pbx_device_create :Chained('base') :PathPart('pbx/device/create') :Args(0) { ); } +sub pbx_device_base :Chained('base') :PathPart('pbx/device') :CaptureArgs(1) { + my ($self, $c, $dev_id) = @_; + + my $dev = $c->model('DB')->resultset('autoprov_field_devices')->find($dev_id); + unless($dev) { + NGCP::Panel::Utils::Message->error( + c => $c, + error => "invalid voip pbx device id $dev_id", + desc => "PBX device with id $dev_id does not exist.", + ); + NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/customer/details', [$c->req->captures->[0]])); + } + + # TODO: in groups, devices etc, check for reseller-id! + + $c->stash( + pbx_device => $dev, + ); +} + +sub pbx_device_edit :Chained('pbx_device_base') :PathPart('edit') :Args(0) { + my ($self, $c) = @_; + + my $posted = ($c->request->method eq 'POST'); + $c->stash->{autoprov_profile_rs} = $c->model('DB')->resultset('autoprov_profiles') + ->search({ + 'device.reseller_id' => $c->stash->{contract}->contact->reseller_id, + },{ + join => { 'config' => 'device' }, + }); + my $form = NGCP::Panel::Form::Customer::PbxFieldDevice->new(ctx => $c); + my $params = { $c->stash->{pbx_device}->get_inflated_columns }; + $params = $params->merge($c->session->{created_objects}); + $form->process( + posted => $posted, + params => $c->request->params, + item => $params, + ); + NGCP::Panel::Utils::Navigation::check_form_buttons( + c => $c, + form => $form, + fields => {}, + back_uri => $c->req->uri, + ); + if($posted && $form->validated) { + try { + my $schema = $c->model('DB'); + $schema->txn_do( sub { + $c->stash->{pbx_device}->update($form->params); + }); + + $c->flash(messages => [{type => 'success', text => 'PBX device successfully updated'}]); + } catch ($e) { + NGCP::Panel::Utils::Message->error( + c => $c, + error => $e, + desc => "Failed to update PBX device", + ); + } + + NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/customer/details', $c->req->captures)); + } + + $c->stash( + edit_flag => 1, + form => $form, + description => 'PBX Device', + ); +} + +sub pbx_device_delete :Chained('pbx_device_base') :PathPart('delete') :Args(0) { + my ($self, $c) = @_; + + try { + $c->stash->{pbx_device}->delete; + $c->flash(messages => [{type => 'success', text => 'PBX Device successfully deleted' }]); + } catch($e) { + NGCP::Panel::Utils::Message->error( + c => $c, + error => "failed to delete PBX device with id '".$c->stash->{pbx_device}->id."': $e", + desc => "Failed to delete PBX device", + ); + } + + NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/customer/details', $c->req->captures)); +} + =head1 AUTHOR diff --git a/lib/NGCP/Panel/Form/Customer/PbxFieldDevice.pm b/lib/NGCP/Panel/Form/Customer/PbxFieldDevice.pm index ccaafd3221..4381258721 100644 --- a/lib/NGCP/Panel/Form/Customer/PbxFieldDevice.pm +++ b/lib/NGCP/Panel/Form/Customer/PbxFieldDevice.pm @@ -11,7 +11,7 @@ has_field 'submitid' => ( type => 'Hidden' ); sub build_render_list {[qw/submitid fields actions/]} sub build_form_element_class {[qw(form-horizontal)]} -has_field 'profile' => ( +has_field 'profile_id' => ( type => 'Select', required => 1, label => 'Device Profile', @@ -34,7 +34,7 @@ has_field 'identifier' => ( label => 'MAC Address / Identifier', ); -has_field 'subscriber' => ( +has_field 'subscriber_id' => ( type => 'Select', required => 1, label => 'Subscriber', @@ -64,7 +64,7 @@ has_field 'save' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/profile identifier subscriber/], + render_list => [qw/profile_id identifier subscriber_id/], ); has_block 'actions' => ( diff --git a/share/templates/customer/details.tt b/share/templates/customer/details.tt index 79025d7794..72e3250eeb 100644 --- a/share/templates/customer/details.tt +++ b/share/templates/customer/details.tt @@ -254,6 +254,7 @@ + @@ -263,14 +264,15 @@ [% FOR dev IN pbx_devices -%] - - + + +
SIP URI MAC Address / Identifier Device Profile
[% dev.subscriber.username %]@[% dev.subscriber.domain %][% dev.token %][% dev.provisioning_voip_subscriber.username %]@[% dev.provisioning_voip_subscriber.domain.domain %][% dev.identifier %] [% dev.profile.name %]
[% UNLESS c.user.readonly -%] - Terminate - Edit + Delete + Edit [% END -%]