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

(-)a/admin/koha2marclinks.pl (-117 / +65 lines)
Lines 1-6 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Copyright 2000-2002 Katipo Communications
3
# Copyright 2000-2002 Katipo Communications
4
# Copyright 2017 Rijksmuseum
4
#
5
#
5
# This file is part of Koha.
6
# This file is part of Koha.
6
#
7
#
Lines 17-37 Link Here
17
# You should have received a copy of the GNU General Public License
18
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
20
use strict;
21
use Modern::Perl;
21
use warnings;
22
use C4::Output;
23
use C4::Auth;
24
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
25
use C4::Context;
26
use C4::Biblio;
27
23
24
use Koha::Database;
25
use C4::Auth;
26
use C4::Biblio;
27
use C4::Output;
28
use Koha::BiblioFrameworks;
29
use Koha::Caches;
30
use Koha::MarcSubfieldStructures;
28
31
29
my $input       = new CGI;
32
my $input       = new CGI;
30
my $tablename   = $input->param('tablename');
31
$tablename      = "biblio" unless ($tablename);
32
my $kohafield   = $input->param('kohafield');
33
my $op          = $input->param('op');
34
my $script_name = 'koha2marclinks.pl';
35
33
36
my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
34
my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
37
    {
35
    {
Lines 44-164 my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( Link Here
44
    }
42
    }
45
);
43
);
46
44
47
if ($op) {
45
my $schema = Koha::Database->new->schema;
48
    $template->param(
49
        script_name => $script_name,
50
        $op         => 1
51
    );    # we show only the TMPL_VAR names $op
52
}
53
else {
54
    $template->param(
55
        script_name => $script_name,
56
        else        => 1
57
    );    # we show only the TMPL_VAR names $op
58
    $op = q{};
59
}
60
61
my $dbh = C4::Context->dbh;
62
my $cache = Koha::Caches->get_instance();
46
my $cache = Koha::Caches->get_instance();
63
47
64
################## ADD_FORM ##################################
48
# Update data before showing the form
65
# called by default. Used to create form to add or  modify a record
49
my $no_upd;
66
if ( $op eq 'add_form' ) {
67
    my $sth =
68
      $dbh->prepare(
69
"select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=? AND frameworkcode=''"
70
      );
71
    $sth->execute( $tablename . "." . $kohafield );
72
    my ( $defaulttagfield, $defaulttagsubfield, $defaultliblibrarian ) =
73
      $sth->fetchrow;
74
50
75
    for ( my $i = 0 ; $i <= 9 ; $i++ ) {
51
if( $input->param('add_field') ) {
76
        my $sth2 =
52
    # add a mapping to all frameworks
77
          $dbh->prepare(
53
    my ($kohafield, $tag, $sub) = split /,/, $input->param('add_field'), 3;
78
"select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like ? AND frameworkcode=''"
54
    Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub })->update({ kohafield => $kohafield });
79
          );
80
        $sth2->execute("$i%");
81
        my @marcarray;
82
        push @marcarray, " ";
83
        while ( my ( $field, $tagsubfield, $liblibrarian ) =
84
            $sth2->fetchrow_array )
85
        {
86
            push @marcarray, "$field $tagsubfield - $liblibrarian";
87
        }
88
        my $marclist = {
89
            values  => \@marcarray,
90
            default => "$defaulttagfield $defaulttagsubfield - $defaultliblibrarian",
91
        };
92
        $template->param( "marclist$i" => $marclist );
93
    }
94
    $template->param(
95
        tablename => $tablename,
96
        kohafield => $kohafield
97
    );
98
55
99
    # END $OP eq ADD_FORM
56
} elsif( $input->param('remove_field') ) {
100
################## ADD_VALIDATE ##################################
57
    # remove a mapping from all frameworks
101
    # called by add_form, used to insert/modify data in DB
58
    my ($tag, $sub) = split /,/, $input->param('remove_field'), 2;
102
}
59
    Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub })->update({ kohafield => undef });
103
elsif ( $op eq 'add_validate' ) {
104
60
105
    #----- empty koha field :
61
} else {
106
    $dbh->do(
62
    $no_upd = 1;
107
"update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'"
63
}
108
    );
109
64
110
    #---- reload if not empty
