Bug 19528: Fix a few typos like corrosponding
[koha.git] / authorities / authorities.pl
index 19a03e8..55d34d1 100755 (executable)
@@ -5,31 +5,34 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Output;
 use C4::AuthoritiesMarc;
+use C4::ImportBatch; #GetImportRecordMarc
 use C4::Context;
-use C4::Koha; # XXX subfield_is_koha_internal_p
+use C4::Koha;
 use Date::Calc qw(Today);
 use MARC::File::USMARC;
 use MARC::File::XML;
 use C4::Biblio;
+use Koha::Authority::Types;
+use Koha::ItemTypes;
 use vars qw( $tagslib);
 use vars qw( $authorised_values_sth);
 use vars qw( $is_a_modif );
@@ -47,6 +50,21 @@ builds list, depending on authorised value...
 
 =cut
 
+sub MARCfindbreeding_auth {
+    my ( $id ) = @_;
+    my ($marc, $encoding) = GetImportRecordMarc($id);
+    if ($marc) {
+        my $record = MARC::Record->new_from_usmarc($marc);
+        if ( !defined(ref($record)) ) {
+                return -1;
+        } else {
+            return $record, $encoding;
+        }
+    } else {
+        return -1;
+    }
+}
+
 sub build_authorized_values_list {
     my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
 
@@ -67,22 +85,17 @@ sub build_authorized_values_list {
             push @authorised_values, $branchcode;
             $authorised_lib{$branchcode} = $branchname;
         }
-
-        #----- itemtypes
     }
     elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
-        my $sth =
-        $dbh->prepare(
-            "select itemtype,description from itemtypes order by description");
-        $sth->execute;
         push @authorised_values, ""
-        unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
-        
+          unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
+            && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
+
         my $itemtype;
-        
-        while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
-            push @authorised_values, $itemtype;
-            $authorised_lib{$itemtype} = $description;
+        my $itemtypes = Koha::ItemTypes->search_with_localization;
+        while ( $itemtype = $itemtypes->next ) {
+            push @authorised_values, $itemtype->itemtype;
+            $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
         }
         $value = $itemtype unless ($value);
 
@@ -93,25 +106,22 @@ sub build_authorized_values_list {
             $tagslib->{$tag}->{$subfield}->{authorised_value} );
 
         push @authorised_values, ""
-        unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
+          unless ( $tagslib->{$tag}->{$subfield}->{mandatory}
+            && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
 
         while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
             push @authorised_values, $value;
             $authorised_lib{$value} = $lib;
         }
     }
-    return CGI::scrolling_list(
-        -name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
-        -values   => \@authorised_values,
-        -default  => $value,
-        -labels   => \%authorised_lib,
-        -override => 1,
-        -size     => 1,
-        -multiple => 0,
-        -tabindex => 1,
-        -id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
-        -class    => "input_marceditor",
-    );
+    return {
+        type     => 'select',
+        id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
+        name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
+        values   => \@authorised_values,
+        labels   => \%authorised_lib,
+        default  => $value,
+    };
 }
 
 
@@ -161,8 +171,7 @@ sub create_input {
     my %subfield_data = (
         tag        => $tag,
         subfield   => $id_subfield,
-        marc_lib   => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
-        marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib}, 
+        marc_lib       => $tagslib->{$tag}->{$subfield}->{lib},
         tag_mandatory  => $tagslib->{$tag}->{mandatory},
         mandatory      => $tagslib->{$tag}->{$subfield}->{mandatory},
         repeatable     => $tagslib->{$tag}->{$subfield}->{repeatable},
@@ -191,72 +200,61 @@ sub create_input {
     # it's a thesaurus / authority field
     }
     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
-        $subfield_data{marc_value} =
-    "<input type=\"text\"
-            id=\"".$subfield_data{id}."\"
-            name=\"".$subfield_data{id}."\"
-            value=\"$value\"
-            class=\"input_marceditor\"
-            tabindex=\"1\" \/>
-        <a href=\"#\" class=\"buttonDot\"
-        onclick=\"openAuth(this.parentNode.getElementsByTagName('input')[1].id,'".$tagslib->{$tag}->{$subfield}->{authtypecode}."','auth'); return false;\" tabindex=\"1\" title=\"Tag Editor\">...</a>
-    ";
-    # it's a plugin field
+        $subfield_data{marc_value} = {
+            type         => 'text1',
+            id           => $subfield_data{id},
+            name         => $subfield_data{id},
+            value        => $value,
+            authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
+        };
     }
