Added a param to catalogsearch to allow selection of order (ASC or DESC)
[koha.git] / C4 / SearchMarc.pm
1 package C4::SearchMarc;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 require Exporter;
22 use DBI;
23 use C4::Context;
24 use C4::Biblio;
25 use C4::Date;
26 use Date::Manip;
27
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29
30 # set the version for version checking
31 $VERSION = 0.02;
32
33 =head1 NAME
34
35 C4::Search - Functions for searching the Koha MARC catalog
36
37 =head1 FUNCTIONS
38
39 This module provides the searching facilities for the Koha MARC catalog
40
41 =cut
42
43 @ISA = qw(Exporter);
44 @EXPORT = qw(&catalogsearch &findseealso &findsuggestion &getMARCnotes &getMARCsubjects);
45
46 =head1 findsuggestion($dbh,$values);
47
48 =head2 $dbh is a link to the DB handler.
49
50 use C4::Context;
51 my $dbh =C4::Context->dbh;
52
53 =head2 $values is a word
54
55 Searches words with the same soundex, ordered by frequency of use.
56 Useful to suggest other searches to the users.
57
58 =cut
59
60 sub findsuggestion {
61         my ($dbh,$values) = @_;
62         my $sth = $dbh->prepare("SELECT count( * ) AS total, word FROM marc_word WHERE sndx_word = soundex( ? ) AND word <> ? GROUP BY word ORDER BY total DESC");
63         my @results;
64         for(my $i = 0 ; $i <= $#{$values} ; $i++) {
65                 if (length(@$values[$i]) >=5) {
66                         $sth->execute(@$values[$i],@$values[$i]);
67                         my $resfound = 1;
68                         my @resline;
69                         while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
70                                 push @results, "@$values[$i]|$word|$count";
71 #                               $results{@$values[$i]} = \@resline;
72                                 $resfound++;
73                         }
74                 }
75         }
76         return \@results;
77 }
78
79 =head1 findseealso($dbh,$fields);
80
81 =head2 $dbh is a link to the DB handler.
82
83 use C4::Context;
84 my $dbh =C4::Context->dbh;
85
86 =head2 $fields is a reference to the fields array
87
88 This function modify the @$fields array and add related fields to search on.
89
90 =cut
91
92 sub findseealso {
93         my ($dbh, $fields) = @_;
94         my $tagslib = MARCgettagslib ($dbh,1);
95         for (my $i=0;$i<=$#{$fields};$i++) {
96                 my ($tag) =substr(@$fields[$i],1,3);
97                 my ($subfield) =substr(@$fields[$i],4,1);
98                 @$fields[$i].=','.$tagslib->{$tag}->{$subfield}->{seealso} if ($tagslib->{$tag}->{$subfield}->{seealso});
99         }
100 }
101
102 =head1  my ($count, @results) = catalogsearch($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby);
103
104 =head2 $dbh is a link to the DB handler.
105
106 use C4::Context;
107 my $dbh =C4::Context->dbh;
108
109 $tags,$and_or, $excluding, $operator, $value are references to array
110
111 =head2 $tags
112
113 contains the list of tags+subfields (for example : $@tags[0] = '200a')
114 A field can be a list of fields : '200f','700a','700b','701a','701b'
115
116 Example
117
118 =head2 $and_or
119
120 contains  a list of strings containing and or or. The 1st value is useless.
121
122 =head2 $excluding
123
124 contains 0 or 1. If 1, then the request is negated.
125
126 =head2 $operator
127
128 contains contains,=,start,>,>=,<,<= the = and start work on the complete subfield. The contains operator works on every word in the subfield.
129
130 examples :
131 contains home, search home anywhere.
132 = home, search a string being home.
133
134 =head2 $value
135
136 contains the value to search
137 If it contains a * or a %, then the search is partial.
138
139 =head2 $offset and $length
140
141 returns $length results, beginning at $offset
142
143 =head2 $orderby
144
145 define the field used to order the request. Any field in the biblio/biblioitem tables can be used. DESC is possible too
146
147 (for example title, title DESC,...)
148
149 =head2 RETURNS
150
151 returns an array containing hashes. The hash contains all biblio & biblioitems fields and a reference to an item hash. The "item hash contains one line for each callnumber & the number of items related to the callnumber.
152
153 =cut
154
155 =head2 my $marcnotesarray = &getMARCnotes($dbh,$bibid,$marcflavour);
156
157 Returns a reference to an array containing all the notes stored in the MARC database for the given bibid.
158 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
159
160 =head2 my $marcsubjctsarray = &getMARCsubjects($dbh,$bibid,$marcflavour);
161
162 Returns a reference to an array containing all the subjects stored in the MARC database for the given bibid.
163 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
164
165 =cut
166
167 sub catalogsearch {
168         my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby, $desc_or_asc) = @_;
169         # build the sql request. She will look like :
170         # select m1.bibid
171         #               from marc_subfield_table as m1, marc_subfield_table as m2
172         #               where m1.bibid=m2.bibid and
173         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
174
175         # last minute stripping out of stuff
176         # doesn't work @$value =~ s/\'/ /;
177         # @$value = map { $_ =~ s/\'/ /g } @$value;
178         
179         # "Normal" statements
180         my @normal_tags = ();
181         my @normal_and_or = ();
182         my @normal_operator = ();
183         my @normal_value = ();
184         # Extracts the NOT statements from the list of statements
185         my @not_tags = ();
186         my @not_and_or = ();
187         my @not_operator = ();
188         my @not_value = ();
189         my $any_not = 0;
190         $orderby = "biblio.title" unless $orderby;
191         $desc_or_asc = "ASC" unless $desc_or_asc;
192         #last minute stripping out of ' and ,
193 # paul : quoting, it's done a few lines lated.
194 #       foreach $_ (@$value) {
195 #               $_=~ s/\'/ /g;
196 #               $_=~ s/\,/ /g;
197 #       }
198
199 # the item.notforloan contains an integer. Every value <>0 means "book unavailable for loan".
200 # but each library can have it's own table of meaning for each value. Get them
201 # 1st search if there is a list of authorised values connected to items.notforloan
202         my $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield="items.notforloan"');
203         $sth->execute;
204         my %notforloanstatus;
205         my ($authorised_valuecode) = $sth->fetchrow;
206         if ($authorised_valuecode) {
207                 $sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=?");
208                 $sth->execute($authorised_valuecode);
209                 while (my ($authorised_value,$lib) = $sth->fetchrow) {
210                         $notforloanstatus{$authorised_value} = $lib?$lib:$authorised_value;
211                 }
212         }
213         for(my $i = 0 ; $i <= $#{$value} ; $i++)
214         {
215                 # replace * by %
216                 @$value[$i] =~ s/\*/%/g;
217                 # remove % at the beginning
218                 @$value[$i] =~ s/^%//g;
219             @$value[$i] =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g if @$operator[$i] eq "contains";
220                 if(@$excluding[$i])     # NOT statements
221                 {
222                         $any_not = 1;
223                         if(@$operator[$i] eq "contains")
224                         {
225                                 foreach my $word (split(/ /, @$value[$i]))      # if operator is contains, splits the words in separate requests
226                                 {
227                                         # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
228 #                                       warn "word : $word";
229                                         $word =~ s/%//g unless length($word)>4;
230                                         unless (C4::Context->stopwords->{uc($word)} or length($word)==1) {      #it's NOT a stopword => use it. Otherwise, ignore
231                                                 push @not_tags, @$tags[$i];
232                                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
233                                                 push @not_operator, @$operator[$i];
234                                                 push @not_value, $word;
235                                         }
236                                 }
237                         }
238                         else
239                         {
240                                 push @not_tags, @$tags[$i];
241                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
242                                 push @not_operator, @$operator[$i];
243                                 push @not_value, @$value[$i];
244                         }
245                 }
246                 else    # NORMAL statements
247                 {
248                         if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
249                         {
250                                 foreach my $word (split(/ /, @$value[$i]))
251                                 {
252                                         # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
253 #                                       warn "word : $word";
254                                         $word =~ s/%//g unless length($word)>4;
255                                         unless (C4::Context->stopwords->{uc($word)} or length($word)==1) {      #it's NOT a stopword => use it. Otherwise, ignore
256                                                 push @normal_tags, @$tags[$i];
257                                                 push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
258                                                 push @normal_operator, @$operator[$i];
259                                                 push @normal_value, $word;
260                                         }
261                                 }
262                         }
263                         else
264                         {
265                                 push @normal_tags, @$tags[$i];
266                                 push @normal_and_or, @$and_or[$i];
267                                 push @normal_operator, @$operator[$i];
268                                 push @normal_value, @$value[$i];
269                         }
270                 }
271         }
272
273         # Finds the basic results without the NOT requests
274         my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
275
276         $sql_where1 .= "and TO_DAYS( NOW( ) ) - TO_DAYS( biblio.timestamp ) <30" if $orderby =~ "biblio.timestamp";
277         my $sth;
278         if ($sql_where2) {
279                 $sth = $dbh->prepare("select distinct m1.bibid from biblio,biblioitems,marc_biblio,$sql_tables where biblio.biblionumber=marc_biblio.biblionumber and biblio.biblionumber=biblioitems.biblionumber and m1.bibid=marc_biblio.bibid and $sql_where2 and ($sql_where1) order by $orderby $desc_or_asc");
280                 warn "Q2 : select distinct m1.bibid from biblio,biblioitems,marc_biblio,$sql_tables where biblio.biblionumber=marc_biblio.biblionumber and biblio.biblionumber=biblioitems.biblionumber and m1.bibid=marc_biblio.bibid and $sql_where2 and ($sql_where1) order by $orderby $desc_or_asc term is  @$value";
281         } else {
282                 $sth = $dbh->prepare("select distinct m1.bibid from biblio,biblioitems,marc_biblio,$sql_tables where biblio.biblionumber=marc_biblio.biblionumber and biblio.biblionumber=biblioitems.biblionumber and m1.bibid=marc_biblio.bibid and $sql_where1 order by $orderby $desc_or_asc");
283                 warn "Q : select distinct m1.bibid from biblio,biblioitems,marc_biblio,$sql_tables where biblio.biblionumber=marc_biblio.biblionumber and biblio.biblionumber=biblioitems.biblionumber and m1.bibid=marc_biblio.bibid and $sql_where1 order by $orderby $desc_or_asc";
284         }
285         $sth->execute();
286         my @result = ();
287         my $subtitle; # Added by JF for Subtitles
288
289         # Processes the NOT if any and there are results
290         my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
291
292         if( ($sth->rows) && $any_not )  # some results to tune up and some NOT statements
293         {
294                 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request($dbh,\@not_tags, \@not_and_or, \@not_operator, \@not_value);
295
296                 my @tmpresult;
297
298                 while (my ($bibid) = $sth->fetchrow) {
299                         push @tmpresult,$bibid;
300                 }
301                 my $sth_not;
302                 warn "NOT : select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)";
303                 if ($not_sql_where2) {
304                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
305                 } else {
306                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
307                 }
308                 $sth_not->execute();
309
310                 if($sth_not->rows)
311                 {
312                         my %not_bibids = ();
313                         while(my $bibid = $sth_not->fetchrow()) {
314                                 $not_bibids{$bibid} = 1;        # populates the hashtable with the bibids matching the NOT statement
315                         }
316
317                         foreach my $bibid (@tmpresult)
318                         {
319                                 if(!$not_bibids{$bibid})
320                                 {
321                                         push @result, $bibid;
322                                 }
323                         }
324                 }
325                 $sth_not->finish();
326         }
327         else    # no NOT statements
328         {
329                 while (my ($bibid) = $sth->fetchrow) {
330                         push @result,$bibid;
331                 }
332         }
333
334         # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
335         my $counter = $offset;
336         # HINT : biblionumber as bn is important. The hash is fills biblionumber with items.biblionumber.
337         # so if you dont' has an item, you get a not nice empty value.
338         $sth = $dbh->prepare("SELECT biblio.biblionumber as bn,biblio.*, biblioitems.*,marc_biblio.bibid,itemtypes.notforloan
339                                                         FROM biblio, marc_biblio 
340                                                         LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
341                                                         LEFT JOIN itemtypes on itemtypes.itemtype=biblioitems.itemtype
342                                                         WHERE biblio.biblionumber = marc_biblio.biblionumber AND bibid = ?");
343         my $sth_subtitle = $dbh->prepare("SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?"); # Added BY JF for Subtitles
344         my @finalresult = ();
345         my @CNresults=();
346         my $totalitems=0;
347         my $oldline;
348         my ($oldbibid, $oldauthor, $oldtitle);
349         my $sth_itemCN = $dbh->prepare("select items.* from items where biblionumber=?");
350         my $sth_issue = $dbh->prepare("select date_due,returndate from issues where itemnumber=?");
351         # parse all biblios between start & end.
352         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
353                 # search & parse all items & note itemcallnumber
354                 $sth->execute($result[$counter]);
355                 my $continue=1;
356                 my $line = $sth->fetchrow_hashref;
357                 my $biblionumber=$line->{bn};
358         # Return subtitles first ADDED BY JF
359                 $sth_subtitle->execute($biblionumber);
360                 my $subtitle_here.= $sth_subtitle->fetchrow." ";
361                 chop $subtitle_here;
362                 $subtitle = $subtitle_here;
363 #               warn "Here's the Biblionumber ".$biblionumber;
364 #                warn "and here's the subtitle: ".$subtitle_here;
365
366         # /ADDED BY JF
367
368 #               $continue=0 unless $line->{bn};
369 #               my $lastitemnumber;
370                 $sth_itemCN->execute($biblionumber);
371                 my @CNresults = ();
372                 my $notforloan=1; # to see if there is at least 1 item that can be issued
373                 while (my $item = $sth_itemCN->fetchrow_hashref) {
374                         # parse the result, putting holdingbranch & itemcallnumber in separate array
375                         # then all other fields in the main array
376                         
377                         # search if item is on loan
378                         my $date_due;
379                         $sth_issue->execute($item->{itemnumber});
380                         while (my $loan = $sth_issue->fetchrow_hashref) {
381                                 if ($loan->{date_due} and !$loan->{returndate}) {
382                                         $date_due = $loan->{date_due};
383                                 }
384                         }
385                         # store this item
386                         my %lineCN;
387                         $lineCN{holdingbranch} = $item->{holdingbranch};
388                         $lineCN{itemcallnumber} = $item->{itemcallnumber};
389                         $lineCN{location} = $item->{location};
390                         $lineCN{date_due} = format_date($date_due);
391                         $lineCN{notforloan} = $notforloanstatus{$item->{notforloan}} if ($item->{notforloan});
392                         $notforloan=0 unless ($item->{notforloan} or $item->{wthdrawn} or $item->{itemlost});
393                         push @CNresults,\%lineCN;
394                         $totalitems++;
395                 }
396                 # save the biblio in the final array, with item and item issue status
397                 my %newline;
398                 %newline = %$line;
399                 $newline{totitem} = $totalitems;
400                 # if $totalitems == 0, check if it's being ordered.
401                 if ($totalitems == 0) {
402                         my $sth = $dbh->prepare("select count(*) from aqorders where biblionumber=?");
403                         $sth->execute($biblionumber);
404                         my ($ordered) = $sth->fetchrow;
405                         $newline{onorder} = 1 if $ordered;
406                 }
407                 $newline{biblionumber} = $biblionumber;
408                 $newline{norequests} = 0;
409                 $newline{norequests} = 1 if ($line->{notforloan}); # itemtype not issuable
410                 $newline{norequests} = 1 if (!$line->{notforloan} && $notforloan); # itemtype issuable but all items not issuable for instance
411                 $newline{subtitle} = $subtitle;  # put the subtitle in ADDED BY JF
412
413                 my @CNresults2= @CNresults;
414                 $newline{CN} = \@CNresults2;
415                 $newline{'even'} = 1 if $#finalresult % 2 == 0;
416                 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
417                 $newline{'timestamp'} = format_date($newline{timestamp});
418                 @CNresults = ();
419                 push @finalresult, \%newline;
420                 $totalitems=0;
421                 $counter++;
422         }
423         my $nbresults = $#result+1;
424         return (\@finalresult, $nbresults);
425 }
426
427 # Creates the SQL Request
428
429 sub create_request {
430         my ($dbh,$tags, $and_or, $operator, $value) = @_;
431
432         my $sql_tables; # will contain marc_subfield_table as m1,...
433         my $sql_where1; # will contain the "true" where
434         my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
435         my $nb_active=0; # will contain the number of "active" entries. an entry is active if a value is provided.
436         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
437
438         my $maxloop=8; # the maximum number of words to avoid a too complex search.
439         $maxloop = @$value if @$value<$maxloop;
440         
441         for(my $i=0; $i<=$maxloop;$i++) {
442                 if (@$value[$i]) {
443                         $nb_active++;
444                         if ($nb_active==1) {
445                                 if (@$operator[$i] eq "start") {
446                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
447                                         $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
448                                         if (@$tags[$i]) {
449                                                 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
450                                         }
451                                         $sql_where1.=")";
452                                 } elsif (@$operator[$i] eq "contains") {
453                                         $sql_tables .= "marc_word as m$nb_table,";
454                                         $sql_where1 .= "(m1.word  like ".$dbh->quote("@$value[$i]");
455                                         if (@$tags[$i]) {
456                                                  $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
457                                         }
458                                         $sql_where1.=")";
459                                 } else {
460                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
461                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
462                                         if (@$tags[$i]) {
463                                                  $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
464                                         }
465                                         $sql_where1.=")";
466                                 }
467                         } else {
468                                 if (@$operator[$i] eq "start") {
469                                         $nb_table++;
470                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
471                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
472                                         if (@$tags[$i]) {
473                                                 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
474                                         }
475                                         $sql_where1.=")";
476                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
477                                 } elsif (@$operator[$i] eq "contains") {
478                                         if (@$and_or[$i] eq 'and') {
479                                                 $nb_table++;
480                                                 $sql_tables .= "marc_word as m$nb_table,";
481                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
482                                                 if (@$tags[$i]) {
483                                                         $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
484                                                 }
485                                                 $sql_where1.=")";
486                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
487                                         } else {
488                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
489                                                 if (@$tags[$i]) {
490                                                         $sql_where1 .="  and m$nb_table.tagsubfield in (@$tags[$i])";
491                                                 }
492                                                 $sql_where1.=")";
493                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
494                                         }
495                                 } else {
496                                         $nb_table++;
497                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
498                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
499                                         if (@$tags[$i]) {
500                                                 $sql_where1 .="  and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
501                                         }
502                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
503                                         $sql_where1.=")";
504                                 }
505                         }
506                 }
507         }
508
509         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
510         {
511                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
512                 $sql_where2 .= ")";
513         }
514         else    # no sql_where2 statement, deleting '('
515         {
516                 $sql_where2 = "";
517         }
518         chop $sql_tables;       # deletes the trailing ','
519         return ($sql_tables, $sql_where1, $sql_where2);
520 }
521
522 sub getMARCnotes {
523         my ($dbh, $bibid, $marcflavour) = @_;
524         my ($mintag, $maxtag);
525         if ($marcflavour eq "MARC21") {
526                 $mintag = "500";
527                 $maxtag = "599";
528         } else {           # assume unimarc if not marc21
529                 $mintag = "300";
530                 $maxtag = "399";
531         }
532
533         my $sth=$dbh->prepare("SELECT subfieldvalue,tag FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
534
535         $sth->execute($bibid,$mintag,$maxtag);
536
537         my @marcnotes;
538         my $note = "";
539         my $tag = "";
540         my $marcnote;
541
542         while (my $data=$sth->fetchrow_arrayref) {
543                 my $value=$data->[0];
544                 my $thistag=$data->[1];
545                 if ($value=~/\.$/) {
546                         $value=$value . "  ";
547                 }
548                 if ($thistag ne $tag && $note ne "") {
549                         $marcnote = {marcnote => $note,};
550                         push @marcnotes, $marcnote;
551                         $note=$value;
552                         $tag=$thistag;
553                 }
554                 if ($note ne $value) {
555                         $note = $note." ".$value;
556                 }
557         }
558
559         if ($note) {
560                 $marcnote = {marcnote => $note};
561                 push @marcnotes, $marcnote;   #load last tag into array
562         }
563
564         $sth->finish;
565         $dbh->disconnect;
566
567         my $marcnotesarray=\@marcnotes;
568         return $marcnotesarray;
569 }  # end getMARCnotes
570
571
572 sub getMARCsubjects {
573     my ($dbh, $bibid, $marcflavour) = @_;
574         my ($mintag, $maxtag);
575         if ($marcflavour eq "MARC21") {
576                 $mintag = "600";
577                 $maxtag = "699";
578         } else {           # assume unimarc if not marc21
579                 $mintag = "600";
580                 $maxtag = "619";
581         }
582         my $sth=$dbh->prepare("SELECT subfieldvalue,subfieldcode FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
583
584         $sth->execute($bibid,$mintag,$maxtag);
585
586         my @marcsubjcts;
587         my $subjct = "";
588         my $subfield = "";
589         my $marcsubjct;
590
591         while (my $data=$sth->fetchrow_arrayref) {
592                 my $value = $data->[0];
593                 my $subfield = $data->[1];
594                 if ($subfield eq "a" && $value ne $subjct) {
595                         $marcsubjct = {MARCSUBJCT => $value,};
596                         push @marcsubjcts, $marcsubjct;
597                         $subjct = $value;
598                 }
599         }
600
601         $sth->finish;
602         $dbh->disconnect;
603
604         my $marcsubjctsarray=\@marcsubjcts;
605         return $marcsubjctsarray;
606 }  #end getMARCsubjects
607
608 END { }       # module clean-up code here (global destructor)
609
610 1;
611 __END__
612
613 =back
614
615 =head1 AUTHOR
616
617 Koha Developement team <info@koha.org>
618
619 =cut