Kumara - predecessor to Koha
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
7.4 KiB

  1. package C4::Search; #asummes C4/Search
  2. #requires DBI.pm to be installed
  3. #uses DBD:Pg
  4. use strict;
  5. require Exporter;
  6. use DBI;
  7. use C4::Database;
  8. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  9. # set the version for version checking
  10. $VERSION = 0.01;
  11. @ISA = qw(Exporter);
  12. @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch);
  13. %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
  14. # your exported package globals go here,
  15. # as well as any optionally exported functions
  16. @EXPORT_OK = qw($Var1 %Hashit);
  17. # non-exported package globals go here
  18. use vars qw(@more $stuff);
  19. # initalize package globals, first exported ones
  20. my $Var1 = '';
  21. my %Hashit = ();
  22. # then the others (which are still accessible as $Some::Module::stuff)
  23. my $stuff = '';
  24. my @more = ();
  25. # all file-scoped lexicals must be created before
  26. # the functions below that use them.
  27. # file-private lexicals go here
  28. my $priv_var = '';
  29. my %secret_hash = ();
  30. # here's a file-private function as a closure,
  31. # callable as &$priv_func; it cannot be prototyped.
  32. my $priv_func = sub {
  33. # stuff goes here.
  34. };
  35. # make all your functions, whether exported or not;
  36. sub KeywordSearch {
  37. my ($env,$type,$search,$num,$offset)=@_;
  38. my $dbh = &C4Connect;
  39. my $query="(Select * from biblio,catalogueentry
  40. where (catalogueentry.catalogueentry=biblio.author and
  41. catalogueentry.entrytype='a' and catalogueentry.catalogueentry like
  42. '$search->{'keyword'}%') union select * from biblio,catalogueentry where
  43. (catalogueentry.catalogueentry=biblio.title and
  44. catalogueentry.entrytype='t' and catalogueentry.catalogueentry like
  45. '%$search->{'keyword'}%')) order by biblio.title";
  46. my $sth=$dbh->prepare($query);
  47. # print $query;
  48. $sth->execute;
  49. my $i=0;
  50. my $i2=0;
  51. my $limit=$num+$offset;
  52. my $count=0;
  53. my @results;
  54. while (my $data=$sth->fetchrow_hashref){
  55. $count++;
  56. }
  57. $sth->finish;
  58. $query=$query." limit $num,$offset";
  59. $sth=$dbh->prepare($query);
  60. $sth->execute;
  61. # print $query;
  62. while ((my $data=$sth->fetchrow_hashref) && $i < $limit){
  63. if ($i >= $offset){
  64. $results[$i2]="$data->{'biblionumber'}\t$data->{'title'}\t$data->{'author'}";
  65. $i2++;
  66. }
  67. $i++;
  68. }
  69. $sth->finish;
  70. $dbh->disconnect;
  71. return($count,@results);
  72. }
  73. sub CatSearch {
  74. my ($env,$type,$search,$num,$offset)=@_;
  75. my $dbh = &C4Connect;
  76. my $query = '';
  77. if ($type eq 'loose') {
  78. $query="Select count(*) from biblio,catalogueentry";
  79. if ($search->{'author'} ne ''){
  80. $query=$query." where ((catalogueentry.catalogueentry = biblio.author)
  81. and (catalogueentry.catalogueentry like '$search->{'author'}%')
  82. and (entrytype = 'a'))";
  83. if ($search->{'title'} ne ''){
  84. $query= "Select biblionumber,title from biblio,catalogueentry where ((catalogueentry.catalogueentry = biblio.author)
  85. and (catalogueentry.catalogueentry like '$search->{'author'}%')
  86. and (entrytype = 'a')) intersect select biblionumber,title from
  87. biblio,catalogueentry where ((catalogueentry.catalogueentry = biblio.title)
  88. and (catalogueentry.catalogueentry like '%$search->{'title'}%')
  89. and (entrytype = 't'))";
  90. }
  91. } else {
  92. if ($search->{'title'} ne ''){
  93. $query=$query." where ((catalogueentry.catalogueentry = biblio.title)
  94. and (catalogueentry.catalogueentry like '%$search->{'title'}%')
  95. and (entrytype = 't'))";
  96. }
  97. }
  98. }
  99. if ($type eq 'subject'){
  100. $query="Select distinct(subject) from catalogueentry,bibliosubject";
  101. if ($search->{'subject'} ne ''){
  102. if ($query =~ /where/){
  103. $query=$query." and ";
  104. } else {
  105. $query=$query." where ";
  106. }
  107. $search->{'subject'}=uc $search->{'subject'};
  108. $query=$query." ((catalogueentry.catalogueentry = bibliosubject.subject)
  109. and (catalogueentry.catalogueentry like '$search->{'subject'}%')
  110. and (entrytype = 's'))";
  111. }
  112. }
  113. if ($type eq 'precise'){
  114. $query="select count(*) from items,biblio ";
  115. if ($search->{'item'} ne ''){
  116. my $search2=uc $search->{'item'};
  117. $query=$query." where barcode='$search2' and
  118. items.biblionumber=biblio.biblionumber ";
  119. }
  120. if ($search->{'isbn'} ne ''){
  121. $query="select count(*) from items,biblio,biblioitems ";
  122. my $search2=uc $search->{'isbn'};
  123. $query=$query." where biblioitems.isbn='$search2' and
  124. items.biblioitemnumber=biblioitems.biblioitemnumber
  125. and biblioitems.biblionumber=biblio.biblionumber";
  126. }
  127. }
  128. #print $query;
  129. my $sth=$dbh->prepare($query);
  130. $sth->execute;
  131. my $count2=0;
  132. if ($type ne 'subject'){
  133. my $count=$sth->fetchrow_hashref;
  134. $count2=$count->{'count'};
  135. } else {
  136. while (my $count = $sth->fetchrow_hashref){
  137. $count2++;
  138. }
  139. }
  140. $sth->finish;
  141. $query=~ s/count\(\*\)/\*/g;
  142. if ($type ne 'precise' && $type ne 'subject'){
  143. $query=$query." order by catalogueentry.catalogueentry limit $num,$offset";
  144. } else {
  145. if ($type eq 'subject'){
  146. $query=$query." order by subject limit $num,$offset";
  147. }
  148. }
  149. $sth=$dbh->prepare($query);
  150. $sth->execute;
  151. my $i=0;
  152. my @results;
  153. while (my $data=$sth->fetchrow_hashref){
  154. if ($type ne 'subject'){
  155. $results[$i]="$data->{'biblionumber'}\t$data->{'title'}\t
  156. $data->{'author'}";
  157. } else {
  158. $results[$i]="$data->{'biblionumber'}\t$data->{'subject'}\t
  159. $data->{'author'}";
  160. }
  161. $i++;
  162. }
  163. $sth->finish;
  164. # print "$query\n";
  165. #only update stats if search is from opac
  166. # updatesearchstats($dbh,$query);
  167. $dbh->disconnect;
  168. return($count2,@results);
  169. }
  170. sub updatesearchstats{
  171. my ($dbh,$query)=@_;
  172. }
  173. sub subsearch {
  174. my ($env,$subject)=@_;
  175. my $dbh=C4Connect();
  176. my $query="Select * from bibliosubject,biblio where subject='$subject'
  177. and bibliosubject.biblionumber=biblio.biblionumber";
  178. my $sth=$dbh->prepare($query);
  179. $sth->execute;
  180. my $i=0;
  181. my @results;
  182. while (my $data=$sth->fetchrow_hashref){
  183. $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
  184. $i++;
  185. }
  186. $sth->finish;
  187. $dbh->disconnect;
  188. return(@results);
  189. }
  190. sub ItemInfo {
  191. my ($env,$biblionumber)=@_;
  192. my $dbh = &C4Connect;
  193. my $query="Select * from items,biblio,biblioitems,branches
  194. where (items.biblioitemnumber = biblioitems.biblioitemnumber)
  195. and biblioitems.biblionumber=biblio.biblionumber
  196. and biblio.biblionumber='$biblionumber' and branches.branchcode=
  197. items.holdingbranch";
  198. my $sth=$dbh->prepare($query);
  199. $sth->execute;
  200. my $i=0;
  201. my @results;
  202. while (my $data=$sth->fetchrow_hashref){
  203. my $iquery = "Select * from issues
  204. where itemnumber = '$data->{'itemnumber'}'
  205. and returndate is null";
  206. my $datedue = '';
  207. my $isth=$dbh->prepare($iquery);
  208. $isth->execute;
  209. if (my $idata=$isth->fetchrow_hashref){
  210. $datedue = $idata->{'date_due'};
  211. }
  212. $isth->finish;
  213. $results[$i]="$data->{'title'}\t$data->{'itemnumber'}\t$datedue\t$data->{'branchname'}";
  214. $i++;
  215. }
  216. $sth->finish;
  217. $dbh->disconnect;
  218. return(@results);
  219. }
  220. sub BornameSearch {
  221. my ($env,$searchstring,$type)=@_;
  222. my $dbh = &C4Connect;
  223. my $query="Select * from borrowers where surname like
  224. '%$searchstring%' or firstname like '%$searchstring%' or othernames like
  225. '%$searchstring'";
  226. print $query,"\n";
  227. my $sth=$dbh->prepare($query);
  228. $sth->execute;
  229. while (my @data=$sth->fetchrow_array){
  230. print "$data[0] $data[2]\n";
  231. }
  232. $sth->execute;
  233. $sth->finish;
  234. $dbh->disconnect;
  235. }
  236. END { } # module clean-up code here (global destructor)