diff --git a/lib/NGCP/Panel/Controller/SubscriberProfile.pm b/lib/NGCP/Panel/Controller/SubscriberProfile.pm index 2de410895c..4e0e757e56 100644 --- a/lib/NGCP/Panel/Controller/SubscriberProfile.pm +++ b/lib/NGCP/Panel/Controller/SubscriberProfile.pm @@ -281,6 +281,7 @@ sub profile_list :Chained('catalog_base') :PathPart('profile') :CaptureArgs(0) { { name => 'id', search => 1, title => $c->loc('#') }, { name => 'name', search => 1, title => $c->loc('Name') }, { name => 'description', search => 1, title => $c->loc('Description') }, + { name => 'catalog_default', search => 0, title => $c->loc('Default') }, ]); $c->stash(template => 'subprofile/profile_list.tt'); @@ -331,7 +332,7 @@ sub profile_create :Chained('profile_list') :PathPart('create') :Args(0) :Does(A my $params = {}; $params = $params->merge($c->session->{created_objects}); my $form = NGCP::Panel::Form::SubscriberProfile::Profile->new(ctx => $c); - $form->create_structure($form->field_names); + #$form->create_structure($form->field_names); $form->process( posted => $posted, params => $c->request->params, @@ -347,19 +348,28 @@ sub profile_create :Chained('profile_list') :PathPart('create') :Args(0) :Does(A try { my $schema = $c->model('DB'); $schema->txn_do(sub { - my $name = delete $form->values->{name}; - my $desc = delete $form->values->{description}; - my $profile = $c->stash->{cat}->voip_subscriber_profiles->create({ - name => $name, - description => $desc, - }); + my $attributes = delete $form->values->{attribute}; + if($form->values->{catalog_default}) { + # new profile is default, clear any previous default profiles + $c->stash->{cat}->voip_subscriber_profiles->update({ + catalog_default => 0, + }); + } elsif(!$c->stash->{cat}->voip_subscriber_profiles->search({ + catalog_default => 1, + })->count) { + + # no previous default profile, make this one default + $form->values->{catalog_default} = 1; + } + my $profile = $c->stash->{cat}->voip_subscriber_profiles->create($form->values); # TODO: should we rather take the name and load the id from db, # instead of trusting the id coming from user input? - foreach my $attr(keys %{ $form->values }) { - next unless($form->values->{$attr}); + use Data::Printer; p $attributes; + foreach my $attr(keys %{ $attributes }) { + next unless($attributes->{$attr}); $profile->profile_attributes->create({ - attribute_id => $form->values->{$attr}, + attribute_id => $attributes->{$attr}, }); } }); @@ -385,10 +395,10 @@ sub profile_edit :Chained('profile_base') :PathPart('edit') :Does(ACL) :ACLDetac my $posted = ($c->request->method eq 'POST'); my $params = { $profile->get_inflated_columns }; foreach my $old_attr($profile->profile_attributes->all) { - $params->{$old_attr->attribute->attribute} = $old_attr->attribute->id; + $params->{attribute}{$old_attr->attribute->attribute} = $old_attr->attribute->id; } my $form = NGCP::Panel::Form::SubscriberProfile::Profile->new(ctx => $c); - $form->create_structure($form->field_names); + #$form->create_structure($form->field_names); $form->process( posted => $posted, params => $c->request->params, @@ -404,24 +414,33 @@ sub profile_edit :Chained('profile_base') :PathPart('edit') :Does(ACL) :ACLDetac try { my $schema = $c->model('DB'); $schema->txn_do(sub { - my $name = delete $form->values->{name}; - my $desc = delete $form->values->{description}; - unless($name eq $profile->name && $desc eq $profile->description) { - $profile->update({ - name => $name, - description => $desc, + my $attributes = delete $form->values->{attribute}; + if($form->values->{catalog_default}) { + # new profile is default, clear any previous default profiles + $c->stash->{cat}->voip_subscriber_profiles->search({ + id => { '!=' => $profile->id }, + })->update({ + catalog_default => 0, }); + } elsif(!$c->stash->{cat}->voip_subscriber_profiles->search({ + catalog_default => 1, + })->count) { + + # no previous default profile, make this one default + $form->values->{catalog_default} = 1; } + $profile->update($form->values); + # TODO: reuse attributes for efficiency reasons? $profile->profile_attributes->delete; # TODO: should we rather take the name and load the id from db, # instead of trusting the id coming from user input? - foreach my $attr(keys %{ $form->values }) { - next unless($form->values->{$attr}); + foreach my $attr(keys %{ $attributes }) { + next unless($attributes->{$attr}); $profile->profile_attributes->create({ - attribute_id => $form->values->{$attr}, + attribute_id => $attributes->{$attr}, }); } @@ -447,13 +466,21 @@ sub profile_delete :Chained('profile_base') :PathPart('delete') :Does(ACL) :ACLD try { my $schema = $c->model('DB'); $schema->txn_do(sub{ + my $profile = $c->stash->{profile}; $schema->resultset('provisioning_voip_subscribers')->search({ - profile_id => $c->stash->{profile}->id, + profile_id => $profile->id, })->update({ # TODO: set this to another profile, or reject deletion if profile is in use profile_id => undef, }); - $c->stash->{profile}->delete; + if($profile->catalog_default && $c->stash->{cat}->voip_subscriber_profiles->count > 1) { + $c->stash->{cat}->voip_subscriber_profiles->search({ + id => { '!=' => $profile->id }, + })->first->update({ + catalog_default => 1, + }); + } + $profile->delete; }); $c->flash(messages => [{type => 'success', text => $c->loc('Subscriber profile successfully deleted')}]); } catch($e) { @@ -488,11 +515,9 @@ sub profile_clone :Chained('profile_base') :PathPart('clone') :Does(ACL) :ACLDet try { my $schema = $c->model('DB'); $schema->txn_do(sub { - my $new_profile = $c->stash->{cat}->voip_subscriber_profiles->create({ - %{ $form->values }, - catalog_id => $c->stash->{cat}->id, - }); - + $form->values->{catalog_default} = 0; + $form->values->{catalog_id} = $c->stash->{cat}->id; + my $new_profile = $c->stash->{cat}->voip_subscriber_profiles->create($form->values); my @old_attributes = $c->stash->{profile}->profile_attributes->all; foreach my $attr (@old_attributes) { $new_profile->profile_attributes->create({ diff --git a/lib/NGCP/Panel/Form/SubscriberProfile/ApiProfile.pm b/lib/NGCP/Panel/Form/SubscriberProfile/ApiProfile.pm index e8af4b2cfb..9434691d53 100644 --- a/lib/NGCP/Panel/Form/SubscriberProfile/ApiProfile.pm +++ b/lib/NGCP/Panel/Form/SubscriberProfile/ApiProfile.pm @@ -16,7 +16,7 @@ has_field 'catalog' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/catalog name description/], + render_list => [qw/catalog name description catalog_default/], ); 1; diff --git a/lib/NGCP/Panel/Form/SubscriberProfile/Profile.pm b/lib/NGCP/Panel/Form/SubscriberProfile/Profile.pm index 21ff35181c..af51026dde 100644 --- a/lib/NGCP/Panel/Form/SubscriberProfile/Profile.pm +++ b/lib/NGCP/Panel/Form/SubscriberProfile/Profile.pm @@ -29,6 +29,22 @@ has_field 'description' => ( }, ); +has_field 'catalog_default' => ( + type => 'Boolean', + label => 'Default Profile', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['Make this profile automatically the default profile for new subscribers having this profile catalog.'], + }, +); + +has_field 'attribute' => ( + type => 'Compound', + label => 'Attributes', + #do_label => 1, +); + has_field 'save' => ( type => 'Submit', value => 'Save', @@ -39,7 +55,7 @@ has_field 'save' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/name description/], + render_list => [qw/name description catalog_default attribute/], ); has_block 'actions' => ( @@ -61,7 +77,7 @@ sub field_list { my $fields = []; foreach my $pref($pref_rs->all) { my $desc = $pref->description; - push @{ $fields }, $pref->attribute => { + push @{ $fields }, 'attribute.'.$pref->attribute => { type => 'Checkbox', label => $pref->attribute, checkbox_value => $pref->id, diff --git a/lib/NGCP/Panel/Form/SubscriberProfile/ProfileAdmin.pm b/lib/NGCP/Panel/Form/SubscriberProfile/ProfileAdmin.pm deleted file mode 100644 index 20128d17cd..0000000000 --- a/lib/NGCP/Panel/Form/SubscriberProfile/ProfileAdmin.pm +++ /dev/null @@ -1,24 +0,0 @@ -package NGCP::Panel::Form::SubscriberProfile::ProfileAdmin; - -use HTML::FormHandler::Moose; -extends 'NGCP::Panel::Form::SubscriberProfile::ProfileReseller'; -use Moose::Util::TypeConstraints; - -has_field 'catalog' => ( - type => '+NGCP::Panel::Field::SubscriberProfileCatalog', - validate_when_empty => 1, - element_attr => { - rel => ['tooltip'], - title => ['The subscriber profile catalog this profile belongs to.'], - }, -); - -has_block 'fields' => ( - tag => 'div', - class => [qw/modal-body/], - render_list => [qw/catalog name description/], -); - -1; - -# vim: set tabstop=4 expandtab: diff --git a/share/templates/widgets/admin_topmenu_settings.tt b/share/templates/widgets/admin_topmenu_settings.tt index 8184383e5b..ffc3601761 100644 --- a/share/templates/widgets/admin_topmenu_settings.tt +++ b/share/templates/widgets/admin_topmenu_settings.tt @@ -26,6 +26,7 @@