-use C4::Acquisition;
-use C4::Interface::CGI::Output;
-use C4::Context;
-use HTML::Template;
-use C4::Search;
-use C4::Serials;
-
-my $query = new CGI;
-my $subscriptionid = $query->param('subscriptionid');
-my $op = $query->param('op');
-my $searchstring = $query->param('member');
-my $dbh = C4::Context->dbh;
-
-my $env;
-
- my ($template, $loggedinuser, $cookie)
-= get_template_and_user({template_name => "serials/member-search.tmpl",
- query => $query,
- type => "intranet",
- authnotrequired => 0,
- flagsrequired => {catalogue => 1},
- debug => 1,
- });
-
-
-if($searchstring){
- my ($count, $members) = &BornameSearch($env, $searchstring, "surname", "advanced");
-
- $template->param(
- subscriptionid => $subscriptionid,
- memberloop => $members,
- member => $searchstring,
- );
-} else {
- $template->param(
- subscriptionid => $subscriptionid,
+use C4::Members; # BornameSearch
+use C4::Branch;
+use C4::Category;
+use File::Basename;
+
+my $cgi = new CGI;
+my $theme = $cgi->param('theme') || "default";
+my $resultsperpage = $cgi->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
+my $startfrom = $cgi->param('startfrom')||1;
+
+my $patron = $cgi->Vars;
+foreach (keys %$patron){
+ delete $$patron{$_} unless($$patron{$_});
+}
+
+my @categories=C4::Category->all;
+my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
+my $subscriptionid = $cgi->param('subscriptionid');
+my $searchstring = $cgi->param('member');
+
+my %categories_dislay;
+my ($template, $loggedinuser, $cookie);
+ ($template, $loggedinuser, $cookie)
+ = get_template_and_user({template_name => "serials/member-search.tmpl",
+ query => $cgi,
+ type => "intranet",
+ authnotrequired => 0,
+ flagsrequired => { serials => 'routing' },
+ });
+
+foreach my $category (@categories){
+ my $hash={
+ category_description=>$$category{description},
+ category_type=>$$category{category_type}
+ };
+ $categories_dislay{$$category{categorycode}} = $hash;
+}
+$template->param(
+ "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
+ );
+if (C4::Context->preference("AddPatronLists")=~/code/){
+ $categories[0]->{'first'}=1;
+}
+
+my $member=$cgi->param('member');
+my $orderby=$cgi->param('orderby');
+$orderby = "surname,firstname" unless $orderby;
+if (defined $member) {
+ $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;
+if (@searchpatron) {
+ ($results) = Search(
+ \@searchpatron,
+ [ { surname => 0 }, { firstname => 0 } ],
+ undef,
+ undef,
+ [ "firstname", "surname", "email", "othernames", "cardnumber" ],
+ "start_with"