[Patches] [PATCH] bug_6210: Select framework if merging two records with different frameworks

koha-patchbot at kohaaloha.com koha-patchbot at kohaaloha.com
Mon Nov 14 19:35:02 NZDT 2011


From: Srdjan Jankovic <srdjan at catalyst.net.nz>
Date: Mon, 14 Nov 2011 19:25:18 +1300
Subject: [PATCH] bug_6210: Select framework if merging two records with different frameworks

---
 C4/Biblio.pm                                       |   39 +++---
 cataloguing/merge.pl                               |  136 +++++++++++--------
 .../prog/en/modules/cataloguing/merge.tt           |   39 +++++-
 3 files changed, 135 insertions(+), 79 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 8747cc5..2c440d1 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -1043,9 +1043,13 @@ for the given frameworkcode
 
 sub GetMarcFromKohaField {
     my ( $kohafield, $frameworkcode ) = @_;
-    return 0, 0 unless $kohafield and defined $frameworkcode;
+    return (0, undef) unless $kohafield and defined $frameworkcode;
     my $relations = C4::Context->marcfromkohafield;
-    return ( $relations->{$frameworkcode}->{$kohafield}->[0], $relations->{$frameworkcode}->{$kohafield}->[1] );
+    if ( my $mf = $relations->{$frameworkcode}->{$kohafield} ) {
+        return @$mf;
+    }
+    warn qq{No marc tags for framework "$frameworkcode" field $kohafield};
+    return (0, undef);
 }
 
 =head2 GetMarcBiblio
