4 # Revision 1.32 2002/12/16 15:08:50 tipaul
5 # small but important bugfix (fixes a problem in export)
7 # Revision 1.31 2002/12/13 16:22:04 tipaul
8 # 1st draft of marc export
10 # Revision 1.30 2002/12/12 21:26:35 tipaul
11 # YAB ! (Yet Another Bugfix) => related to biblio modif
12 # (some warning cleaning too)
14 # Revision 1.29 2002/12/12 16:35:00 tipaul
15 # adding authentification with Auth.pm and
16 # MAJOR BUGFIX on marc biblio modification
18 # Revision 1.28 2002/12/10 13:30:03 tipaul
19 # fugfixes from Dombes Abbey work
21 # Revision 1.27 2002/11/19 12:36:16 tipaul
23 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
25 # Revision 1.26 2002/11/12 15:58:43 tipaul
28 # * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
30 # Revision 1.25 2002/10/25 10:58:26 tipaul
32 # * bugfixes and improvements
34 # Revision 1.24 2002/10/24 12:09:01 arensb
35 # Fixed "no title" warning when generating HTML documentation from POD.
37 # Revision 1.23 2002/10/16 12:43:08 arensb
38 # Added some FIXME comments.
40 # Revision 1.22 2002/10/15 13:39:17 tipaul
41 # removing Acquisition.pm
42 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
44 # Revision 1.21 2002/10/13 11:34:14 arensb
45 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
46 # Thus, $x = $x+2 becomes $x += 2, and so forth.
48 # Revision 1.20 2002/10/13 08:28:32 arensb
49 # Deleted unused variables.
50 # Removed trailing whitespace.
52 # Revision 1.19 2002/10/13 05:56:10 arensb
53 # Added some FIXME comments.
55 # Revision 1.18 2002/10/11 12:34:53 arensb
56 # Replaced &requireDBI with C4::Context->dbh
58 # Revision 1.17 2002/10/10 14:48:25 tipaul
61 # Revision 1.16 2002/10/07 14:04:26 tipaul
62 # road to 1.3.1 : viewing MARC biblio
64 # Revision 1.15 2002/10/05 09:49:25 arensb
65 # Merged with arensb-context branch: use C4::Context->dbh instead of
66 # &C4Connect, and generally prefer C4::Context over C4::Database.
68 # Revision 1.14 2002/10/03 11:28:18 tipaul
69 # Extending Context.pm to add stopword management and using it in MARC-API.
70 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
72 # Revision 1.13 2002/10/02 16:26:44 tipaul
75 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
76 # Merged in changes from main branch.
78 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
79 # Added a whole mess of FIXME comments.
81 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
82 # Added some missing semicolons.
84 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
85 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
88 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
89 # Added a whole mess of FIXME comments.
91 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
92 # Added some missing semicolons.
94 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
95 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
98 # Revision 1.12 2002/10/01 11:48:51 arensb
99 # Added some FIXME comments, mostly marking duplicate functions.
101 # Revision 1.11 2002/09/24 13:49:26 tipaul
102 # long WAS the road to 1.3.0...
103 # coming VERY SOON NOW...
104 # modifying installer and buildrelease to update the DB
106 # Revision 1.10 2002/09/22 16:50:08 arensb
107 # Added some FIXME comments.
109 # Revision 1.9 2002/09/20 12:57:46 tipaul
110 # long is the road to 1.4.0
111 # * MARCadditem and MARCmoditem now wroks
112 # * various bugfixes in MARC management
113 # !!! 1.3.0 should be released very soon now. Be careful !!!
115 # Revision 1.8 2002/09/10 13:53:52 tipaul
116 # MARC API continued...
118 # * multiple item management : MARCadditem and MARCmoditem have been added. They suppose that ALL the MARC field linked to koha-item are in the same MARC tag (on the same line of MARC file)
120 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
122 # Revision 1.7 2002/08/14 18:12:51 tonnesen
123 # Added copyright statement to all .pl and .pm files
125 # Revision 1.6 2002/07/25 13:40:31 tipaul
126 # pod documenting the API.
128 # Revision 1.5 2002/07/24 16:11:37 tipaul
130 # Database.pm and Output.pm are almost not modified (var test...)
132 # Biblio.pm is almost completly rewritten.
134 # WHAT DOES IT ??? ==> END of Hitchcock suspens
136 # 1st, it does... nothing...
137 # Every old API should be there. So if MARC-stuff is not done, the behaviour is EXACTLY the same (if there is no added bug, of course). So, if you use normal acquisition, you won't find anything new neither on screen or old-DB tables ...
139 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
140 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
141 # * a "OLDnewbiblio" sub, which is a copy/paste of the previous newbiblio sub. Then, when you want to add the MARC-DB stuff, you can modify the newbiblio sub without modifying the OLDnewbiblio one. If we correct a bug in 1.2 in newbiblio, we can do the same in main branch by correcting OLDnewbiblio.
142 # * The MARC stuff is usually done through a sub named MARCxxx where xxx is the same as OLDxxx. For example, newbiblio calls MARCnewbiblio. the MARCxxx subs use a MARC::Record as parameter.
143 # The last thing to solve was to manage biblios through real MARC import : they must populate the old-db, but must populate the MARC-DB too, without loosing information (if we go from MARC::Record to old-data then back to MARC::Record, we loose A LOT OF ROWS). To do this, there are subs beginning by "NEWxxx" : they manage datas with MARC::Record datas. they call OLDxxx sub too (to populate old-DB), but MARCxxx subs too, with a complete MARC::Record ;-)
145 # In Biblio.pm, there are some subs that permits to build a old-style record from a MARC::Record, and the opposite. There is also a sub finding a MARC-bibid from a old-biblionumber and the opposite too.
146 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
150 # Copyright 2000-2002 Katipo Communications
152 # This file is part of Koha.
154 # Koha is free software; you can redistribute it and/or modify it under the
155 # terms of the GNU General Public License as published by the Free Software
156 # Foundation; either version 2 of the License, or (at your option) any later
159 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
160 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
161 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
163 # You should have received a copy of the GNU General Public License along with
164 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
165 # Suite 330, Boston, MA 02111-1307 USA
173 use vars qw($VERSION @ISA @EXPORT);
175 # set the version for version checking
180 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
181 # as the old-style API and the NEW one are the only public functions.
184 &updateBiblio &updateBiblioItem &updateItem
185 &itemcount &newbiblio &newbiblioitem
186 &modnote &newsubject &newsubtitle
187 &modbiblio &checkitems
188 &newitems &modbibitem
189 &modsubtitle &modsubject &modaddauthor &moditem &countitems
190 &delitem &deletebiblioitem &delbiblio
191 &getitemtypes &getbiblio
192 &getbiblioitembybiblionumber
193 &getbiblioitem &getitemsbybiblioitem &isbnsearch
195 &newcompletebiblioitem
197 &MARCfind_oldbiblionumber_from_MARCbibid
198 &MARCfind_MARCbibid_from_oldbiblionumber
199 &MARCfind_marc_from_kohafield
203 &NEWnewbiblio &NEWnewitem
204 &NEWmodbiblio &NEWmoditem
206 &MARCaddbiblio &MARCadditem
207 &MARCmodsubfield &MARCaddsubfield
208 &MARCmodbiblio &MARCmoditem
209 &MARCkoha2marcBiblio &MARCmarc2koha
210 &MARCkoha2marcItem &MARChtml2marc
211 &MARCgetbiblio &MARCgetitem
212 &MARCaddword &MARCdelword
217 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
220 # all the following subs takes a MARC::Record as parameter and manage
221 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
222 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
226 C4::Biblio - acquisition, catalog management functions
230 move from 1.2 to 1.4 version :
231 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
232 In the 1.4 version, we want to do 2 differents things :
233 - keep populating the old-DB, that has a LOT less datas than MARC
234 - populate the MARC-DB
235 To populate the DBs we have 2 differents sources :
236 - the standard acquisition system (through book sellers), that does'nt use MARC data
237 - the MARC acquisition system, that uses MARC data.
239 Thus, we have 2 differents cases :
240 - with the standard acquisition system, we have non MARC data and want to populate old-DB and MARC-DB, knowing it's an incomplete MARC-record
241 - with the MARC acquisition system, we have MARC datas, and want to loose nothing in MARC-DB. So, we can't store datas in old-DB, then copy in MARC-DB. we MUST have an API for true MARC data, that populate MARC-DB then old-DB
243 That's why we need 4 subs :
244 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
245 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
246 all I<subs beginning by NEW> manage both OLD-DB and MARC tables. They use MARC::Record as parameters. it's the API that MUST be used in MARC acquisition system
247 all I<subs beginning by seomething else> are the old-style API. They use old-DB as parameter, then call internally the OLD and MARC subs.
249 - NEW and old-style API should be used in koha to manage biblio
250 - MARCsubs are divided in 2 parts :
251 * some of them manage MARC parameters. They are heavily used in koha.
252 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
253 - OLD are used internally only
255 all subs requires/use $dbh as 1st parameter.
257 I<NEWxxx related subs>
259 all subs requires/use $dbh as 1st parameter.
260 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
262 I<OLDxxx related subs>
264 all subs requires/use $dbh as 1st parameter.
265 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
267 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
268 The OLDxxx is called by the original xxx sub.
269 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
271 WARNING : there is 1 difference between initialxxx and OLDxxx :
272 the db header $dbh is always passed as parameter to avoid over-DB connexion
278 =item @tagslib = &MARCgettagslib($dbh,1|0);
280 last param is 1 for liblibrarian and 0 for libopac
281 returns a hash with tag/subfield meaning
282 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
284 finds MARC tag and subfield for a given kohafield
285 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
287 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
289 finds a old-db biblio number for a given MARCbibid number
291 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
293 finds a MARC bibid from a old-db biblionumber
295 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
297 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
299 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
301 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
303 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
305 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
307 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
309 builds a hash with old-db datas from a MARC::Record
311 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
313 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
315 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
317 adds a subfield in a biblio (in the MARC tables only).
319 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
321 Returns a MARC::Record for the biblio $bibid.
323 =item &MARCmodbiblio($dbh,$bibid,$delete,$record);
325 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
326 if $delete == 1, every field/subfield not found is deleted in the biblio
327 otherwise, only data passed to MARCmodbiblio is managed.
328 thus, you can change only a small part of a biblio (like an item, or a subtitle, or a additionalauthor...)
330 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
332 MARCmodsubfield changes the value of a given subfield
334 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
336 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
337 Returns -1 if more than 1 answer
339 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
341 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
343 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
345 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
347 =item &MARCdelbiblio($dbh,$bibid);
349 MARCdelbiblio delete biblio $bibid
351 =item &MARCkoha2marcOnefield
353 used by MARCkoha2marc and should not be useful elsewhere
355 =item &MARCmarc2kohaOnefield
357 used by MARCmarc2koha and should not be useful elsewhere
361 used to manage MARC_word table and should not be useful elsewhere
365 used to manage MARC_word table and should not be useful elsewhere
370 my ($dbh,$forlibrarian)= @_;
372 if ($forlibrarian eq 1) {
373 $sth=$dbh->prepare("select tagfield,liblibrarian as lib from marc_tag_structure order by tagfield");
375 $sth=$dbh->prepare("select tagfield,libopac as lib from marc_tag_structure order by tagfield");
378 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
379 while ( ($tag,$lib,$tab) = $sth->fetchrow) {
380 $res->{$tag}->{lib}=$lib;
381 $res->{$tab}->{tab}="";
384 if ($forlibrarian eq 1) {
385 $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield");
387 $sth=$dbh->prepare("select tagfield,tagsubfield,libopac as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield");
392 my $authorised_value;
393 my $thesaurus_category;
395 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder) = $sth->fetchrow) {
396 $res->{$tag}->{$subfield}->{lib}=$lib;
397 $res->{$tag}->{$subfield}->{tab}=$tab;
398 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
399 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
400 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
401 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
402 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
407 sub MARCfind_marc_from_kohafield {
408 my ($dbh,$kohafield) = @_;
409 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
410 $sth->execute($kohafield);
411 my ($tagfield,$tagsubfield) = $sth->fetchrow;
412 return ($tagfield,$tagsubfield);
415 sub MARCfind_oldbiblionumber_from_MARCbibid {
416 my ($dbh,$MARCbibid) = @_;
417 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
418 $sth->execute($MARCbibid);
419 my ($biblionumber) = $sth->fetchrow;
420 return $biblionumber;
423 sub MARCfind_MARCbibid_from_oldbiblionumber {
424 my ($dbh,$oldbiblionumber) = @_;
425 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
426 $sth->execute($oldbiblionumber);
427 my ($bibid) = $sth->fetchrow;
432 # pass the MARC::Record to this function, and it will create the records in the marc tables
433 my ($dbh,$record,$biblionumber) = @_;
434 my @fields=$record->fields();
436 # adding main table, and retrieving bibid
437 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
438 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
439 $sth->execute($biblionumber);
440 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
442 ($bibid)=$sth->fetchrow;
445 # now, add subfields...
446 foreach my $field (@fields) {
447 my @subfields=$field->subfields();
449 foreach my $subfieldcount (0..$#subfields) {
450 &MARCaddsubfield($dbh,$bibid,
452 $field->indicator(1).$field->indicator(2),
454 $subfields[$subfieldcount][0],
456 $subfields[$subfieldcount][1]
460 $dbh->do("unlock tables");
465 # pass the MARC::Record to this function, and it will create the records in the marc tables
466 my ($dbh,$record,$biblionumber) = @_;
467 # warn "adding : ".$record->as_formatted();
468 # search for MARC biblionumber
469 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
470 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
471 my @fields=$record->fields();
472 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
473 $sth->execute($bibid);
474 my ($fieldcount) = $sth->fetchrow;
475 # now, add subfields...
476 foreach my $field (@fields) {
477 my @subfields=$field->subfields();
479 foreach my $subfieldcount (0..$#subfields) {
480 &MARCaddsubfield($dbh,$bibid,
482 $field->indicator(1).$field->indicator(2),
484 $subfields[$subfieldcount][0],
486 $subfields[$subfieldcount][1]
488 # warn "ADDING :$bibid,".
490 $field->indicator(1).$field->indicator(2).",
492 $subfields[$subfieldcount][0],
494 $subfields[$subfieldcount][1]";
497 $dbh->do("unlock tables");
501 sub MARCaddsubfield {
502 # Add a new subfield to a tag into the DB.
503 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
504 # if not value, end of job, we do nothing
505 if (length($subfieldvalue) ==0) {
508 if (not($subfieldcode)) {
511 if (length($subfieldvalue)>255) {
512 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
513 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
514 $sth->execute($subfieldvalue);
515 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
517 my ($res)=$sth->fetchrow;
518 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
520 $sth->execute($bibid,'0'.$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
522 $sth->execute($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
525 print STDERR "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
527 # $dbh->do("unlock tables");
529 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
530 $sth->execute($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
532 print STDERR "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
535 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
539 # Returns MARC::Record of the biblio passed in parameter.
541 my $record = MARC::Record->new();
542 #---- TODO : the leader is missing
543 $record->leader(' ');
544 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
545 from marc_subfield_table
546 where bibid=? order by tag,tagorder,subfieldcode
548 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
549 $sth->execute($bibid);
554 while (my $row=$sth->fetchrow_hashref) {
555 if ($row->{'valuebloblink'}) { #---- search blob if there is one
556 $sth2->execute($row->{'valuebloblink'});
557 my $row2=$sth2->fetchrow_hashref;
559 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
561 # warn "$row->{bibid} = $row->{tag} - $row->{subfieldcode} -> value : $row->{subfieldvalue}";
562 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
563 if (length($prevtag) <3) {
564 $prevtag = "0".$prevtag;
567 # warn "NEW : subfieldcode : $prevtag".substr($previndicator,0,1).substr($previndicator,1,1),;
568 # foreach my $x (keys %subfieldlist) {
569 # warn " $x => ".$subfieldlist{$x};
571 my $field = MARC::Field->new( $prevtag, substr($previndicator,0,1), substr($previndicator,1,1), %subfieldlist);
573 $record->add_fields($field);
574 $prevtagorder=$row->{tagorder};
575 $prevtag = $row->{tag};
576 $previndicator=$row->{tag_indicator};
578 %subfieldlist->{$row->{'subfieldcode'}} = $row->{'subfieldvalue'};
580 # warn "subfieldcode : $row->{'subfieldcode'} / value : $row->{'subfieldvalue'}, tag : $row->{tag}";
581 # if (%subfieldlist->{$row->{'subfieldcode'}}) {
582 # %subfieldlist->{$row->{'subfieldcode'}}.='|';
584 %subfieldlist->{$row->{'subfieldcode'}} .= $row->{'subfieldvalue'};
585 $prevtag= $row->{tag};
586 $previndicator=$row->{tag_indicator};
589 # the last has not been included inside the loop... do it now !
590 my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
591 # warn "NEW : subfieldcode : $prevtag".substr($previndicator,0,1).substr($previndicator,1,1),;
592 # foreach my $x (keys %subfieldlist) {
593 # warn " $x => ".$subfieldlist{$x};
595 $record->add_fields($field);
599 # Returns MARC::Record of the biblio passed in parameter.
600 my ($dbh,$bibid,$itemnumber)=@_;
601 my $record = MARC::Record->new();
602 # search MARC tagorder
603 my $sth2 = $dbh->prepare("select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?");
604 $sth2->execute($bibid,$itemnumber);
605 my ($tagorder) = $sth2->fetchrow_array();
606 #---- TODO : the leader is missing
607 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
608 from marc_subfield_table
609 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
611 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
612 $sth->execute($bibid,$tagorder);
613 while (my $row=$sth->fetchrow_hashref) {
614 if ($row->{'valuebloblink'}) { #---- search blob if there is one
615 $sth2->execute($row->{'valuebloblink'});
616 my $row2=$sth2->fetchrow_hashref;
618 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
620 if ($record->field($row->{'tag'})) {
622 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
623 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
624 if (length($row->{'tag'}) <3) {
625 $row->{'tag'} = "0".$row->{'tag'};
627 $field =$record->field($row->{'tag'});
629 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
630 $record->delete_field($field);
631 $record->add_fields($field);
634 if (length($row->{'tag'}) < 3) {
635 $row->{'tag'} = "0".$row->{'tag'};
637 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
638 $record->add_fields($temp);
646 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
647 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
648 # warn "OLD : ".$oldrecord->as_formatted();
649 # warn "----------------------------------\nNEW : ".$record->as_formatted();
651 # if nothing to change, don't waste time...
652 if ($oldrecord eq $record) {
653 # warn "NOTHING TO CHANGE";
656 # otherwise, skip through each subfield...
657 my @fields = $record->fields();
659 foreach my $field (@fields) {
660 my $oldfield = $oldrecord->field($field->tag());
661 my @subfields=$field->subfields();
664 foreach my $subfield (@subfields) {
666 if ($oldfield eq 0 or (! $oldfield->subfield(@$subfield[0])) ) {
667 # just adding datas...
668 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
669 1,@$subfield[0],$subfieldorder,@$subfield[1]);
671 # modify the subfield if it's a different string
672 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
673 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
674 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
683 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
684 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
685 # if nothing to change, don't waste time...
686 if ($oldrecord eq $record) {
687 # warn "nothing to change";
690 # warn "MARCmoditem : ".$record->as_formatted;
691 # warn "OLD : ".$oldrecord->as_formatted;
693 # otherwise, skip through each subfield...
694 my @fields = $record->fields();
695 # search old MARC item
696 my $sth2 = $dbh->prepare("select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?");
697 $sth2->execute($bibid,$itemnumber);
698 my ($tagorder) = $sth2->fetchrow_array();
699 foreach my $field (@fields) {
700 my $oldfield = $oldrecord->field($field->tag());
701 my @subfields=$field->subfields();
703 foreach my $subfield (@subfields) {
705 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
706 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
707 # just adding datas...
708 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
709 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
710 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
711 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
713 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
714 # modify he subfield if it's a different string
715 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
716 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
717 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
718 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
721 warn "nothing to change : ".$oldfield->subfield(@$subfield[0]);
729 sub MARCmodsubfield {
730 # Subroutine changes a subfield value given a subfieldid.
731 my ($dbh, $subfieldid, $subfieldvalue )=@_;
732 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
733 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
734 $sth1->execute($subfieldid);
735 my ($oldvaluebloblink)=$sth1->fetchrow;
738 # if too long, use a bloblink
739 if (length($subfieldvalue)>255 ) {
740 # if already a bloblink, update it, otherwise, insert a new one.
741 if ($oldvaluebloblink) {
742 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
743 $sth->execute($subfieldvalue,$oldvaluebloblink);
745 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
746 $sth->execute($subfieldvalue);
747 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
749 my ($res)=$sth->fetchrow;
750 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
751 $sth->execute($subfieldid);
754 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
755 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
756 $sth->execute($subfieldvalue, $subfieldid);
758 $dbh->do("unlock tables");
760 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
761 $sth->execute($subfieldid);
762 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
764 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
765 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
766 return($subfieldid, $subfieldvalue);
769 sub MARCfindsubfield {
770 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
774 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
775 if ($subfieldvalue) {
776 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
778 if ($subfieldorder<1) {
781 $query .= " and subfieldorder=$subfieldorder";
783 my $sti=$dbh->prepare($query);
784 $sti->execute($bibid,$tag, $subfieldcode);
785 while (($subfieldid) = $sti->fetchrow) {
787 $lastsubfieldid=$subfieldid;
789 if ($resultcounter>1) {
790 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
791 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
794 return $lastsubfieldid;
798 sub MARCfindsubfieldid {
799 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
800 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
801 where bibid=? and tag=? and tagorder=?
802 and subfieldcode=? and subfieldorder=?");
803 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
804 my ($res) = $sth->fetchrow;
806 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
807 where bibid=? and tag=? and tagorder=?
808 and subfieldcode=?");
809 $sth->execute($bibid,$tag,$tagorder,$subfield);
810 ($res) = $sth->fetchrow;
815 sub MARCdelsubfield {
816 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
817 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
818 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
819 tag='$tag' and tagorder='$tagorder'
820 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
825 # delete a biblio for a $bibid
826 my ($dbh,$bibid) = @_;
827 $dbh->do("delete from marc_subfield_table where bibid='$bibid'");
828 $dbh->do("delete from marc_biblio where bibid='$bibid'");
831 sub MARCkoha2marcBiblio {
832 # this function builds partial MARC::Record from the old koha-DB fields
833 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
834 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
835 my $record = MARC::Record->new();
836 #--- if bibid, then retrieve old-style koha data
837 if ($biblionumber>0) {
838 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
839 from biblio where biblionumber=?");
840 $sth2->execute($biblionumber);
841 my $row=$sth2->fetchrow_hashref;
843 foreach $code (keys %$row) {
845 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
849 #--- if biblioitem, then retrieve old-style koha data
850 if ($biblioitemnumber>0) {
851 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
852 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
853 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
855 WHERE biblionumber=? and biblioitemnumber=?
857 $sth2->execute($biblionumber,$biblioitemnumber);
858 my $row=$sth2->fetchrow_hashref;
860 foreach $code (keys %$row) {
862 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
867 # TODO : retrieve notes, additionalauthors
870 sub MARCkoha2marcItem {
871 # this function builds partial MARC::Record from the old koha-DB fields
872 my ($dbh,$biblionumber,$itemnumber) = @_;
873 # my $dbh=&C4Connect;
874 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
875 my $record = MARC::Record->new();
876 #--- if item, then retrieve old-style koha data
878 # print STDERR "prepare $biblionumber,$itemnumber\n";
879 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
880 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
881 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
882 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
884 WHERE itemnumber=?");
885 $sth2->execute($itemnumber);
886 my $row=$sth2->fetchrow_hashref;
888 foreach $code (keys %$row) {
890 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
895 # TODO : retrieve notes, additionalauthors
898 sub MARCkoha2marcSubtitle {
899 # this function builds partial MARC::Record from the old koha-DB fields
900 my ($dbh,$bibnum,$subtitle) = @_;
901 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
902 my $record = MARC::Record->new();
903 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
907 sub MARCkoha2marcOnefield {
908 my ($sth,$record,$kohafieldname,$value)=@_;
911 $sth->execute($kohafieldname);
912 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
913 if ($record->field($tagfield)) {
914 my $tag =$record->field($tagfield);
916 $tag->add_subfields($tagsubfield,$value);
917 $record->delete_field($tag);
918 $record->add_fields($tag);
921 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
928 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
929 my $prevtag = @$rtags[0];
930 my $record = MARC::Record->new();
932 for (my $i=0; $i< @$rtags; $i++) {
933 # rebuild MARC::Record
934 if (@$rtags[$i] ne $prevtag) {
938 $indicators{$prevtag}.=' ';
939 my $field = MARC::Field->new( $prevtag, substr($indicators{$prevtag},0,1),substr($indicators{$prevtag},1,1), %subfieldlist);
940 $record->add_fields($field);
941 $prevtag = @$rtags[$i];
943 %subfieldlist->{@$rsubfields[$i]} = @$rvalues[$i];
945 # if (%subfieldlist->{@$rsubfields[$i]}) {
946 # %subfieldlist->{@$rsubfields[$i]} .= '|';
948 %subfieldlist->{@$rsubfields[$i]} .=@$rvalues[$i];
949 $prevtag= @$rtags[$i];
952 # the last has not been included inside the loop... do it now !
953 my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
954 $record->add_fields($field);
959 my ($dbh,$record) = @_;
960 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
962 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
965 # print STDERR $record->as_formatted;
966 while (($field)=$sth2->fetchrow) {
967 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
969 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
971 while (($field)=$sth2->fetchrow) {
972 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
974 $sth2=$dbh->prepare("SHOW COLUMNS from items");
976 while (($field)=$sth2->fetchrow) {
977 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
979 # additional authors : specific
980 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
984 sub MARCmarc2kohaOneField {
985 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
986 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
987 # warn "kohatable / $kohafield / $result / ";
991 $sth->execute($kohatable.".".$kohafield);
992 ($tagfield,$subfield) = $sth->fetchrow;
993 foreach my $field ($record->field($tagfield)) {
994 if ($field->subfield($subfield)) {
995 if ($result->{$kohafield}) {
996 $result->{$kohafield} .= " | ".$field->subfield($subfield);
998 $result->{$kohafield}=$field->subfield($subfield);
1006 # split a subfield string and adds it into the word table.
1008 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1009 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1010 my @words = split / /,$sentence;
1011 my $stopwords= C4::Context->stopwords;
1012 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1013 values (?,?,?,?,?,?,soundex(?))");
1014 foreach my $word (@words) {
1015 # we record only words longer than 2 car and not in stopwords hash
1016 if (length($word)>1 and !($stopwords->{uc($word)})) {
1017 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1019 print STDERR "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1026 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1027 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1028 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1029 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1034 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1037 # all the following subs are useful to manage MARC-DB with complete MARC records.
1038 # it's used with marcimport, and marc management tools
1042 =item (oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1044 creates a new biblio from a MARC::Record. The 3rd and 4th parameter are hashes and may be ignored. If only 2 params are passed to the sub, the old-db hashes
1045 are builded from the MARC::Record. If they are passed, they are used.
1047 =item NEWnewitem($dbh,$olditem);
1049 adds an item in the db. $olditem is a old-db hash.
1054 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1055 # note $oldbiblio and $oldbiblioitem are not mandatory.
1056 # if not present, they will be builded from $record with MARCmarc2koha function
1057 if (($oldbiblio) and not($oldbiblioitem)) {
1058 print STDERR "NEWnewbiblio : missing parameter\n";
1059 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1065 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1066 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1067 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1069 my $olddata = MARCmarc2koha($dbh,$record);
1070 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1071 $olddata->{'biblionumber'} = $oldbibnum;
1072 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1074 # we must add bibnum and bibitemnum in MARC::Record...
1075 # we build the new field with biblionumber and biblioitemnumber
1076 # we drop the original field
1077 # we add the new builded field.
1078 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1079 # (steve and paul : thinks 090 is a good choice)
1080 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1081 $sth->execute("biblio.biblionumber");
1082 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1083 $sth->execute("biblioitems.biblioitemnumber");
1084 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1085 if ($tagfield1 != $tagfield2) {
1086 print STDERR "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1087 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1090 my $newfield = MARC::Field->new( $tagfield1,'','',
1091 "$tagsubfield1" => $oldbibnum,
1092 "$tagsubfield2" => $oldbibitemnum);
1093 # drop old field and create new one...
1094 my $old_field = $record->field($tagfield1);
1095 $record->delete_field($old_field);
1096 $record->add_fields($newfield);
1097 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1098 return ($bibid,$oldbibnum,$oldbibitemnum );
1102 my ($dbh,$record,$bibid) =@_;
1103 &MARCmodbiblio($dbh,$record,$bibid);
1104 my $oldbiblio = MARCmarc2koha($dbh,$record);
1105 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1106 OLDmodbibitem($dbh,$oldbiblio);
1112 my ($dbh, $record,$bibid) = @_;
1113 # add item in old-DB
1114 my $item = &MARCmarc2koha($dbh,$record);
1115 # needs old biblionumber and biblioitemnumber
1116 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1117 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1118 $sth->execute($item->{'biblionumber'});
1119 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1120 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1121 # add itemnumber to MARC::Record before adding the item.
1122 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1123 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1125 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1129 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1130 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1131 my $olditem = MARCmarc2koha($dbh,$record);
1132 OLDmoditem($dbh,$olditem);
1137 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1141 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1143 adds a record in biblio table. Datas are in the hash $biblio.
1145 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1147 modify a record in biblio table. Datas are in the hash $biblio.
1149 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1151 modify subtitles in bibliosubtitle table.
1153 =item OLDmodaddauthor($dbh,$bibnum,$author);
1155 adds or modify additional authors
1156 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1158 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1160 modify/adds subjects
1162 =item OLDmodbibitem($dbh, $biblioitem);
1166 =item OLDmodnote($dbh,$bibitemnum,$note
1168 modify a note for a biblioitem
1170 =item OLDnewbiblioitem($dbh,$biblioitem);
1172 adds a biblioitem ($biblioitem is a hash with the values)
1174 =item OLDnewsubject($dbh,$bibnum);
1178 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1180 create a new subtitle
1182 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1184 create a item. $item is a hash and $barcode the barcode.
1186 =item OLDmoditem($dbh,$item);
1190 =item OLDdelitem($dbh,$itemnum);
1194 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1196 deletes a biblioitem
1197 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1199 =item OLDdelbiblio($dbh,$biblio);
1206 my ($dbh,$biblio) = @_;
1207 # my $dbh = &C4Connect;
1208 my $query = "Select max(biblionumber) from biblio";
1209 my $sth = $dbh->prepare($query);
1211 my $data = $sth->fetchrow_arrayref;
1212 my $bibnum = $$data[0] + 1;
1215 $biblio->{'title'} = $dbh->quote($biblio->{'title'});
1216 $biblio->{'author'} = $dbh->quote($biblio->{'author'});
1217 $biblio->{'copyright'} = $dbh->quote($biblio->{'copyright'});
1218 $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
1219 $biblio->{'notes'} = $dbh->quote($biblio->{'notes'});
1220 $biblio->{'abstract'} = $dbh->quote($biblio->{'abstract'});
1221 if ($biblio->{'seriestitle'}) { $series = 1 };
1224 $query = "insert into biblio set
1225 biblionumber = $bibnum,
1226 title = $biblio->{'title'},
1227 author = $biblio->{'author'},
1228 copyrightdate = $biblio->{'copyright'},
1230 seriestitle = $biblio->{'seriestitle'},
1231 notes = $biblio->{'notes'},
1232 abstract = $biblio->{'abstract'}";
1234 $sth = $dbh->prepare($query);
1243 my ($dbh,$biblio) = @_;
1244 # my $dbh = C4Connect;
1248 $biblio->{'title'} = $dbh->quote($biblio->{'title'});
1249 $biblio->{'author'} = $dbh->quote($biblio->{'author'});
1250 $biblio->{'abstract'} = $dbh->quote($biblio->{'abstract'});
1251 $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
1252 $biblio->{'seriestitle'} = $dbh->quote($biblio->{'serirestitle'});
1253 $biblio->{'serial'} = $dbh->quote($biblio->{'serial'});
1254 $biblio->{'unititle'} = $dbh->quote($biblio->{'unititle'});
1255 $biblio->{'notes'} = $dbh->quote($biblio->{'notes'});
1257 $query = "Update biblio set
1258 title = $biblio->{'title'},
1259 author = $biblio->{'author'},
1260 abstract = $biblio->{'abstract'},
1261 copyrightdate = $biblio->{'copyrightdate'},
1262 seriestitle = $biblio->{'seriestitle'},
1263 serial = $biblio->{'serial'},
1264 unititle = $biblio->{'unititle'},
1265 notes = $biblio->{'notes'}
1266 where biblionumber = $biblio->{'biblionumber'}";
1267 $sth = $dbh->prepare($query);
1271 return($biblio->{'biblionumber'});
1274 sub OLDmodsubtitle {
1275 my ($dbh,$bibnum, $subtitle) = @_;
1276 # my $dbh = C4Connect;
1277 my $query = "update bibliosubtitle set
1278 subtitle = '$subtitle'
1279 where biblionumber = $bibnum";
1280 my $sth = $dbh->prepare($query);
1288 sub OLDmodaddauthor {
1289 my ($dbh,$bibnum, $author) = @_;
1290 # my $dbh = C4Connect;
1291 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1292 my $sth = $dbh->prepare($query);
1297 if ($author ne '') {
1298 $query = "Insert into additionalauthors set
1300 biblionumber = '$bibnum'";
1301 $sth = $dbh->prepare($query);
1307 } # sub modaddauthor
1311 my ($dbh,$bibnum, $force, @subject) = @_;
1312 # my $dbh = C4Connect;
1313 my $count = @subject;
1315 for (my $i = 0; $i < $count; $i++) {
1316 $subject[$i] =~ s/^ //g;
1317 $subject[$i] =~ s/ $//g;
1318 my $query = "select * from catalogueentry
1319 where entrytype = 's'
1320 and catalogueentry = '$subject[$i]'";
1321 my $sth = $dbh->prepare($query);
1324 if (my $data = $sth->fetchrow_hashref) {
1326 if ($force eq $subject[$i]) {
1327 # subject not in aut, chosen to force anway
1328 # so insert into cataloguentry so its in auth file
1329 $query = "Insert into catalogueentry
1330 (entrytype,catalogueentry)
1331 values ('s','$subject[$i]')";
1332 my $sth2 = $dbh->prepare($query);
1337 $error = "$subject[$i]\n does not exist in the subject authority file";
1338 $query = "Select * from catalogueentry
1339 where entrytype = 's'
1340 and (catalogueentry like '$subject[$i] %'
1341 or catalogueentry like '% $subject[$i] %'
1342 or catalogueentry like '% $subject[$i]')";
1343 my $sth2 = $dbh->prepare($query);
1346 while (my $data = $sth2->fetchrow_hashref) {
1347 $error .= "<br>$data->{'catalogueentry'}";
1355 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1356 my $sth = $dbh->prepare($query);
1359 for (my $i = 0; $i < $count; $i++) {
1360 $sth = $dbh->prepare("Insert into bibliosubject
1361 values ('$subject[$i]', $bibnum)");
1373 my ($dbh,$biblioitem) = @_;
1374 # my $dbh = C4Connect;
1377 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1378 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1379 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1380 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1381 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1382 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1383 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1384 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1385 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1386 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1387 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1388 $biblioitem->{'notes'} = $dbh->quote($biblioitem->{'notes'});
1389 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1390 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1392 $query = "Update biblioitems set
1393 itemtype = $biblioitem->{'itemtype'},
1394 url = $biblioitem->{'url'},
1395 isbn = $biblioitem->{'isbn'},
1396 publishercode = $biblioitem->{'publishercode'},
1397 publicationyear = $biblioitem->{'publicationyear'},
1398 classification = $biblioitem->{'classification'},
1399 dewey = $biblioitem->{'dewey'},
1400 subclass = $biblioitem->{'subclass'},
1401 illus = $biblioitem->{'illus'},
1402 pages = $biblioitem->{'pages'},
1403 volumeddesc = $biblioitem->{'volumeddesc'},
1404 notes = $biblioitem->{'notes'},
1405 size = $biblioitem->{'size'},
1406 place = $biblioitem->{'place'}
1407 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1415 my ($dbh,$bibitemnum,$note)=@_;
1416 # my $dbh=C4Connect;
1417 my $query="update biblioitems set notes='$note' where
1418 biblioitemnumber='$bibitemnum'";
1419 my $sth=$dbh->prepare($query);
1425 sub OLDnewbiblioitem {
1426 my ($dbh,$biblioitem) = @_;
1427 # my $dbh = C4Connect;
1428 my $query = "Select max(biblioitemnumber) from biblioitems";
1429 my $sth = $dbh->prepare($query);
1434 $data = $sth->fetchrow_arrayref;
1435 $bibitemnum = $$data[0] + 1;
1439 $sth = $dbh->prepare("insert into biblioitems set
1440 biblioitemnumber = ?, biblionumber = ?,
1441 volume = ?, number = ?,
1442 classification = ?, itemtype = ?,
1444 issn = ?, dewey = ?,
1445 subclass = ?, publicationyear = ?,
1446 publishercode = ?, volumedate = ?,
1447 volumeddesc = ?, illus = ?,
1448 pages = ?, notes = ?,
1450 marc = ?, place = ?");
1451 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1452 $biblioitem->{'volume'}, $biblioitem->{'number'},
1453 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1454 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1455 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1456 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1457 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1458 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1459 $biblioitem->{'pages'}, $biblioitem->{'notes'},
1460 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1461 $biblioitem->{'marc'}, $biblioitem->{'place'});
1464 return($bibitemnum);
1468 my ($dbh,$bibnum)=@_;
1469 # my $dbh=C4Connect;
1470 my $query="insert into bibliosubject (biblionumber) values
1472 my $sth=$dbh->prepare($query);
1479 sub OLDnewsubtitle {
1480 my ($dbh,$bibnum, $subtitle) = @_;
1481 # my $dbh = C4Connect;
1482 $subtitle = $dbh->quote($subtitle);
1483 my $query = "insert into bibliosubtitle set
1484 biblionumber = $bibnum,
1485 subtitle = $subtitle";
1486 my $sth = $dbh->prepare($query);
1496 my ($dbh,$item, $barcode) = @_;
1497 # my $dbh = C4Connect;
1498 my $query = "Select max(itemnumber) from items";
1499 my $sth = $dbh->prepare($query);
1505 $data = $sth->fetchrow_hashref;
1506 $itemnumber = $data->{'max(itemnumber)'} + 1;
1509 $sth=$dbh->prepare("Insert into items set
1510 itemnumber = ?, biblionumber = ?,
1511 biblioitemnumber = ?, barcode = ?,
1512 booksellerid = ?, dateaccessioned = NOW(),
1513 homebranch = ?, holdingbranch = ?,
1514 price = ?, replacementprice = ?,
1515 replacementpricedate = NOW(), itemnotes = ?,
1518 $sth->execute($itemnumber, $item->{'biblionumber'},
1519 $item->{'biblioitemnumber'},$barcode,
1520 $item->{'booksellerid'},
1521 $item->{'homebranch'},$item->{'homebranch'},
1522 $item->{'price'},$item->{'replacementprice'},
1523 $item->{'itemnotes'},$item->{'loan'});
1526 if (defined $sth->errstr) {
1527 $error .= $sth->errstr;
1532 return($itemnumber,$error);
1536 my ($dbh,$item) = @_;
1537 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1538 # my $dbh=C4Connect;
1539 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1540 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1541 where itemnumber=$item->{'itemnum'}";
1542 if ($item->{'barcode'} eq ''){
1543 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1545 if ($item->{'lost'} ne ''){
1546 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1547 barcode='$item->{'barcode'}',
1548 itemnotes='$item->{'notes'}',
1549 homebranch='$item->{'homebranch'}',
1550 itemlost='$item->{'lost'}',
1551 wthdrawn='$item->{'wthdrawn'}'
1552 where itemnumber=$item->{'itemnum'}";
1554 if ($item->{'replacement'} ne ''){
1555 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1557 my $sth=$dbh->prepare($query);
1564 my ($dbh,$itemnum)=@_;
1565 # my $dbh=C4Connect;
1566 my $query="select * from items where itemnumber=$itemnum";
1567 my $sth=$dbh->prepare($query);
1569 my @data=$sth->fetchrow_array;
1571 $query="Insert into deleteditems values (";
1572 foreach my $temp (@data){
1573 $query .= "'$temp',";
1577 $sth=$dbh->prepare($query);
1580 $query = "Delete from items where itemnumber=$itemnum";
1581 $sth=$dbh->prepare($query);
1587 sub OLDdeletebiblioitem {
1588 my ($dbh,$biblioitemnumber) = @_;
1589 # my $dbh = C4Connect;
1590 my $query = "Select * from biblioitems
1591 where biblioitemnumber = $biblioitemnumber";
1592 my $sth = $dbh->prepare($query);
1597 if (@results = $sth->fetchrow_array) {
1598 $query = "Insert into deletedbiblioitems values (";
1599 foreach my $value (@results) {
1600 $value = $dbh->quote($value);
1601 $query .= "$value,";
1604 $query =~ s/\,$/\)/;
1607 $query = "Delete from biblioitems
1608 where biblioitemnumber = $biblioitemnumber";
1612 # Now delete all the items attached to the biblioitem
1613 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1614 $sth = $dbh->prepare($query);
1616 while (@results = $sth->fetchrow_array) {
1617 $query = "Insert into deleteditems values (";
1618 foreach my $value (@results) {
1619 $value = $dbh->quote($value);
1620 $query .= "$value,";
1622 $query =~ s/\,$/\)/;
1626 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1629 } # sub deletebiblioitem
1632 my ($dbh,$biblio)=@_;
1633 # my $dbh=C4Connect;
1634 my $query="select * from biblio where biblionumber=$biblio";
1635 my $sth=$dbh->prepare($query);
1637 if (my @data=$sth->fetchrow_array){
1639 $query="Insert into deletedbiblio values (";
1640 foreach my $temp (@data){
1641 $temp=~ s/\'/\\\'/g;
1642 $query .= "'$temp',";
1646 $sth=$dbh->prepare($query);
1649 $query = "Delete from biblio where biblionumber=$biblio";
1650 $sth=$dbh->prepare($query);
1666 my $dbh = C4::Context->dbh;
1667 my $query="Select count(*) from items where biblionumber=$biblio";
1669 my $sth=$dbh->prepare($query);
1671 my $data=$sth->fetchrow_hashref;
1673 return($data->{'count(*)'});
1678 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1680 Looks up the order with the given biblionumber and biblioitemnumber.
1682 Returns a two-element array. C<$ordernumber> is the order number.
1683 C<$order> is a reference-to-hash describing the order; its keys are
1684 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1685 tables of the Koha database.
1689 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1690 # Pick one and stick with it.
1693 my $dbh = C4::Context->dbh;
1694 my $query="Select ordernumber
1696 where biblionumber=? and biblioitemnumber=?";
1697 my $sth=$dbh->prepare($query);
1698 $sth->execute($bib,$bi);
1699 # FIXME - Use fetchrow_array(), since we're only interested in the one
1701 my $ordnum=$sth->fetchrow_hashref;
1703 my $order=getsingleorder($ordnum->{'ordernumber'});
1705 return ($order,$ordnum->{'ordernumber'});
1708 =item getsingleorder
1710 $order = &getsingleorder($ordernumber);
1712 Looks up an order by order number.
1714 Returns a reference-to-hash describing the order. The keys of
1715 C<$order> are fields from the biblio, biblioitems, aqorders, and
1716 aqorderbreakdown tables of the Koha database.
1720 # FIXME - This is effectively identical to
1721 # &C4::Catalogue::getsingleorder.
1722 # Pick one and stick with it.
1723 sub getsingleorder {
1725 my $dbh = C4::Context->dbh;
1726 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1727 where aqorders.ordernumber=?
1728 and biblio.biblionumber=aqorders.biblionumber and
1729 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1730 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1731 my $sth=$dbh->prepare($query);
1732 $sth->execute($ordnum);
1733 my $data=$sth->fetchrow_hashref;
1740 my $dbh = C4::Context->dbh;
1741 my $bibnum=OLDnewbiblio($dbh,$biblio);
1748 $biblionumber = &modbiblio($biblio);
1750 Update a biblio record.
1752 C<$biblio> is a reference-to-hash whose keys are the fields in the
1753 biblio table in the Koha database. All fields must be present, not
1754 just the ones you wish to change.
1756 C<&modbiblio> updates the record defined by
1757 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1759 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1766 my $dbh = C4::Context->dbh;
1767 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1768 return($biblionumber);
1774 &modsubtitle($biblionumber, $subtitle);
1776 Sets the subtitle of a book.
1778 C<$biblionumber> is the biblionumber of the book to modify.
1780 C<$subtitle> is the new subtitle.
1785 my ($bibnum, $subtitle) = @_;
1786 my $dbh = C4::Context->dbh;
1787 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1792 &modaddauthor($biblionumber, $author);
1794 Replaces all additional authors for the book with biblio number
1795 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1796 C<&modaddauthor> deletes all additional authors.
1801 my ($bibnum, $author) = @_;
1802 my $dbh = C4::Context->dbh;
1803 &OLDmodaddauthor($dbh,$bibnum,$author);
1804 } # sub modaddauthor
1808 $error = &modsubject($biblionumber, $force, @subjects);
1810 $force - a subject to force
1812 $error - Error message, or undef if successful.
1817 my ($bibnum, $force, @subject) = @_;
1818 my $dbh = C4::Context->dbh;
1819 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1824 my ($biblioitem) = @_;
1825 my $dbh = C4::Context->dbh;
1826 &OLDmodbibitem($dbh,$biblioitem);
1827 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1828 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},0,$MARCbibitem);
1832 my ($bibitemnum,$note)=@_;
1833 my $dbh = C4::Context->dbh;
1834 &OLDmodnote($dbh,$bibitemnum,$note);
1838 my ($biblioitem) = @_;
1839 my $dbh = C4::Context->dbh;
1840 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1841 # print STDERR "bibitemnum : $bibitemnum\n";
1842 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1843 # print STDERR $MARCbiblio->as_formatted();
1844 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1845 return($bibitemnum);
1850 my $dbh = C4::Context->dbh;
1851 &OLDnewsubject($dbh,$bibnum);
1855 my ($bibnum, $subtitle) = @_;
1856 my $dbh = C4::Context->dbh;
1857 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
1861 my ($item, @barcodes) = @_;
1862 my $dbh = C4::Context->dbh;
1866 foreach my $barcode (@barcodes) {
1867 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
1869 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
1870 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
1877 my $dbh = C4::Context->dbh;
1878 &OLDmoditem($dbh,$item);
1879 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
1880 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
1881 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
1885 my ($count,@barcodes)=@_;
1886 my $dbh = C4::Context->dbh;
1888 for (my $i=0;$i<$count;$i++){
1889 $barcodes[$i]=uc $barcodes[$i];
1890 my $query="Select * from items where barcode='$barcodes[$i]'";
1891 my $sth=$dbh->prepare($query);
1893 if (my $data=$sth->fetchrow_hashref){
1894 $error.=" Duplicate Barcode: $barcodes[$i]";
1902 my ($bibitemnum)=@_;
1903 my $dbh = C4::Context->dbh;
1904 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1905 my $sth=$dbh->prepare($query);
1907 my $data=$sth->fetchrow_hashref;
1909 return($data->{'count(*)'});
1914 my $dbh = C4::Context->dbh;
1915 &OLDdelitem($dbh,$itemnum);
1918 sub deletebiblioitem {
1919 my ($biblioitemnumber) = @_;
1920 my $dbh = C4::Context->dbh;
1921 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
1922 } # sub deletebiblioitem
1927 my $dbh = C4::Context->dbh;
1928 &OLDdelbiblio($dbh,$biblio);
1932 my $dbh = C4::Context->dbh;
1933 my $query = "select * from itemtypes";
1934 my $sth = $dbh->prepare($query);
1935 # || die "Cannot prepare $query" . $dbh->errstr;
1940 # || die "Cannot execute $query\n" . $sth->errstr;
1941 while (my $data = $sth->fetchrow_hashref) {
1942 $results[$count] = $data;
1947 return($count, @results);
1948 } # sub getitemtypes
1951 my ($biblionumber) = @_;
1952 my $dbh = C4::Context->dbh;
1953 my $query = "Select * from biblio where biblionumber = $biblionumber";
1954 my $sth = $dbh->prepare($query);
1955 # || die "Cannot prepare $query\n" . $dbh->errstr;
1960 # || die "Cannot execute $query\n" . $sth->errstr;
1961 while (my $data = $sth->fetchrow_hashref) {
1962 $results[$count] = $data;
1967 return($count, @results);
1971 my ($biblioitemnum) = @_;
1972 my $dbh = C4::Context->dbh;
1973 my $query = "Select * from biblioitems where
1974 biblioitemnumber = $biblioitemnum";
1975 my $sth = $dbh->prepare($query);
1981 while (my $data = $sth->fetchrow_hashref) {
1982 $results[$count] = $data;
1987 return($count, @results);
1988 } # sub getbiblioitem
1990 sub getbiblioitembybiblionumber {
1991 my ($biblionumber) = @_;
1992 my $dbh = C4::Context->dbh;
1993 my $query = "Select * from biblioitems where biblionumber =
1995 my $sth = $dbh->prepare($query);
2001 while (my $data = $sth->fetchrow_hashref) {
2002 $results[$count] = $data;
2007 return($count, @results);
2010 sub getitemsbybiblioitem {
2011 my ($biblioitemnum) = @_;
2012 my $dbh = C4::Context->dbh;
2013 my $query = "Select * from items, biblio where
2014 biblio.biblionumber = items.biblionumber and biblioitemnumber
2016 my $sth = $dbh->prepare($query);
2017 # || die "Cannot prepare $query\n" . $dbh->errstr;
2022 # || die "Cannot execute $query\n" . $sth->errstr;
2023 while (my $data = $sth->fetchrow_hashref) {
2024 $results[$count] = $data;
2029 return($count, @results);
2030 } # sub getitemsbybiblioitem
2034 my $dbh = C4::Context->dbh;
2040 $isbn = $dbh->quote($isbn);
2041 $query = "Select distinct biblio.* from biblio, biblioitems where
2042 biblio.biblionumber = biblioitems.biblionumber
2044 $sth = $dbh->prepare($query);
2047 while (my $data = $sth->fetchrow_hashref) {
2048 $results[$count] = $data;
2053 return($count, @results);
2057 # At the moment this is just a straight copy of the subject code. Needs heavy
2058 # modification to work for additional authors, obviously.
2059 # Check for additional author changes
2061 # my $newadditionalauthor='';
2062 # my $additionalauthors;
2063 # foreach $newadditionalauthor (@{$biblio->{'additionalauthor'}}) {
2064 # $additionalauthors->{$newadditionalauthor}=1;
2065 # if ($origadditionalauthors->{$newadditionalauthor}) {
2066 # $additionalauthors->{$newadditionalauthor}=2;
2068 # my $q_newadditionalauthor=$dbh->quote($newadditionalauthor);
2069 # my $sth=$dbh->prepare("insert into biblioadditionalauthors (additionalauthor,biblionumber) values ($q_newadditionalauthor, $biblionumber)");
2071 # logchange('kohadb', 'add', 'biblio', 'additionalauthor', $newadditionalauthor);
2073 # $subfields->{1}->{'Subfield_Mark'}='a';
2074 # $subfields->{1}->{'Subfield_Value'}=$newadditionalauthor;
2077 # foreach $Record_ID (@marcrecords) {
2078 # addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
2079 # logchange('marc', 'add', $Record_ID, '650', 'a', $newadditionalauthor);
2083 # my $origadditionalauthor;
2084 # foreach $origadditionalauthor (keys %$origadditionalauthors) {
2085 # if ($additionalauthors->{$origadditionalauthor} == 1) {
2086 # my $q_origadditionalauthor=$dbh->quote($origadditionalauthor);
2087 # logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'additionalauthor', $origadditionalauthor);
2088 # my $sth=$dbh->prepare("delete from biblioadditionalauthors where biblionumber=$biblionumber and additionalauthor=$q_origadditionalauthor");
2098 # Subroutine to log changes to databases
2099 # Eventually, this subroutine will be used to create a log of all changes made,
2100 # with the possibility of "undo"ing some changes
2102 if ($database eq 'kohadb') {
2108 # print STDERR "KOHA: $type $section $item $original $new\n";
2109 } elsif ($database eq 'marc') {
2111 my $Record_ID=shift;
2114 my $subfield_ID=shift;
2117 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2121 #------------------------------------------------
2124 #---------------------------------------
2125 # Find a biblio entry, or create a new one if it doesn't exist.
2126 # If a "subtitle" entry is in hash, add it to subtitle table
2127 sub getoraddbiblio {
2131 # FIXME - Unused argument
2132 $biblio, # hash ref to fields
2143 $dbh = C4::Context->dbh;
2145 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2146 $sth=$dbh->prepare("select biblionumber
2148 where title=? and author=?
2149 and copyrightdate=? and seriestitle=?");
2151 $biblio->{title}, $biblio->{author},
2152 $biblio->{copyright}, $biblio->{seriestitle} );
2154 ($biblionumber) = $sth->fetchrow;
2155 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2157 # Doesn't exist. Add new one.
2158 print "<PRE>Adding biblio</PRE>\n" if $debug;
2159 ($biblionumber,$error)=&newbiblio($biblio);
2160 if ( $biblionumber ) {
2161 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2162 if ( $biblio->{subtitle} ) {
2163 &newsubtitle($biblionumber,$biblio->{subtitle} );
2166 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2170 return $biblionumber,$error;
2172 } # sub getoraddbiblio
2174 END { } # module clean-up code here (global destructor)
2180 Koha Developement team <info@koha.org>
2182 Paul POULAIN paul.poulain@free.fr