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.

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