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

(-)a/C4/Search.pm (-20 / +32 lines)
Lines 48-53 BEGIN { Link Here
48
    @ISA       = qw(Exporter);
48
    @ISA       = qw(Exporter);
49
    @EXPORT_OK = qw(
49
    @EXPORT_OK = qw(
50
        FindDuplicate
50
        FindDuplicate
51
        FindBiblioFromMetadata
51
        SimpleSearch
52
        SimpleSearch
52
        searchResults
53
        searchResults
53
        getRecords
54
        getRecords
Lines 80-86 This module provides searching functions for Koha's bibliographic databases Link Here
80
81
81
=head2 FindDuplicate
82
=head2 FindDuplicate
82
83
83
($biblionumber,$biblionumber,$title) = FindDuplicate($record);
84
($biblionumber,$title) = FindDuplicate($record);
84
85
85
This function attempts to find duplicate records using a hard-coded, fairly simplistic algorithm
86
This function attempts to find duplicate records using a hard-coded, fairly simplistic algorithm
86
87
Lines 88-124 This function attempts to find duplicate records using a hard-coded, fairly simp Link Here
88
89
89
sub FindDuplicate {
90
sub FindDuplicate {
90
    my ($record) = @_;
91
    my ($record) = @_;
91
    my $dbh      = C4::Context->dbh;
92
    my $result = TransformMarcToKoha( { record => $record } );
92
    my $result   = TransformMarcToKoha( { record => $record } );
93
    return FindBiblioFromMetadata($result);
93
    my $sth;
94
}
95
96
=head2 FindBiblioFromMetadata
97
98
($biblionumber, $title) = FindBiblioFromData($data);
99
100
This function attempts to find records matching data using a hard-coded, fairly simplistic algorithm
101
102
=cut
103
104
sub FindBiblioFromMetadata {
105
    my $data = shift;
94
    my $query;
106
    my $query;
95
107
96
    # search duplicate on ISBN, easy and fast..
108
    # search duplicate on ISBN, easy and fast..
97
    # ... normalize first
109
    # ... normalize first
98
    if ( $result->{isbn} ) {
110
    if ( $data->{isbn} ) {
99
        $result->{isbn} =~ s/\(.*$//;
111
        $data->{isbn} =~ s/\(.*$//;
100
        $result->{isbn} =~ s/\s+$//;
112
        $data->{isbn} =~ s/\s+$//;
101
        $result->{isbn} =~ s/\|/OR/;
113
        $data->{isbn} =~ s/\|/OR/;
102
        $query = "isbn:$result->{isbn}";
114
        $query = "isbn:$data->{isbn}";
103
    } else {
115
    } else {
104
116
105
        my $titleindex  = 'ti,ext';
117
        my $titleindex  = 'ti,ext';
106
        my $authorindex = 'au,ext';
118
        my $authorindex = 'au,ext';
107
        my $op          = 'AND';
119
        my $op          = 'AND';
108
120
109
        $result->{title} =~ s /\\//g;
121
        $data->{title} =~ s /\\//g;
110
        $result->{title} =~ s /\"//g;
122
        $data->{title} =~ s /\"//g;
111
        $result->{title} =~ s /\(//g;
123
        $data->{title} =~ s /\(//g;
112
        $result->{title} =~ s /\)//g;
124
        $data->{title} =~ s /\)//g;
113
125
114
        $query = "$titleindex:\"$result->{title}\"";
126
        $query = "$titleindex:\"$data->{title}\"";
115
        if ( $result->{author} ) {
127
        if ( $data->{author} ) {
116
            $result->{author} =~ s /\\//g;
128
            $data->{author} =~ s /\\//g;
117
            $result->{author} =~ s /\"//g;
129
            $data->{author} =~ s /\"//g;
118
            $result->{author} =~ s /\(//g;
130
            $data->{author} =~ s /\(//g;
119
            $result->{author} =~ s /\)//g;
131
            $data->{author} =~ s /\)//g;
120
132
121
            $query .= " $op $authorindex:\"$result->{author}\"";
133
            $query .= " $op $authorindex:\"$data->{author}\"";
122
        }
134
        }
123
    }
135
    }
124
136
(-)a/C4/Suggestions.pm (-41 lines)
Lines 34-40 use base qw(Exporter); Link Here
34
our @EXPORT = qw(
34
our @EXPORT = qw(
35
    ModStatus
35
    ModStatus
36
    ModSuggestion
36
    ModSuggestion
37
    MarcRecordFromNewSuggestion
38
);
37
);
39
38
40
=head1 NAME
39
=head1 NAME
Lines 133-178 sub ModSuggestion { Link Here
133
    return 1;    # No useful if the exception is raised earlier
132
    return 1;    # No useful if the exception is raised earlier
134
}
133
}
135
134
136
=head2 MarcRecordFromNewSuggestion
137
138
    $record = MarcRecordFromNewSuggestion ( $suggestion )
139
140
This function build a marc record object from a suggestion
141
142
=cut
143
144
sub MarcRecordFromNewSuggestion {
145
    my ($suggestion) = @_;
146
    my $record = MARC::Record->new();
147
148
    if ( my $isbn = $suggestion->{isbn} ) {
149
        for my $field (qw(biblioitems.isbn biblioitems.issn)) {
150
            my ( $tag, $subfield ) = GetMarcFromKohaField($field);
151
            $record->append_fields( MARC::Field->new( $tag, ' ', ' ', $subfield => $isbn ) );
152
        }
153
    } else {
154
        my ( $title_tag, $title_subfield ) = GetMarcFromKohaField('biblio.title');
155
        $record->append_fields( MARC::Field->new( $title_tag, ' ', ' ', $title_subfield => $suggestion->{title} ) );
156
157
        my ( $author_tag, $author_subfield ) = GetMarcFromKohaField('biblio.author');
158
        if ( $record->field($author_tag) ) {
159
            $record->field($author_tag)->add_subfields( $author_subfield => $suggestion->{author} );
160
        } else {
161
            $record->append_fields(
162
                MARC::Field->new( $author_tag, ' ', ' ', $author_subfield => $suggestion->{author} ) );
163
        }
164
    }
165
166
    my ( $it_tag, $it_subfield ) = GetMarcFromKohaField('biblioitems.itemtype');
167
    if ( $record->field($it_tag) ) {
168
        $record->field($it_tag)->add_subfields( $it_subfield => $suggestion->{itemtype} );
169
    } else {
170
        $record->append_fields( MARC::Field->new( $it_tag, ' ', ' ', $it_subfield => $suggestion->{itemtype} ) );
171
    }
172
173
    return $record;
174
}
175
176
1;
135
1;
177
__END__
136
__END__
178
137
(-)a/opac/opac-suggestions.pl (-9 / +5 lines)
Lines 21-34 use CGI qw ( -utf8 ); Link Here
21
use Encode;
21
use Encode;
22
use C4::Auth qw( get_template_and_user );
22
use C4::Auth qw( get_template_and_user );
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(
26
use C4::Koha   qw( GetAuthorisedValues );
27
    MarcRecordFromNewSuggestion
28
);
29
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 (-10 / +8 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 156-169 if ( $op =~ /cud-save/ ) { Link Here
156
156
157
    my @messages;
157
    my @messages;
158
158
159
    my $biblio = MarcRecordFromNewSuggestion(
159
    my $metadata = {
160
        {
160
        title    => $suggestion_only->{title},
161
            title    => $suggestion_only->{title},
161
        author   => $suggestion_only->{author},
162
            author   => $suggestion_only->{author},
162
        itemtype => $suggestion_only->{itemtype},
163
            itemtype => $suggestion_only->{itemtype},
163
        isbn     => $suggestion_only->{isbn},
164
            isbn     => $suggestion_only->{isbn},
164
    };
165
        }
166
    );
167
165
168
    my $manager = Koha::Patrons->find( $suggestion_only->{managedby} );
166
    my $manager = Koha::Patrons->find( $suggestion_only->{managedby} );
169
    if ( $manager && not $manager->has_permission( { suggestions => 'suggestions_manage' } ) ) {
167
    if ( $manager && not $manager->has_permission( { suggestions => 'suggestions_manage' } ) ) {
Lines 176-182 if ( $op =~ /cud-save/ ) { Link Here
176
        delete $suggestion_ref->{managedby};
174
        delete $suggestion_ref->{managedby};
177
        Init($suggestion_ref);
175
        Init($suggestion_ref);
178
    } elsif ( !$suggestion_only->{suggestionid}
176
    } elsif ( !$suggestion_only->{suggestionid}
179
        && ( my ( $duplicatebiblionumber, $duplicatetitle ) = FindDuplicate($biblio) )
177
        && ( my ( $duplicatebiblionumber, $duplicatetitle ) = FindBiblioFromMetadata($metadata) )
180
        && !$save_confirmed )
178
        && !$save_confirmed )
181
    {
179
    {
182
        push @messages,
180
        push @messages,
(-)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 340-363 my $suggestion2 = { Link Here
340
    itemtype => "LIV"
340
    itemtype => "LIV"
341
};
341
};
342
342
343
my $record = MarcRecordFromNewSuggestion($suggestion2);
344
345
is( "MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object" );
346
347
my ( $title_tag, $title_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.title');
348
349
is(
350
    $record->field($title_tag)->subfield($title_subfield), "Cuisine d'automne",
351
    "Record from suggestion title should be 'Cuisine d'automne'"
352
);
353
354
my ( $author_tag, $author_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.author');
355
356
is(
357
    $record->field($author_tag)->subfield($author_subfield), "Catherine",
358
    "Record from suggestion author should be 'Catherine'"
359
);
360
361
subtest 'EmailPurchaseSuggestions' => sub {
343
subtest 'EmailPurchaseSuggestions' => sub {
362
    plan tests => 11;
344
    plan tests => 11;
363
345
Lines 553-584 subtest 'place_hold tests' => sub { Link Here
553
535
554
};
536
};
555
537
556
subtest 'Suggestion with ISBN' => sub {
557
    my $suggestion_with_isbn = {
558
        isbn   => '1940997232',
559
        title  => "The Clouds",
560
        author => "Aristophanes",
561
    };
562
    my $record = MarcRecordFromNewSuggestion($suggestion_with_isbn);
563
    is( "MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object" );
564
565
    my ( $isbn_tag, $isbn_subfield ) = C4::Biblio::GetMarcFromKohaField('biblioitems.isbn');
566
    is(
567
        $record->field($isbn_tag)->subfield($isbn_subfield), "1940997232",
568
        "ISBN Record from suggestion ISBN should be '1940997232'"
569
    );
570
571
    my ( $issn_tag, $issn_subfield ) = C4::Biblio::GetMarcFromKohaField('biblioitems.issn');
572
    is(
573
        $record->field($issn_tag)->subfield($issn_subfield), "1940997232",
574
        "ISSN Record from suggestion ISBN should also be '1940997232'"
575
    );
576
577
    my ( $title_tag, $title_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.title');
578
    is( $record->field($title_tag), undef, "Record from suggestion title should be empty" );
579
580
    my ( $author_tag, $author_subfield ) = C4::Biblio::GetMarcFromKohaField('biblio.author');
581
    is( $record->field($author_tag), undef, "Record from suggestion author should be empty" );
582
};
583
584
$schema->storage->txn_rollback;
538
$schema->storage->txn_rollback;
585
- 

Return to bug 39732