Bug 6790: Remove unnecessary variable from getroutinglist return
authorColin Campbell <colin.campbell@ptfs-europe.com>
Fri, 26 Aug 2011 15:17:11 +0000 (16:17 +0100)
committerPaul Poulain <paul.poulain@biblibre.com>
Mon, 23 Jan 2012 16:59:27 +0000 (17:59 +0100)
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>
C4/Serials.pm
koha-tmpl/intranet-tmpl/prog/en/modules/serials/routing.tt
serials/routing-preview.pl
serials/routing.pl
serials/subscription-detail.pl

index 9ec4630..9cda52e 100644 (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
index 85c42b1..9851ebf 100644 (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>
index 64849b5..6746f53 100755 (executable)
@@ -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),
     );
index 2bd7d69..f093da7 100755 (executable)
@@ -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;
-
-    push(@results, $data);
-}
+    $member->{routingid}=$routing->{routingid} || q{};
+    $member->{ranking} = $routing->{ranking} || q{};
 
-# 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;
index 7386820..8e7f418 100755 (executable)
@@ -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...)