Tidying up, and getting it working again
[koha.git] / opac / searchoptions.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 # Searching within results is done by simply adding on parameters onto the query, being careful to have the requried marclist etc fields.
5 use C4::Database;
6 use C4::Interface::CGI::Output;
7 use C4::Context;
8 use CGI;
9 my $query = new CGI;
10 my $newquery='';
11 my $allitemtypesbool = $query->param("allitemtypes");
12 my $allbranchesbool = $query->param("allbranches");
13 my $allcategoriesbool = $query->param("allcategories");
14 my $allsubcategoriesbool = $query->param("allsubcategories");
15 my $allmediatypesbool = $query->param("allmediatypes");
16 my $nbstatements = $query->param("nbstatementsori");
17 my $orderby = $query->param("orderbyori");
18 my @keywords = $query->param("keyword");
19 my @marclist = $query->param('marclist');
20 my @and_or = $query->param('and_or');
21 my @excluding = $query->param('excluding');
22 my @operator = $query->param('operator');
23 my @value = $query->param('value');
24 my $searchtype = $query->param('searchtype');
25 my $category = $query->param('categorylist');
26 my @itemtypeswanted = $query->param("itemtypeswanted");
27 my $itemtypessearched = $query->param("itemtypessearched");
28 my @mediatypeswanted = $query->param("mediatypeswanted");
29 my @subcategorieswanted = $query->param("subcategorieswanted");
30 my @brancheswanted = $query->param("brancheswanted");
31 my $avail = $query->param("avail");
32
33 my $brancheslist;
34 my $count=0;
35 my $newquery='';
36 my $keywordfoundbool=0;
37 my $itemtypeslist;
38 my $itemtypescatlist;
39 my $itemtypessubcatlist;
40 my $mediatypeslist;
41
42 $count=0;
43 my $dbh=C4::Context->dbh;
44 $newquery='op=do_search&nbstatements='.$nbstatements;
45 if ($allcategoriesbool == 0 && $category ne ''){
46
47     my $sth=$dbh->prepare("select itemtypecodes from categorytable where categorycode=?");
48     $sth->execute($category);
49     $itemtypescatlist = $sth->fetchrow .'|';
50     $sth->finish;
51 }
52 if ($allmediatypesbool == 0 && @mediatypeswanted ne ''){
53     foreach my $mediatype (@mediatypeswanted){
54         my $sth=$dbh->prepare("select itemtypecodes from mediatypetable where mediatypecode=?");
55         $sth->execute($mediatype);
56         $mediatypeslist .= $sth->fetchrow.'|';
57         $sth->finish;
58
59     }
60 }
61 if ($allsubcategoriesbool == 0 && @subcategorieswanted ne ''){
62     foreach my $subcategory (@subcategorieswanted){
63         my $sth=$dbh->prepare("select itemtypecodes from subcategorytable where subcategorycode=?");
64         $sth->execute($subcategory);
65         $itemtypessubcatlist .= $sth->fetchrow.'|';
66         $sth->finish;
67
68     }
69 }
70 if ($allitemtypesbool == 0){
71
72 $itemtypeslist .=$itemtypescatlist.$itemtypessubcatlist.$mediatypeslist.$itemtypessearched.join ("|", @itemtypeswanted)
73 } else {
74 $itemtypeslist .=$itemtypescatlist.$itemtypessubcatlist.$mediatypeslist.$itemtypessearched
75 }
76
77 if ($allbranchesbool == 0){
78    $brancheslist = join("|",@brancheswanted)
79 }
80
81 if ($searchtype eq 'NewSearch'){
82
83      $newquery .= '&marclist=';
84      $newquery .= '&and_or=and';
85      $newquery .= '&excluding=';
86      $newquery .= '&operator=contains';
87      $newquery .= '&value=';
88      $newquery .=join(" ",@keywords)
89 } elsif ($searchtype eq 'SearchWithin'){
90     foreach my $marclistitem (@marclist) {
91          $newquery .= '&marclist='.$marclist[$count];
92          $newquery .= '&and_or='.$and_or[$count];
93          $newquery .= '&excluding='.$excluding[$count];
94          $newquery .= '&operator=';
95          $newquery .= $operator[$count];
96          $newquery .= '&value='.$value[$count];
97          if ($marclist[$count] eq ''){
98              if ($keywordfoundbool=0){
99                 $keywordfoundbool=1;
100                 $newquery .=" ".join(" ",@keywords)
101              }
102          }
103         $count++;
104
105     }
106     if ($keywordfoundbool == 0 && $query->param('keysub') ne ''){
107         $newquery .= '&marclist=&and_or=and&excluding=&operator=contains&value=';
108         $newquery .=join(" ",@keywords)
109     }
110 }
111 $newquery .= '&orderby='.$orderby;
112 if ($itemtypeslist ne ''){
113     $newquery .= '&itemtypesstring="'.$itemtypeslist.'"'
114 }
115 if ($brancheslist ne ''){
116     $newquery .= '&branchesstring="'.$brancheslist.'"';
117 }
118 if ($avail ne ''){
119     $newquery .= '&avail=1'
120 }
121 print $query->redirect("/cgi-bin/koha/opac-search.pl?$newquery");