Bug 12478: starting authority search in staff client
[koha.git] / authorities / authorities.pl
index f9a79ad..bc2c8d4 100755 (executable)
@@ -5,18 +5,18 @@
 #
 # 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;
@@ -26,11 +26,12 @@ 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 vars qw( $tagslib);
 use vars qw( $authorised_values_sth);
 use vars qw( $is_a_modif );
@@ -83,23 +84,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}
             && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) );
-        
+
         my $itemtype;
-        
-        while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
-            push @authorised_values, $itemtype;
-            $authorised_lib{$itemtype} = $description;
+        my $itemtypes = GetItemTypes( style => 'array' );
+        for my $itemtype ( @$itemtypes ) {
+            push @authorised_values, $itemtype->{itemtype};
+            $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
         }
         $value = $itemtype unless ($value);
 
@@ -175,8 +170,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},
@@ -212,37 +206,37 @@ sub create_input {
             value        => $value,
             authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
         };
-    # it's a plugin field
     }
-    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} = {
-            type       => 'text2',
-            id         => $subfield_data{id},
-            name       => $subfield_data{id},
-            value      => $value,
-            maxlength  => $max_length,
-            function   => $function_name,
-            index_tag  => $index_tag,
-            javascript => $javascript,
-        };
-        # it's an hidden field
     }
+    # it's an hidden field
     elsif ( $tag eq '' ) {
         $subfield_data{marc_value} = {
             type      => 'hidden',
@@ -574,7 +568,7 @@ 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)
@@ -618,7 +612,7 @@ if ($op eq "add") {
     # build indicator hash.
     my @ind_tag = $input->param('ind_tag');
     my @indicator = $input->param('indicator');
-    my $record = TransformHtmlToMarc($input);
+    my $record = TransformHtmlToMarc($input, 0);
 
     my ($duplicateauthid,$duplicateauthvalue);
      ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
@@ -666,23 +660,14 @@ if ($op eq "duplicate")
                         authid                      => $authid , authtypecode=>$authtypecode,  );
 }
 
-$template->param(authid                       => $authid,
-                 authtypecode => $authtypecode,
-                 linkid=>$linkid,
-);
+my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
 
-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;
-}
-
-$template->param(authtypesloop => \@authtypesloop,
-                authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
-                hide_marc => C4::Context->preference('hide_marc'),
-                );
+$template->param(
+    authority_types => $authority_types,
+    authtypecode    => $authtypecode,
+    authid          => $authid,
+    linkid          => $linkid,
+    authtypetext    => $authority_types->find($authtypecode)->authtypetext,
+    hide_marc       => C4::Context->preference('hide_marc'),
+);
 output_html_with_http_headers $input, $cookie, $template->output;