Bug 12648: Refactoring to prepare user search for reuse

A previous enhancement allows to link basket with patrons.
Next patches will use the same way to link order with patrons.

In order to avoir c/p of code, this patch refactores this part of code.

Test plan:
1/ Verify there is no regression on adding/modifying users to a basket.
(acqui/basket.pl?basketno=XXX, "Managed by", "Add user").
2/ Note that you get a friendly message if the user is already present in the
list and when the user has correctly been added to the list.
3/ Note that the list uses the member search service (ie. DataTable +
serverside processing).

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
This commit is contained in:
Jonathan Druart 2014-07-24 13:33:06 +02:00 committed by Tomas Cohen Arazi
parent 1072b92676
commit bb1beb4f5d
7 changed files with 176 additions and 117 deletions

View file

@ -13,9 +13,13 @@ sub search {
my $categorycode = $params->{categorycode};
my $branchcode = $params->{branchcode};
my $searchtype = $params->{searchtype};
my $searchfieldstype = $params->{searchfieldstype};
my $searchfieldstype = $params->{searchfieldstype} || 'standard';
my $dt_params = $params->{dt_params};
unless ( $searchmember ) {
$searchmember = $dt_params->{sSearch};
}
my ($iTotalRecords, $iTotalDisplayRecords);
# If branches are independant and user is not superlibrarian

View file

@ -1,23 +1,21 @@
#!/usr/bin/perl
# script to find a basket user
# Copyright 2012 BibLibre SARL
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# Copyright 2014 BibLibre
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
@ -31,7 +29,7 @@ my $input = new CGI;
my $dbh = C4::Context->dbh;
my ( $template, $loggedinuser, $cookie, $staff_flags ) = get_template_and_user(
{ template_name => "acqui/aqbasketuser_search.tt",
{ template_name => "acqui/add_user_search.tt",
query => $input,
type => "intranet",
authnotrequired => 0,
@ -42,18 +40,35 @@ my ( $template, $loggedinuser, $cookie, $staff_flags ) = get_template_and_user(
my $q = $input->param('q') || '';
my $op = $input->param('op') || '';
my $referer = $input->referer();
# If this script is called by acqui/basket.pl
# the patrons to return should be superlibrarian or have the order_manage
# acquisition flag.
my $search_patrons_with_acq_perm_only =
( $referer =~ m|acqui/basket.pl| )
? 1 : 0;
if( $op eq "do_search" ) {
my $results = C4::Members::Search( $q, "surname");
my @users_loop;
my $nresults = 0;
foreach my $res (@$results) {
my $perms = haspermission( $res->{userid} );
my $subperms = get_user_subpermissions( $res->{userid} );
my $should_be_returned = 1;
if( $perms->{superlibrarian} == 1
|| $perms->{acquisition} == 1
|| $subperms->{acquisition}->{'order_manage'} ) {
if ( $search_patrons_with_acq_perm_only ) {
$should_be_returned = 0;
my $perms = haspermission( $res->{userid} );
my $subperms = get_user_subpermissions( $res->{userid} );
if( $perms->{superlibrarian} == 1
|| $perms->{acquisition} == 1
|| $subperms->{acquisition}->{'order_manage'} ) {
$should_be_returned = 1;
}
}
if ( $should_be_returned ) {
my %row = (
borrowernumber => $res->{borrowernumber},
cardnumber => $res->{cardnumber},
@ -74,4 +89,7 @@ if( $op eq "do_search" ) {
);
}
$template->param(
patrons_with_acq_perm_only => $search_patrons_with_acq_perm_only,
);
output_html_with_http_headers( $input, $cookie, $template->output );

View file

@ -0,0 +1,95 @@
[% USE Koha %]
[% INCLUDE 'doc-head-open.inc' %]
<title>Koha &rsaquo; Patron search</title>
[% INCLUDE 'doc-head-close.inc' %]
<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
[% INCLUDE 'datatables.inc' %]
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
$("#info").hide();
$("#error").hide();
// Apply DataTables on the results table
dtMemberResults = $("#memberresultst").dataTable($.extend(true, {}, dataTablesDefaults, {
'bServerSide': true,
'sAjaxSource': "/cgi-bin/koha/svc/members/search",
'fnServerData': function(sSource, aoData, fnCallback) {
aoData.push({
'name': 'template_path',
'value': 'acqui/tables/members_results.tt',
});
$.ajax({
'dataType': 'json',
'type': 'POST',
'url': sSource,
'data': aoData,
'success': function(json){
fnCallback(json);
}
});
},
'aoColumns':[
{ 'mDataProp': 'dt_cardnumber' },
{ 'mDataProp': 'dt_name' },
{ 'mDataProp': 'dt_branch' },
{ 'mDataProp': 'dt_category' },
{ 'mDataProp': 'dt_action', 'bSortable': false }
],
'bAutoWidth': false,
'sPaginationType': 'full_numbers',
"iDisplayLength": [% Koha.Preference('PatronsPerPage') %],
"bProcessing": true,
}));
dtMemberResults.fnAddFilters("filter", 750);
});
// modify parent window owner element
function add_user(borrowernumber, borrowername) {
var p = window.opener;
$("#info").hide();
$("#error").hide();
if ( p.add_user(borrowernumber, borrowername) < 0 ) {
$("#error").html(_("Borrower '%s' is already in the list.").format(borrowername));
$("#error").show();
} else {
$("#info").html(_("Borrower '%s' added.").format(borrowername));
$("#info").show();
}
}
//]]>
</script>
</head>
<body>
<div id="patron_search" class="yui-t7">
<div id="bd">
<div class="yui-g">
<h3>Search for patron</h3>
[% IF patrons_with_acq_perm_only %]
<div class="hint">Only staff with superlibrarian or acquisitions permissions (or order_manage permission if granular permissions are enabled) are returned in the search results</div>
[% END %]
<div id="info" class="dialog message"></div>
<div id="error" class="dialog alert"></div>
<table id="memberresultst">
<thead>
<tr>
<th>Card</th>
<th>Name</th>
<th>Library</th>
<th>Category</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div id="closewindow"><a href="#" class="close">Close</a></div>
</div>
</div>
[% INCLUDE 'intranet-bottom.inc' %]

View file

@ -1,79 +0,0 @@
[% INCLUDE 'doc-head-open.inc' %]
<title>Koha &rsaquo; Basket User Search</title>
[% INCLUDE 'doc-head-close.inc' %]
<style type="text/css">
#custom-doc {
width:44.46em;
*width:43.39em;
min-width:578px;
margin:auto;
text-align:left;
}
</style>
<script type="text/javascript">
//<![CDATA[
// modify parent window owner element
function add_user(borrowernumber, borrowername) {
var p = window.opener;
if(p.add_basket_user(borrowernumber, borrowername) < 0) {
alert(_("Borrower '%s' is already in the list.").format(borrowername));
}
}
//]]>
</script>
</head>
<body>
<div id="custom-doc" class="yui-t7">
<div id="bd">
<div class="yui-g">
<h3>Search for Basket User</h3>
<form action="/cgi-bin/koha/acqui/aqbasketuser_search.pl" method="post">
<fieldset>
<input type="hidden" name="op" id="op" value="do_search" />
<input type="text" name="q" id="q" value="[% q %]" class="focus" />
<input type="submit" class="button" value="Search" />
</fieldset>
<div class="hint">Only staff with superlibrarian or acquisitions permissions (or order_manage permission if granular permissions are enabled) are returned in the search results</div>
</form>
[% IF (q) %]
<p>Searched for <span class="ex">[% q %]</span>, [% nresults %] patron(s) found.</p>
[% END %]
[% IF ( users_loop ) %]
<table>
<thead>
<tr>
<th>Cardnumber</th>
<th>Name</th>
<th>Branch</th>
<th>Categorycode</th>
<th>Select?</th>
</tr>
</thead>
<tbody>
[% FOREACH user IN users_loop %]
[% IF ( loop.odd ) %]
<tr>
[% ELSE %]
<tr class="highlight">
[% END %]
<td>[% user.cardnumber %]</td>
<td>[% user.surname %], [% user.firstname %]</td>
<td>[% user.branchcode %]</td>
<td>[% user.categorycode %]</td>
<td>
<a style="cursor:pointer" onclick="add_user('[% user.borrowernumber %]', '[% user.firstname %] [% user.surname %]');">Add</a>
</td>
</tr>
[% END %]
</table>
[% END %]
<div id="closewindow"><a href="#" class="close">Close</a></div>
</div>
</div>
[% INCLUDE 'intranet-bottom.inc' %]

View file

@ -105,16 +105,16 @@
});
});
function basketUserSearchPopup(f) {
function UserSearchPopup(f) {
window.open(
"/cgi-bin/koha/acqui/aqbasketuser_search.pl",
'BasketUserSearchPopup',
"/cgi-bin/koha/acqui/add_user_search.pl",
'UserSearchPopup',
'width=740,height=450,toolbar=no,'
);
}
function add_basket_user(borrowernumber, borrowername) {
var ids = $("#basketusers_ids").val();
function add_user(borrowernumber, borrowername) {
var ids = $("#users_ids").val();
if(ids.length > 0) {
ids = ids.split(':');
} else {
@ -122,21 +122,21 @@
}
if (ids.indexOf(borrowernumber) < 0) {
ids.push(borrowernumber);
$("#basketusers_ids").val(ids.join(':'));
$("#users_ids").val(ids.join(':'));
var li = '<li id="user_'+borrowernumber+'">'+borrowername
+ ' [<a style="cursor:pointer" onclick="del_basket_user('+borrowernumber+');">'
+ ' [<a style="cursor:pointer" onclick="del_user('+borrowernumber+');">'
+ _("Delete user") + '</a>]</li>';
$("#basketusers_names").append(li);
$("#users_names").append(li);
return 0;
}
return -1;
}
function del_basket_user(borrowernumber) {
function del_user(borrowernumber) {
$("#user_"+borrowernumber).remove();
var ids = $("#basketusers_ids").val().split(':');
var ids = $("#users_ids").val().split(':');
ids.splice(ids.indexOf(borrowernumber.toString()), 1);
$("#basketusers_ids").val(ids.join(':'));
$("#users_ids").val(ids.join(':'));
}
//]]>
</script>
@ -277,18 +277,18 @@
<form action="" method="post">
<span class="label">Managed by:</span>
<div style="float:left">
<ul id="basketusers_names" style="padding-left:0">
[% FOREACH user IN basketusers %]
<ul id="users_names" style="padding-left:0">
[% FOREACH user IN users %]
<li id="user_[% user.borrowernumber %]">
[% user.firstname %] [% user.surname %]
[<a onclick="del_basket_user([% user.borrowernumber %]);" style="cursor:pointer">Delete user</a>]
[<a onclick="del_user([% user.borrowernumber %]);" style="cursor:pointer">Delete user</a>]
</li>
[% END %]
</ul>
<input type="hidden" id="basketno" name="basketno" value="[% basketno %]" />
<input type="hidden" id="basketusers_ids" name="basketusers_ids" value="[% basketusers_ids %]" />
<input type="hidden" id="users_ids" name="users_ids" value="[% users_ids %]" />
<input type="hidden" id="op" name="op" value="mod_users" />
<input type="button" id="add_user" onclick="basketUserSearchPopup();" value="Add user" />
<input type="button" id="add_user" onclick="UserSearchPopup();" value="Add user" />
<input type="submit" value="Save changes" />
</div>
</form>

View file

@ -0,0 +1,21 @@
{
"sEcho": [% sEcho %],
"iTotalRecords": [% iTotalRecords %],
"iTotalDisplayRecords": [% iTotalDisplayRecords %],
"aaData": [
[% FOREACH data IN aaData %]
{
"dt_cardnumber":
"[% data.cardnumber %]",
"dt_name":
"<span style='white-space:nowrap'><a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=[% data.borrowernumber %]'>[% INCLUDE 'patron-title.inc' borrowernumber = data.borrowernumber category_type = data.category_type firstname = data.firstname surname = data.surname othernames = data.othernames cardnumber = data.cardnumber invert_name = 1%]</a></span>",
"dt_branch":
"[% data.branchname |html %]",
"dt_category":
"[% data.category_description |html %] ([% data.category_type |html %])",
"dt_action":
"<a style=\"cursor:pointer\" onclick=\"add_user('[% data.borrowernumber %]', '[% data.surname %]', '[% data.firstname %]');\">Add</a>"
}[% UNLESS loop.last %],[% END %]
[% END %]
]
}

View file

@ -43,7 +43,7 @@ my $firstletter = $input->param('firstletter');
my $categorycode = $input->param('categorycode');
my $branchcode = $input->param('branchcode');
my $searchtype = $input->param('searchtype');
my $searchfieldstype = $input->param('searchfieldstype');
my $searchfieldstype = $input->param('searchfieldstype') || 'standard';
if ( $searchfieldstype eq "dateofbirth" ) {
$searchmember = output_pref({dt => dt_from_string($searchmember), dateformat => 'iso', dateonly => 1});