View | Details | Raw Unified | Return to bug 39732
Collapse All | Expand All

(-)a/C4/Search.pm (-20 / +34 lines)
Lines 21-26 use base 'Exporter'; Link Here
21
BEGIN {
21
BEGIN {
22
    our @EXPORT_OK = qw(
22
    our @EXPORT_OK = qw(
23
        FindDuplicate
23
        FindDuplicate
24
        FindBiblioFromMetadata
24
        SimpleSearch
25
        SimpleSearch
25
        searchResults
26
        searchResults
26
        getRecords
27
        getRecords
Lines 90-124 sub FindDuplicate { Link Here
90
    my ($record) = @_;
91
    my ($record) = @_;
91
    my $dbh      = C4::Context->dbh;
92
    my $dbh      = C4::Context->dbh;
92
    my $result   = TransformMarcToKoha( { record => $record } );
93
    my $result   = TransformMarcToKoha( { record => $record } );
94
    return FindBiblioFromMetadata($result);
95
}
96
97
=head2 FindBiblioFromMetadata
98
99
($biblionumber, $title) = FindBiblioFromMetadata($data);
100
101
This function attempts to find records matching data using a hard-coded, fairly simplistic algorithm
102
103
=cut
104
105
sub FindBiblioFromMetadata {
106
    my $metadata = shift;
93
    my $sth;
107
    my $sth;
94
    my $query;
108
    my $query;
95
109
96
    # search duplicate on ISBN, easy and fast..
110
    # search duplicate on ISBN, easy and fast..
97
    # ... normalize first
111
    # ... normalize first
98
    if ( $result->{isbn} ) {
112
    if ( $metadata->{isbn} ) {
99
        $result->{isbn} =~ s/\(.*$//;
113
        $metadata->{isbn} =~ s/\(.*$//;
100
        $result->{isbn} =~ s/\s+$//;
114
        $metadata->{isbn} =~ s/\s+$//;
101
        $result->{isbn} =~ s/\|/OR/;
115
        $metadata->{isbn} =~ s/\|/OR/;
102
        $query = "isbn:$result->{isbn}";
116
        $query = "isbn:$metadata->{isbn}";
103
    } else {
117
    } else {
104
118
105
        my $titleindex  = 'ti,ext';
119
        my $titleindex  = 'ti,ext';
106
        my $authorindex = 'au,ext';
120
        my $authorindex = 'au,ext';
107
        my $op          = 'AND';
121
        my $op          = 'AND';
108
122
109
        $result->{title} =~ s /\\//g;
123
        $metadata->{title} =~ s /\\//g;
110
        $result->{title} =~ s /\"//g;
124
        $metadata->{title} =~ s /\"//g;
111
        $result->{title} =~ s /\(//g;
125
        $metadata->{title} =~ s /\(//g;
112
        $result->{title} =~ s /\)//g;
126
        $metadata->{title} =~ s /\)//g;
113
127
114
        $query = "$titleindex:\"$result->{title}\"";
128
        $query = "$titleindex:\"$metadata->{title}\"";
115
        if ( $result->{author} ) {
129
        if ( $metadata->{author} ) {
116
            $result->{author} =~ s /\\//g;
130
            $metadata->{author} =~ s /\\//g;
117
            $result->{author} =~ s /\"//g;
131
            $metadata->{author} =~ s /\"//g;
118
            $result->{author} =~ s /\(//g;
132
            $metadata->{author} =~ s /\(//g;
119
            $result->{author} =~ s /\)//g;
133
            $metadata->{author} =~ s /\)//g;
120
134
121
            $query .= " $op $authorindex:\"$result->{author}\"";
135
            $query .= " $op $authorindex:\"$metadata->{author}\"";
122
        }
136
        }
123
    }
137
    }
124
138
Lines 132-143 sub FindDuplicate { Link Here
132
                $possible_duplicate_record
146
                $possible_duplicate_record
133
            );
147
            );
134
148
135
            my $result = TransformMarcToKoha( { record => $marcrecord } );
149
            my $metadata = TransformMarcToKoha( { record => $marcrecord } );
136
150
137
            # FIXME :: why 2 $biblionumber ?
151
            # FIXME :: why 2 $biblionumber ?
138
            if ($result) {
152
            if ($metadata) {
139
                push @results, $result->{'biblionumber'};
153
                push @results, $metadata->{'biblionumber'};
140
                push @results, $result->{'title'};
154
                push @results, $metadata->{'title'};
141
            }
155
            }
142
        }
156
        }
143
    }
157
    }
(-)a/C4/Suggestions.pm (-41 lines)
Lines 26-32 BEGIN { Link Here
26
        GetSuggestion
26
        GetSuggestion
27
        ModStatus
27
        ModStatus
28
        ModSuggestion
28
        ModSuggestion
29
        MarcRecordFromNewSuggestion
30
    );
29
    );
31
}
30
}
32
31
Lines 136-181 sub ModSuggestion { Link Here
136
    return 1;    # No useful if the exception is raised earlier
135
    return 1;    # No useful if the exception is raised earlier
137
}
136
}
138
137
139
=head2 MarcRecordFromNewSuggestion
140
141
    $record = MarcRecordFromNewSuggestion ( $suggestion )