-    elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
-
-        # opening plugin. Just check whether we are on a developer computer on a production one
-        # (the cgidir differs)
-        my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
-        unless (-r $cgidir and -d $cgidir) {
-            $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
+    elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) { # plugin
+        require Koha::FrameworkPlugin;
+        my $plugin = Koha::FrameworkPlugin->new({
+            name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
+        });
+        my $pars=  { dbh => $dbh, record => $rec, tagslib =>$tagslib,
+            id => $subfield_data{id}, tabloop => $tabloop };
+        $plugin->build( $pars );
+        if( !$plugin->errstr ) {
+            $subfield_data{marc_value} = {
+                type       => 'text2',
+                id        => $subfield_data{id},
+                name      => $subfield_data{id},
+                value     => $value,
+                maxlength => $max_length,
+                javascript => $plugin->javascript,
+                noclick    => $plugin->noclick,
+            };
+        } else { # warn and supply default field
+            warn $plugin->errstr;
+            $subfield_data{marc_value} = {
+                type      => 'text',
+                id        => $subfield_data{id},
+                name      => $subfield_data{id},
+                value     => $value,
+                maxlength => $max_length,
+            };
         }
-        my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
-        do $plugin || die "Plugin Failed: ".$plugin;
-        my $extended_param;
-        eval{
-            $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
-        };
-        my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
-#         my ( $function_name, $javascript,$extended_param );
-        
-        $subfield_data{marc_value} =
-            "<input tabindex=\"1\"
-                    type=\"text\"
-                    id=\"".$subfield_data{id}."\"
-                    size=\"67\"
-                    maxlength=\"$max_length\"
-                    name=\"".$subfield_data{id}."\"
-                    value=\"$value\"
-                    class=\"input_marceditor\"
-                    onfocus=\"Focus$function_name($index_tag)\"
-                    onblur=\"Blur$function_name($index_tag); \" \/>
-                    <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
-                    $javascript";
-        # it's an hidden field
     }
+    # it's an hidden field
     elsif ( $tag eq '' ) {
-        $subfield_data{marc_value} =
-            "<input tabindex=\"1\"
-                    type=\"hidden\"
-                    id=\"".$subfield_data{id}."\"
-                    name=\"".$subfield_data{id}."\"
-                    size=\"67\"
-                    maxlength=\"$max_length\"
-                    value=\"$value\" \/>
-            ";
+        $subfield_data{marc_value} = {
+            type      => 'hidden',
+            id        => $subfield_data{id},
+            name      => $subfield_data{id},
+            value     => $value,
+            maxlength => $max_length,
+        }
     }
     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
-        $subfield_data{marc_value} =
-            "<input type=\"text\"
-                    id=\"".$subfield_data{id}."\"
-                    name=\"".$subfield_data{id}."\"
-                    class=\"input_marceditor\"
-                    tabindex=\"1\"
-                    size=\"67\"
-                    maxlength=\"$max_length\"
-                    value=\"$value\"
-            \/>";
+        $subfield_data{marc_value} = {
+            type => 'text',
+            id        => $subfield_data{id},
+            name      => $subfield_data{id},
+            value     => $value,
+            maxlength => $max_length,
+        };
 
         # it's a standard field
     }
