Added PODs.
[koha.git] / C4 / Catalogue.pm
1 package C4::Catalogue; #asummes C4/Acquisitions.pm
2
3 # Continue working on updateItem!!!!!!
4 #
5 # updateItem is looking not bad.  Need to add addSubfield and deleteSubfield
6 # functions
7 #
8 # Trying to track down $dbh's that aren't disconnected....
9 #
10
11
12
13 # Copyright 2000-2002 Katipo Communications
14 #
15 # This file is part of Koha.
16 #
17 # Koha is free software; you can redistribute it and/or modify it under the
18 # terms of the GNU General Public License as published by the Free Software
19 # Foundation; either version 2 of the License, or (at your option) any later
20 # version.
21 #
22 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License along with
27 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
28 # Suite 330, Boston, MA  02111-1307 USA
29
30 use strict;
31 require Exporter;
32 use C4::Database;
33 use MARC::Record;
34 use C4::Biblio;
35
36 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37
38 # set the version for version checking
39 $VERSION = 0.01;
40
41 @ISA = qw(Exporter);
42 @EXPORT = qw(
43              &basket &newbasket
44
45              &getorders &getallorders &getrecorders 
46              &getorder &neworder &delorder
47              &ordersearch
48              &modorder &getsingleorder &invoice &receiveorder
49              &updaterecorder &newordernum 
50
51              &bookfunds &bookfundbreakdown &updatecost 
52              &curconvert &getcurrencies &updatecurrencies &getcurrency 
53
54              &findall &needsmod &branches &updatesup &insertsup
55              &bookseller &breakdown &checkitems
56              &websitesearch &addwebsite &updatewebsite &deletewebsite
57 );
58 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
59
60 # your exported package globals go here,
61 # as well as any optionally exported functions
62
63 @EXPORT_OK   = qw($Var1 %Hashit);
64
65
66 # non-exported package globals go here
67 use vars qw(@more $stuff);
68
69 # initalize package globals, first exported ones
70
71 my $Var1   = '';
72 my %Hashit = ();
73
74
75 # then the others (which are still accessible as $Some::Module::stuff)
76 my $stuff  = '';
77 my @more   = ();
78
79 # all file-scoped lexicals must be created before
80 # the functions below that use them.
81
82 # file-private lexicals go here
83 my $priv_var    = '';
84 my %secret_hash = ();
85
86 # here's a file-private function as a closure,
87 # callable as &$priv_func;  it cannot be prototyped.
88 my $priv_func = sub {
89   # stuff goes here.
90   };
91   
92 # make all your functions, whether exported or not;
93
94
95 #
96 #
97 #
98 # BASKETS
99 #
100 #
101 #
102 sub basket {
103   my ($basketno,$supplier)=@_;
104   my $dbh=C4Connect;
105   my $query="Select *,biblio.title from aqorders,biblio,biblioitems 
106   where basketno='$basketno'
107   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
108   =aqorders.biblioitemnumber 
109   and (datecancellationprinted is NULL or datecancellationprinted =
110   '0000-00-00')";
111   if ($supplier ne ''){
112     $query.=" and aqorders.booksellerid='$supplier'";
113   }
114   $query.=" group by aqorders.ordernumber";
115   my $sth=$dbh->prepare($query);
116   $sth->execute;
117   my @results;
118 #  print $query;
119   my $i=0;
120   while (my $data=$sth->fetchrow_hashref){
121     $results[$i]=$data;
122     $i++;
123   }
124   $sth->finish;
125   $dbh->disconnect;
126   return($i,@results);
127 }
128
129 sub newbasket {
130   my $dbh=C4Connect;
131   my $query="Select max(basketno) from aqorders";
132   my $sth=$dbh->prepare($query);
133   $sth->execute;
134   my $data=$sth->fetchrow_arrayref;
135   my $basket=$$data[0];
136   $basket++;
137   $sth->finish;
138   $dbh->disconnect;
139   return($basket);
140 }
141 sub neworder {
142   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
143   if ($budget eq 'now'){
144     $budget="now()";
145   } else {
146     $budget="'2001-07-01'";
147   }
148   if ($sub eq 'yes'){
149     $sub=1;
150   } else {
151     $sub=0;
152   }
153   my $dbh=C4Connect;
154   my $query="insert into aqorders (biblionumber,title,basketno,
155   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
156   biblioitemnumber,rrp,ecost,gst,unitprice,subscription,booksellerinvoicenumber)
157
158   values
159   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
160   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst','$cost',
161   '$sub','$invoice')";
162   my $sth=$dbh->prepare($query);
163 #  print $query;
164   $sth->execute;
165   $sth->finish;
166   $query="select * from aqorders where
167   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
168   $sth=$dbh->prepare($query);
169   $sth->execute;
170   my $data=$sth->fetchrow_hashref;
171   $sth->finish;
172   $ordnum=$data->{'ordernumber'};
173   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
174   ($ordnum,'$bookfund')";
175   $sth=$dbh->prepare($query);
176 #  print $query;
177   $sth->execute;
178   $sth->finish;
179   $dbh->disconnect;
180 }
181
182 sub delorder {
183   my ($bibnum,$ordnum)=@_;
184   my $dbh=C4Connect;
185   my $query="update aqorders set datecancellationprinted=now()
186   where biblionumber='$bibnum' and
187   ordernumber='$ordnum'";
188   my $sth=$dbh->prepare($query);
189   #print $query;
190   $sth->execute;
191   $sth->finish;
192   my $count=itemcount($bibnum);
193   if ($count == 0){
194     delbiblio($bibnum);
195   }
196   $dbh->disconnect;
197 }
198
199 sub modorder {
200   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
201   my $dbh=C4Connect;
202   my $query="update aqorders set title='$title',
203   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
204   rrp='$rrp',ecost='$ecost',unitprice='$cost',
205   booksellerinvoicenumber='$invoice'
206   where
207   ordernumber=$ordnum and biblionumber=$bibnum";
208   my $sth=$dbh->prepare($query);
209 #  print $query;
210   $sth->execute;
211   $sth->finish;
212   $query="update aqorderbreakdown set bookfundid=$bookfund where
213   ordernumber=$ordnum";
214   $sth=$dbh->prepare($query);
215 #  print $query;
216   $sth->execute;
217   $sth->finish;
218   $dbh->disconnect;
219 }
220
221 sub newordernum {
222   my $dbh=C4Connect;
223   my $query="Select max(ordernumber) from aqorders";
224   my $sth=$dbh->prepare($query);
225   $sth->execute;
226   my $data=$sth->fetchrow_arrayref;
227   my $ordnum=$$data[0];
228   $ordnum++;
229   $sth->finish;
230   $dbh->disconnect;
231   return($ordnum);
232 }
233
234 sub receiveorder {
235   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
236   my $dbh=C4Connect;
237   my $query="update aqorders set quantityreceived='$quantrec',
238   datereceived=now(),booksellerinvoicenumber='$invoiceno',
239   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
240   rrp='$rrp'
241   where biblionumber=$biblio and ordernumber=$ordnum
242   ";
243 #  print $query;
244   my $sth=$dbh->prepare($query);
245   $sth->execute;
246   $sth->finish;
247   $query="update aqorderbreakdown set bookfundid=$bookfund where
248   ordernumber=$ordnum";
249   $sth=$dbh->prepare($query);
250 #  print $query;
251   $sth->execute;
252   $sth->finish;  
253   $dbh->disconnect;
254 }
255 sub updaterecorder{
256   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
257   my $dbh=C4Connect;
258   my $query="update aqorders set
259   unitprice='$cost', rrp='$rrp'
260   where biblionumber=$biblio and ordernumber=$ordnum
261   ";
262 #  print $query;
263   my $sth=$dbh->prepare($query);
264   $sth->execute;
265   $sth->finish;
266   $query="update aqorderbreakdown set bookfundid=$bookfund where
267   ordernumber=$ordnum";
268   $sth=$dbh->prepare($query);
269 #  print $query;
270   $sth->execute;
271   $sth->finish;  
272   $dbh->disconnect;
273 }
274
275 #
276 #
277 # ORDERS
278 #
279 #
280
281 sub getorders {
282   my ($supplierid)=@_;
283   my $dbh=C4Connect;
284   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where 
285   booksellerid='$supplierid' and (quantity > quantityreceived or
286   quantityreceived is NULL)
287   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
288   $query.=" group by basketno order by entrydate desc";
289   #print $query;
290   my $sth=$dbh->prepare($query);
291   $sth->execute;
292   my @results;
293   my $i=0;
294   while (my $data=$sth->fetchrow_hashref){
295     $results[$i]=$data;
296     $i++;
297   }
298   $sth->finish;
299   $dbh->disconnect;
300   return ($i,\@results);
301 }
302
303 sub getorder{
304   my ($bi,$bib)=@_;
305   my $dbh=C4Connect;
306   my $query="Select ordernumber from aqorders where biblionumber=$bib and
307   biblioitemnumber='$bi'";
308   my $sth=$dbh->prepare($query);
309   $sth->execute;
310   my $ordnum=$sth->fetchrow_hashref;
311   $sth->finish;
312   my $order=getsingleorder($ordnum->{'ordernumber'});
313   $dbh->disconnect;
314 #  print $query;
315   return ($order,$ordnum->{'ordernumber'});
316 }
317
318 sub getsingleorder {
319   my ($ordnum)=@_;
320   my $dbh=C4Connect;
321   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
322   where aqorders.ordernumber='$ordnum' 
323   and biblio.biblionumber=aqorders.biblionumber and
324   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
325   aqorders.ordernumber=aqorderbreakdown.ordernumber";
326   my $sth=$dbh->prepare($query);
327   $sth->execute;
328   my $data=$sth->fetchrow_hashref;
329   $sth->finish;
330   $dbh->disconnect;
331   return($data);
332 }
333
334 sub getallorders {
335   #gets all orders from a certain supplier, orders them alphabetically
336   my ($supid)=@_;
337   my $dbh=C4Connect;
338   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
339   and (cancelledby is NULL or cancelledby = '')
340   and (quantityreceived < quantity or quantityreceived is NULL)
341   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
342   aqorders.biblioitemnumber
343   group by aqorders.biblioitemnumber
344   order by
345   biblio.title";
346   my $i=0;
347   my @results;
348   my $sth=$dbh->prepare($query);
349   $sth->execute;
350   while (my $data=$sth->fetchrow_hashref){
351     $results[$i]=$data;
352     $i++;
353   }
354   $sth->finish;
355   $dbh->disconnect;
356   return($i,@results);
357 }
358
359 sub getrecorders {
360   #gets all orders from a certain supplier, orders them alphabetically
361   my ($supid)=@_;
362   my $dbh=C4Connect;
363   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
364   and (cancelledby is NULL or cancelledby = '')
365   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
366   aqorders.biblioitemnumber and
367   aqorders.quantityreceived>0
368   and aqorders.datereceived >=now()
369   group by aqorders.biblioitemnumber 
370   order by
371   biblio.title";
372   my $i=0;
373   my @results;
374   my $sth=$dbh->prepare($query);
375   $sth->execute;
376   while (my $data=$sth->fetchrow_hashref){
377     $results[$i]=$data;
378     $i++;
379   }
380   $sth->finish;
381   $dbh->disconnect;
382   return($i,@results);
383 }
384
385 sub ordersearch {
386   my ($search,$biblio,$catview) = @_;
387   my $dbh   = C4Connect;
388   my $query = "Select *,biblio.title from aqorders,biblioitems,biblio
389 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
390 and biblio.biblionumber=aqorders.biblionumber
391 and ((datecancellationprinted is NULL)
392 or (datecancellationprinted = '0000-00-00'))
393 and ((";
394   my @data  = split(' ',$search);
395   my $count = @data;
396   for (my $i = 0; $i < $count; $i++) {
397     $query .= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
398   }
399   $query=~ s/ and $//;
400   $query.=" ) or biblioitems.isbn='$search'
401   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
402   if ($catview ne 'yes'){
403     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
404   }
405   $query.=" group by aqorders.ordernumber";
406   my $sth=$dbh->prepare($query);
407   $sth->execute;
408   my $i=0;
409   my @results;
410   while (my $data=$sth->fetchrow_hashref){
411      my $sth2=$dbh->prepare("Select * from biblio where
412      biblionumber='$data->{'biblionumber'}'");
413      $sth2->execute;
414      my $data2=$sth2->fetchrow_hashref;
415      $sth2->finish;
416      $data->{'author'}=$data2->{'author'};
417      $data->{'seriestitle'}=$data2->{'seriestitle'};
418      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
419     ordernumber=$data->{'ordernumber'}");
420     $sth2->execute;
421     $data2=$sth2->fetchrow_hashref;
422     $sth2->finish;
423     $data->{'branchcode'}=$data2->{'branchcode'};
424     $data->{'bookfundid'}=$data2->{'bookfundid'};
425     $results[$i]=$data;
426     $i++;
427   }
428   $sth->finish;
429   $dbh->disconnect;
430   return($i,@results);
431 }
432
433 #
434 #
435 # MONEY
436 #
437 #
438 sub invoice {
439   my ($invoice)=@_;
440   my $dbh=C4Connect;
441   my $query="Select * from aqorders,biblio,biblioitems where
442   booksellerinvoicenumber='$invoice'
443   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
444   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
445   my $i=0;
446   my @results;
447   my $sth=$dbh->prepare($query);
448   $sth->execute;
449   while (my $data=$sth->fetchrow_hashref){
450     $results[$i]=$data;
451     $i++;
452   }
453   $sth->finish;
454   $dbh->disconnect;
455   return($i,@results);
456 }
457
458 sub bookfunds {
459   my $dbh=C4Connect;
460   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
461   =aqbudget.bookfundid
462   group by aqbookfund.bookfundid order by bookfundname";
463   my $sth=$dbh->prepare($query);
464   $sth->execute;
465   my @results;
466   my $i=0;
467   while (my $data=$sth->fetchrow_hashref){
468     $results[$i]=$data;
469     $i++;
470   }
471   $sth->finish;
472   $dbh->disconnect;
473   return($i,@results);
474 }
475
476 sub bookfundbreakdown {
477   my ($id)=@_;
478   my $dbh=C4Connect;
479   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
480   from aqorders,aqorderbreakdown where bookfundid='$id' and 
481   aqorders.ordernumber=aqorderbreakdown.ordernumber
482   and (datecancellationprinted is NULL or
483   datecancellationprinted='0000-00-00')";
484   my $sth=$dbh->prepare($query);
485   $sth->execute;
486   my $comtd=0;
487   my $spent=0;
488   while (my $data=$sth->fetchrow_hashref){
489     if ($data->{'subscription'} == 1){
490       $spent+=$data->{'quantity'}*$data->{'unitprice'};
491     } else {
492       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
493       $comtd+=($data->{'ecost'})*$leftover;
494       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
495     }
496   }
497   $sth->finish;
498   $dbh->disconnect;
499   return($spent,$comtd);
500 }
501
502 sub curconvert {
503   my ($currency,$price)=@_;
504   my $dbh=C4Connect;
505   my $query="Select rate from currency where currency='$currency'";
506   my $sth=$dbh->prepare($query);
507   $sth->execute;
508   my $data=$sth->fetchrow_hashref;
509   $sth->finish;
510   $dbh->disconnect;
511   my $cur=$data->{'rate'};
512   if ($cur==0){
513     $cur=1;
514   }
515   my $price=$price / $cur;
516   return($price);
517 }
518
519 sub getcurrencies {
520   my $dbh=C4Connect;
521   my $query="Select * from currency";
522   my $sth=$dbh->prepare($query);
523   $sth->execute;
524   my @results;
525   my $i=0;
526   while (my $data=$sth->fetchrow_hashref){
527     $results[$i]=$data;
528     $i++;
529   }
530   $sth->finish;
531   $dbh->disconnect;
532   return($i,\@results);
533
534
535 sub getcurrency {
536   my ($cur)=@_;
537   my $dbh=C4Connect;
538   my $query="Select * from currency where currency='$cur'";
539   my $sth=$dbh->prepare($query);
540   $sth->execute;
541
542   my $data=$sth->fetchrow_hashref;
543   $sth->finish;
544   $dbh->disconnect;
545   return($data);
546
547
548 sub updatecurrencies {
549   my ($currency,$rate)=@_;
550   my $dbh=C4Connect;
551   my $query="update currency set rate=$rate where currency='$currency'";
552   my $sth=$dbh->prepare($query);
553   $sth->execute;
554   $sth->finish;
555   $dbh->disconnect;
556
557
558 sub updatecost{
559   my($price,$rrp,$itemnum)=@_;
560   my $dbh=C4Connect;
561   my $query="update items set price='$price',replacementprice='$rrp'
562   where itemnumber=$itemnum";
563   my $sth=$dbh->prepare($query);
564   $sth->execute;
565   $sth->finish;
566   $dbh->disconnect;
567 }
568
569 #
570 #
571 # OTHERS
572 #
573 #
574
575 sub bookseller {
576   my ($searchstring)=@_;
577   my $dbh=C4Connect;
578   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
579   id = '$searchstring'";
580   my $sth=$dbh->prepare($query);
581   $sth->execute;
582   my @results;
583   my $i=0;
584   while (my $data=$sth->fetchrow_hashref){
585     $results[$i]=$data;
586     $i++;
587   }
588   $sth->finish;
589   $dbh->disconnect;
590   return($i,@results);
591 }
592 sub breakdown {
593   my ($id)=@_;
594   my $dbh=C4Connect;
595   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
596   my $sth=$dbh->prepare($query);
597   $sth->execute;
598   my @results;
599   my $i=0;
600   while (my $data=$sth->fetchrow_hashref){
601     $results[$i]=$data;
602     $i++;
603   }
604   $sth->finish;
605   $dbh->disconnect;
606   return($i,\@results);
607 }
608
609 sub branches {
610     my $dbh   = C4Connect;
611     my $query = "Select * from branches";
612     my $sth   = $dbh->prepare($query);
613     my $i     = 0;
614     my @results;
615
616     $sth->execute;
617     while (my $data = $sth->fetchrow_hashref) {
618         $results[$i] = $data;
619         $i++;
620     } # while
621
622     $sth->finish;
623     $dbh->disconnect;
624     return($i, @results);
625 } # sub branches
626
627 sub findall {
628   my ($biblionumber)=@_;
629   my $dbh=C4Connect;
630   my $query="Select * from biblioitems,items,itemtypes where 
631   biblioitems.biblionumber=$biblionumber 
632   and biblioitems.biblioitemnumber=items.biblioitemnumber and
633   itemtypes.itemtype=biblioitems.itemtype
634   order by items.biblioitemnumber";
635   my $sth=$dbh->prepare($query);
636   $sth->execute;
637   my @results;
638   my $i;
639   while (my $data=$sth->fetchrow_hashref){
640     $results[$i]=$data;
641     $i++;
642   }
643   $sth->finish;
644   $dbh->disconnect;
645   return(@results);
646 }
647
648 sub needsmod{
649   my ($bibitemnum,$itemtype)=@_;
650   my $dbh=C4Connect;
651   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
652   and itemtype='$itemtype'";
653   my $sth=$dbh->prepare($query);
654   $sth->execute;
655   my $result=0;
656   if (my $data=$sth->fetchrow_hashref){
657     $result=1;
658   }
659   $sth->finish;
660   $dbh->disconnect;
661   return($result);
662 }
663
664 sub updatesup {
665    my ($data)=@_;
666    my $dbh=C4Connect;
667    my $query="Update aqbooksellers set
668    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
669    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
670    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
671    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
672    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
673    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
674    '$data->{'contnotes'}', active=$data->{'active'},
675    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
676    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
677    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
678    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
679    nocalc='$data->{'nocalc'}'
680    where id='$data->{'id'}'";
681    my $sth=$dbh->prepare($query);
682    $sth->execute;
683    $sth->finish;
684    $dbh->disconnect;
685 #   print $query;
686 }
687
688 sub insertsup {
689   my ($data)=@_;
690   my $dbh=C4Connect;
691   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
692   $sth->execute;
693   my $data2=$sth->fetchrow_hashref;
694   $sth->finish;
695   $data2->{'max(id)'}++;
696   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
697   $sth->execute;
698   $sth->finish;
699   $data->{'id'}=$data2->{'max(id)'};
700   $dbh->disconnect;
701   updatesup($data);
702   return($data->{'id'});
703 }
704
705 sub websitesearch {
706     my ($keywordlist) = @_;
707     my $dbh   = C4Connect;
708     my $query = "Select distinct biblio.* from biblio, biblioitems where
709 biblio.biblionumber = biblioitems.biblionumber and (";
710     my $count = 0;
711     my $sth;
712     my @results;
713     my @keywords = split(/ +/, $keywordlist);
714     my $keyword = shift(@keywords);
715
716     $keyword =~ s/%/\\%/g;
717     $keyword =~ s/_/\\_/;
718     $keyword = "%" . $keyword . "%";
719     $keyword = $dbh->quote($keyword);
720     $query  .= " (url like $keyword)";
721
722     foreach $keyword (@keywords) {
723         $keyword =~ s/%/\\%/;
724         $keyword =~ s/_/\\_/;
725         $keyword = "%" . $keyword . "%";
726         $keyword = $dbh->quote($keyword);
727         $query  .= " or (url like $keyword)";
728     } # foreach
729
730     $query .= ")";
731     $sth    = $dbh->prepare($query);
732     $sth->execute;
733
734     while (my $data = $sth->fetchrow_hashref) {
735         $results[$count] = $data;
736         $count++;
737     } # while
738
739     $sth->finish;
740     $dbh->disconnect;
741     return($count, @results);
742 } # sub websitesearch
743
744
745 sub addwebsite {
746     my ($website) = @_;
747     my $dbh = C4Connect;
748     my $query;
749     
750     $website->{'biblionumber'} = $dbh->quote($website->{'biblionumber'});
751     $website->{'title'}        = $dbh->quote($website->{'title'});
752     $website->{'description'}  = $dbh->quote($website->{'description'});
753     $website->{'url'}          = $dbh->quote($website->{'url'});
754     
755     $query = "Insert into websites set
756 biblionumber = $website->{'biblionumber'},
757 title        = $website->{'title'},
758 description  = $website->{'description'},
759 url          = $website->{'url'}";
760     
761     $dbh->do($query);
762     
763     $dbh->disconnect;
764 } # sub website
765
766
767 sub updatewebsite {
768     my ($website) = @_;
769     my $dbh = C4Connect;
770     my $query;
771     
772     $website->{'title'}      = $dbh->quote($website->{'title'});
773     $website->{'description'} = $dbh->quote($website->{'description'});
774     $website->{'url'}        = $dbh->quote($website->{'url'});
775     
776     $query = "Update websites set
777 title       = $website->{'title'},
778 description = $website->{'description'},
779 url         = $website->{'url'}
780 where websitenumber = $website->{'websitenumber'}";
781
782     $dbh->do($query);
783     
784     $dbh->disconnect;
785 } # sub updatewebsite
786
787
788 sub deletewebsite {
789     my ($websitenumber) = @_;
790     my $dbh = C4Connect;
791     my $query = "Delete from websites where websitenumber = $websitenumber";
792     
793     $dbh->do($query);
794     
795     $dbh->disconnect;
796 } # sub deletewebsite
797
798 END { }       # module clean-up code here (global destructor)