Processes the Search options sidebar input, and places it into terms and values for...
[koha-ffzg.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 $subfoundbool=0;
37 my $itemtypefoundbool=0;
38 my $branchfoundbool=0;
39 my $itemtypeslist;
40 my $itemtypescatlist;
41 my $itemtypessubcatlist;
42 my $mediatypeslist;
43 $count=0;
44 my $dbh=C4::Context->dbh;
45 $newquery='op=do_search&nbstatements='.$nbstatements;
46 if ($allcategoriesbool eq '' && $category ne ''){
47
48     my $sth=$dbh->prepare("select itemtypecodes from categorytable where categorycode=?");
49     $sth->execute($category);
50     $itemtypescatlist = $sth->fetchrow .'|';
51     $sth->finish;
52 }
53 if ($allmediatypesbool eq '' && @mediatypeswanted ne ''){
54     foreach my $mediatype (@mediatypeswanted){
55         my $sth=$dbh->prepare("select itemtypecodes from mediatypetable where mediatypecode=?");
56         $sth->execute($mediatype);
57         $mediatypeslist .= $sth->fetchrow.'|';
58         $sth->finish;
59
60     }
61 }
62 if ($allsubcategoriesbool eq '' && @subcategorieswanted ne ''){
63     foreach my $subcategory (@subcategorieswanted){
64         my $sth=$dbh->prepare("select itemtypecodes from subcategorytable where subcategorycode=?");
65         $sth->execute($subcategory);
66         $itemtypessubcatlist .= $sth->fetchrow.'|';
67         $sth->finish;
68
69     }
70 }
71 if ($allitemtypesbool ne ''){
72 #warn @itemtypeswanted;
73 $itemtypeslist .=$itemtypescatlist.$itemtypessubcatlist.$mediatypeslist.$itemtypessearched.join ("|", @itemtypeswanted)
74 } else {
75 $itemtypeslist .=$itemtypescatlist.$itemtypessubcatlist.$mediatypeslist.$itemtypessearched
76 }
77 #warn $itemtypeslist;
78 if ($allbranchesbool == 0){
79    $brancheslist = join("|",@brancheswanted)
80 }
81
82 if ($searchtype eq 'NewSearch'){
83
84      $newquery .= '&marclist=';
85      $newquery .= '&and_or=and';
86      $newquery .= '&excluding=';
87      $newquery .= '&operator=contains';
88      $newquery .= '&value=';
89      $newquery .=join(" ",@keywords)
90 } elsif ($searchtype eq 'SearchWithin'){
91     foreach my $marclistitem (@marclist) {
92          $newquery .= '&marclist='.$marclist[$count];
93          $newquery .= '&and_or='.$and_or[$count];
94          $newquery .= '&excluding='.$excluding[$count];
95          $newquery .= '&operator=';
96          $newquery .= $operator[$count];
97          $newquery .= '&value='.$value[$count];
98          if ($marclist[$count] eq ''){
99              if ($subfoundbool=0){
100                 $subfoundbool=1;
101                 $newquery .=" ".join(" ",@keywords)
102              }
103          }
104         $count++;
105
106     }
107     if ($subfoundbool == 0 && $query->param('keysub') ne ''){
108         $newquery .= '&marclist=&and_or=and&excluding=&operator=contains&value=';
109         $newquery .=join(" ",@keywords)
110     }
111 }
112 $newquery .= '&orderby='.$orderby;
113 if ($itemtypeslist ne ''){
114     $newquery .= '&itemtypesstring="'.$itemtypeslist.'"'
115 }
116 if ($brancheslist ne ''){
117     $newquery .= '&branchesstring="'.$brancheslist.'"';
118 }
119 if ($avail ne ''){
120     $newquery .= '&avail=1'
121 }
122 print $query->redirect("/cgi-bin/koha/opac-search.pl?$newquery");