followup bug fix 3712
[koha.git] / admin / marc_subfields_structure.pl
index a9045bf..c8ee3df 100755 (executable)
 
 use strict;
 use C4::Output;
-use C4::Interface::CGI::Output;
 use C4::Auth;
 use CGI;
 use C4::Context;
 
 
-sub StringSearch {
-    my ( $env, $searchstring, $frameworkcode ) = @_;
+sub string_search {
+    my ( $searchstring, $frameworkcode ) = @_;
     my $dbh = C4::Context->dbh;
     $searchstring =~ s/\'/\\\'/g;
     my @data  = split( ' ', $searchstring );
@@ -50,6 +49,14 @@ sub StringSearch {
     return ( $cnt, \@results );
 }
 
+sub marc_subfield_structure_exists {
+    my ($tagfield, $tagsubfield, $frameworkcode) = @_;
+    my $dbh  = C4::Context->dbh;
+    my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
+    my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
+    return @$rows > 0;
+}
+
 my $input         = new CGI;
 my $tagfield      = $input->param('tagfield');
 my $tagsubfield   = $input->param('tagsubfield');
@@ -104,13 +111,13 @@ if ( $op eq 'add_form' ) {
     while ( ( my $field ) = $sth2->fetchrow_array ) {
         push @kohafields, "biblio." . $field;
     }
-    my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
+    $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
     $sth2->execute;
     while ( ( my $field ) = $sth2->fetchrow_array ) {
         if ( $field eq 'notes' ) { $field = 'bnotes'; }
         push @kohafields, "biblioitems." . $field;
     }
-    my $sth2 = $dbh->prepare("SHOW COLUMNS from items");
+    $sth2 = $dbh->prepare("SHOW COLUMNS from items");
     $sth2->execute;
     while ( ( my $field ) = $sth2->fetchrow_array ) {
         push @kohafields, "items." . $field;
@@ -127,6 +134,7 @@ if ( $op eq 'add_form' ) {
     }
     push( @authorised_values, "branches" );
     push( @authorised_values, "itemtypes" );
+    push( @authorised_values, "cn_source" );
 
     # build thesaurus categories list
     $sth2->finish;
@@ -156,6 +164,7 @@ if ( $op eq 'add_form' ) {
             push( @value_builder, $line );
         }
     }
+    @value_builder= sort {$a cmp $b} @value_builder;
     closedir DIR;
 
     # build values list
@@ -165,16 +174,9 @@ if ( $op eq 'add_form' ) {
       );    # and tagsubfield='$tagsubfield'");
     $sth->execute( $tagfield, $frameworkcode );
     my @loop_data = ();
-    my $toggle    = 1;
     my $i         = 0;
     while ( $data = $sth->fetchrow_hashref ) {
         my %row_data;    # get a fresh hash for the row data
-        if ( $toggle eq 1 ) {
-            $toggle = 0;
-        }
-        else {
-            $toggle = 1;
-        }
         $row_data{defaultvalue} = $data->{defaultvalue};
         $row_data{tab} = CGI::scrolling_list(
             -name   => 'tab',
@@ -197,15 +199,15 @@ if ( $op eq 'add_form' ) {
             },
             -default  => $data->{'tab'},
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{tagsubfield} =
             $data->{'tagsubfield'}
           . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
           . $data->{'tagsubfield'}
-          . "\" id=\"tagsubfield\">";
-        $row_data{subfieldcode} = $data->{'tagsubfield'};
+          . "\" id=\"tagsubfield\" />";
+        $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
+        $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
@@ -215,49 +217,43 @@ if ( $op eq 'add_form' ) {
             -values   => \@kohafields,
             -default  => "$data->{'kohafield'}",
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{authorised_value} = CGI::scrolling_list(
-            -name     => 'authorised_value',
-            -id       => 'authorised_value',
+            -name     => "authorised_value",
+            -id       => "authorised_value$i",
             -values   => \@authorised_values,
             -default  => $data->{'authorised_value'},
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{value_builder} = CGI::scrolling_list(
-            -name     => 'value_builder',
-            -id       => 'value_builder',
+            -name     => "value_builder",
+            -id       => "value_builder$i",
             -values   => \@value_builder,
             -default  => $data->{'value_builder'},
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{authtypes} = CGI::scrolling_list(
-            -name     => 'authtypecode',
-            -id       => 'authtypecode',
+            -name     => "authtypecode",
+            -id       => "authtypecode$i",
             -values   => \@authtypes,
             -default  => $data->{'authtypecode'},
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{repeatable} = CGI::checkbox(
             -name     => "repeatable$i",
             -checked  => $data->{'repeatable'} ? 'checked' : '',
             -value    => 1,
-            -tabindex => '',
             -label    => '',
             -id       => "repeatable$i"
         );
         $row_data{mandatory} = CGI::checkbox(
-            -name     => "mandatory$i",
+            -name     => "mandatory",
             -checked  => $data->{'mandatory'} ? 'checked' : '',
             -value    => 1,
-            -tabindex => '',
             -label    => '',
             -id       => "mandatory$i"
         );
@@ -267,19 +263,20 @@ if ( $op eq 'add_form' ) {
             -id       => "isurl$i",
             -checked  => $data->{'isurl'} ? 'checked' : '',
             -value    => 1,
-            -tabindex => '',
             -label    => ''
         );
         $row_data{row}    = $i;
-        $row_data{toggle} = $toggle;
         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
         push( @loop_data, \%row_data );
         $i++;
     }
 
     # add more_subfields empty lines for add if needed
-    for ( my $j = $i ; $j <= $more_subfields + $i ; $j++ ) {
+    for ( my $j = 1 ; $j <= 1 ; $j++ ) {
         my %row_data;    # get a fresh hash for the row data
+        $row_data{'new_subfield'} = 1;
+        $row_data{'subfieldcode'} = '';
+
         $row_data{tab} = CGI::scrolling_list(
             -name   => 'tab',
             -id     => "tab$j",
@@ -301,13 +298,12 @@ if ( $op eq 'add_form' ) {
             },
             -default  => "",
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{tagsubfield} =
             "<input type=\"text\" name=\"tagsubfield\" value=\""
           . $data->{'tagsubfield'}
-          . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
+          . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
         $row_data{liblibrarian} = "";
         $row_data{libopac}      = "";
         $row_data{seealso}      = "";
@@ -317,7 +313,6 @@ if ( $op eq 'add_form' ) {
             -values   => \@kohafields,
             -default  => "",
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{hidden}     = "";
@@ -326,7 +321,6 @@ if ( $op eq 'add_form' ) {
             -id       => "repeatable$j",
             -checked  => '',
             -value    => 1,
-            -tabindex => '',
             -label    => ''
         );
         $row_data{mandatory} = CGI::checkbox(
@@ -334,7 +328,6 @@ if ( $op eq 'add_form' ) {
             -id       => "mandatory$j",
             -checked  => '',
             -value    => 1,
-            -tabindex => '',
             -label    => ''
         );
         $row_data{isurl} = CGI::checkbox(
@@ -342,36 +335,31 @@ if ( $op eq 'add_form' ) {
             -id       => "isurl$j",
             -checked  => '',
             -value    => 1,
-            -tabindex => '',
             -label    => ''
         );
         $row_data{value_builder} = CGI::scrolling_list(
-            -name     => 'value_builder',
-            -id       => 'value_builder',
+            -name     => "value_builder",
+            -id       => "value_builder$j",
             -values   => \@value_builder,
             -default  => $data->{'value_builder'},
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{authorised_value} = CGI::scrolling_list(
-            -name     => 'authorised_value',
-            -id       => 'authorised_value',
+            -name     => "authorised_value",
+            -id       => "authorised_value$j",
             -values   => \@authorised_values,
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{authtypes} = CGI::scrolling_list(
-            -name     => 'authtypecode',
-            -id       => 'authtypecode',
+            -name     => "authtypecode",
+            -id       => "authtypecode$j",
             -values   => \@authtypes,
             -size     => 1,
-            -tabindex => '',
             -multiple => 0,
         );
         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
-        $row_data{toggle} = $toggle;
         $row_data{row}    = $j;
         push( @loop_data, \%row_data );
     }
@@ -379,8 +367,7 @@ if ( $op eq 'add_form' ) {
     $template->param( 'heading-edit-subfields-p' => 1 );
     $template->param(
         action   => "Edit subfields",
-        tagfield =>
-"<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
+        tagfield => $tagfield,
         loop           => \@loop_data,
         more_subfields => $more_subfields,
         more_tag       => $tagfield
@@ -393,10 +380,18 @@ if ( $op eq 'add_form' ) {
 elsif ( $op eq 'add_validate' ) {
     my $dbh = C4::Context->dbh;
     $template->param( tagfield => "$input->param('tagfield')" );
-    my $sth = $dbh->prepare(
-"replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
-                                    values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
-    );
+#     my $sth = $dbh->prepare(
+# "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
+#                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
+#     );
+    my $sth_insert = $dbh->prepare(qq{
+        insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
+        values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+    });
+    my $sth_update = $dbh->prepare(qq{
+        update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?,  link=?, defaultvalue=?
+        where tagfield=? and tagsubfield=? and frameworkcode=?
+    });
     my @tagsubfield       = $input->param('tagsubfield');
     my @liblibrarian      = $input->param('liblibrarian');
     my @libopac           = $input->param('libopac');
@@ -414,6 +409,7 @@ elsif ( $op eq 'add_validate' ) {
         my $tagfield    = $input->param('tagfield');
         my $tagsubfield = $tagsubfield[$i];
         $tagsubfield = "@" unless $tagsubfield ne '';
+        $tagsubfield = "@" if $tagsubfield eq '_';
         my $liblibrarian     = $liblibrarian[$i];
         my $libopac          = $libopac[$i];
         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
@@ -431,29 +427,57 @@ elsif ( $op eq 'add_validate' ) {
         
         if ($liblibrarian) {
             unless ( C4::Context->config('demo') eq 1 ) {
-                $sth->execute(
-                    $tagfield,
-                    $tagsubfield,
-                    $liblibrarian,
-                    $libopac,
-                    $repeatable,
-                    $mandatory,
-                    $kohafield,
-                    $tab,
-                    $seealso,
-                    $authorised_value,
-                    $authtypecode,
-                    $value_builder,
-                    $hidden,
-                    $isurl,
-                    $frameworkcode,
-                    $link,
-                    $defaultvalue,
-                );
+                if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
+                    $sth_update->execute(
+                        $tagfield,
+                        $tagsubfield,
+                        $liblibrarian,
+                        $libopac,
+                        $repeatable,
+                        $mandatory,
+                        $kohafield,
+                        $tab,
+                        $seealso,
+                        $authorised_value,
+                        $authtypecode,
+                        $value_builder,
+                        $hidden,
+                        $isurl,
+                        $frameworkcode,
+                        $link,
+                        $defaultvalue,
+                        (
+                            $tagfield,
+                            $tagsubfield,
+                            $frameworkcode,
+                        ),
+                    );
+                } else {
+                    $sth_insert->execute(
+                        $tagfield,
+                        $tagsubfield,
+                        $liblibrarian,
+                        $libopac,
+                        $repeatable,
+                        $mandatory,
+                        $kohafield,
+                        $tab,
+                        $seealso,
+                        $authorised_value,
+                        $authtypecode,
+                        $value_builder,
+                        $hidden,
+                        $isurl,
+                        $frameworkcode,
+                        $link,
+                        $defaultvalue,
+                    );
+                }
             }
         }
     }
-    $sth->finish;
+    $sth_insert->finish;
+    $sth_update->finish;
     print
 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
     exit;
@@ -469,8 +493,7 @@ elsif ( $op eq 'delete_confirm' ) {
 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
       );
 
-    #FIXME : called with 2 bind variables when 3 are needed
-    $sth->execute( $tagfield, $tagsubfield );
+    $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
     my $data = $sth->fetchrow_hashref;
     $sth->finish;
     $template->param(
@@ -505,9 +528,7 @@ elsif ( $op eq 'delete_confirmed' ) {
 ################## DEFAULT ##################################
 }
 else {    # DEFAULT
-    my $env;
-    my ( $count, $results ) = StringSearch( $env, $tagfield, $frameworkcode );
-    my $toggle    = 1;
+    my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
     my @loop_data = ();
     for (
         my $i = $offset ;
@@ -515,12 +536,6 @@ else {    # DEFAULT
         $i++
       )
     {
-        if ( $toggle eq 1 ) {
-            $toggle = 0;
-        }
-        else {
-            $toggle = 1;
-        }
         my %row_data;    # get a fresh hash for the row data
         $row_data{tagfield}         = $results->[$i]{'tagfield'};
         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
@@ -539,8 +554,7 @@ else {    # DEFAULT
         $row_data{delete}           =
 "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield="
           . $results->[$i]{'tagsubfield'}
-          . "&frameworkcode=$frameworkcode";
-        $row_data{toggle} = $toggle;
+          . "&amp;frameworkcode=$frameworkcode";
 
         if ( $row_data{tab} eq -1 ) {
             $row_data{subfield_ignored} = 1;
@@ -557,12 +571,12 @@ else {    # DEFAULT
     if ( $offset > 0 ) {
         my $prevpage = $offset - $pagesize;
         $template->param(
-            prev => "<a href=\"$script_name?offset=$prevpage\">" );
+            prev => "<a href=\"$script_name?offset=$prevpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
     }
     if ( $offset + $pagesize < $count ) {
         my $nextpage = $offset + $pagesize;
         $template->param(
-            next => "<a href=\"$script_name?offset=$nextpage\">" );
+            next => "<a href=\"$script_name?offset=$nextpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
     }
 }    #---- END $OP eq DEFAULT