@@ -271,30 +269,24 @@ sub create_input {
                 && C4::Context->preference("marcflavour") eq "MARC21" )
         )
         {
-            $subfield_data{marc_value} =
-                "<textarea cols=\"70\"
-                        rows=\"4\"
-                        id=\"".$subfield_data{id}."\"
-                        name=\"".$subfield_data{id}."\"
-                        class=\"input_marceditor\"
-                        tabindex=\"1\"
-                        size=\"67\"
-                        maxlength=\"$max_length\"
-                        >$value</textarea>
-                ";
+            $subfield_data{marc_value} = {
+                type => 'textarea',
+                id        => $subfield_data{id},
+                name      => $subfield_data{id},
+                value     => $value,
+                maxlength => $max_length,
+            };
+
         }
         else {
-            $subfield_data{marc_value} =
-                "<input type=\"text\"
-                        id=\"".$subfield_data{id}."\"
-                        name=\"".$subfield_data{id}."\"
-                        value=\"$value\"
-                        tabindex=\"1\"
-                        size=\"67\"
-                        maxlength=\"$max_length\"
-                        class=\"input_marceditor\"
-                \/>
-                ";
+            $subfield_data{marc_value} = {
+                type => 'text',
+                id        => $subfield_data{id},
+                name      => $subfield_data{id},
+                value     => $value,
+                maxlength => $max_length,
+            };
+
         }
     }
     $subfield_data{'index_subfield'} = $index_subfield;
@@ -326,6 +318,32 @@ sub CreateKey {
     return int(rand(1000000));
 }
 
