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.

637 lines
18 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 C4::InterfaceCDK;
  9. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  10. # set the version for version checking
  11. $VERSION = 0.01;
  12. @ISA = qw(Exporter);
  13. @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
  14. &itemdata &bibdata &GetItems &borrdata &getacctlist &itemnodata &itemcount
  15. &OpacSearch &borrdata2 &NewBorrowerNumber &bibitemdata);
  16. %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
  17. # your exported package globals go here,
  18. # as well as any optionally exported functions
  19. @EXPORT_OK = qw($Var1 %Hashit);
  20. # non-exported package globals go here
  21. use vars qw(@more $stuff);
  22. # initalize package globals, first exported ones
  23. my $Var1 = '';
  24. my %Hashit = ();
  25. # then the others (which are still accessible as $Some::Module::stuff)
  26. my $stuff = '';
  27. my @more = ();
  28. # all file-scoped lexicals must be created before
  29. # the functions below that use them.
  30. # file-private lexicals go here
  31. my $priv_var = '';
  32. my %secret_hash = ();
  33. # here's a file-private function as a closure,
  34. # callable as &$priv_func; it cannot be prototyped.
  35. my $priv_func = sub {
  36. # stuff goes here.
  37. };
  38. # make all your functions, whether exported or not;
  39. sub NewBorrowerNumber {
  40. my $dbh=C4Connect;
  41. my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
  42. $sth->execute;
  43. my $data=$sth->fetchrow_hashref;
  44. $sth->finish;
  45. $data->{'max(borrowernumber)'}++;
  46. return($data->{'max(borrowernumber)'});
  47. }
  48. sub OpacSearch {
  49. my ($env,$type,$search,$num,$offset)=@_;
  50. my $dbh = &C4Connect;
  51. my @key=split(' ',$search->{'keyword'});
  52. my $count=@key;
  53. my $i=1;
  54. my @results;
  55. my $query ="Select count(*) from biblio where
  56. (title like '%$key[0]%'";
  57. while ($i < $count){
  58. $query=$query." and title like '%$key[$i]%'";
  59. $i++;
  60. }
  61. $query=$query.") or (author like '%$key[0]%'";
  62. $i=1;
  63. while ($i < $count){
  64. $query=$query." and author like '%$key[$i]%'";
  65. $i++;
  66. }
  67. $query=$query.") order by title";
  68. my $sth=$dbh->prepare($query);
  69. $sth->execute;
  70. my $data=$sth->fetchrow_hashref;
  71. my $count=$data->{'count(*)'};
  72. $sth->finish;
  73. $query=~ s/count\(\*\)/\*/;
  74. $query= $query." limit $offset,$num";
  75. $sth=$dbh->prepare($query);
  76. # print $query;
  77. $sth->execute;
  78. $i=0;
  79. while (my $data=$sth->fetchrow_hashref){
  80. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}";
  81. $i++;
  82. }
  83. $sth->finish;
  84. $dbh->disconnect;
  85. return($count,@results);
  86. }
  87. sub KeywordSearch {
  88. my ($env,$type,$search,$num,$offset)=@_;
  89. my $dbh = &C4Connect;
  90. $search->{'keyword'}=~ s/ +$//;
  91. my @key=split(' ',$search->{'keyword'});
  92. my $count=@key;
  93. my $i=1;
  94. my @results;
  95. my $query ="Select * from biblio where
  96. (title like '%$key[0]%'";
  97. while ($i < $count){
  98. $query=$query." and title like '%$key[$i]%'";
  99. $i++;
  100. }
  101. $query=$query.") order by author,title";
  102. my $sth=$dbh->prepare($query);
  103. $sth->execute;
  104. $i=0;
  105. while (my $data=$sth->fetchrow_hashref){
  106. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  107. # print $results[$i];
  108. $i++;
  109. }
  110. $sth->finish;
  111. $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
  112. like '%$search->{'keyword'}%'");
  113. $sth->execute;
  114. while (my $data=$sth->fetchrow_hashref){
  115. my $sth2=$dbh->prepare("Select * from biblio where
  116. biblionumber=$data->{'biblionumber'}");
  117. $sth2->execute;
  118. while (my $data2=$sth2->fetchrow_hashref){
  119. $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data->{'copyrightdate'}";
  120. # print $results[$i];
  121. $i++;
  122. }
  123. $sth2->finish;
  124. }
  125. my $i2=1;
  126. @results=sort @results;
  127. my @res;
  128. my $count=@results;
  129. $i=1;
  130. $res[0]=$results[0];
  131. while ($i2 < $count){
  132. if ($results[$i2] ne $res[$i-1]){
  133. $res[$i]=$results[$i2];
  134. $i++;
  135. }
  136. $i2++;
  137. }
  138. $i2=0;
  139. my @res2;
  140. $count=@res;
  141. while ($i2 < $num && $i2 < $count){
  142. $res2[$i2]=$res[$i2+$offset];
  143. # print $res2[$i2];
  144. $i2++;
  145. }
  146. $sth->finish;
  147. $dbh->disconnect;
  148. return($i,@res2);
  149. }
  150. sub CatSearch {
  151. my ($env,$type,$search,$num,$offset)=@_;
  152. my $dbh = &C4Connect;
  153. my $query = '';
  154. my @results;
  155. my $title = lc($search->{'title'});
  156. if ($type eq 'loose') {
  157. if ($search->{'author'} ne ''){
  158. my @key=split(' ',$search->{'author'});
  159. my $count=@key;
  160. my $i=1;
  161. $query="select count(*) from
  162. biblio,biblioitems
  163. where biblioitems.biblionumber=biblio.biblionumber and (biblio.author like '%$key[0]%'";
  164. while ($i < $count){
  165. $query=$query." and author like '%$key[$i]%'";
  166. $i++;
  167. }
  168. $query=$query.")";
  169. if ($search->{'title'} ne ''){
  170. $query=$query. " and title like '%$search->{'title'}%'";
  171. }
  172. if ($search->{'class'} ne ''){
  173. $query.=" and biblioitems.itemtype='$search->{'class'}'";
  174. }
  175. } else {
  176. if ($search->{'title'} ne ''){
  177. my @key=split(' ',$search->{'title'});
  178. my $count=@key;
  179. my $i=1;
  180. $query="select count(*) from biblio,bibliosubtitle
  181. where
  182. (biblio.biblionumber=bibliosubtitle.biblionumber) and
  183. ((title like '%$key[0]%' or subtitle like '%$key[0]%')";
  184. while ($i<$count){
  185. $query=$query." and (title like '%$key[$i]%' or subtitle like '%$key[$i]%')";
  186. $i++;
  187. }
  188. $query=$query.")";
  189. # if ($search->{'class'} ne ''){
  190. # $query.=" and biblioitems.itemtype='$search->{'class'}'";
  191. # }
  192. } elsif ($search->{'class'} ne ''){
  193. $query="select count(*) from biblioitems,biblio where itemtype =
  194. '$search->{'class'}' and biblio.biblionumber=biblioitems.biblionumber";
  195. }
  196. }
  197. }
  198. if ($type eq 'subject'){
  199. $query="select distinct(subject) from bibliosubject where subject like
  200. '$search->{'subject'}%'";
  201. }
  202. if ($type eq 'precise'){
  203. $query="select count(*) from items,biblio ";
  204. if ($search->{'item'} ne ''){
  205. my $search2=uc $search->{'item'};
  206. $query=$query." where barcode='$search2' and
  207. items.biblionumber=biblio.biblionumber ";
  208. }
  209. if ($search->{'isbn'} ne ''){
  210. my $search2=uc $search->{'isbn'};
  211. my $query1 = "select * from biblioitems where isbn='$search2'";
  212. my $sth1=$dbh->prepare($query1);
  213. $sth1->execute;
  214. my $i2=0;
  215. while (my $data=$sth1->fetchrow_hashref) {
  216. $query="select * from biblioitems,items,biblio where
  217. biblioitems.biblioitemnumber = '$data->{'biblioitemnumber'}'
  218. and biblioitems.biblionumber =
  219. biblio.biblionumber and items.biblioitemnumber =
  220. biblioitems.biblioitemnumber";
  221. my $sth=$dbh->prepare($query);
  222. $sth->execute;
  223. my $data=$sth->fetchrow_hashref;
  224. $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}";
  225. $i2++;
  226. $sth->finish;
  227. }
  228. $sth1->finish;
  229. }
  230. }
  231. #print $query;
  232. my $sth=$dbh->prepare($query);
  233. $sth->execute;
  234. my $count=0;
  235. if ($type eq 'subject'){
  236. while (my $data=$sth->fetchrow_arrayref){
  237. $count++;
  238. }
  239. } else {
  240. my $data=$sth->fetchrow_arrayref;
  241. $count=$data->[0];
  242. }
  243. $sth->finish;
  244. $query=~ s/count\(\*\)/\*/g;
  245. if ($type ne 'precise' && $type ne 'subject'){
  246. if ($search->{'author'} ne ''){
  247. $query=$query." order by author,title limit $offset,$num";
  248. } else {
  249. $query=$query." order by title limit $offset,$num";
  250. }
  251. } else {
  252. if ($type eq 'subject'){
  253. $query=$query." order by subject limit $offset,$num";
  254. }
  255. }
  256. $sth=$dbh->prepare($query);
  257. $sth->execute;
  258. my $i=0;
  259. my $i2=0;
  260. my $limit=$num+$offset;
  261. # if ($search->{'title'} ne '' || $search->{'author'} ne '' ){
  262. # while ((my $data=$sth->fetchrow_hashref) && $i < $limit){
  263. # if ($i >= $offset){
  264. #
  265. # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}";
  266. # $i2++;
  267. # }
  268. # $i++;
  269. # }
  270. # } else {
  271. while (my $data=$sth->fetchrow_hashref){
  272. if ($type ne 'subject'){
  273. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  274. } elsif ($search->{'isbn'} ne ''){
  275. } else {
  276. $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  277. }
  278. $i++;
  279. }
  280. # }
  281. $sth->finish;
  282. # print "$query\n";
  283. #only update stats if search is from opac
  284. # updatesearchstats($dbh,$query);
  285. $dbh->disconnect;
  286. if ($search->{'isbn'} ne ''){
  287. $count=1;
  288. }
  289. return($count,@results);
  290. }
  291. sub updatesearchstats{
  292. my ($dbh,$query)=@_;
  293. }
  294. sub subsearch {
  295. my ($env,$subject)=@_;
  296. my $dbh=C4Connect();
  297. my $query="Select * from biblio,bibliosubject where
  298. biblio.biblionumber=bibliosubject.biblionumber and
  299. bibliosubject.subject='$subject'";
  300. my $sth=$dbh->prepare($query);
  301. $sth->execute;
  302. my $i=0;
  303. # print $query;
  304. my @results;
  305. while (my $data=$sth->fetchrow_hashref){
  306. $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
  307. $i++;
  308. }
  309. $sth->finish;
  310. $dbh->disconnect;
  311. return(@results);
  312. }
  313. sub ItemInfo {
  314. my ($env,$biblionumber)=@_;
  315. my $dbh = &C4Connect;
  316. my $query="Select * from items,biblio,biblioitems,branches
  317. where (items.biblioitemnumber = biblioitems.biblioitemnumber)
  318. and biblioitems.biblionumber=biblio.biblionumber
  319. and biblio.biblionumber='$biblionumber' and branches.branchcode=
  320. items.holdingbranch";
  321. my $sth=$dbh->prepare($query);
  322. $sth->execute;
  323. my $i=0;
  324. my @results;
  325. while (my $data=$sth->fetchrow_hashref){
  326. my $iquery = "Select * from issues
  327. where itemnumber = '$data->{'itemnumber'}'
  328. and returndate is null";
  329. my $datedue = '';
  330. my $isth=$dbh->prepare($iquery);
  331. $isth->execute;
  332. if (my $idata=$isth->fetchrow_hashref){
  333. $datedue = $idata->{'date_due'};
  334. }
  335. $isth->finish;
  336. my $class = $data->{'classification'};
  337. my $dewey = $data->{'dewey'};
  338. $dewey =~ s/0+$//;
  339. if ($dewey < 10){$dewey='00'.$dewey;}
  340. if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
  341. if ($dewey eq "000.") { $dewey = "";};
  342. $class = $class.$dewey;
  343. $class = $class.$data->{'subclass'};
  344. # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
  345. my @temp=split('-',$data->{'datelastseen'});
  346. my $date="$temp[2]/$temp[1]/$temp[0]";
  347. $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$class\t$data->{'itemnumber'}\t$data->{'itemtype'}\t$date\t$data->{'biblioitemnumber'}";
  348. $i++;
  349. }
  350. $sth->finish;
  351. $dbh->disconnect;
  352. return(@results);
  353. }
  354. sub GetItems {
  355. my ($env,$biblionumber)=@_;
  356. #debug_msg($env,"GetItems");
  357. my $dbh = &C4Connect;
  358. my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
  359. #debug_msg($env,$query);
  360. my $sth=$dbh->prepare($query);
  361. $sth->execute;
  362. #debug_msg($env,"executed query");
  363. my $i=0;
  364. my @results;
  365. while (my $data=$sth->fetchrow_hashref) {
  366. #debug_msg($env,$data->{'biblioitemnumber'});
  367. my $dewey = $data->{'dewey'};
  368. $dewey =~ s/0+$//;
  369. my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
  370. $line = $line."\t$data->{'classification'}\t$dewey";
  371. $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
  372. $line = $line."\t$data->{'volume'}\t$data->{number}";
  373. my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
  374. $isth->execute;
  375. while (my $idata = $isth->fetchrow_hashref) {
  376. my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
  377. if ($idata->{'notforloan'} == 1) {
  378. $iline = $iline."NFL ";
  379. }
  380. if ($idata->{'itemlost'} == 1) {
  381. $iline = $iline."LOST ";
  382. }
  383. $line = $line."\t$iline";
  384. }
  385. $isth->finish;
  386. $results[$i] = $line;
  387. $i++;
  388. }
  389. $sth->finish;
  390. $dbh->disconnect;
  391. return(@results);
  392. }
  393. sub itemdata {
  394. my ($barcode)=@_;
  395. my $dbh=C4Connect;
  396. my $query="Select * from items,biblioitems where barcode='$barcode'
  397. and items.biblioitemnumber=biblioitems.biblioitemnumber";
  398. # print $query;
  399. my $sth=$dbh->prepare($query);
  400. $sth->execute;
  401. my $data=$sth->fetchrow_hashref;
  402. $sth->finish;
  403. $dbh->disconnect;
  404. return($data);
  405. }
  406. sub bibdata {
  407. my ($bibnum)=@_;
  408. my $dbh=C4Connect;
  409. my $query="Select * from biblio,biblioitems,bibliosubject,bibliosubtitle where biblio.biblionumber=$bibnum
  410. and biblioitems.biblionumber=$bibnum and
  411. (bibliosubject.biblionumber=$bibnum or bibliosubject.biblionumber=1) and
  412. (bibliosubtitle.biblionumber=$bibnum or bibliosubtitle.biblionumber=1)";
  413. # print $query;
  414. my $sth=$dbh->prepare($query);
  415. $sth->execute;
  416. my $data=$sth->fetchrow_hashref;
  417. $sth->finish;
  418. $dbh->disconnect;
  419. return($data);
  420. }
  421. sub bibitemdata {
  422. my ($bibitem)=@_;
  423. my $dbh=C4Connect;
  424. my $query="Select * from biblio,biblioitems where biblio.biblionumber=
  425. biblioitems.biblionumber and biblioitemnumber=$bibitem";
  426. # print $query;
  427. my $sth=$dbh->prepare($query);
  428. $sth->execute;
  429. my $data=$sth->fetchrow_hashref;
  430. $sth->finish;
  431. $dbh->disconnect;
  432. return($data);
  433. }
  434. sub itemnodata {
  435. my ($env,$dbh,$itemnumber) = @_;
  436. my $query="Select * from biblio,items,biblioitems
  437. where items.itemnumber = '$itemnumber'
  438. and biblio.biblionumber = items.biblionumber
  439. and biblioitems.biblioitemnumber = items.biblioitemnumber";
  440. my $sth=$dbh->prepare($query);
  441. $sth->execute;
  442. my $data=$sth->fetchrow_hashref;
  443. $sth->finish;
  444. return($data);
  445. }
  446. #used by member enquiries from the intranet
  447. #called by member.pl
  448. sub BornameSearch {
  449. my ($env,$searchstring,$type)=@_;
  450. my $dbh = &C4Connect;
  451. my @data=split(' ',$searchstring);
  452. my $count=@data;
  453. my $query="Select * from borrowers
  454. where ((surname like '%$data[0]%'
  455. or firstname like '%$data[0]%'
  456. or othernames like '%$data[0]%')
  457. ";
  458. for (my $i=1;$i<$count;$i++){
  459. $query=$query." and (surname like '%$data[$i]%'
  460. or firstname like '%$data[$i]%'
  461. or othernames like '%$data[$i]%')";
  462. }
  463. $query=$query.") or cardnumber = '$searchstring'
  464. order by surname,firstname";
  465. print $query,"\n";
  466. my $sth=$dbh->prepare($query);
  467. $sth->execute;
  468. my @results;
  469. my $cnt=0;
  470. while (my $data=$sth->fetchrow_hashref){
  471. push(@results,$data);
  472. $cnt ++;
  473. }
  474. # $sth->execute;
  475. $sth->finish;
  476. $dbh->disconnect;
  477. return ($cnt,\@results);
  478. }
  479. sub borrdata {
  480. my ($cardnumber,$bornum)=@_;
  481. $cardnumber = uc $cardnumber;
  482. my $dbh=C4Connect;
  483. my $query;
  484. if ($bornum eq ''){
  485. $query="Select * from borrowers where cardnumber='$cardnumber'";
  486. } else {
  487. $query="Select * from borrowers where borrowernumber='$bornum'";
  488. }
  489. #print $query;
  490. my $sth=$dbh->prepare($query);
  491. $sth->execute;
  492. my $data=$sth->fetchrow_hashref;
  493. $sth->finish;
  494. $dbh->disconnect;
  495. return($data);
  496. }
  497. sub borrdata2 {
  498. my ($env,$bornum)=@_;
  499. my $dbh=C4Connect;
  500. my $query="Select count(*) from issues where borrowernumber='$bornum' and
  501. returndate is NULL";
  502. # print $query;
  503. my $sth=$dbh->prepare($query);
  504. $sth->execute;
  505. my $data=$sth->fetchrow_hashref;
  506. $sth->finish;
  507. $sth=$dbh->prepare("Select count(*) from issues where
  508. borrowernumber='$bornum' and date_due < now() and returndate is NULL");
  509. $sth->execute;
  510. my $data2=$sth->fetchrow_hashref;
  511. $sth->finish;
  512. $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
  513. borrowernumber='$bornum'");
  514. $sth->execute;
  515. my $data3=$sth->fetchrow_hashref;
  516. $sth->finish;
  517. $dbh->disconnect;
  518. return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
  519. }
  520. sub getacctlist {
  521. my ($env,$params) = @_;
  522. my $dbh=C4Connect;
  523. my @acctlines;
  524. my $numlines;
  525. my $query = "Select borrowernumber, accountno, date, amount, description,
  526. dispute, accounttype, amountoutstanding, barcode, title
  527. from accountlines,items,biblio
  528. where borrowernumber = $params->{'borrowernumber'} ";
  529. if ($params->{'acctno'} ne "") {
  530. my $query = $query." and accountlines.accountno = $params->{'acctno'} ";
  531. }
  532. my $query = $query." and accountlines.itemnumber = items.itemnumber
  533. and items.biblionumber = biblio.biblionumber
  534. and accountlines.amountoutstanding<>0 order by date";
  535. my $sth=$dbh->prepare($query);
  536. $sth->execute;
  537. my $total=0;
  538. while (my $data=$sth->fetchrow_hashref){
  539. $acctlines[$numlines] = $data;
  540. $numlines++;
  541. $total = $total+ $data->{'amountoutstanding'};
  542. }
  543. return ($numlines,\@acctlines,$total);
  544. }
  545. sub itemcount {
  546. my ($env,$bibnum)=@_;
  547. my $dbh=C4Connect;
  548. my $query="Select * from items where
  549. biblionumber=$bibnum";
  550. my $sth=$dbh->prepare($query);
  551. # print $query;
  552. $sth->execute;
  553. my $count=0;
  554. my $lcount=0;
  555. my $nacount=0;
  556. my $fcount=0;
  557. my $scount=0;
  558. while (my $data=$sth->fetchrow_hashref){
  559. $count++;
  560. my $query2="select * from issues where itemnumber=
  561. '$data->{'itemnumber'}' and returndate is NULL";
  562. my $sth2=$dbh->prepare($query2);
  563. $sth2->execute;
  564. if (my $data2=$sth2->fetchrow_hashref){
  565. $nacount++;
  566. } else {
  567. if ($data->{'holdingbranch'} eq 'C'){
  568. $lcount++;
  569. }
  570. if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
  571. $fcount++;
  572. }
  573. if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
  574. $scount++;
  575. }
  576. }
  577. $sth2->finish;
  578. }
  579. $sth->finish;
  580. $dbh->disconnect;
  581. return ($count,$lcount,$nacount,$fcount,$scount);
  582. }
  583. END { } # module clean-up code here (global destructor)