65
# Clear the cache when needed
111
    my @temp = split / /, $input->param('marc');
66
unless( $no_upd ) {
112
    $dbh->do(
67
    for( qw| default_value_for_mod_marc- MarcSubfieldStructure- | ) {
113
"update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'"
68
        $cache->clear_from_cache($_);
114
    );
69
    }
115
    # We could get a list of all frameworks and do them one-by-one, or zap
70
}
116
    # everything.
117
    $cache->flush_all();
118
    print $input->redirect("/cgi-bin/koha/admin/koha2marclinks.pl?tablename=$tablename");
119
    exit;
120
71
121
    # END $OP eq ADD_VALIDATE
72
# Build/Show the form
122
################## DEFAULT ##################################
73
my $dbix_map = {
74
    # Koha to MARC mappings are found in only three tables
75
    biblio => 'Biblio',
76
    biblioitems => 'Biblioitem',
77
    items => 'Item',
78
};
79
my @cols;
80
foreach my $tbl ( sort keys %{$dbix_map} ) {
81
    push @cols,
82
        map { "$tbl.$_" } $schema->source( $dbix_map->{$tbl} )->columns;
123
}
83
}
124
else {    # DEFAULT
84
my $kohafields = Koha::MarcSubfieldStructures->search({
125
    my $sth =
85
    frameworkcode => q{},
126
      $dbh->prepare(
86
    kohafield => { '>', '' },
127
q|select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure where kohafield is not NULL and kohafield != ''|
87
});
128
      );
88
my @loop_data;
129
    $sth->execute;
89
foreach my $col ( @cols ) {
130
    my %fields;
90
    my $found;
131
    while ( ( my $tagfield, my $tagsubfield, my $liblibrarian, my $kohafield ) =
91
    my $readonly = $col =~ /\.(biblio|biblioitem|item)number$/;
132
        $sth->fetchrow ) {
92
    foreach my $row ( $kohafields->search({ kohafield => $col }) ) {
133
        $fields{$kohafield}->{tagfield}     = $tagfield;
93
        $found = 1;
134
        $fields{$kohafield}->{tagsubfield}  = $tagsubfield;
94
        push @loop_data, {
135
        $fields{$kohafield}->{liblibrarian} = $liblibrarian;
95
            kohafield    => $col,
96
            tagfield     => $row->tagfield,
97
            tagsubfield  => $row->tagsubfield,
98
            liblibrarian => $row->liblibrarian,
99
            readonly     => $readonly,
100
        };
136
    }
101
    }
102
    push @loop_data, {
103
            kohafield    => $col,
104
            readonly     => $readonly,
105
    } if !$found;
106
}
137
107
138
  #XXX: This might not work. Maybe should use a DBI call instead of SHOW COLUMNS
108
$template->param(
139
    my $sth2 = $dbh->prepare("SHOW COLUMNS from $tablename");
109
    loop       => \@loop_data,
140
    $sth2->execute;
110
);
141
111
142
    my @loop_data = ();
143
    while ( ( my $field ) = $sth2->fetchrow_array ) {
144
        my %row_data;    # get a fresh hash for the row data
145
        $row_data{tagfield} = $fields{ $tablename . "." . $field }->{tagfield};
146
        $row_data{tagsubfield} =
147
          $fields{ $tablename . "." . $field }->{tagsubfield};
148
        $row_data{liblibrarian} =
149
          $fields{ $tablename . "." . $field }->{liblibrarian};
150
        $row_data{kohafield} = $field;
151
        $row_data{edit} =
152
"$script_name?op=add_form&amp;tablename=$tablename&amp;kohafield=$field";
153
        push( @loop_data, \%row_data );
154
    }
155
    my $tablenames = {
156
        values  => [ 'biblio', 'biblioitems', 'items' ],
157
        default => $tablename,
158
    };
159
    $template->param(
160
        loop      => \@loop_data,
161
        tablename => $tablenames,
162
    );
163
}    #---- END $OP eq DEFAULT
164
output_html_with_http_headers $input, $cookie, $template->output;
112
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/koha2marclinks.tt (-84 / +41 lines)
Lines 1-19 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
1
[% INCLUDE 'doc-head-open.inc' %]
2
<title>Koha &rsaquo; Administration &rsaquo;
2
<title>Koha &rsaquo; Administration &rsaquo; Koha to MARC mapping</title>
3
[% IF ( add_form ) %] Koha to MARC mapping &rsaquo; Connect [% tablename %].[% kohafield %] to a MARC subfield[% END %]
4
[% IF ( else ) %]Koha to MARC mapping [% tagfield %][% END %]</title>
5
3
6
[% INCLUDE 'doc-head-close.inc' %]
4
[% INCLUDE 'doc-head-close.inc' %]
7
<script type="text/javascript">
5
<script type="text/javascript">
8
//<![CDATA[
6
//<![CDATA[
9
$(document).ready(function() {
7
$(document).ready(function() {
10
    [% IF tablename.default == 'biblio' %]
11
        $("#tablename option:first").attr('selected','selected');
12
    [% END %]
13
    $('#tablename').change(function() {
14
        $('#koha2marc').submit();
15
    });
16
});
8
});
9
10
function AddFld(kohafield) {
11
    var fieldstr = prompt( _("Adding a mapping for: ") + kohafield + ".\n" + _("Please enter field tag and subfield code, separated by a comma. (For control fields: add '@' as subfield code.)\nThe change will be applied immediately.") );
12
    var temp = fieldstr.split(',');
13
    if( temp.length == 2 ) {
14
        $('#add_field').val( kohafield+','+fieldstr );
15
        $('#koha2marc').submit();
16
    }
17
}
18
19
function RemFld(tagfield, subfield ) {
20
    if( confirm( _("Mapping will be removed for: ") + tagfield + subfield + ".\n" + _("The change will be applied immediately." ))) {
21
        $('#remove_field').val(tagfield+','+subfield);
22
        $('#koha2marc').submit();
23
    }
24
}
17
//]]>
25
//]]>
18
</script>
26
</script>
19
</head>
27
</head>
Lines 22-127 $(document).ready(function() { Link Here
22
[% INCLUDE 'header.inc' %]
30
[% INCLUDE 'header.inc' %]
23
[% INCLUDE 'prefs-admin-search.inc' %]
31
[% INCLUDE 'prefs-admin-search.inc' %]
24
32
25
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; [% IF ( add_form ) %] <a href="/cgi-bin/koha/admin/koha2marclinks.pl">Koha to MARC Mapping</a> &rsaquo; Connect [% tablename %].[% kohafield %] to a MARC subfield[% END %]
33
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; <a href="/cgi-bin/koha/admin/koha2marclinks.pl">Koha to MARC Mapping</a> &rsaquo;</div>
26
[% IF ( else ) %]Koha to MARC mapping[% tagfield %][% END %]</div>
27
[% IF ( add_form ) %]
28
<div id="doc" class="yui-t7">
29
34
30
<div id="bd">
31
    <div id="yui-main">
32
    <div class="yui-g">
33
[% ELSE %]
34
<div id="doc3" class="yui-t2">
35
<div id="doc3" class="yui-t2">
35
36
36
<div id="bd">
37
<div id="bd">
37
    <div id="yui-main">
38
    <div id="yui-main">
38
    <div class="yui-b">
39
    <div class="yui-b">
39
[% END %]
40
41
[% IF ( add_form ) %]
42
<h1>Connect [% tablename %].[% kohafield %] to a MARC subfield</h1>
43
    <p>Choose and validate 1 MARC subfield for <strong>[% tablename %].[% kohafield %]</strong>.</p>
44
    <p><b>Note: </b>All frameworks will be modified which is usually what you need, but you have been warned.</p>
45
    <fieldset class="rows">
46
        <ol>
47
        [% FOR i IN [ 0 .. 9 ] %]
48
            [% marclist = "marclist$i" %]
49
            <li>
50
                <form action="[% script_name %]" method="post">
51
                <input type="hidden" name="op" value="add_validate" />
52
                <input type="hidden" name="tablename" value="[% tablename %]" />
53
                <input type="hidden" name="kohafield" value="[% kohafield %]" />
54
                <label>[% i %]00s</label>
55
                <select name="marc" id="marc" size="1" width="570" style="width: 570px;">
56
                    [% FOREACH value IN $marclist.values %]
57
                        [% IF ( value == $marclist.default ) %]
58
                            <option value="[% value %]" selected="selected">[% value %]</option>
59
                        [% ELSE %]
60
                            <option value="[% value %]">[% value %]</option>
61
                        [% END %]
62
                    [% END %]
63
                </select>
64
                <input type="submit" value="OK" />
65
                </form>
66
            </li>
67
        [% END %]
68
        </ol>
69
    </fieldset>
70
	<fieldset class="action">
71
            <form action="[% script_name %]" name="Aform" method="post">
72
                <input type="hidden" name="op" value="add_validate" />
73
                <input type="hidden" name="tablename" value="[% tablename %]" />
74
                <input type="hidden" name="kohafield" value="[% kohafield %]" />
75
                <input type="hidden" name="choice" value="" />
76
                <input type="submit" value="Click to &quot;Unmap&quot;" />
77
            </form>
78
	<a class="cancel" href="/cgi-bin/koha/admin/koha2marclinks.pl">Cancel</a>
79
	</fieldset>
80
[% END %]
81
82
40
83
[% IF ( else ) %]
41
<h1>Koha to MARC mapping</h1>
42
<br/>
43
<form action="/cgi-bin/koha/admin/koha2marclinks.pl" method="post" id="koha2marc">
84
44
85
<h1>Koha to MARC mapping [% tagfield %]</h1>
45
<table id="kohafields">
86
<form action="[% script_name %]" method="post" id="koha2marc">
46
<thead><tr>
87
<p>
88
    <select name="tablename" id="tablename" size="1">
89
    [% FOREACH value IN tablename.values %]
90
        [% IF ( value == tablename.default ) %]
91
        <option value="[% value %]" selected="selected">[% value %]</option>
92
        [% ELSE %]
93
        <option value="[% value %]">[% value %]</option>
94
        [% END %]
95
    [% END %]
96
    </select>
97
</p>
98
</form>
99
<table>
100
<tr>
101
    <th>Koha field</th>
47
    <th>Koha field</th>
102
    <th>Tag</th>
48
    <th>Tag</th>
103
    <th>Subfield</th>
49
    <th>Subfield</th>
104
    <th>Lib</th>
50
    <th>Lib</th>
105
    <th>&nbsp;</th>
51
    <th>&nbsp;</th>
106
</tr>
52
</tr></thead>
53
<tbody>
107
[% FOREACH loo IN loop %]
54
[% FOREACH loo IN loop %]
108
<tr>
55
<tr>
109
    <td><a href="[% loo.edit %]">[% loo.kohafield %]</a></td>
56
    <td>[% loo.kohafield %]</td>
110
    <td>[% loo.tagfield %]</td>
57
    <td>[% loo.tagfield %]</td>
111
    <td>[% loo.tagsubfield %]</td>
58
    <td>[% loo.tagsubfield %]</td>
112
    <td>[% loo.liblibrarian %]</td>
59
    <td>[% loo.liblibrarian %]</td>
113
    <td><a href="[% loo.edit %]" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a></td>
60
    <td>
61
        [% IF !loo.readonly %]
62
            <a onclick="AddFld('[% loo.kohafield %]');" class="btn btn-default btn-xs">Add</a>
63
            [% IF loo.tagfield %] <a onclick="RemFld('[% loo.tagfield %]','[% loo.tagsubfield %]');" class="btn btn-default btn-xs">Remove</a> [% END %]
64
        [% END %]
65
    </td>
66
    </td>
114
</tr>
67
</tr>
115
[% END %]
68
[% END %]
69
</tbody>
116
</table>
70
</table>
117
[% END %]
71
72
<input id="remove_field" name="remove_field" type="hidden" value=""/>
73
<input id="add_field" name="add_field" type="hidden" value=""/>
74
75
</form>
118
76
119
</div>
77
</div>
120
</div>
78
</div>
121
[% UNLESS ( add_form ) %]
79
122
<div class="yui-b">
80
<div class="yui-b">
123
[% INCLUDE 'admin-menu.inc' %]
81
[% INCLUDE 'admin-menu.inc' %]
124
</div>
82
</div>
125
[% END %]
83
126
</div>
84
</div>
127
[% INCLUDE 'intranet-bottom.inc' %]
85
[% INCLUDE 'intranet-bottom.inc' %]
128
- 

Return to bug 10306