@@ -3182,9 +3186,24 @@ sub _koha_marc_update_bib_ids {
     # we drop the original field
     # we add the new builded field.
     my ( $biblio_tag,     $biblio_subfield )     = GetMarcFromKohaField( "biblio.biblionumber",          $frameworkcode );
+    die qq{No biblionumber tag for framework "$frameworkcode"} unless $biblio_tag;
     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
+    die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblio_tag;
+
+    if ( $biblio_tag == $biblioitem_tag ) {
+
+        # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
+        my $new_field = MARC::Field->new(
+            $biblio_tag, '', '',
+            "$biblio_subfield"     => $biblionumber,
+            "$biblioitem_subfield" => $biblioitemnumber
+        );
 
-    if ( $biblio_tag != $biblioitem_tag ) {
+        # drop old field and create new one...
+        my $old_field = $record->field($biblio_tag);
+        $record->delete_field($old_field) if $old_field;
+        $record->insert_fields_ordered($new_field);
+    } else {
 
         # biblionumber & biblioitemnumber are in different fields
 
@@ -3212,20 +3231,6 @@ sub _koha_marc_update_bib_ids {
         $old_field = $record->field($biblioitem_tag);
         $record->delete_field($old_field) if $old_field;
         $record->insert_fields_ordered($new_field);
-
-    } else {
-
-        # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
-        my $new_field = MARC::Field->new(
-            $biblio_tag, '', '',
-            "$biblio_subfield"     => $biblionumber,
-            "$biblioitem_subfield" => $biblioitemnumber
-        );
-
-        # drop old field and create new one...
-        my $old_field = $record->field($biblio_tag);
-        $record->delete_field($old_field) if $old_field;
-        $record->insert_fields_ordered($new_field);
     }
 }
 
diff --git a/cataloguing/merge.pl b/cataloguing/merge.pl
index e3ee725..3d23cfa 100755
--- a/cataloguing/merge.pl
+++ b/cataloguing/merge.pl
@@ -27,6 +27,7 @@ use C4::Auth;
 use C4::Items;
 use C4::Biblio;
 use C4::Serials;
+use C4::Koha;
 use C4::Reserves qw/MergeHolds/;
 
 my $input = new CGI;
@@ -61,7 +62,7 @@ if ($merge) {
     # Rewriting the leader
     $record->leader(GetMarcBiblio($tobiblio)->leader());
 
-    my $frameworkcode = &GetFrameworkCode($tobiblio);
+    my $frameworkcode = $input->param('frameworkcode');
     my @notmoveditems;
 
     # Modifying the reference record
@@ -108,77 +109,98 @@ if ($merge) {
 	push @errors, $error if ($error); 
     }
 
-    # Errors
-    my @errors_loop  = map{{error => $_}}@errors;
-
     # Parameters
     $template->param(
-	errors  => \@errors_loop,
 	result => 1,
 	biblio1 => $input->param('biblio1')
     );
 
-
 #-------------------------
 # Show records to merge
 #-------------------------
 } else {
-
     my $mergereference = $input->param('mergereference');
     my $biblionumber = $input->param('biblionumber');
 
-    my $data1 = GetBiblioData($biblionumber[0]);
-    my $data2 = GetBiblioData($biblionumber[1]);
-
-    # Ask the user to choose which record will be the kept
-    if (not $mergereference) {
-	$template->param(
-	    choosereference => 1,	
-	    biblio1 => $biblionumber[0],
-	    biblio2 => $biblionumber[1],
-	    title1 => $data1->{'title'},
-	    title2 => $data2->{'title'}
-	    );
-    } else {
-
-	if (scalar(@biblionumber) != 2) {
-	    push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
-	}
-
-	# Checks if both records use the same framework
-	my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
-	my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
-	my $framework;
-	if ($frameworkcode1 ne $frameworkcode2) {
-	    push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
-	} else {
-	    $framework = $frameworkcode1;	
-	}
-
-	# Getting MARC Structure
-	my $tagslib = GetMarcStructure(1, $framework);
-
-	my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
-
-	# Creating a loop for display
-	my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
-	my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
-
-	# Errors
-	my @errors_loop  = map{{error => $_}}@errors;
-
-	# Parameters
-	$template->param(
-	    errors  => \@errors_loop,
-	    biblio1 => $mergereference,
-	    biblio2 => $notreference,
-	    mergereference => $mergereference,
-	    record1 => @record1,
-	    record2 => @record2,
-	    framework => $framework
-	    );
+    if (scalar(@biblionumber) != 2) {
+        push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
+    }
+    else {
+        my $data1 = GetBiblioData($biblionumber[0]);
+        my $data2 = GetBiblioData($biblionumber[1]);
+
+        # Checks if both records use the same framework
+        my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
+        my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
+
+        if ($mergereference) {
+
+            my $framework;
+            if ($frameworkcode1 ne $frameworkcode2) {
+                $framework = $input->param('frameworkcode')
+                  or push @errors, "Famework not selected.";
+            } else {
+                $framework = $frameworkcode1;	
+            }
+
+            # Getting MARC Structure
+            my $tagslib = GetMarcStructure(1, $framework);
+
+            my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
+
+            # Creating a loop for display
+            my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
+            my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
+
+            # Parameters
+            $template->param(
+                biblio1 => $mergereference,
+                biblio2 => $notreference,
+                mergereference => $mergereference,
+                record1 => @record1,
+                record2 => @record2,
+                framework => $framework,
+            );
+        }
+        else {
+
+        # Ask the user to choose which record will be the kept
+            $template->param(
+                choosereference => 1,	
+                biblio1 => $biblionumber[0],
+                biblio2 => $biblionumber[1],
+                title1 => $data1->{'title'},
+                title2 => $data2->{'title'}
+            );
+            if ($frameworkcode1 ne $frameworkcode2) {
+                my $frameworks = getframeworks;
+                my @frameworkselect;
+                foreach my $thisframeworkcode ( keys %$frameworks ) {
+                    my %row = (
+                        value         => $thisframeworkcode,
+                        frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
+                    );
+                    if ($frameworkcode1 eq $thisframeworkcode){
+                        $row{'selected'} = 1;
+                        }
+                    push @frameworkselect, \%row;
+                }
+                $template->param(
+                    frameworkselect => \@frameworkselect,
+                    frameworkcode1 => $frameworkcode1,
+                    frameworkcode2 => $frameworkcode2,
+                );
+            }
+        }
     }
 }
+
+if (@errors) {
+    # Errors
+    my @errors_loop  = map{{error => $_}}@errors;
+    $template->param( errors  => \@errors_loop );
+}
+
 output_html_with_http_headers $input, $cookie, $template->output;
 exit;
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt
index 0d8a84c..3300f78 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt
@@ -161,6 +161,10 @@ $(document).ready(function(){
 
 });
 
+function changeFramework(fw) {
+    $("#Frameworks").val(fw);
+}
+
 //]]>
 </script>
 </head>
@@ -194,10 +198,34 @@ $(document).ready(function(){
 <form id="mergeform" action="/cgi-bin/koha/cataloguing/merge.pl" method="post">
     <fieldset class="rows">
 	<legend>Merge reference</legend>
+    <table>
+      <tr>
+        <td style="border-width: 0px; text-align: left">
 	<ol>
-	<li class="radio"><input type="radio" value="[% biblio1 %]" checked="checked" id="mergereference1" name="mergereference" /><label style="float:none" for="mergereference1">[% title1 %] ([% biblio1 %])</label></li>
-	<li class="radio"><input type="radio" value="[% biblio2 %]" id="mergereference2" name="mergereference" /><label style="float:none" for="mergereference2">[% title2 %] ([% biblio2 %])</label></li>
+	<li class="radio"><input type="radio" value="[% biblio1 %]" checked="checked" id="mergereference1" name="mergereference" onclick="changeFramework('[% frameworkcode1 %]')" /><label style="float:none" for="mergereference1">[% title1 %] ([% biblio1 %])</label></li>
+	<li class="radio"><input type="radio" value="[% biblio2 %]" id="mergereference2" name="mergereference" onclick="changeFramework('[% frameworkcode2 %]')" /><label style="float:none" for="mergereference2">[% title2 %] ([% biblio2 %])</label></li>
 </ol>
+        </td>
+        <td style="border-width: 0px">
+    [% IF frameworkselect %]
+          <label style="float:none" for="frameworkcode">Framework:</label><br>
+            <select name="frameworkcode" id="Frameworks">
+                            <option value="Default">Default</option>
+                            [% FOREACH frameworkcodeloo IN frameworkselect %]
+                                [% IF ( frameworkcodeloo.selected ) %]
+                                    <option value="[% frameworkcodeloo.value %]" selected="selected">
+                                [% ELSE %]
+                                    <option value="[% frameworkcodeloo.value %]">
+                                [% END %]
+                                 [% frameworkcodeloo.frameworktext %]                              
+                                 </option>
+                            [% END %]
+            </select>
+    [% END %]
+        </td>
+      </tr>
+    </table>
+
 	<input type="hidden" name="biblionumber" value="[% biblio1 %]" />
 	<input type="hidden" name="biblionumber" value="[% biblio2 %]" />
 	<fieldset class="action"><input type="submit" value="Next" /></fieldset>
@@ -332,14 +360,15 @@ $(document).ready(function(){
 		[% END %]
 
 	    </ul>
+</div>
+</div> <!-- // #result -->
+</div> <!-- .yui-u -->
 
 <input type="hidden" name="biblio1" value="[% biblio1 %]" />
 <input type="hidden" name="biblio2" value="[% biblio2 %]" />
 <input type="hidden" name="mergereference" value="[% mergereference %]" />
+<input type="hidden" name="frameworkcode" value="[% framework %]" />
 
-</div>
-</div> <!-- // #result -->
-</div> <!-- .yui-u -->
 <fieldset class="action"><input type="submit" name="merge" value="Merge" /></fieldset>
 </div>
 </form>
-- 
1.6.5




More information about the Patches mailing list