fix parsing of X-ica using http://www.cap.srce.hr/IzgledX.aspx
[koha.git] / members / member.pl
index f6bc2f2..c5ea894 100755 (executable)
 # 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, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
+#use warnings; FIXME - Bug 2505
 use C4::Auth;
 use C4::Output;
 use CGI;
@@ -36,33 +37,33 @@ my $quicksearch = $input->param('quicksearch');
 my $startfrom = $input->param('startfrom')||1;
 my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
 
-my ($template, $loggedinuser, $cookie);
-if($quicksearch){
-    ($template, $loggedinuser, $cookie)
-    = get_template_and_user({template_name => "members/member-quicksearch-results.tmpl",
-                 query => $input,
-                 type => "intranet",
-                 authnotrequired => 0,
-                 flagsrequired => {borrowers => 1},
-                 });
-} else {
-    ($template, $loggedinuser, $cookie)
+my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "members/member.tmpl",
                  query => $input,
                  type => "intranet",
                  authnotrequired => 0,
                  flagsrequired => {borrowers => 1},
                  });
-}
+
 my $theme = $input->param('theme') || "default";
 
 my $patron = $input->Vars;
 foreach (keys %$patron){
-       delete $$patron{$_} unless($$patron{$_}); 
+       delete $$patron{$_} unless($$patron{$_});
 }
-
 my @categories=C4::Category->all;
-my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
+
+my $branches = GetBranches;
+my @branchloop;
+
+foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
+  my $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
+  my %row = ( value => $_,
+        selected => $selected,
+        branchname => $branches->{$_}->{branchname},
+      );
+  push @branchloop, \%row;
+}
 
 my %categories_dislay;
 
@@ -81,33 +82,37 @@ if (C4::Context->preference("AddPatronLists")=~/code/){
 }  
 
 my $member=$input->param('member');
-my $orderby=$input->param('orderby');
-$orderby = "surname,firstname" unless $orderby;
+my $orderbyparams=$input->param('orderby');
+my @orderby;
+if ($orderbyparams){
+       my @orderbyelt=split(/,/,$orderbyparams);
+       push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
+}
+else {
+       @orderby = ({surname=>0},{firstname=>0});
+}
+
+warn "[$member]";
+# FIXME dpavlin - FER
+$member =~ s/^.*%B601983\d\d(\d{10})\d&.*$/S$1/ && warn "JMBAG: $1";
 $member =~ s/,//g;   #remove any commas from search string
 $member =~ s/\*/%/g;
 
 my ($count,$results);
 
-if (C4::Context->preference("IndependantBranches")){
-   if (C4::Context->userenv && C4::Context->userenv->{flags} % 2 !=1 && C4::Context->userenv->{'branch'}){
-        $$patron{branchcode}=C4::Context->userenv->{'branch'} unless (C4::Context->userenv->{'branch'} eq "insecure");
-   }
-}
-$$patron{firstname}.="\%" if ($$patron{firstname});
-$$patron{surname}.="\%" if ($$patron{surname});
-
 my @searchpatron;
 push @searchpatron, $member if ($member);
 push @searchpatron, $patron if (keys %$patron);
 my $from= ($startfrom-1)*$resultsperpage;
 my $to=$from+$resultsperpage;
  #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
- ($results)=Search(\@searchpatron,{surname=>1,firstname=>1},undef,undef,["firstname","surname","email","othernames","cardnumber"],"start_with"  ) if (@searchpatron);
+ my $search_scope=($quicksearch?"field_start_with":"contain");
+ ($results)=Search(\@searchpatron,\@orderby,undef,undef,["firstname","surname","email","othernames","cardnumber","userid"],$search_scope  ) if (@searchpatron);
 if ($results){
        $count =scalar(@$results);
 }
 my @resultsdata;
-my $to=($count>$to?$to:$count);
+$to=($count>$to?$to:$count);
 my $index=$from;
 foreach my $borrower(@$results[$from..$to-1]){
   #find out stats
@@ -127,11 +132,6 @@ foreach my $borrower(@$results[$from..$to-1]){
   push(@resultsdata, \%row);
 }
 
-if ($$patron{branchcode}){
-       foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
-               $$branch{selected}=1;
-       }
-}
 if ($$patron{categorycode}){
        foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
                $$category{selected}=1;
@@ -139,7 +139,7 @@ if ($$patron{categorycode}){
 }
 my %parameters=
         (  %$patron
-               , 'orderby'                     => $orderby 
+               , 'orderby'                     => $orderbyparams 
                , 'resultsperpage'      => $resultsperpage 
         , 'type'=> 'intranet'); 
 my $base_url =
@@ -149,24 +149,23 @@ my $base_url =
     map { "$_=$parameters{$_}" } (keys %parameters)
   );
 
+my @letters = map { {letter => $_} } ( 'A' .. 'Z');
+
 $template->param(
+    letters => \@letters,
     paginationbar => pagination_bar(
-        $base_url,  int( $count / $resultsperpage ) + 1,
+        $base_url,
+        int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
         $startfrom, 'startfrom'
     ),
     startfrom => $startfrom,
     from      => ($startfrom-1)*$resultsperpage+1,  
     to        => $to,
-    multipage => ($count != $to+1 || $startfrom!=1),
-);
-$template->param(
-    branchloop=>$branches,
-       categoryloop=>\@categories,
-);
-
-
-$template->param( 
-        searching       => "1",
+    multipage => ($count != $to || $startfrom!=1),
+    advsearch => ($$patron{categorycode} || $$patron{branchcode}),
+    branchloop=>\@branchloop,
+    categories=>\@categories,
+    searching       => "1",
                actionname              =>basename($0),
                %$patron,
         numresults      => $count,