(bug #3734) Fix previous patch of isbd view
[koha.git] / C4 / Biblio.pm
index ce9c431..6599c33 100755 (executable)
@@ -62,6 +62,7 @@ BEGIN {
                &GetBiblioItemInfosOf
                &GetBiblioItemByBiblioNumber
                &GetBiblioFromItemNumber
+               &GetBiblioSummary
                
                &GetRecordValue
                &GetFieldMapping
@@ -742,13 +743,17 @@ Return the ISBD view which can be included in opac and intranet
 =cut
 
 sub GetISBDView {
-    my $biblionumber    = shift;
+    my ($biblionumber, $template) = @_;
     my $record          = GetMarcBiblio($biblionumber);
     my $itemtype        = &GetFrameworkCode($biblionumber);
     my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
     my $tagslib      = &GetMarcStructure( 1, $itemtype );
     
     my $ISBD = C4::Context->preference('ISBD');
+    if($template eq "opac"){
+        $ISBD = C4::Context->preference('OPACISBD');
+    }
+    
     my $bloc = $ISBD;
     my $res;
     my $blocres;
@@ -786,7 +791,6 @@ sub GetISBDView {
                     my $tagsubf = $tag . $subfvalue;
                     $calculated =~
                           s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
-                    $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
                 
                     # field builded, store the result
                     if ( $calculated && !$hasputtextbefore )
@@ -812,22 +816,16 @@ sub GetISBDView {
               else {
                 my @subf = $field->subfields;
                 for my $i ( 0 .. $#subf ) {
-                my $valuecode   = $subf[$i][1];
-                my $subfieldcode  = $subf[$i][0];
-                my $subfieldvalue =
-                GetAuthorisedValueDesc( $tag, $subf[$i][0],
-                  $subf[$i][1], '', $tagslib );
-                my $tagsubf = $tag . $subfieldcode;
-    
-                $calculated =~ s/                  # replace all {{}} codes by the value code.
-                                  \{\{$tagsubf\}\} # catch the {{actualcode}}
-                                /
-                                  $valuecode     # replace by the value code
-                               /gx;
-    
-                $calculated =~
-            s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
-            $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
+                    my $valuecode   = $subf[$i][1];
+                    my $subfieldcode  = $subf[$i][0];
+                    my $subfieldvalue =
+                    GetAuthorisedValueDesc( $tag, $subf[$i][0],
+                      $subf[$i][1], '', $tagslib );
+                    my $tagsubf = $tag . $subfieldcode;
+        
+                    $calculated =~ s/\{\{$tagsubf\}\}/$valuecode/gx;
+                    $calculated =~
+                        s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
                 }
     
                 # field builded, store the result
@@ -1199,8 +1197,8 @@ sub GetCOinSBiblio {
         $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
 
         # Setting datas
-        $aulast     = $record->subfield('700','a');
-        $aufirst    = $record->subfield('700','b');
+        $aulast     = $record->subfield('700','a') || '';
+        $aufirst    = $record->subfield('700','b') || '';
         $oauthors   = "&rft.au=$aufirst $aulast";
         # others authors
         if($record->field('200')){
@@ -1210,10 +1208,10 @@ sub GetCOinSBiblio {
         }
         $title      = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
                                          "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
-        $pubyear    = $record->subfield('210','d');
-        $publisher  = $record->subfield('210','c');
-        $isbn       = $record->subfield('010','a');
-        $issn       = $record->subfield('011','a');
+        $pubyear    = $record->subfield('210','d') || '';
+        $publisher  = $record->subfield('210','c') || '';
+        $isbn       = $record->subfield('010','a') || '';
+        $issn       = $record->subfield('011','a') || '';
     }else{
         # MARC21 need some improve
         my $fmts;
@@ -1246,6 +1244,119 @@ sub GetCOinSBiblio {
     return $coins_value;
 }
 
+=head2 GetBiblioSummary
+
+=over 4
+
+$summary = GetBiblioSummary($marcrecord);
+
+Return the summary of a record.
+
+=back
+
+=cut
+
+sub GetBiblioSummary {
+    my $recorddata =shift @_;
+    
+    my $marcflavour = C4::Context->preference("marcflavour");
+    my $marc=MARC::Record::new_from_xml($recorddata,"utf-8",$marcflavour);
+    
+    my $str;
+    
+    if($marcflavour eq "MARC21"){
+        $str="<b>".$marc->subfield('245',"a")."</b>" if $marc->subfield('245','a');
+        $str.= " <i>".$marc->subfield('245',"b")."</i> " if $marc->subfield('245','b');
+        
+        if ($marc->field('245')){
+            $str.=" / ";
+            foreach ($marc->field('100')->subfield("a")) {
+                $str.=$_." ; ";
+            }
+            $str=~s/ ; $/. /; 
+        }
+      
+        if ($marc->field('260')){
+            $str.=" - ";
+            $str.=$marc->subfield('260',"a")." " if $marc->subfield('260','a');
+            $str.=" : ".$marc->subfield('260',"b")." " if $marc->subfield('260','b');
+            $str.=", ".$marc->subfield('260',"c")." " if $marc->subfield('260','c');
+        }
+        if ($marc->field('300')){
+            $str.=" - ";
+            $str.=$marc->subfield('300','a') if ($marc->subfield(300,'a'));
+            $str.=" ; ".$marc->subfield('300','b') if $marc->subfield('300','b');
+            $str.=" ; ".$marc->subfield('300','c') if $marc->subfield('300','c');
+            $str.=" ; ".$marc->subfield('300','e') if $marc->subfield('300','e');
+        }
+        foreach ($marc->field('500')){
+            $str.= " - ";
+            foreach ($_->subfield("a")){
+                $str.=$_."; "
+            } 
+        }
+        my $itemtypes=GetItemTypes();
+        $str.=" - <u>".$itemtypes->{$marc->subfield('942','c')}->{'description'}."</u> ";
+        $str.="<br />\n";
+       
+    }else{
+        $str = "<b>".$marc->subfield('200','a')."</b>"   if $marc->subfield('200','a');
+        $str.= " <i>".$marc->subfield('200','e')."</i> " if $marc->subfield('200','e');
+        if ($marc->field('200')){
+            $str.=" / ";
+            foreach ($marc->field('200')->subfield("f")) {
+                $str.=$_." ; ";
+            }
+            $str=~s/ ; $/. /; 
+        }
+        
+        if ($marc->subfield('200','g')){
+            $str.=" ; ";
+            foreach ($marc->field('200')->subfield("g")){
+                $str.=$_." ; ";
+            } 
+            $str=~s/ ; $/. /; 
+        }
+        
+        if ($marc->field('461')){
+            $str.="- In :";
+            $str.=     $marc->subfield('461','t') if $marc->subfield('461','t');
+            $str.=", ".$marc->subfield('461','d') if $marc->subfield('461','d');
+            $str.=", ".$marc->subfield('461','v') if $marc->subfield('461','v');
+            $str.=", ".$marc->subfield('461','h') if $marc->subfield('461','h');
+            $str.=" ; ".$marc->subfield('461','x') if $marc->subfield('461','x');
+        }
+        
+        if ($marc->field('210')){
+            $str.=" - ";
+            $str.=$marc->subfield('210',"a")." " if $marc->subfield('210','a');
+            $str.=" : ".$marc->subfield('210',"c")." " if $marc->subfield('210','c');
+            $str.=", ".$marc->subfield('210',"d")." " if $marc->subfield('210','d');
+        }
+        
+        if ($marc->field('215')){
+            $str.=" - ";
+            $str.=$marc->subfield('215','a') if ($marc->subfield(215,'a'));
+            $str.=" ; ".$marc->subfield('215','d') if $marc->subfield('215','d');
+            $str.=" ; ".$marc->subfield('215','c') if $marc->subfield('215','c');
+            $str.=" ; ".$marc->subfield('215','e') if $marc->subfield('215','e');
+        }
+        foreach ($marc->field('300')){
+            $str.=" - ";
+            foreach ($_->subfield("a")){
+                $str.=$_."; "
+            } 
+        }
+        
+        my $itemtypes=GetItemTypes;
+        if($itemtypes->{$marc->subfield('200','b')}){
+            $str.=" - <u>".$itemtypes->{$marc->subfield('200','b')}->{'description'}."</u> ";
+        }
+        $str.="<br />\n";         
+    }    
+    return $str;
+}
+
 =head2 GetAuthorisedValueDesc
 
 =over 4
@@ -1765,20 +1876,22 @@ sub TransformHtmlToXml {
 #         }
         if ( ( @$tags[$i] ne $prevtag ) ) {
             $j++ unless ( @$tags[$i] eq "" );
+                       my $indicator1=eval{substr( @$indicator[$j], 0, 1 )};
+                       my $indicator2=eval{substr( @$indicator[$j], 1, 1 )};
+            my $ind1 = _default_ind_to_space($indicator1);
+            my $ind2;
+            if ( @$indicator[$j] ) {
+               $ind2 = _default_ind_to_space($indicator2);
+            }
+            else {
+               warn "Indicator in @$tags[$i] is empty";
+               $ind2 = " ";
+            }
             if ( !$first ) {
                 $xml .= "</datafield>\n";
                 if (   ( @$tags[$i] && @$tags[$i] > 10 )
                     && ( @$values[$i] ne "" ) )
                 {
-                    my $ind1 = substr( @$indicator[$j], 0, 1 );
-                    my $ind2;
-                    if ( @$indicator[$j] ) {
-                        $ind2 = substr( @$indicator[$j], 1, 1 );
-                    }
-                    else {
-                        warn "Indicator in @$tags[$i] is empty";
-                        $ind2 = " ";
-                    }
                     $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                     $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                     $first = 0;
@@ -1802,8 +1915,6 @@ sub TransformHtmlToXml {
                         $first = 1;
                     }
                     else {
-                        my $ind1 = default_ind_to_space( substr( @$indicator[$j], 0, 1 ) );
-                        my $ind2 = default_ind_to_space( substr( @$indicator[$j], 1, 1 ) );
                         $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                         $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                         $first = 0;
@@ -1812,12 +1923,21 @@ sub TransformHtmlToXml {
             }
         }
         else {    # @$tags[$i] eq $prevtag
-            if ( @$values[$i] eq "" ) {
+                       my $indicator1=eval{substr( @$indicator[$j], 0, 1 )};
+                       my $indicator2=eval{substr( @$indicator[$j], 1, 1 )};
+            my $ind1 = _default_ind_to_space($indicator1);
+            my $ind2;
+            if ( @$indicator[$j] ) {
+               $ind2 = _default_ind_to_space($indicator2);
+            }
+            else {
+               warn "Indicator in @$tags[$i] is empty";
+               $ind2 = " ";
+            }
+         if ( @$values[$i] eq "" ) {
             }
             else {
                 if ($first) {
-                    my $ind1 = default_ind_to_space( substr( @$indicator[$j], 0, 1 ) );
-                    my $ind2 = default_ind_to_space( substr( @$indicator[$j], 1, 1 ) );
                     $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                     $first = 0;
                 }
@@ -1844,16 +1964,16 @@ sub TransformHtmlToXml {
     return $xml;
 }
 
-=head2 default_ind_to_space
+=head2 _default_ind_to_space
 
 Passed what should be an indicator returns a space
 if its undefined or zero length
 
 =cut
 
-sub default_ind_to_space {
+sub _default_ind_to_space {
     my $s = shift;
-    if (!defined $s || $s eq q{} ) {
+    if (!defined $s || $s eq q{}) {
         return ' ';
     }
     return $s;
@@ -1931,8 +2051,8 @@ sub TransformHtmlToMarc {
         elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
             my $tag  = $1;
             
-            my $ind1 = substr($cgi->param($param),0,1);
-            my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
+            my $ind1 = _default_ind_to_space(substr($cgi->param($param),          0, 1));
+            my $ind2 = _default_ind_to_space(substr($cgi->param($params->[$i+1]), 0, 1));
             $newfield=0;
             my $j=$i+2;
             
@@ -1961,8 +2081,8 @@ sub TransformHtmlToMarc {
                         if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
                             $newfield = MARC::Field->new(
                                 $tag,
-                                ''.$ind1,
-                                ''.$ind2,
+                                $ind1,
+                                $ind2,
                                 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
                             );
                         }
@@ -2268,11 +2388,15 @@ sub PrepareItemrecordDisplay {
     my ( $bibnum, $itemnum, $defaultvalues ) = @_;
 
     my $dbh = C4::Context->dbh;
+       my $today_iso = C4::Dates->today('iso');
     my $frameworkcode = &GetFrameworkCode( $bibnum );
     my ( $itemtagfield, $itemtagsubfield ) =
       &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
     my $tagslib = &GetMarcStructure( 1, $frameworkcode );
     my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
+       # FIXME : I'd rather have GetMarcBiblio called out of this.
+       # Since it gets the whole Biblio record for each item
+    my $marcrecord = GetMarcBiblio( $bibnum) if ($bibnum);
     my @loop_data;
     my $authorised_values_sth =
       $dbh->prepare(
@@ -2305,9 +2429,15 @@ sub PrepareItemrecordDisplay {
                   if ($itemrecord) {
                       ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord );
                   }
-                  if (!defined $value) {
-                      $value = q||;
-                  }
+                                 unless ($value) {
+                                               $value   = $tagslib->{$tag}->{$subfield}->{defaultvalue};
+                                               $value ||= $defaultvalues->{$tagslib->{$tag}->{$subfield}->{'kohafield'}};
+                                               # get today date & replace YYYY, MM, DD if provided in the default value
+                                               my ( $year, $month, $day ) = split ',', $today_iso;     # FIXME: iso dates don't have commas!
+                                               $value =~ s/YYYY/$year/g;
+                                               $value =~ s/MM/$month/g;
+                                               $value =~ s/DD/$day/g;
+                                 }
                   $value =~ s/"/&quot;/g;
 
                 # search for itemcallnumber if applicable
@@ -2319,7 +2449,7 @@ sub PrepareItemrecordDisplay {
                       substr( C4::Context->preference('itemcallnumber'), 0, 3 );
                     my $CNsubfield =
                       substr( C4::Context->preference('itemcallnumber'), 3, 1 );
-                    my $temp = $itemrecord->field($CNtag) if ($itemrecord);
+                    my $temp = $marcrecord->field($CNtag) if ($marcrecord);
                     if ($temp) {
                         $value = $temp->subfield($CNsubfield);
                     }