From 7b5fe19d963c302ac9251ccea9500ace138a54b8 Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Wed, 24 Jan 2024 10:25:17 +0100 Subject: [PATCH] MT#58709 rework NGCP::Utils::Sound::get_handlers_rs * get_handlers_rs() is renamed into get_file_handles() and reworked from an indented 'from' resultset into 2 separate resultsets, one fetching all the handles and another one files for the sound set, it is now returned as an array with the similar data. Reason for this rework is the deprecated use of 'from', that resulted in a lot of warnings in panel-fcgi.log: DBIx::Class::SQLMaker::ClassicExtensions::_join_condition(): ResultSet {from} structures with conditions not conforming to the SQL::Abstract::Classic syntax are deprecated: you either need to stop abusing {from} altogether, or express the condition properly using the { -ident => ... } * adjust file_handles data key names to better follow the naming conventions and readability, e.g.: groupname -> group_name, fileid -> file_id Change-Id: Ia91c9104c0b4dc79ee5bc359eb0b6e035e56ef3e (cherry picked from commit a4d54b10e4584dd4424fb73448256bc548699c05) --- lib/NGCP/Panel/Controller/API/SoundSets.pm | 4 +- .../Panel/Controller/API/SoundSetsItem.pm | 4 +- lib/NGCP/Panel/Controller/Sound.pm | 24 +++-- lib/NGCP/Panel/Utils/Sounds.pm | 101 +++++++++++------- share/templates/sound/handles_list.tt | 28 ++--- 5 files changed, 93 insertions(+), 68 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/SoundSets.pm b/lib/NGCP/Panel/Controller/API/SoundSets.pm index 0619916d0b..b0a2924944 100644 --- a/lib/NGCP/Panel/Controller/API/SoundSets.pm +++ b/lib/NGCP/Panel/Controller/API/SoundSets.pm @@ -66,12 +66,12 @@ sub create_item { } if ($copy_from_default_params->{copy_from_default}) { my $error; - my $handles_rs = NGCP::Panel::Utils::Sounds::get_handles_rs(c => $c, set_rs => $item); + my $file_handles = NGCP::Panel::Utils::Sounds::get_file_handles(c => $c, set_id => $item->id); NGCP::Panel::Utils::Sounds::apply_default_soundset_files( c => $c, lang => $copy_from_default_params->{language}, set_id => $item->id, - handles_rs => $handles_rs, + file_handles => $file_handles, loopplay => $copy_from_default_params->{loopplay}, override => $copy_from_default_params->{replace_existing}, error_ref => \$error, diff --git a/lib/NGCP/Panel/Controller/API/SoundSetsItem.pm b/lib/NGCP/Panel/Controller/API/SoundSetsItem.pm index b363383afb..14101de8f1 100644 --- a/lib/NGCP/Panel/Controller/API/SoundSetsItem.pm +++ b/lib/NGCP/Panel/Controller/API/SoundSetsItem.pm @@ -35,12 +35,12 @@ sub update_item_model { $item->update($resource); if ($copy_from_default_params->{copy_from_default}) { my $error; - my $handles_rs = NGCP::Panel::Utils::Sounds::get_handles_rs(c => $c, set_rs => $item); + my $file_handles = NGCP::Panel::Utils::Sounds::get_file_handles(c => $c, set_id => $item->id); NGCP::Panel::Utils::Sounds::apply_default_soundset_files( c => $c, lang => $copy_from_default_params->{language}, set_id => $item->id, - handles_rs => $handles_rs, + file_handles => $file_handles, loopplay => $copy_from_default_params->{loopplay}, override => $copy_from_default_params->{replace_existing}, error_ref => \$error, diff --git a/lib/NGCP/Panel/Controller/Sound.pm b/lib/NGCP/Panel/Controller/Sound.pm index fd5570c5b9..ee0852bd89 100644 --- a/lib/NGCP/Panel/Controller/Sound.pm +++ b/lib/NGCP/Panel/Controller/Sound.pm @@ -11,6 +11,7 @@ use File::Type; use NGCP::Panel::Utils::Sounds; use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Utils::Sems; +use List::Util qw(any); sub auto :Private { my ($self, $c) = @_; @@ -504,18 +505,18 @@ sub handles_list :Chained('base') :PathPart('handles') :CaptureArgs(0) { $c->stash(handles_base_uri => $c->uri_for_action("/sound/handles_root", [$c->req->captures->[0]])); - my $handles_rs = NGCP::Panel::Utils::Sounds::get_handles_rs(c => $c, set_rs => $c->stash->{set_result}); - - my @rows = $handles_rs->all; + my $set_id = $c->stash->{set_result}->id; + my $file_handles = NGCP::Panel::Utils::Sounds::get_file_handles(c => $c, set_id => $set_id); my %groups; - for my $handle (@rows) { - $groups{ $handle->get_column('groupname') } = [] - unless exists $groups{ $handle->get_column('groupname') }; - push @{ $groups{ $handle->get_column('groupname') } }, $handle; + for my $handle (@{$file_handles}) { + my $group_name = $handle->{group_name}; + my $group = $groups{$group_name} //= []; + push @{$group}, $handle; } + $c->stash(sound_groups => \%groups); - $c->stash(handles_rs => $handles_rs); + $c->stash(file_handles => $file_handles); $c->stash(has_edit => 1); $c->stash(has_delete => 1); @@ -538,8 +539,9 @@ sub handles_base :Chained('handles_list') :PathPart('') :CaptureArgs(1) { ); NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); } - my @tmph = $c->stash->{handles_rs}->all; - unless($c->stash->{handles_rs}->find({ 'handles.id' => $handle_id })) { + my $file_handles = $c->stash->{file_handles}; + my $handle_exists = any { $_->{handle_id} == $handle_id } @{$file_handles}; + unless ($handle_exists) { NGCP::Panel::Utils::Message::error( c => $c, log => 'Sound handle id does not exist', @@ -783,7 +785,7 @@ sub handles_load_default :Chained('handles_list') :PathPart('loaddefault') :Args c => $c, lang => $form->params->{language}, set_id => $c->stash->{set_result}->id, - handles_rs => $c->stash->{handles_rs}, + file_handles => $c->stash->{file_handles}, loopplay => $form->params->{loopplay}, override => $form->params->{replace_existing}, error_ref => \$error, diff --git a/lib/NGCP/Panel/Utils/Sounds.pm b/lib/NGCP/Panel/Utils/Sounds.pm index c664fdeae3..25e83a671f 100644 --- a/lib/NGCP/Panel/Utils/Sounds.pm +++ b/lib/NGCP/Panel/Utils/Sounds.pm @@ -161,64 +161,87 @@ sub stash_soundset_list { return; } -sub get_handles_rs { +sub get_file_handles { my (%params) = @_; my $c = $params{c}; - my $set_rs = $params{set_rs}; - - my $handles_rs = $c->model('DB')->resultset('voip_sound_groups') - ->search({ - },{ - select => ['groups.name', \'handles.name', \'handles.id', 'files.filename', 'files.loopplay', 'files.codec', 'files.id', - \'IF(files.use_parent IS NOT NULL, files.use_parent, 1)'], - as => [ 'groupname', 'handlename', 'handleid', 'filename', 'loopplay', 'codec', 'fileid', 'use_parent'], - alias => 'groups', - from => [ - { groups => 'provisioning.voip_sound_groups' }, - [ - { handles => 'provisioning.voip_sound_handles', -join_type=>'left'}, - { 'groups.id' => 'handles.group_id'}, - ], - [ - { files => 'provisioning.voip_sound_files', -join_type => 'left'}, - { 'handles.id' => { '=' => \'files.handle_id'}, 'files.set_id' => $set_rs->id}, - ], - ], - order_by => { -asc => 'handles.name' } - }); + my $set_id = $params{set_id}; + + my @file_handles = (); + + my $handles_rs = $c->model('DB')->resultset('voip_sound_handles')->search({ + },{ + alias => 'handles', + select => [ + 'group.name','handles.name', 'handles.id', + ], + as => [ + 'group_name', 'handle_name', 'handle_id', + ], + join => 'group', + order_by => { -asc => 'handles.name' } + }); unless($c->config->{features}->{cloudpbx}) { - $handles_rs = $handles_rs->search({ 'groups.name' => { '!=' => 'pbx' } }); + $handles_rs = $handles_rs->search({ 'group.name' => { '!=' => 'pbx' } }); } unless($c->config->{features}->{cloudpbx} || $c->config->{features}->{musiconhold}) { - $handles_rs = $handles_rs->search({ 'groups.name' => { '!=' => 'music_on_hold' } }); + $handles_rs = $handles_rs->search({ 'group.name' => { '!=' => 'music_on_hold' } }); } unless($c->config->{features}->{callingcard}) { - $handles_rs = $handles_rs->search({ 'groups.name' => { '!=' => 'calling_card' } }); + $handles_rs = $handles_rs->search({ 'group.name' => { '!=' => 'calling_card' } }); } unless($c->config->{features}->{mobilepush}) { - $handles_rs = $handles_rs->search({ 'groups.name' => { '!=' => 'mobile_push' } }); + $handles_rs = $handles_rs->search({ 'group.name' => { '!=' => 'mobile_push' } }); + } + + my $files_rs = $c->model('DB')->resultset('voip_sound_files')->search({ + 'me.set_id' => $set_id, + }); + + my %files = (); + + foreach my $file ($files_rs->all) { + $files{$file->handle_id} = { + filename => $file->filename, + loopplay => $file->loopplay, + codec => $file->codec, + file_id => $file->id, + use_parent => $file->use_parent // 1, + } } - return $handles_rs; + + foreach my $handle ($handles_rs->all) { + my %file_handle = $handle->get_inflated_columns; + my $file = $files{$file_handle{handle_id}} // { + filename => undef, + loopplay => undef, + codec => undef, + file_id => undef, + use_parent => undef, + }; + push @file_handles, {%file_handle, %{$file}}; + } + + return \@file_handles; } sub apply_default_soundset_files{ my (%params) = @_; - my ($c, $lang, $set_id, $handles_rs, $loopplay, $override, $error_ref) = @params{qw/c lang set_id handles_rs loopplay override error_ref/}; + my ($c, $lang, $set_id, $file_handles, $loopplay, $override, $error_ref) = @params{qw/c lang set_id file_handles loopplay override error_ref/}; $loopplay = $loopplay ? 1 : 0; my $schema = $c->model('DB'); my $base = "/var/lib/ngcp-soundsets"; - foreach my $h($handles_rs->all) { - my $hname = $h->get_column("handlename"); + foreach my $h (@{$file_handles}) { + my $handle_name = $h->{handle_name}; my @paths = ( - "$base/system/$lang/$hname.wav", - "$base/customer/$lang/$hname.wav", - "/var/lib/asterisk/sounds/$lang/digits/$hname.wav", + "$base/system/$lang/$handle_name.wav", + "$base/customer/$lang/$handle_name.wav", + "/var/lib/asterisk/sounds/$lang/digits/$handle_name.wav", ); my $path; foreach my $p(@paths) { @@ -231,8 +254,8 @@ sub apply_default_soundset_files{ my $data_ref; my $codec = 'WAV'; - my $handle_id = $h->get_column("handleid"); - my $file_id = $h->get_column("fileid"); + my $handle_id = $h->{handle_id}; + my $file_id = $h->{file_id}; my $fres; my $fname = basename($path); @@ -243,7 +266,7 @@ sub apply_default_soundset_files{ } if (defined $file_id) { if ($override) { - $c->log->debug("override $path as $hname for existing id $file_id"); + $c->log->debug("override $path as $handle_name for existing id $file_id"); $fres = $schema->resultset('voip_sound_files')->find($file_id); $fres->update({ @@ -252,10 +275,10 @@ sub apply_default_soundset_files{ loopplay => $loopplay, }); } else { - $c->log->debug("skip $path as $hname exists via id $file_id and override is not set"); + $c->log->debug("skip $path as $handle_name exists via id $file_id and override is not set"); } } else { - $c->log->debug("inserting $path as $hname with new id"); + $c->log->debug("inserting $path as $handle_name with new id"); $fres = $schema->resultset('voip_sound_files') ->create({ diff --git a/share/templates/sound/handles_list.tt b/share/templates/sound/handles_list.tt index fc689af2ce..dc62aa641c 100644 --- a/share/templates/sound/handles_list.tt +++ b/share/templates/sound/handles_list.tt @@ -52,38 +52,38 @@ [% FOREACH r IN group.value %] - [% r.get_column("handlename") %] + [% r.handle_name %] - [% r.get_column("fileid") - ? r.get_column("filename") - ? r.get_column("filename") : c.loc("(empty)") + [% r.file_id + ? r.filename + ? r.filename : c.loc("(empty)") : '' %] - + [% IF set_result.parent_id -%] - + [% END -%]
- [% IF r.get_column("filename").size -%] + [% IF r.filename.size -%] [% IF can_edit_handles -%] - [% c.loc('Edit') %] - [% c.loc('Delete') %] + [% c.loc('Edit') %] + [% c.loc('Delete') %] [% END -%] - [% c.loc('Play') %] - [% ELSIF r.get_column("fileid") -%] + [% c.loc('Play') %] + [% ELSIF r.file_id -%] [% IF can_edit_handles -%] - [% c.loc('Edit') %] - [% c.loc('Delete') %] + [% c.loc('Edit') %] + [% c.loc('Delete') %] [% END -%] [% ELSIF can_edit_handles -%] - [% c.loc('Add') %] + [% c.loc('Add') %] [% END -%]