4 # Revision 1.43 2003/04/10 13:56:02 tipaul
6 # * worked in 1.9.0, but not in 1.9.1 :
7 # - modif of a biblio didn't work
8 # - empty fields where not shown when modifying a biblio. empty fields managed by the library (ie in tab 0->9 in MARC parameter table) MUST be entered, even if not presented.
10 # * did not work before :
11 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
12 # - dropped the last subfield of the MARC form :-(
15 # - MARCmodbiblio now works by deleting and recreating the biblio. It's not perf optimized, but MARC is a "do_something_impossible_to_trace" standard, so, it's the best solution. not a problem for me, as biblio are rarely modified.
16 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
18 # Revision 1.42 2003/04/04 08:41:11 tipaul
19 # last commits before 1.9.1
21 # Revision 1.41 2003/04/01 12:26:43 tipaul
24 # Revision 1.40 2003/03/11 15:14:03 tipaul
27 # Revision 1.39 2003/03/07 16:35:42 tipaul
28 # * moving generic functions to Koha.pm
29 # * improvement of SearchMarc.pm
33 # Revision 1.38 2003/02/27 16:51:59 tipaul
34 # * moving prepare / execute to ? form.
37 # * road to 1.9.2 => acquisition and cataloguing merging
39 # Revision 1.37 2003/02/12 11:03:03 tipaul
40 # Support for 000 -> 010 fields.
41 # Those fields doesn't have subfields.
42 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
43 # Note it's only virtual : when rebuilding the MARC::Record, the koha API handle correctly "@" subfields => the resulting MARC record has a 00x field without subfield.
45 # Revision 1.36 2003/02/12 11:01:01 tipaul
46 # Support for 000 -> 010 fields.
47 # Those fields doesn't have subfields.
48 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
49 # Note it's only virtual : when rebuilding the MARC::Record, the koha API handle correctly "@" subfields => the resulting MARC record has a 00x field without subfield.
51 # Revision 1.35 2003/02/03 18:46:00 acli
52 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
53 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
54 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
55 # mandatory tag and mandatory subfields in an optional tag
57 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
58 # smaller, and to add some POD; need further testing for this
60 # Added function to check if a MARC subfield name is "koha-internal" (instead
61 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
63 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
65 # Revision 1.34 2003/01/28 14:50:04 tipaul
66 # fixing MARCmodbiblio API and reindenting code
68 # Revision 1.33 2003/01/23 12:22:37 tipaul
69 # adding char_decode to decode MARC21 or UNIMARC extended chars
71 # Revision 1.32 2002/12/16 15:08:50 tipaul
72 # small but important bugfix (fixes a problem in export)
74 # Revision 1.31 2002/12/13 16:22:04 tipaul
75 # 1st draft of marc export
77 # Revision 1.30 2002/12/12 21:26:35 tipaul
78 # YAB ! (Yet Another Bugfix) => related to biblio modif
79 # (some warning cleaning too)
81 # Revision 1.29 2002/12/12 16:35:00 tipaul
82 # adding authentification with Auth.pm and
83 # MAJOR BUGFIX on marc biblio modification
85 # Revision 1.28 2002/12/10 13:30:03 tipaul
86 # fugfixes from Dombes Abbey work
88 # Revision 1.27 2002/11/19 12:36:16 tipaul
90 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
92 # Revision 1.26 2002/11/12 15:58:43 tipaul
95 # * 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)
97 # Revision 1.25 2002/10/25 10:58:26 tipaul
99 # * bugfixes and improvements
101 # Revision 1.24 2002/10/24 12:09:01 arensb
102 # Fixed "no title" warning when generating HTML documentation from POD.
104 # Revision 1.23 2002/10/16 12:43:08 arensb
105 # Added some FIXME comments.
107 # Revision 1.22 2002/10/15 13:39:17 tipaul
108 # removing Acquisition.pm
109 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
111 # Revision 1.21 2002/10/13 11:34:14 arensb
112 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
113 # Thus, $x = $x+2 becomes $x += 2, and so forth.
115 # Revision 1.20 2002/10/13 08:28:32 arensb
116 # Deleted unused variables.
117 # Removed trailing whitespace.
119 # Revision 1.19 2002/10/13 05:56:10 arensb
120 # Added some FIXME comments.
122 # Revision 1.18 2002/10/11 12:34:53 arensb
123 # Replaced &requireDBI with C4::Context->dbh
125 # Revision 1.17 2002/10/10 14:48:25 tipaul
128 # Revision 1.16 2002/10/07 14:04:26 tipaul
129 # road to 1.3.1 : viewing MARC biblio
131 # Revision 1.15 2002/10/05 09:49:25 arensb
132 # Merged with arensb-context branch: use C4::Context->dbh instead of
133 # &C4Connect, and generally prefer C4::Context over C4::Database.
135 # Revision 1.14 2002/10/03 11:28:18 tipaul
136 # Extending Context.pm to add stopword management and using it in MARC-API.
137 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
139 # Revision 1.13 2002/10/02 16:26:44 tipaul
142 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
143 # Merged in changes from main branch.
145 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
146 # Added a whole mess of FIXME comments.
148 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
149 # Added some missing semicolons.
151 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
152 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
155 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
156 # Added a whole mess of FIXME comments.
158 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
159 # Added some missing semicolons.
161 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
162 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
165 # Revision 1.12 2002/10/01 11:48:51 arensb
166 # Added some FIXME comments, mostly marking duplicate functions.
168 # Revision 1.11 2002/09/24 13:49:26 tipaul
169 # long WAS the road to 1.3.0...
170 # coming VERY SOON NOW...
171 # modifying installer and buildrelease to update the DB
173 # Revision 1.10 2002/09/22 16:50:08 arensb
174 # Added some FIXME comments.
176 # Revision 1.9 2002/09/20 12:57:46 tipaul
177 # long is the road to 1.4.0
178 # * MARCadditem and MARCmoditem now wroks
179 # * various bugfixes in MARC management
180 # !!! 1.3.0 should be released very soon now. Be careful !!!
182 # Revision 1.8 2002/09/10 13:53:52 tipaul
183 # MARC API continued...
185 # * 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)
187 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
189 # Revision 1.7 2002/08/14 18:12:51 tonnesen
190 # Added copyright statement to all .pl and .pm files
192 # Revision 1.6 2002/07/25 13:40:31 tipaul
193 # pod documenting the API.
195 # Revision 1.5 2002/07/24 16:11:37 tipaul
197 # Database.pm and Output.pm are almost not modified (var test...)
199 # Biblio.pm is almost completly rewritten.
201 # WHAT DOES IT ??? ==> END of Hitchcock suspens
203 # 1st, it does... nothing...
204 # 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 ...
206 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
207 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
208 # * 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.
209 # * 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.
210 # 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 ;-)
212 # 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.
213 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
217 # Copyright 2000-2002 Katipo Communications
219 # This file is part of Koha.
221 # Koha is free software; you can redistribute it and/or modify it under the
222 # terms of the GNU General Public License as published by the Free Software
223 # Foundation; either version 2 of the License, or (at your option) any later
226 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
227 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
228 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
230 # You should have received a copy of the GNU General Public License along with
231 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
232 # Suite 330, Boston, MA 02111-1307 USA
240 use vars qw($VERSION @ISA @EXPORT);
242 # set the version for version checking
247 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
248 # as the old-style API and the NEW one are the only public functions.
251 &updateBiblio &updateBiblioItem &updateItem
252 &itemcount &newbiblio &newbiblioitem
253 &modnote &newsubject &newsubtitle
254 &modbiblio &checkitems
255 &newitems &modbibitem
256 &modsubtitle &modsubject &modaddauthor &moditem &countitems
257 &delitem &deletebiblioitem &delbiblio
258 &getitemtypes &getbiblio
259 &getbiblioitembybiblionumber
260 &getbiblioitem &getitemsbybiblioitem
262 &newcompletebiblioitem
264 &MARCfind_oldbiblionumber_from_MARCbibid
265 &MARCfind_MARCbibid_from_oldbiblionumber
266 &MARCfind_marc_from_kohafield
270 &NEWnewbiblio &NEWnewitem
271 &NEWmodbiblio &NEWmoditem
273 &MARCaddbiblio &MARCadditem
274 &MARCmodsubfield &MARCaddsubfield
275 &MARCmodbiblio &MARCmoditem
276 &MARCkoha2marcBiblio &MARCmarc2koha
277 &MARCkoha2marcItem &MARChtml2marc
278 &MARCgetbiblio &MARCgetitem
279 &MARCaddword &MARCdelword
285 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
288 # all the following subs takes a MARC::Record as parameter and manage
289 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
290 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
294 C4::Biblio - acquisition, catalog management functions
298 move from 1.2 to 1.4 version :
299 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
300 In the 1.4 version, we want to do 2 differents things :
301 - keep populating the old-DB, that has a LOT less datas than MARC
302 - populate the MARC-DB
303 To populate the DBs we have 2 differents sources :
304 - the standard acquisition system (through book sellers), that does'nt use MARC data
305 - the MARC acquisition system, that uses MARC data.
307 Thus, we have 2 differents cases :
308 - 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
309 - 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
311 That's why we need 4 subs :
312 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
313 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
314 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
315 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.
317 - NEW and old-style API should be used in koha to manage biblio
318 - MARCsubs are divided in 2 parts :
319 * some of them manage MARC parameters. They are heavily used in koha.
320 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
321 - OLD are used internally only
323 all subs requires/use $dbh as 1st parameter.
325 I<NEWxxx related subs>
327 all subs requires/use $dbh as 1st parameter.
328 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
330 I<OLDxxx related subs>
332 all subs requires/use $dbh as 1st parameter.
333 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
335 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
336 The OLDxxx is called by the original xxx sub.
337 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
339 WARNING : there is 1 difference between initialxxx and OLDxxx :
340 the db header $dbh is always passed as parameter to avoid over-DB connexion
346 =item @tagslib = &MARCgettagslib($dbh,1|0);
348 last param is 1 for liblibrarian and 0 for libopac
349 returns a hash with tag/subfield meaning
350 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
352 finds MARC tag and subfield for a given kohafield
353 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
355 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
357 finds a old-db biblio number for a given MARCbibid number
359 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
361 finds a MARC bibid from a old-db biblionumber
363 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
365 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
367 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
369 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
371 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
373 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
375 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
377 builds a hash with old-db datas from a MARC::Record
379 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
381 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
383 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
385 adds a subfield in a biblio (in the MARC tables only).
387 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
389 Returns a MARC::Record for the biblio $bibid.
391 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
393 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
394 if $delete == 1, every field/subfield not found is deleted in the biblio
395 otherwise, only data passed to MARCmodbiblio is managed.
396 thus, you can change only a small part of a biblio (like an item, or a subtitle, or a additionalauthor...)
398 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
400 MARCmodsubfield changes the value of a given subfield
402 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
404 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
405 Returns -1 if more than 1 answer
407 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
409 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
411 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
413 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
415 =item &MARCdelbiblio($dbh,$bibid);
417 MARCdelbiblio delete biblio $bibid
419 =item &MARCkoha2marcOnefield
421 used by MARCkoha2marc and should not be useful elsewhere
423 =item &MARCmarc2kohaOnefield
425 used by MARCmarc2koha and should not be useful elsewhere
429 used to manage MARC_word table and should not be useful elsewhere
433 used to manage MARC_word table and should not be useful elsewhere
438 my ($dbh,$forlibrarian)= @_;
440 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
441 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
443 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
444 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
445 $res->{$tag}->{lib}=$lib;
446 $res->{$tab}->{tab}=""; # XXX
447 $res->{$tag}->{mandatory}=$mandatory;
450 $sth=$dbh->prepare("select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield");
454 my $authorised_value;
455 my $thesaurus_category;
457 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder) = $sth->fetchrow) {
458 $res->{$tag}->{$subfield}->{lib}=$lib;
459 $res->{$tag}->{$subfield}->{tab}=$tab;
460 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
461 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
462 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
463 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
464 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
469 sub MARCfind_marc_from_kohafield {
470 my ($dbh,$kohafield) = @_;
471 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
472 $sth->execute($kohafield);
473 my ($tagfield,$tagsubfield) = $sth->fetchrow;
474 return ($tagfield,$tagsubfield);
477 sub MARCfind_oldbiblionumber_from_MARCbibid {
478 my ($dbh,$MARCbibid) = @_;
479 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
480 $sth->execute($MARCbibid);
481 my ($biblionumber) = $sth->fetchrow;
482 return $biblionumber;
485 sub MARCfind_MARCbibid_from_oldbiblionumber {
486 my ($dbh,$oldbiblionumber) = @_;
487 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
488 $sth->execute($oldbiblionumber);
489 my ($bibid) = $sth->fetchrow;
494 # pass the MARC::Record to this function, and it will create the records in the marc tables
495 my ($dbh,$record,$biblionumber,$bibid) = @_;
496 my @fields=$record->fields();
498 # adding main table, and retrieving bibid
499 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
500 # if bibid empty => true add, find a new bibid number
502 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
503 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
504 $sth->execute($biblionumber);
505 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
507 ($bibid)=$sth->fetchrow;
511 # now, add subfields...
512 foreach my $field (@fields) {
514 if ($field->tag() <10) {
515 &MARCaddsubfield($dbh,$bibid,
524 my @subfields=$field->subfields();
525 foreach my $subfieldcount (0..$#subfields) {
526 &MARCaddsubfield($dbh,$bibid,
528 $field->indicator(1).$field->indicator(2),
530 $subfields[$subfieldcount][0],
532 $subfields[$subfieldcount][1]
537 $dbh->do("unlock tables");
542 # pass the MARC::Record to this function, and it will create the records in the marc tables
543 my ($dbh,$record,$biblionumber) = @_;
544 # warn "adding : ".$record->as_formatted();
545 # search for MARC biblionumber
546 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
547 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
548 my @fields=$record->fields();
549 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
550 $sth->execute($bibid);
551 my ($fieldcount) = $sth->fetchrow;
552 # now, add subfields...
553 foreach my $field (@fields) {
554 my @subfields=$field->subfields();
556 foreach my $subfieldcount (0..$#subfields) {
557 &MARCaddsubfield($dbh,$bibid,
559 $field->indicator(1).$field->indicator(2),
561 $subfields[$subfieldcount][0],
563 $subfields[$subfieldcount][1]
567 $dbh->do("unlock tables");
571 sub MARCaddsubfield {
572 # Add a new subfield to a tag into the DB.
573 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
574 # if not value, end of job, we do nothing
575 if (length($subfieldvalues) ==0) {
578 if (not($subfieldcode)) {
581 my @subfieldvalues = split /\|/,$subfieldvalues;
582 foreach my $subfieldvalue (@subfieldvalues) {
583 if (length($subfieldvalue)>255) {
584 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
585 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
586 $sth->execute($subfieldvalue);
587 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
589 my ($res)=$sth->fetchrow;
590 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
591 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
593 warn "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
595 # $dbh->do("unlock tables");
597 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
598 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
600 warn "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
603 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
608 # Returns MARC::Record of the biblio passed in parameter.
610 my $record = MARC::Record->new();
611 #---- TODO : the leader is missing
612 $record->leader(' ');
613 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
614 from marc_subfield_table
615 where bibid=? order by tag,tagorder,subfieldcode
617 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
618 $sth->execute($bibid);
622 my $field; # for >=10 tags
623 my $prevvalue; # for <10 tags
624 while (my $row=$sth->fetchrow_hashref) {
625 if ($row->{'valuebloblink'}) { #---- search blob if there is one
626 $sth2->execute($row->{'valuebloblink'});
627 my $row2=$sth2->fetchrow_hashref;
629 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
631 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
634 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
636 $record->add_fields($field);
639 $prevtagorder=$row->{tagorder};
640 $prevtag = $row->{tag};
641 $previndicator=$row->{tag_indicator};
642 if ($row->{tag}<10) {
643 $prevvalue = $row->{subfieldvalue};
645 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
648 if ($row->{tag} <10) {
649 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
651 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
653 $prevtag= $row->{tag};
654 $previndicator=$row->{tag_indicator};
657 # the last has not been included inside the loop... do it now !
659 $record->add_fields($prevtag,$prevvalue);
661 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
662 $record->add_fields($field);
667 # Returns MARC::Record of the biblio passed in parameter.
668 my ($dbh,$bibid,$itemnumber)=@_;
669 my $record = MARC::Record->new();
670 # search MARC tagorder
671 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=?");
672 $sth2->execute($bibid,$itemnumber);
673 my ($tagorder) = $sth2->fetchrow_array();
674 #---- TODO : the leader is missing
675 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
676 from marc_subfield_table
677 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
679 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
680 $sth->execute($bibid,$tagorder);
681 while (my $row=$sth->fetchrow_hashref) {
682 if ($row->{'valuebloblink'}) { #---- search blob if there is one
683 $sth2->execute($row->{'valuebloblink'});
684 my $row2=$sth2->fetchrow_hashref;
686 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
688 if ($record->field($row->{'tag'})) {
690 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
691 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
692 if (length($row->{'tag'}) <3) {
693 $row->{'tag'} = "0".$row->{'tag'};
695 $field =$record->field($row->{'tag'});
697 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
698 $record->delete_field($field);
699 $record->add_fields($field);
702 if (length($row->{'tag'}) < 3) {
703 $row->{'tag'} = "0".$row->{'tag'};
705 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
706 $record->add_fields($temp);
714 my ($dbh,$bibid,$record,$delete)=@_;
715 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
716 if ($oldrecord eq $record) {
719 # 1st delete the biblio,
721 &MARCdelbiblio($dbh,$bibid,1);
722 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
723 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
727 my ($dbh,$bibid,$keep_items) = @_;
728 # if the keep_item is set to 1, then all items are preserved.
729 # This flag is set when the delbiblio is called by modbiblio
730 # due to a too complex structure of MARC (repeatable fields and subfields),
731 # the best solution for a modif is to delete / recreate the record.
732 if ($keep_items eq 1) {
733 #search item field code
734 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
736 my $itemtag = $sth->fetchrow_hashref->{tagfield};
737 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
738 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
740 $dbh->do("delete from marc_biblio where bibid=$bibid");
741 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
742 $dbh->do("delete from marc_word where bibid=$bibid");
746 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
747 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
748 # if nothing to change, don't waste time...
749 if ($oldrecord eq $record) {
752 # warn "MARCmoditem : ".$record->as_formatted;
753 # warn "OLD : ".$oldrecord->as_formatted;
755 # otherwise, skip through each subfield...
756 my @fields = $record->fields();
757 # search old MARC item
758 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=?");
759 $sth2->execute($bibid,$itemnumber);
760 my ($tagorder) = $sth2->fetchrow_array();
761 foreach my $field (@fields) {
762 my $oldfield = $oldrecord->field($field->tag());
763 my @subfields=$field->subfields();
765 foreach my $subfield (@subfields) {
767 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
768 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
769 # just adding datas...
770 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
771 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
772 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
773 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
775 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
776 # modify he subfield if it's a different string
777 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
778 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
779 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
780 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
788 sub MARCmodsubfield {
789 # Subroutine changes a subfield value given a subfieldid.
790 my ($dbh, $subfieldid, $subfieldvalue )=@_;
791 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
792 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
793 $sth1->execute($subfieldid);
794 my ($oldvaluebloblink)=$sth1->fetchrow;
797 # if too long, use a bloblink
798 if (length($subfieldvalue)>255 ) {
799 # if already a bloblink, update it, otherwise, insert a new one.
800 if ($oldvaluebloblink) {
801 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
802 $sth->execute($subfieldvalue,$oldvaluebloblink);
804 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
805 $sth->execute($subfieldvalue);
806 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
808 my ($res)=$sth->fetchrow;
809 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
810 $sth->execute($subfieldid);
813 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
814 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
815 $sth->execute($subfieldvalue, $subfieldid);
817 $dbh->do("unlock tables");
819 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
820 $sth->execute($subfieldid);
821 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
823 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
824 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
825 return($subfieldid, $subfieldvalue);
828 sub MARCfindsubfield {
829 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
833 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
834 if ($subfieldvalue) {
835 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
837 if ($subfieldorder<1) {
840 $query .= " and subfieldorder=$subfieldorder";
842 my $sti=$dbh->prepare($query);
843 $sti->execute($bibid,$tag, $subfieldcode);
844 while (($subfieldid) = $sti->fetchrow) {
846 $lastsubfieldid=$subfieldid;
848 if ($resultcounter>1) {
849 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
850 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
853 return $lastsubfieldid;
857 sub MARCfindsubfieldid {
858 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
859 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
860 where bibid=? and tag=? and tagorder=?
861 and subfieldcode=? and subfieldorder=?");
862 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
863 my ($res) = $sth->fetchrow;
865 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
866 where bibid=? and tag=? and tagorder=?
867 and subfieldcode=?");
868 $sth->execute($bibid,$tag,$tagorder,$subfield);
869 ($res) = $sth->fetchrow;
874 sub MARCdelsubfield {
875 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
876 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
877 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
878 tag='$tag' and tagorder='$tagorder'
879 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
883 sub MARCkoha2marcBiblio {
884 # this function builds partial MARC::Record from the old koha-DB fields
885 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
886 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
887 my $record = MARC::Record->new();
888 #--- if bibid, then retrieve old-style koha data
889 if ($biblionumber>0) {
890 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
891 from biblio where biblionumber=?");
892 $sth2->execute($biblionumber);
893 my $row=$sth2->fetchrow_hashref;
895 foreach $code (keys %$row) {
897 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
901 #--- if biblioitem, then retrieve old-style koha data
902 if ($biblioitemnumber>0) {
903 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
904 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
905 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
907 WHERE biblionumber=? and biblioitemnumber=?
909 $sth2->execute($biblionumber,$biblioitemnumber);
910 my $row=$sth2->fetchrow_hashref;
912 foreach $code (keys %$row) {
914 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
919 # TODO : retrieve notes, additionalauthors
922 sub MARCkoha2marcItem {
923 # this function builds partial MARC::Record from the old koha-DB fields
924 my ($dbh,$biblionumber,$itemnumber) = @_;
925 # my $dbh=&C4Connect;
926 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
927 my $record = MARC::Record->new();
928 #--- if item, then retrieve old-style koha data
930 # print STDERR "prepare $biblionumber,$itemnumber\n";
931 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
932 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
933 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
934 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
936 WHERE itemnumber=?");
937 $sth2->execute($itemnumber);
938 my $row=$sth2->fetchrow_hashref;
940 foreach $code (keys %$row) {
942 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
947 # TODO : retrieve notes, additionalauthors
950 sub MARCkoha2marcSubtitle {
951 # this function builds partial MARC::Record from the old koha-DB fields
952 my ($dbh,$bibnum,$subtitle) = @_;
953 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
954 my $record = MARC::Record->new();
955 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
959 sub MARCkoha2marcOnefield {
960 my ($sth,$record,$kohafieldname,$value)=@_;
963 $sth->execute($kohafieldname);
964 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
965 if ($record->field($tagfield)) {
966 my $tag =$record->field($tagfield);
968 $tag->add_subfields($tagsubfield,$value);
969 $record->delete_field($tag);
970 $record->add_fields($tag);
973 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
980 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
982 my $record = MARC::Record->new();
983 # my %subfieldlist=();
984 my $prevvalue; # if tag <10
985 my $field; # if tag >=10
986 for (my $i=0; $i< @$rtags; $i++) {
987 # rebuild MARC::Record
988 if (@$rtags[$i] ne $prevtag) {
991 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
995 $record->add_fields($field);
998 $indicators{@$rtags[$i]}.=' ';
999 if (@$rtags[$i] <10) {
1000 $prevvalue= @$rvalues[$i];
1002 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1004 $prevtag = @$rtags[$i];
1006 if (@$rtags[$i] <10) {
1007 $prevvalue=@$rvalues[$i];
1009 if (@$rvalues[$i]) {
1010 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1013 $prevtag= @$rtags[$i];
1016 # the last has not been included inside the loop... do it now !
1017 $record->add_fields($field);
1018 warn $record->as_formatted;
1023 my ($dbh,$record) = @_;
1024 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1026 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1029 # print STDERR $record->as_formatted;
1030 while (($field)=$sth2->fetchrow) {
1031 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1033 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1035 while (($field)=$sth2->fetchrow) {
1036 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1038 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1040 while (($field)=$sth2->fetchrow) {
1041 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1043 # additional authors : specific
1044 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1048 sub MARCmarc2kohaOneField {
1049 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1050 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1051 # warn "kohatable / $kohafield / $result / ";
1055 $sth->execute($kohatable.".".$kohafield);
1056 ($tagfield,$subfield) = $sth->fetchrow;
1057 foreach my $field ($record->field($tagfield)) {
1058 if ($field->subfield($subfield)) {
1059 if ($result->{$kohafield}) {
1060 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1062 $result->{$kohafield}=$field->subfield($subfield);
1070 # split a subfield string and adds it into the word table.
1072 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1073 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1074 my @words = split / /,$sentence;
1075 my $stopwords= C4::Context->stopwords;
1076 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1077 values (?,?,?,?,?,?,soundex(?))");
1078 foreach my $word (@words) {
1079 # we record only words longer than 2 car and not in stopwords hash
1080 if (length($word)>1 and !($stopwords->{uc($word)})) {
1081 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1083 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1090 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1091 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1092 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1093 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1098 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1101 # all the following subs are useful to manage MARC-DB with complete MARC records.
1102 # it's used with marcimport, and marc management tools
1106 =item (oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1108 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
1109 are builded from the MARC::Record. If they are passed, they are used.
1111 =item NEWnewitem($dbh, $record,$bibid);
1113 adds an item in the db.
1118 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1119 # note $oldbiblio and $oldbiblioitem are not mandatory.
1120 # if not present, they will be builded from $record with MARCmarc2koha function
1121 if (($oldbiblio) and not($oldbiblioitem)) {
1122 print STDERR "NEWnewbiblio : missing parameter\n";
1123 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1129 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1130 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1131 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1133 my $olddata = MARCmarc2koha($dbh,$record);
1134 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1135 $olddata->{'biblionumber'} = $oldbibnum;
1136 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1138 # we must add bibnum and bibitemnum in MARC::Record...
1139 # we build the new field with biblionumber and biblioitemnumber
1140 # we drop the original field
1141 # we add the new builded field.
1142 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1143 # (steve and paul : thinks 090 is a good choice)
1144 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1145 $sth->execute("biblio.biblionumber");
1146 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1147 $sth->execute("biblioitems.biblioitemnumber");
1148 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1149 if ($tagfield1 != $tagfield2) {
1150 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1151 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1154 my $newfield = MARC::Field->new( $tagfield1,'','',
1155 "$tagsubfield1" => $oldbibnum,
1156 "$tagsubfield2" => $oldbibitemnum);
1157 # drop old field and create new one...
1158 my $old_field = $record->field($tagfield1);
1159 $record->delete_field($old_field);
1160 $record->add_fields($newfield);
1161 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1162 return ($bibid,$oldbibnum,$oldbibitemnum );
1166 my ($dbh,$record,$bibid) =@_;
1167 &MARCmodbiblio($dbh,$bibid,$record,0);
1168 my $oldbiblio = MARCmarc2koha($dbh,$record);
1169 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1170 OLDmodbibitem($dbh,$oldbiblio);
1176 my ($dbh, $record,$bibid) = @_;
1177 # add item in old-DB
1178 my $item = &MARCmarc2koha($dbh,$record);
1179 # needs old biblionumber and biblioitemnumber
1180 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1181 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1182 $sth->execute($item->{'biblionumber'});
1183 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1184 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1185 # add itemnumber to MARC::Record before adding the item.
1186 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1187 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1189 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1193 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1194 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1195 my $olditem = MARCmarc2koha($dbh,$record);
1196 OLDmoditem($dbh,$olditem);
1201 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1205 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1207 adds a record in biblio table. Datas are in the hash $biblio.
1209 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1211 modify a record in biblio table. Datas are in the hash $biblio.
1213 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1215 modify subtitles in bibliosubtitle table.
1217 =item OLDmodaddauthor($dbh,$bibnum,$author);
1219 adds or modify additional authors
1220 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1222 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1224 modify/adds subjects
1226 =item OLDmodbibitem($dbh, $biblioitem);
1230 =item OLDmodnote($dbh,$bibitemnum,$note
1232 modify a note for a biblioitem
1234 =item OLDnewbiblioitem($dbh,$biblioitem);
1236 adds a biblioitem ($biblioitem is a hash with the values)
1238 =item OLDnewsubject($dbh,$bibnum);
1242 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1244 create a new subtitle
1246 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1248 create a item. $item is a hash and $barcode the barcode.
1250 =item OLDmoditem($dbh,$item);
1254 =item OLDdelitem($dbh,$itemnum);
1258 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1260 deletes a biblioitem
1261 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1263 =item OLDdelbiblio($dbh,$biblio);
1270 my ($dbh,$biblio) = @_;
1271 # my $dbh = &C4Connect;
1272 my $query = "Select max(biblionumber) from biblio";
1273 my $sth = $dbh->prepare($query);
1275 my $data = $sth->fetchrow_arrayref;
1276 my $bibnum = $$data[0] + 1;
1279 if ($biblio->{'seriestitle'}) { $series = 1 };
1281 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1282 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1283 $sth = $dbh->prepare($query);
1284 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1292 my ($dbh,$biblio) = @_;
1293 # my $dbh = C4Connect;
1297 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1298 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1299 $sth = $dbh->prepare($query);
1300 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1301 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1304 return($biblio->{'biblionumber'});
1307 sub OLDmodsubtitle {
1308 my ($dbh,$bibnum, $subtitle) = @_;
1309 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1310 my $sth = $dbh->prepare($query);
1311 $sth->execute($subtitle,$bibnum);
1316 sub OLDmodaddauthor {
1317 my ($dbh,$bibnum, $author) = @_;
1318 # my $dbh = C4Connect;
1319 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1320 my $sth = $dbh->prepare($query);
1325 if ($author ne '') {
1326 $query = "Insert into additionalauthors set
1329 $sth = $dbh->prepare($query);
1331 $sth->execute($author,$bibnum);
1335 } # sub modaddauthor
1339 my ($dbh,$bibnum, $force, @subject) = @_;
1340 # my $dbh = C4Connect;
1341 my $count = @subject;
1343 for (my $i = 0; $i < $count; $i++) {
1344 $subject[$i] =~ s/^ //g;
1345 $subject[$i] =~ s/ $//g;
1346 my $query = "select * from catalogueentry
1347 where entrytype = 's'
1348 and catalogueentry = '$subject[$i]'";
1349 my $sth = $dbh->prepare($query);
1352 if (my $data = $sth->fetchrow_hashref) {
1354 if ($force eq $subject[$i]) {
1355 # subject not in aut, chosen to force anway
1356 # so insert into cataloguentry so its in auth file
1357 $query = "Insert into catalogueentry
1358 (entrytype,catalogueentry)
1359 values ('s','$subject[$i]')";
1360 my $sth2 = $dbh->prepare($query);
1365 $error = "$subject[$i]\n does not exist in the subject authority file";
1366 $query = "Select * from catalogueentry
1367 where entrytype = 's'
1368 and (catalogueentry like '$subject[$i] %'
1369 or catalogueentry like '% $subject[$i] %'
1370 or catalogueentry like '% $subject[$i]')";
1371 my $sth2 = $dbh->prepare($query);
1374 while (my $data = $sth2->fetchrow_hashref) {
1375 $error .= "<br>$data->{'catalogueentry'}";
1383 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1384 my $sth = $dbh->prepare($query);
1387 for (my $i = 0; $i < $count; $i++) {
1388 $sth = $dbh->prepare("Insert into bibliosubject
1389 values ('$subject[$i]', $bibnum)");
1401 my ($dbh,$biblioitem) = @_;
1402 # my $dbh = C4Connect;
1405 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1406 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1407 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1408 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1409 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1410 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1411 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1412 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1413 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1414 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1415 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1416 $biblioitem->{'notes'} = $dbh->quote($biblioitem->{'notes'});
1417 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1418 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1420 $query = "Update biblioitems set
1421 itemtype = $biblioitem->{'itemtype'},
1422 url = $biblioitem->{'url'},
1423 isbn = $biblioitem->{'isbn'},
1424 publishercode = $biblioitem->{'publishercode'},
1425 publicationyear = $biblioitem->{'publicationyear'},
1426 classification = $biblioitem->{'classification'},
1427 dewey = $biblioitem->{'dewey'},
1428 subclass = $biblioitem->{'subclass'},
1429 illus = $biblioitem->{'illus'},
1430 pages = $biblioitem->{'pages'},
1431 volumeddesc = $biblioitem->{'volumeddesc'},
1432 notes = $biblioitem->{'notes'},
1433 size = $biblioitem->{'size'},
1434 place = $biblioitem->{'place'}
1435 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1443 my ($dbh,$bibitemnum,$note)=@_;
1444 # my $dbh=C4Connect;
1445 my $query="update biblioitems set notes='$note' where
1446 biblioitemnumber='$bibitemnum'";
1447 my $sth=$dbh->prepare($query);
1453 sub OLDnewbiblioitem {
1454 my ($dbh,$biblioitem) = @_;
1455 # my $dbh = C4Connect;
1456 my $query = "Select max(biblioitemnumber) from biblioitems";
1457 my $sth = $dbh->prepare($query);
1462 $data = $sth->fetchrow_arrayref;
1463 $bibitemnum = $$data[0] + 1;
1467 $sth = $dbh->prepare("insert into biblioitems set
1468 biblioitemnumber = ?, biblionumber = ?,
1469 volume = ?, number = ?,
1470 classification = ?, itemtype = ?,
1472 issn = ?, dewey = ?,
1473 subclass = ?, publicationyear = ?,
1474 publishercode = ?, volumedate = ?,
1475 volumeddesc = ?, illus = ?,
1476 pages = ?, notes = ?,
1478 marc = ?, place = ?");
1479 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1480 $biblioitem->{'volume'}, $biblioitem->{'number'},
1481 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1482 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1483 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1484 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1485 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1486 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1487 $biblioitem->{'pages'}, $biblioitem->{'notes'},
1488 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1489 $biblioitem->{'marc'}, $biblioitem->{'place'});
1492 return($bibitemnum);
1496 my ($dbh,$bibnum)=@_;
1497 # my $dbh=C4Connect;
1498 my $query="insert into bibliosubject (biblionumber) values
1500 my $sth=$dbh->prepare($query);
1507 sub OLDnewsubtitle {
1508 my ($dbh,$bibnum, $subtitle) = @_;
1509 # my $dbh = C4Connect;
1510 my $query = "insert into bibliosubtitle set
1513 my $sth = $dbh->prepare($query);
1515 $sth->execute($bibnum,$subtitle);
1523 my ($dbh,$item, $barcode) = @_;
1524 # my $dbh = C4Connect;
1525 my $query = "Select max(itemnumber) from items";
1526 my $sth = $dbh->prepare($query);
1532 $data = $sth->fetchrow_hashref;
1533 $itemnumber = $data->{'max(itemnumber)'} + 1;
1536 $sth=$dbh->prepare("Insert into items set
1537 itemnumber = ?, biblionumber = ?,
1538 biblioitemnumber = ?, barcode = ?,
1539 booksellerid = ?, dateaccessioned = NOW(),
1540 homebranch = ?, holdingbranch = ?,
1541 price = ?, replacementprice = ?,
1542 replacementpricedate = NOW(), itemnotes = ?,
1545 $sth->execute($itemnumber, $item->{'biblionumber'},
1546 $item->{'biblioitemnumber'},$barcode,
1547 $item->{'booksellerid'},
1548 $item->{'homebranch'},$item->{'homebranch'},
1549 $item->{'price'},$item->{'replacementprice'},
1550 $item->{'itemnotes'},$item->{'loan'});
1551 if (defined $sth->errstr) {
1552 $error .= $sth->errstr;
1555 return($itemnumber,$error);
1559 my ($dbh,$item) = @_;
1560 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1561 # my $dbh=C4Connect;
1562 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1563 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1564 where itemnumber=$item->{'itemnum'}";
1565 if ($item->{'barcode'} eq ''){
1566 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1568 if ($item->{'lost'} ne ''){
1569 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1570 barcode='$item->{'barcode'}',
1571 itemnotes='$item->{'notes'}',
1572 homebranch='$item->{'homebranch'}',
1573 itemlost='$item->{'lost'}',
1574 wthdrawn='$item->{'wthdrawn'}'
1575 where itemnumber=$item->{'itemnum'}";
1577 if ($item->{'replacement'} ne ''){
1578 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1580 my $sth=$dbh->prepare($query);
1587 my ($dbh,$itemnum)=@_;
1588 # my $dbh=C4Connect;
1589 my $query="select * from items where itemnumber=$itemnum";
1590 my $sth=$dbh->prepare($query);
1592 my @data=$sth->fetchrow_array;
1594 $query="Insert into deleteditems values (";
1595 foreach my $temp (@data){
1596 $query .= "'$temp',";
1600 $sth=$dbh->prepare($query);
1603 $query = "Delete from items where itemnumber=$itemnum";
1604 $sth=$dbh->prepare($query);
1610 sub OLDdeletebiblioitem {
1611 my ($dbh,$biblioitemnumber) = @_;
1612 # my $dbh = C4Connect;
1613 my $query = "Select * from biblioitems
1614 where biblioitemnumber = $biblioitemnumber";
1615 my $sth = $dbh->prepare($query);
1620 if (@results = $sth->fetchrow_array) {
1621 $query = "Insert into deletedbiblioitems values (";
1622 foreach my $value (@results) {
1623 $value = $dbh->quote($value);
1624 $query .= "$value,";
1627 $query =~ s/\,$/\)/;
1630 $query = "Delete from biblioitems
1631 where biblioitemnumber = $biblioitemnumber";
1635 # Now delete all the items attached to the biblioitem
1636 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1637 $sth = $dbh->prepare($query);
1639 while (@results = $sth->fetchrow_array) {
1640 $query = "Insert into deleteditems values (";
1641 foreach my $value (@results) {
1642 $value = $dbh->quote($value);
1643 $query .= "$value,";
1645 $query =~ s/\,$/\)/;
1649 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1652 } # sub deletebiblioitem
1655 my ($dbh,$biblio)=@_;
1656 # my $dbh=C4Connect;
1657 my $query="select * from biblio where biblionumber=$biblio";
1658 my $sth=$dbh->prepare($query);
1660 if (my @data=$sth->fetchrow_array){
1662 $query="Insert into deletedbiblio values (";
1663 foreach my $temp (@data){
1664 $temp=~ s/\'/\\\'/g;
1665 $query .= "'$temp',";
1669 $sth=$dbh->prepare($query);
1672 $query = "Delete from biblio where biblionumber=$biblio";
1673 $sth=$dbh->prepare($query);
1689 my $dbh = C4::Context->dbh;
1690 my $query="Select count(*) from items where biblionumber=$biblio";
1692 my $sth=$dbh->prepare($query);
1694 my $data=$sth->fetchrow_hashref;
1696 return($data->{'count(*)'});
1701 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1703 Looks up the order with the given biblionumber and biblioitemnumber.
1705 Returns a two-element array. C<$ordernumber> is the order number.
1706 C<$order> is a reference-to-hash describing the order; its keys are
1707 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1708 tables of the Koha database.
1712 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1713 # Pick one and stick with it.
1716 my $dbh = C4::Context->dbh;
1717 my $query="Select ordernumber
1719 where biblionumber=? and biblioitemnumber=?";
1720 my $sth=$dbh->prepare($query);
1721 $sth->execute($bib,$bi);
1722 # FIXME - Use fetchrow_array(), since we're only interested in the one
1724 my $ordnum=$sth->fetchrow_hashref;
1726 my $order=getsingleorder($ordnum->{'ordernumber'});
1728 return ($order,$ordnum->{'ordernumber'});
1731 =item getsingleorder
1733 $order = &getsingleorder($ordernumber);
1735 Looks up an order by order number.
1737 Returns a reference-to-hash describing the order. The keys of
1738 C<$order> are fields from the biblio, biblioitems, aqorders, and
1739 aqorderbreakdown tables of the Koha database.
1743 # FIXME - This is effectively identical to
1744 # &C4::Catalogue::getsingleorder.
1745 # Pick one and stick with it.
1746 sub getsingleorder {
1748 my $dbh = C4::Context->dbh;
1749 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1750 where aqorders.ordernumber=?
1751 and biblio.biblionumber=aqorders.biblionumber and
1752 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1753 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1754 my $sth=$dbh->prepare($query);
1755 $sth->execute($ordnum);
1756 my $data=$sth->fetchrow_hashref;
1763 my $dbh = C4::Context->dbh;
1764 my $bibnum=OLDnewbiblio($dbh,$biblio);
1771 $biblionumber = &modbiblio($biblio);
1773 Update a biblio record.
1775 C<$biblio> is a reference-to-hash whose keys are the fields in the
1776 biblio table in the Koha database. All fields must be present, not
1777 just the ones you wish to change.
1779 C<&modbiblio> updates the record defined by
1780 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1782 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1789 my $dbh = C4::Context->dbh;
1790 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1791 return($biblionumber);
1797 &modsubtitle($biblionumber, $subtitle);
1799 Sets the subtitle of a book.
1801 C<$biblionumber> is the biblionumber of the book to modify.
1803 C<$subtitle> is the new subtitle.
1808 my ($bibnum, $subtitle) = @_;
1809 my $dbh = C4::Context->dbh;
1810 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1815 &modaddauthor($biblionumber, $author);
1817 Replaces all additional authors for the book with biblio number
1818 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1819 C<&modaddauthor> deletes all additional authors.
1824 my ($bibnum, $author) = @_;
1825 my $dbh = C4::Context->dbh;
1826 &OLDmodaddauthor($dbh,$bibnum,$author);
1827 } # sub modaddauthor
1831 $error = &modsubject($biblionumber, $force, @subjects);
1833 $force - a subject to force
1835 $error - Error message, or undef if successful.
1840 my ($bibnum, $force, @subject) = @_;
1841 my $dbh = C4::Context->dbh;
1842 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1847 my ($biblioitem) = @_;
1848 my $dbh = C4::Context->dbh;
1849 &OLDmodbibitem($dbh,$biblioitem);
1850 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1851 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1855 my ($bibitemnum,$note)=@_;
1856 my $dbh = C4::Context->dbh;
1857 &OLDmodnote($dbh,$bibitemnum,$note);
1861 my ($biblioitem) = @_;
1862 my $dbh = C4::Context->dbh;
1863 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1864 # print STDERR "bibitemnum : $bibitemnum\n";
1865 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1866 # print STDERR $MARCbiblio->as_formatted();
1867 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1868 return($bibitemnum);
1873 my $dbh = C4::Context->dbh;
1874 &OLDnewsubject($dbh,$bibnum);
1878 my ($bibnum, $subtitle) = @_;
1879 my $dbh = C4::Context->dbh;
1880 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
1884 my ($item, @barcodes) = @_;
1885 my $dbh = C4::Context->dbh;
1889 foreach my $barcode (@barcodes) {
1890 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
1892 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
1893 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
1900 my $dbh = C4::Context->dbh;
1901 &OLDmoditem($dbh,$item);
1902 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
1903 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
1904 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
1908 my ($count,@barcodes)=@_;
1909 my $dbh = C4::Context->dbh;
1911 for (my $i=0;$i<$count;$i++){
1912 $barcodes[$i]=uc $barcodes[$i];
1913 my $query="Select * from items where barcode='$barcodes[$i]'";
1914 my $sth=$dbh->prepare($query);
1916 if (my $data=$sth->fetchrow_hashref){
1917 $error.=" Duplicate Barcode: $barcodes[$i]";
1925 my ($bibitemnum)=@_;
1926 my $dbh = C4::Context->dbh;
1927 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1928 my $sth=$dbh->prepare($query);
1930 my $data=$sth->fetchrow_hashref;
1932 return($data->{'count(*)'});
1937 my $dbh = C4::Context->dbh;
1938 &OLDdelitem($dbh,$itemnum);
1941 sub deletebiblioitem {
1942 my ($biblioitemnumber) = @_;
1943 my $dbh = C4::Context->dbh;
1944 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
1945 } # sub deletebiblioitem
1950 my $dbh = C4::Context->dbh;
1951 &OLDdelbiblio($dbh,$biblio);
1955 my $dbh = C4::Context->dbh;
1956 my $query = "select * from itemtypes";
1957 my $sth = $dbh->prepare($query);
1958 # || die "Cannot prepare $query" . $dbh->errstr;
1963 # || die "Cannot execute $query\n" . $sth->errstr;
1964 while (my $data = $sth->fetchrow_hashref) {
1965 $results[$count] = $data;
1970 return($count, @results);
1971 } # sub getitemtypes
1974 my ($biblionumber) = @_;
1975 my $dbh = C4::Context->dbh;
1976 my $query = "Select * from biblio where biblionumber = $biblionumber";
1977 my $sth = $dbh->prepare($query);
1978 # || die "Cannot prepare $query\n" . $dbh->errstr;
1983 # || die "Cannot execute $query\n" . $sth->errstr;
1984 while (my $data = $sth->fetchrow_hashref) {
1985 $results[$count] = $data;
1990 return($count, @results);
1994 my ($biblioitemnum) = @_;
1995 my $dbh = C4::Context->dbh;
1996 my $query = "Select * from biblioitems where
1997 biblioitemnumber = $biblioitemnum";
1998 my $sth = $dbh->prepare($query);
2004 while (my $data = $sth->fetchrow_hashref) {
2005 $results[$count] = $data;
2010 return($count, @results);
2011 } # sub getbiblioitem
2013 sub getbiblioitembybiblionumber {
2014 my ($biblionumber) = @_;
2015 my $dbh = C4::Context->dbh;
2016 my $query = "Select * from biblioitems where biblionumber =
2018 my $sth = $dbh->prepare($query);
2024 while (my $data = $sth->fetchrow_hashref) {
2025 $results[$count] = $data;
2030 return($count, @results);
2033 sub getitemsbybiblioitem {
2034 my ($biblioitemnum) = @_;
2035 my $dbh = C4::Context->dbh;
2036 my $query = "Select * from items, biblio where
2037 biblio.biblionumber = items.biblionumber and biblioitemnumber
2039 my $sth = $dbh->prepare($query);
2040 # || die "Cannot prepare $query\n" . $dbh->errstr;
2045 # || die "Cannot execute $query\n" . $sth->errstr;
2046 while (my $data = $sth->fetchrow_hashref) {
2047 $results[$count] = $data;
2052 return($count, @results);
2053 } # sub getitemsbybiblioitem
2057 # Subroutine to log changes to databases
2058 # Eventually, this subroutine will be used to create a log of all changes made,
2059 # with the possibility of "undo"ing some changes
2061 if ($database eq 'kohadb') {
2067 # print STDERR "KOHA: $type $section $item $original $new\n";
2068 } elsif ($database eq 'marc') {
2070 my $Record_ID=shift;
2073 my $subfield_ID=shift;
2076 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2080 #------------------------------------------------
2083 #---------------------------------------
2084 # Find a biblio entry, or create a new one if it doesn't exist.
2085 # If a "subtitle" entry is in hash, add it to subtitle table
2086 sub getoraddbiblio {
2090 # FIXME - Unused argument
2091 $biblio, # hash ref to fields
2102 $dbh = C4::Context->dbh;
2104 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2105 $sth=$dbh->prepare("select biblionumber
2107 where title=? and author=?
2108 and copyrightdate=? and seriestitle=?");
2110 $biblio->{title}, $biblio->{author},
2111 $biblio->{copyright}, $biblio->{seriestitle} );
2113 ($biblionumber) = $sth->fetchrow;
2114 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2116 # Doesn't exist. Add new one.
2117 print "<PRE>Adding biblio</PRE>\n" if $debug;
2118 ($biblionumber,$error)=&newbiblio($biblio);
2119 if ( $biblionumber ) {
2120 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2121 if ( $biblio->{subtitle} ) {
2122 &newsubtitle($biblionumber,$biblio->{subtitle} );
2125 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2129 return $biblionumber,$error;
2131 } # sub getoraddbiblio
2134 # converts ISO 5426 coded string to ISO 8859-1
2135 # sloppy code : should be improved in next issue
2138 if (C4::Context->preference("marcflavour") eq "UNIMARC") {
2255 # this handles non-sorting blocks (if implementation requires this)
2256 $string = nsb_clean($_) ;
2261 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2262 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2263 # handles non sorting blocks
2267 s/[ ]{0,1}$NSE/) /gm ;
2272 END { } # module clean-up code here (global destructor)
2278 Koha Developement team <info@koha.org>
2280 Paul POULAIN paul.poulain@free.fr