Bug 6790: Remove unnecessary variable from getroutinglist return

getroutinglist returns a count variable to indicate how many elements
are in the array. This is almost always a serious code smell. (We are
programming in a list manipulating language) The routine was executing
am unnecessary loop just to maintain that var.
Removed the variable from the routine and perldoc
refactored calls of the routine removed the c-style loops for
more idiomatic and maintainable for loops
renamed some opaquely named variables
removed a call to the routine where nothing was done with the data
moved some html out of the calling script and into the template

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
This commit is contained in:
Colin Campbell 2011-08-26 16:17:11 +01:00 committed by Paul Poulain
parent b32d2d2165
commit d2c24f3bbf
5 changed files with 64 additions and 86 deletions

View file

@ -2019,12 +2019,11 @@ sub delroutingmember {
=head2 getroutinglist
($count,@routinglist) = getroutinglist($subscriptionid)
@routinglist = getroutinglist($subscriptionid)
this gets the info from the subscriptionroutinglist for $subscriptionid
return :
a count of the number of members on routinglist
the routinglist as an array. Each element of the array contains a hash_ref containing
routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
@ -2034,20 +2033,14 @@ sub getroutinglist {
my ($subscriptionid) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(
"SELECT routingid, borrowernumber, ranking, biblionumber
'SELECT routingid, borrowernumber, ranking, biblionumber
FROM subscription
JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
"
WHERE subscription.subscriptionid = ? ORDER BY ranking ASC'
);
$sth->execute($subscriptionid);
my @routinglist;
my $count = 0;
while ( my $line = $sth->fetchrow_hashref ) {
$count++;
push( @routinglist, $line );
}
return ( $count, @routinglist );
my $routinglist = $sth->fetchall_arrayref({});
return @{$routinglist};
}
=head2 countissuesfrom

View file

@ -47,16 +47,30 @@ function search_member(subscriptionid){
[% END %]
</select> [% issue %]</li>
[% IF ( memberloop ) %]
[% IF memberloop %]
<li><span class="label">Recipients:</span><table style="clear:none;margin:0;">
<tr><th>Name</th>
<th>Rank</th>
<th>Delete</th></tr>
[% FOREACH memberloo IN memberloop %]
<tr><td>[% memberloo.name %]</td>
<td>[% memberloo.routingbox %]</td>
<td><a href="/cgi-bin/koha/serials/routing.pl?routingid=[% memberloo.routingid %]&amp;subscriptionid=[% subscriptionid %]&amp;op=delete">Delete</a></td></tr>
[% END %]
<th>Delete</th>
</tr>
[% USE m_loop = iterator(memberloop) %]
[% FOREACH member IN m_loop %]
<tr><td>[% member.name %]</td>
<td>
<select name="itemrank" onchange="reorder_item([%- subscriptionid -%], [%- member.routingid -%], this.option[this.selectedIndex].value)">
[% rankings = [1 .. m_loop.size] %]
[% FOREACH r IN rankings %]
[% IF r == member.ranking %]
<option selected="selected" value="[% r %]">[% r %]</option>
[% ELSE %]
<option value="[% r %]">[% r %]</option>
[% END %]
[% END %]
</select>
</td>
<td><a href="/cgi-bin/koha/serials/routing.pl?routingid=[% member.routingid %]&amp;subscriptionid=[% subscriptionid %]&amp;op=delete">Delete</a></td>
</tr>
[% END %]
</table><p style="margin-left:10em;"><a onclick="search_member([% subscriptionid %]); return false"
href="/cgi-bin/koha/serials/member-search.pl?subscriptionid=[% subscriptionid %]" class="button">Add recipients</a> &nbsp; <a
href="/cgi-bin/koha/serials/routing.pl?subscriptionid=[% subscriptionid %]&amp;op=delete" class="button">Delete All</a></p></li>

View file

@ -56,7 +56,7 @@ if($edit){
print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
}
my ($routing, @routinglist) = getroutinglist($subscriptionid);
my @routinglist = getroutinglist($subscriptionid);
my $subs = GetSubscription($subscriptionid);
my ($tmp ,@serials) = GetSerials($subscriptionid);
my ($template, $loggedinuser, $cookie);
@ -81,18 +81,17 @@ if($ok){
my $const = 'o';
my $notes;
my $title = $subs->{'bibliotitle'};
for(my $i=0;$i<$routing;$i++){
my $sth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ?");
$sth->execute($biblio,$routinglist[$i]->{'borrowernumber'});
my $data = $sth->fetchrow_hashref;
for my $routing ( @routinglist ) {
my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? LIMIT 1');
$sth->execute($biblio,$routing->{borrowernumber});
my $reserve = $sth->fetchrow_hashref;
# warn "$routinglist[$i]->{'borrowernumber'} is the same as $data->{'borrowernumber'}";
if($routinglist[$i]->{'borrowernumber'} == $data->{'borrowernumber'}){
ModReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
} else {
AddReserve($branch,$routinglist[$i]->{'borrowernumber'},$biblio,$const,\@bibitems,$routinglist[$i]->{'ranking'},'',$notes,$title);
}
}
if($routing->{borrowernumber} == $reserve->{borrowernumber}){
ModReserve($routing->{ranking},$biblio,$routing->{borrowernumber},$branch);
} else {
AddReserve($branch,$routing->{borrowernumber},$biblio,$const,\@bibitems,$routing->{ranking}, undef, undef, $notes,$title);
}
}
}
($template, $loggedinuser, $cookie)
@ -115,15 +114,11 @@ if($ok){
});
}
my @results;
my $data;
for(my $i=0;$i<$routing;$i++){
$data=GetMember('borrowernumber' => $routinglist[$i]->{'borrowernumber'});
$data->{'location'}=$data->{'branchcode'};
$data->{'name'}="$data->{'firstname'} $data->{'surname'}";
$data->{'routingid'}=$routinglist[$i]->{'routingid'};
$data->{'subscriptionid'}=$subscriptionid;
push(@results, $data);
my $memberloop = [];
for my $routing (@routinglist) {
my $member = GetMember( borrowernumber => $routing->{borrowernumber} );
$member->{name} = "$member->{firstname} $member->{surname}";
push @{$memberloop}, $member;
}
my $routingnotes = $serials[0]->{'routingnotes'};
@ -134,7 +129,7 @@ $template->param(
issue => $issue,
issue_escaped => URI::Escape::uri_escape($issue),
subscriptionid => $subscriptionid,
memberloop => \@results,
memberloop => $memberloop,
routingnotes => $routingnotes,
hasRouting => check_routing($subscriptionid),
);

View file

@ -60,13 +60,13 @@ if($op eq 'add'){
addroutingmember($borrowernumber,$subscriptionid);
}
if($op eq 'save'){
my $sth = $dbh->prepare("UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?");
my $sth = $dbh->prepare('UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?');
$sth->execute($notes,$subscriptionid);
my $urldate = URI::Escape::uri_escape($date_selected);
print $query->redirect("routing-preview.pl?subscriptionid=$subscriptionid&issue=$urldate");
}
my ($routing, @routinglist) = getroutinglist($subscriptionid);
my @routinglist = getroutinglist($subscriptionid);
my $subs = GetSubscription($subscriptionid);
my ($count,@serials) = GetSerials($subscriptionid);
my $serialdates = GetLatestSerials($subscriptionid,$count);
@ -86,65 +86,41 @@ foreach my $dateseq (@{$serialdates}) {
}
my ($template, $loggedinuser, $cookie)
= get_template_and_user({template_name => "serials/routing.tmpl",
= get_template_and_user({template_name => 'serials/routing.tmpl',
query => $query,
type => "intranet",
type => 'intranet',
authnotrequired => 0,
flagsrequired => {serials => 'routing'},
debug => 1,
});
my @results;
my $data;
for(my $i=0;$i<$routing;$i++){
$data=GetMember('borrowernumber' => $routinglist[$i]->{'borrowernumber'});
$data->{'location'}=$data->{'branchcode'};
if ($data->{firstname} ) {
$data->{name} = $data->{firstname} . q| |;
my $member_loop = [];
for my $routing ( @routinglist ) {
my $member=GetMember('borrowernumber' => $routing->{borrowernumber});
$member->{location} = $member->{branchcode};
if ($member->{firstname} ) {
$member->{name} = $member->{firstname} . q| |;
}
else {
$data->{name} = q{};
$member->{name} = q{};
}
if ($data->{surname} ) {
$data->{name} .= $data->{surname};
if ($member->{surname} ) {
$member->{name} .= $member->{surname};
}
$data->{'routingid'}=$routinglist[$i]->{'routingid'};
$data->{'subscriptionid'}=$subscriptionid;
if (! $routinglist[$i]->{routingid} ) {
$routinglist[$i]->{routingid} = q||;
}
my $rankingbox = '<select name="itemrank" onchange="reorder_item('
. $subscriptionid . ',' .$routinglist[$i]->{'routingid'} . ',this.options[this.selectedIndex].value)">';
for(my $j=1; $j <= $routing; $j++) {
$rankingbox .= "<option ";
if($routinglist[$i]->{ranking} && $routinglist[$i]->{ranking} == $j){
$rankingbox .= " selected=\"selected\"";
}
$rankingbox .= " value=\"$j\">$j</option>";
}
$rankingbox .= "</select>";
$data->{'routingbox'} = $rankingbox;
$member->{routingid}=$routing->{routingid} || q{};
$member->{ranking} = $routing->{ranking} || q{};
push(@results, $data);
}
# for adding routing list
my $new;
if ($op eq 'new') {
$new = 1;
} else {
# for modify routing list default
$new = 0;
push(@{$member_loop}, $member);
}
$template->param(
title => $subs->{'bibliotitle'},
title => $subs->{bibliotitle},
subscriptionid => $subscriptionid,
memberloop => \@results,
op => $new,
memberloop => $member_loop,
op => $op eq 'new',
dates => $dates,
routingnotes => $serials[0]->{'routingnotes'},
hasRouting => check_routing($subscriptionid),
);
output_html_with_http_headers $query, $cookie, $template->output;
output_html_with_http_headers $query, $cookie, $template->output;

View file

@ -46,7 +46,7 @@ if ($op && $op eq 'del') {
print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
exit;
}
my ($routing, @routinglist) = getroutinglist($subscriptionid);
my ($totalissues,@serialslist) = GetSerials($subscriptionid);
$totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
# the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)