Bug 12409: Fix fields order on exporting to bibtex
[koha_fer] / C4 / ImportBatch.pm
index 61d5b86..a487cc6 100644 (file)
@@ -26,6 +26,7 @@ use C4::Biblio;
 use C4::Items;
 use C4::Charset;
 use C4::AuthoritiesMarc;
+use C4::MarcModificationTemplates;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -316,18 +317,19 @@ sub ModAuthInBatch {
 =head2 BatchStageMarcRecords
 
   ($batch_id, $num_records, $num_items, @invalid_records) = 
-    BatchStageMarcRecords($record_type, $encoding, $marc_records, $file_name,
+    BatchStageMarcRecords($encoding, $marc_records, $file_name, $marc_modification_template,
                           $comments, $branch_code, $parse_items,
                           $leave_as_staging, 
                           $progress_interval, $progress_callback);
 
 =cut
 
-sub  BatchStageMarcRecords {
+sub BatchStageMarcRecords {
     my $record_type = shift;
     my $encoding = shift;
     my $marc_records = shift;
     my $file_name = shift;
+    my $marc_modification_template = shift;
     my $comments = shift;
     my $branch_code = shift;
     my $parse_items = shift;
@@ -378,6 +380,8 @@ sub  BatchStageMarcRecords {
 
         $encoding = $charset_guessed unless $encoding;
 
+        ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
+
         my $import_record_id;
         if (scalar($marc_record->fields()) == 0) {
             push @invalid_records, $marc_blob;
@@ -1024,9 +1028,18 @@ starting at the given offset.
 =cut
 
 sub GetImportRecordsRange {
-    my ($batch_id, $offset, $results_per_group, $status) = @_;
+    my ( $batch_id, $offset, $results_per_group, $status, $parameters ) = @_;
 
     my $dbh = C4::Context->dbh;
+
+    my $order_by = $parameters->{order_by} || 'import_record_id';
+    ( $order_by ) = grep( /^$order_by$/, qw( import_record_id title status overlay_status ) ) ? $order_by : 'import_record_id';
+
+    my $order_by_direction =
+      uc( $parameters->{order_by_direction} ) eq 'DESC' ? 'DESC' : 'ASC';
+
+    $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
+
     my $query = "SELECT title, author, isbn, issn, authorized_heading, import_records.import_record_id,
                                            record_sequence, status, overlay_status,
                                            matched_biblionumber, matched_authid, record_type
@@ -1040,7 +1053,8 @@ sub GetImportRecordsRange {
         $query .= " AND status=?";
         push(@params,$status);
     }
-    $query.=" ORDER BY import_record_id";
+
+    $query.=" ORDER BY $order_by $order_by_direction";
 
     if($results_per_group){
         $query .= " LIMIT ?";
@@ -1433,7 +1447,7 @@ sub _add_biblio_fields {
     my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
     my $dbh = C4::Context->dbh;
     # FIXME no controlnumber, originalsource
-    $isbn = C4::Koha::_isbn_cleanup($isbn); # FIXME C4::Koha::_isbn_cleanup should be made public
+    $isbn = C4::Koha::GetNormalizedISBN($isbn);
     my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
     $sth->execute($import_record_id, $title, $author, $isbn, $issn);
     $sth->finish();