142
143
This function build a marc record object from a suggestion
144
145
=cut
146
147
sub MarcRecordFromNewSuggestion {
148
    my ($suggestion) = @_;
149
    my $record = MARC::Record->new();
150
151
    if ( my $isbn = $suggestion->{isbn} ) {
152
        for my $field (qw(biblioitems.isbn biblioitems.issn)) {
153
            my ( $tag, $subfield ) = GetMarcFromKohaField($field);
154
            $record->append_fields( MARC::Field->new( $tag, ' ', ' ', $subfield => $isbn ) );
155
        }
156
    } else {
157
        my ( $title_tag, $title_subfield ) = GetMarcFromKohaField('biblio.title');
158
        $record->append_fields( MARC::Field->new( $title_tag, ' ', ' ', $title_subfield => $suggestion->{title} ) );
159
160
        my ( $author_tag, $author_subfield ) = GetMarcFromKohaField('biblio.author');
161
        if ( $record->field($author_tag) ) {
162
            $record->field($author_tag)->add_subfields( $author_subfield => $suggestion->{author} );
163
        } else {
164
            $record->append_fields(
165
                MARC::Field->new( $author_tag, ' ', ' ', $author_subfield => $suggestion->{author} ) );
166
        }
167
    }
168
169
    my ( $it_tag, $it_subfield ) = GetMarcFromKohaField('biblioitems.itemtype');
170
    if ( $record->field($it_tag) ) {
171
        $record->field($it_tag)->add_subfields( $it_subfield => $suggestion->{itemtype} );
172
    } else {
173
        $record->append_fields( MARC::Field->new( $it_tag, ' ', ' ', $it_subfield => $suggestion->{itemtype} ) );
174
    }
175
176
    return $record;
177
}
178
179
1;
138
1;
180
__END__
139
__END__
181
140
(-)a/opac/opac-suggestions.pl (-6 / +2 lines)
Lines 23-34 use C4::Auth qw( get_template_and_user ); Link Here
23
use C4::Members;
23
use C4::Members;
24
use C4::Koha        qw( GetAuthorisedValues );
24
use C4::Koha        qw( GetAuthorisedValues );
25
use C4::Output      qw( output_html_with_http_headers );
25
use C4::Output      qw( output_html_with_http_headers );
26
use C4::Suggestions qw(
27
    MarcRecordFromNewSuggestion
28
);
29
use C4::Koha qw( GetAuthorisedValues );
26
use C4::Koha qw( GetAuthorisedValues );
30
use C4::Scrubber;
27
use C4::Scrubber;
31
use C4::Search qw( FindDuplicate );
28
use C4::Search qw( FindBiblioFromMetadata );
32
29
33
use Koha::AuthorisedValues;
30
use Koha::AuthorisedValues;
34
use Koha::Libraries;
31
use Koha::Libraries;
Lines 145-152 $suggestion->{suggestedby} = $borrowernumber; Link Here
145
if ( $op eq "cud-add_validate" && not $biblionumber )
142
if ( $op eq "cud-add_validate" && not $biblionumber )
146
{    # If we are creating the suggestion from an existing record we do not want to search for duplicates
143
{    # If we are creating the suggestion from an existing record we do not want to search for duplicates
147
    $op = 'cud-add_confirm';
144
    $op = 'cud-add_confirm';
148
    my $biblio = MarcRecordFromNewSuggestion($suggestion);
145
    if ( my ( $duplicatebiblionumber, $duplicatetitle ) =FindBiblioFromMetadata($suggestion) ) {
149
    if ( my ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($biblio) ) {
150
        push @messages,
146
        push @messages,
151
            { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
147
            { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
152
        $need_confirm = 1;
148
        $need_confirm = 1;
(-)a/suggestion/suggestion.pl (-11 / +10 lines)
Lines 25-31 use C4::Output qw( output_html_with_http_headers output_and_exit_if_error ); Link Here
25
use C4::Suggestions;
25
use C4::Suggestions;
26
use C4::Koha    qw( GetAuthorisedValues );
26
use C4::Koha    qw( GetAuthorisedValues );
27
use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget );
27
use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget );
28
use C4::Search  qw( FindDuplicate GetDistinctValues );
28
use C4::Search  qw( FindBiblioFromMetadata GetDistinctValues );
29
use C4::Members;
29
use C4::Members;
30
use Koha::DateUtils qw( dt_from_string );
30
use Koha::DateUtils qw( dt_from_string );
31
use Koha::AuthorisedValues;
31
use Koha::AuthorisedValues;
Lines 144-157 if ( $op =~ /cud-save/ ) { Link Here
144
144
145
    my @messages;
145
    my @messages;
146
146
147
    my $biblio = MarcRecordFromNewSuggestion(
147
    my $metadata = {
148
        {
148
        title    => $suggestion_only->{title},
149
            title    => $suggestion_only->{title},
149
        author   => $suggestion_only->{author},
150
            author   => $suggestion_only->{author},
150
        itemtype => $suggestion_only->{itemtype},
151
            itemtype => $suggestion_only->{itemtype},
151
        isbn     => $suggestion_only->{isbn},
152
            isbn     => $suggestion_only->{isbn},
152
    };
153
        }
154
    );
155
153
156
    my $manager = Koha::Patrons->find( $suggestion_only->{managedby} );
154
    my $manager = Koha::Patrons->find( $suggestion_only->{managedby} );
157
    if ( $manager && not $manager->has_permission( { suggestions => 'suggestions_manage' } ) ) {
155
    if ( $manager && not $manager->has_permission( { suggestions => 'suggestions_manage' } ) ) {
Lines 160-174 if ( $op =~ /cud-save/ ) { Link Here
160
            messages => \@messages,
158
            messages => \@messages,
161
        );
159
        );
162
    } elsif ( !$suggestion_only->{suggestionid}
160
    } elsif ( !$suggestion_only->{suggestionid}
163
        && ( my ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($biblio) )
161
        && ( my ( $duplicatebiblionumber, $duplicatetitle ) = FindBiblioFromMetadata($metadata) )
164
        && !$save_confirmed )
162
        && !$save_confirmed )
165
    {
163
    {
166
        push @messages,
164
        push @messages,
167
            { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
165
            { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
168
        $template->param(
166
        $template->param(
169
            messages     => \@messages,
167
            messages     => \@messages,
170
            need_confirm => 1
168
            need_confirm => 1,
171
        );
169
        );
170
        $stored_suggestion = $suggestion_only;
172
        $op = 'save';
171
        $op = 'save';
173
    } else {
172
    } else {
174
173
(-)a/t/db_dependent/Suggestions.t (-49 / +2 lines)
Lines 19-25 use Modern::Perl; Link Here
19
19
20
use DateTime::Duration;
20
use DateTime::Duration;
21
use Test::NoWarnings;
21
use Test::NoWarnings;
22
use Test::More tests => 44;
22
use Test::More tests => 40;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use t::lib::Mocks;
25
use t::lib::Mocks;
Lines 38-44 use Koha::Suggestions; Link Here
38
BEGIN {
38
BEGIN {
39
    use_ok(
39
    use_ok(
40
        'C4::Suggestions',
40
        'C4::Suggestions',
41
        qw( ModSuggestion MarcRecordFromNewSuggestion )
41
        qw( ModSuggestion )
42
    );
42
    );
43
}
43
}
44
44
Lines 328-351 my $suggestion2 = { Link Here
328
    itemtype => "LIV"
328
    itemtype => "LIV"
329
};
329
};
330
330
331
my $record = MarcRecordFromNewSuggestion($suggestion2);
332
333
is( "MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object" );
334
335
my ( $title_tag, $title_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.title');
336
337
is(
338
    $record->field($title_tag)->subfield($title_subfield), "Cuisine d'automne",
339
    "Record from suggestion title should be 'Cuisine d'automne'"
340
);
341
342
my ( $author_tag, $author_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.author');
343
344
is(
345
    $record->field($author_tag)->subfield($author_subfield), "Catherine",
346
    "Record from suggestion author should be 'Catherine'"
347
);
348
349
subtest 'EmailPurchaseSuggestions' => sub {
331
subtest 'EmailPurchaseSuggestions' => sub {
350
    plan tests => 11;
332
    plan tests => 11;
351
333
Lines 541-572 subtest 'place_hold tests' => sub { Link Here
541
523
542
};
524
};
543
525
544
subtest 'Suggestion with ISBN' => sub {
545
    my $suggestion_with_isbn = {
546
        isbn   => '1940997232',
547
        title  => "The Clouds",
548
        author => "Aristophanes",
549
    };
550
    my $record = MarcRecordFromNewSuggestion($suggestion_with_isbn);
551
    is( "MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object" );
552
553
    my ( $isbn_tag, $isbn_subfield ) = C4::Biblio::GetMarcFromKohaField('biblioitems.isbn');
554
    is(
555
        $record->field($isbn_tag)->subfield($isbn_subfield), "1940997232",
556
        "ISBN Record from suggestion ISBN should be '1940997232'"
557
    );
558
559
    my ( $issn_tag, $issn_subfield ) = C4::Biblio::GetMarcFromKohaField('biblioitems.issn');
560
    is(
561
        $record->field($issn_tag)->subfield($issn_subfield), "1940997232",
562
        "ISSN Record from suggestion ISBN should also be '1940997232'"
563
    );
564
565
    my ( $title_tag, $title_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.title');
566
    is( $record->field($title_tag), undef, "Record from suggestion title should be empty" );
567
568
    my ( $author_tag, $author_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.author');
569
    is( $record->field($author_tag), undef, "Record from suggestion author should be empty" );
570
};
571
572
$schema->storage->txn_rollback;
526
$schema->storage->txn_rollback;
573
- 

Return to bug 39732