From 522becf57d9d2f7b4339822af41a0e80594a538a Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 24 Jan 2019 15:40:26 +0100 Subject: [PATCH] Bug 22193: Code refactoring/improvments Signed-off-by: Owen Leonard --- C4/Serials.pm | 8 +++--- Koha/SharedContent.pm | 30 ++++++++++++++++++++++ koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc | 18 ++++++++++--- .../prog/en/includes/serials-toolbar.inc | 4 +-- svc/mana/{share => comment} | 25 +++--------------- svc/mana/increment | 10 +++++++- 6 files changed, 63 insertions(+), 32 deletions(-) rename svc/mana/{share => comment} (65%) diff --git a/C4/Serials.pm b/C4/Serials.pm index e929e21..bf2a2fa 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -281,10 +281,10 @@ sub GetSubscription { }); $subscription->{additional_fields} = $additional_field_values->{$subscriptionid}; - if ( my $mana_id = $subscription->{mana_id} ) { - my $mana_subscription = Koha::SharedContent::get_entity_by_id( - 'subscription', $mana_id, {usecomments => 1}); - $subscription->{comments} = $mana_subscription->{data}->{comments}; + if ( C4::Context->preference('Mana') == 1 ) { + $subscription->{comments} = Koha::SharedContent::get_entity_comments( + 'subscription', $subscription->{mana_id} + ); } return $subscription; diff --git a/Koha/SharedContent.pm b/Koha/SharedContent.pm index 587285d..e69ec88 100644 --- a/Koha/SharedContent.pm +++ b/Koha/SharedContent.pm @@ -134,6 +134,36 @@ sub get_entity_by_id { return process_request(build_request('getwithid', @_)); } +=head3 get_entity_comments + +=cut + +sub get_entity_comments { + my ($resource, $id) = @_; + + unless ($id) { + return []; + } + + my $entity = get_entity_by_id( + $resource, $id, { usecomments => 1 } + ); + + return $entity->{data}->{comments}; +} + +=head3 send_entity_comment + +=cut + +sub send_entity_comment { + my ($content) = @_; + + my $result = process_request(build_request('post', 'resource_comment', $content)); + + return $result; +} + =head3 search_entities =cut diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc index 2dd7e86..69f1bb1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc @@ -7,16 +7,28 @@ $(document).ready(function() { url: "/cgi-bin/koha/svc/mana/increment", data: {id: mana_id, resource: resource, field: fieldvalue, step: stepvalue}, datatype: "json", - }) + }).done(function(result) { + data = JSON.parse(result); + + html = data.message + ' (' + data.new_count + ')'; + comment = $('li.mana-comment[data-id="' + data.id + '"] a').html(html); + }); } function mana_comment( target_id, manamsg, resource_type ) { $.ajax( { type: "POST", - url: "/cgi-bin/koha/svc/mana/share", + url: "/cgi-bin/koha/svc/mana/comment", data: {message: manamsg, resource: resource_type , resource_id: target_id}, datatype: "json", - }) + }).done(function(result) { + data = JSON.parse(result); + comment = $('
  • ').addClass('mana-comment') + .attr('data-id', data.id) + .append('' + manamsg + ' (1)'); + + $('.mana-comment-divider').before(comment); + }); } $(document).on('click', 'ul li.mana-comment', function() { diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc index e8aabde..9e2f21c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc @@ -70,8 +70,8 @@ [% c.message | html %] ([% c.nb | html %]) [% END %] - -
  • Other
  • + +
  • New
  • diff --git a/svc/mana/share b/svc/mana/comment similarity index 65% rename from svc/mana/share rename to svc/mana/comment index 508eec2..9d58143 100755 --- a/svc/mana/share +++ b/svc/mana/comment @@ -44,26 +44,7 @@ my $content; $content->{resource_id} = $input->param("resource_id"); $content->{resource_type} = $input->param("resource"); $content->{message} = $input->param("message"); -Koha::SharedContent::send_entity('', undef, undef, 'resource_comment', $content); -my $package = "Koha::".ucfirst($input->param('resource')); -my $resource; -my $result; -eval{ - $result = Koha::SharedContent::get_entity_by_id( - scalar $input->param('resource'), - scalar $input->param('id') - ); -}; -if ( $@ or $result->{code} == 500 ){ - $resource->{errmsg} = "Error: mana access got broken, please try again later\n\n ( error: $@ )"; -} -else{ - if ( $input->param( 'saveinbase' )) { - $resource = { id => $package->new_from_mana($result->{data})->id }; - } - else{ - $resource = $result->{data}; - } -} -print(to_json($resource)); +my $result = Koha::SharedContent::send_entity_comment($content); + +print(to_json($result)); diff --git a/svc/mana/increment b/svc/mana/increment index 5b58496..ccd29eb 100755 --- a/svc/mana/increment +++ b/svc/mana/increment @@ -45,4 +45,12 @@ my $result = Koha::SharedContent::increment_entity_value( scalar $input->param('step') ); -return $result; +my $entity = Koha::SharedContent::get_entity_by_id( + scalar $input->param('resource'), + scalar $input->param('id') +); + +$result->{new_count} = $entity->{data}->{nb}; +$result->{message} = $entity->{data}->{message}; + +print(to_json($result)); -- 2.1.4