+=item GetMandatoryFieldZ3950
+
+    This function return an hashref which contains all mandatory field
+    to search with z3950 server.
+
+=cut
+
+sub GetMandatoryFieldZ3950 {
+    my $authtypecode = shift;
+    if ( C4::Context->preference('marcflavour') eq 'MARC21' ){
+        return {
+            '100a' => 'authorpersonal',
+            '110a' => 'authorcorp',
+            '111a' => 'authormeetingcon',
+            '130a' => 'uniformtitle',
+            '150a' => 'topic',
+        };
+    }else{
+        return {
+            '200a' => 'authorpersonal',
+            '210a' => 'authormeetingcon', #210 in UNIMARC is used for both corporation and meeting
+            '230a' => 'uniformtitle',
+        };
+    }
+}
+
 sub build_tabs {
     my ( $template, $record, $dbh, $encoding,$input ) = @_;
 
@@ -386,6 +404,7 @@ sub build_tabs {
                             $subfield = '@';
                         }
                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
+                        next if $tagslib->{$tag}->{$subfield}->{hidden};
                         push(
                             @subfields_data,
                             &create_input(
@@ -401,6 +420,7 @@ sub build_tabs {
                             my $value    = $subfields[$subfieldcount][1];
                             next if ( length $subfield != 1 );
                             next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
+                            next if $tagslib->{$tag}->{$subfield}->{hidden};
                             push(
                                 @subfields_data,
                                 &create_input(
@@ -417,10 +437,7 @@ sub build_tabs {
                         next if ( length $subfield != 1 );
                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
                         next if ( $tag < 10 );
-                        next
-                        if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
-                            or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
-                        );    #check for visibility flag
+                        next if $tagslib->{$tag}->{$subfield}->{hidden};
                         next if ( defined( $field->subfield($subfield) ) );
                         push(
                             @subfields_data,
@@ -459,9 +476,7 @@ sub build_tabs {
                 my @subfields_data;
                 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
                     next if ( length $subfield != 1 );
-                    next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
-                                or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
-                            ;    #check for visibility flag
+                    next if $tagslib->{$tag}->{$subfield}->{hidden};
                     next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
                     push(
                         @subfields_data,
@@ -520,7 +535,10 @@ sub build_hidden_data {
             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
-            $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
+            $subfield_data{marc_value} = {
+                type => 'hidden_simple',
+                name => 'field_value[]',
+            };
             push(@loop_data, \%subfield_data);
             $i++
         }
@@ -544,25 +562,35 @@ my $nonav = $input->param('nonav');
 my $myindex = $input->param('index');
 my $linkid=$input->param('linkid');
 my $authtypecode = $input->param('authtypecode');
+my $breedingid    = $input->param('breedingid');
 
 my $dbh = C4::Context->dbh;
 if(!$authtypecode) {
-  $authtypecode = $authid? &GetAuthTypeCode($authid): '';
+    $authtypecode = $authid ? Koha::Authorities->find($authid)->authtypecode : '';
 }
 
 my ($template, $loggedinuser, $cookie)
-    = get_template_and_user({template_name => "authorities/authorities.tmpl",
+    = get_template_and_user({template_name => "authorities/authorities.tt",
                             query => $input,
                             type => "intranet",
                             authnotrequired => 0,
                             flagsrequired => {editauthorities => 1},
                             debug => 1,
                             });
-$template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
+$template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,breedingid=>$breedingid,);
+
 $tagslib = GetTagsLabels(1,$authtypecode);
+$mandatory_z3950 = GetMandatoryFieldZ3950($authtypecode);
+
 my $record=-1;
 my $encoding="";
-$record = GetAuthority($authid) if ($authid);
+if (($authid) && !($breedingid)){
+    $record = GetAuthority($authid);
+}
+if ($breedingid) {
+    ( $record, $encoding ) = MARCfindbreeding_auth( $breedingid );
+}
+
 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
 $is_a_modif=0;
@@ -576,13 +604,13 @@ $op ||= q{};
 if ($op eq "add") {
 #------------------------------------------------------------------------------------------------------------------------------
     # rebuild
-    my @tags = $input->param('tag');
-    my @subfields = $input->param('subfield');
-    my @values = $input->param('field_value');
+    my @tags = $input->multi_param('tag');
+    my @subfields = $input->multi_param('subfield');
+    my @values = $input->multi_param('field_value');
     # build indicator hash.
-    my @ind_tag = $input->param('ind_tag');
-    my @indicator = $input->param('indicator');
-    my $record = TransformHtmlToMarc($input);
+    my @ind_tag = $input->multi_param('ind_tag');
+    my @indicator = $input->multi_param('indicator');
+    my $record = TransformHtmlToMarc($input, 0);
 
     my ($duplicateauthid,$duplicateauthvalue);
      ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
@@ -611,7 +639,7 @@ if ($op eq "add") {
     }
 } elsif ($op eq "delete") {
 #------------------------------------------------------------------------------------------------------------------------------
-        &DelAuthority($authid);
+        DelAuthority({ authid => $authid });
         if ($nonav){
             print $input->redirect("auth_finder.pl");
         }else{
@@ -630,23 +658,15 @@ if ($op eq "duplicate")
                         authid                      => $authid , authtypecode=>$authtypecode,  );
 }
 
-$template->param(authid                       => $authid,
-                 authtypecode => $authtypecode,
-                 linkid=>$linkid,
-);
-
-my $authtypes = getauthtypes;
-my @authtypesloop;
-foreach my $thisauthtype (keys %$authtypes) {
-    my %row =(value => $thisauthtype,
-                selected => $thisauthtype eq $authtypecode,
-                authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
-            );
-    push @authtypesloop, \%row;
-}
+my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
 
-$template->param(authtypesloop => \@authtypesloop,
-                authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
-                hide_marc => C4::Context->preference('hide_marc'),
-                );
+my $type = $authority_types->find($authtypecode);
+$template->param(
+    authority_types => $authority_types,
+    authtypecode    => $authtypecode,
+    authid          => $authid,
+    linkid          => $linkid,
+    authtypetext    => $type ? $type->authtypetext : "",
+    hide_marc       => C4::Context->preference('hide_marc'),
+);
 output_html_with_http_headers $input, $cookie, $template->output;