[Koha-patches] [PATCH] Bug 2919 -- fix value_builder plugins for dateaccessioned, barcode

Joe Atzberger joe.atzberger at liblime.com
Thu Feb 5 20:04:20 CET 2009


This patch depends on my previous one "Begin cleanup on additem" and
fixes one bug introduced in that patch.

Overhaul of additem sections to factor out common pieces especially attributes.
Plugin js vastly simplified and reimplemented in jquery.  Performance improved.

Note that a particularly busy cataloging department should still leave
barcode blank for server-side autopopulation to avoid collision.
---
 cataloguing/additem.pl                             |  274 +++++++-------------
 cataloguing/value_builder/barcode.pl               |  164 +++++-------
 cataloguing/value_builder/dateaccessioned.pl       |   68 +++---
 .../prog/en/modules/cataloguing/additem.tmpl       |   35 ++-
 4 files changed, 208 insertions(+), 333 deletions(-)

diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index b197589..5a776fd 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -28,8 +28,7 @@ use C4::Context;
 use C4::Koha; # XXX subfield_is_koha_internal_p
 use C4::Branch; # XXX subfield_is_koha_internal_p
 use C4::ClassSource;
-
-use Date::Calc qw(Today);
+use C4::Dates;
 
 use MARC::File::XML;
 
@@ -75,9 +74,11 @@ my ($template, $loggedinuser, $cookie)
                  debug => 1,
                  });
 
-# find itemtype
 my $frameworkcode = &GetFrameworkCode($biblionumber);
 
+my $today_iso = C4::Dates->today('iso');
+$template->param(today_iso => $today_iso);
+
 my $tagslib = &GetMarcStructure(1,$frameworkcode);
 my $record = GetMarcBiblio($biblionumber);
 my $oldrecord = TransformMarcToKoha($dbh,$record);
@@ -88,14 +89,14 @@ my @errors; # store errors found while checking data BEFORE saving item.
 if ($op eq "additem") {
 #-------------------------------------------------------------------------------
     # rebuild
-    my @tags = $input->param('tag');
+    my @tags      = $input->param('tag');
     my @subfields = $input->param('subfield');
-    my @values = $input->param('field_value');
+    my @values    = $input->param('field_value');
     # build indicator hash.
-    my @ind_tag = $input->param('ind_tag');
+    my @ind_tag   = $input->param('ind_tag');
     my @indicator = $input->param('indicator');
     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
-        my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
+    my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
     # if autoBarcode is set to 'incremental', calculate barcode...
 	# NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
 	# NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
@@ -128,7 +129,7 @@ if ($op eq "additem") {
 #-------------------------------------------------------------------------------
 # retrieve item if exist => then, it's a modif
     $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
-    $nextop="saveitem";
+    $nextop = "saveitem";
 #-------------------------------------------------------------------------------
 } elsif ($op eq "delitem") {
 #-------------------------------------------------------------------------------
@@ -174,7 +175,7 @@ if ($op eq "additem") {
         push @errors,"barcode_not_unique";
     } else {
         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
-    $itemnumber="";
+        $itemnumber="";
     }
     $nextop="additem";
 }
@@ -196,13 +197,14 @@ my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebran
 
 foreach my $field (@fields) {
     next if ($field->tag()<10);
-    my @subf=$field->subfields;
+    my @subf = $field->subfields;
+    (defined @subf) or @subf = ();
     my %this_row;
 # loop through each subfield
     for my $i (0..$#subf) {
         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 
                 && ($field->tag() ne $itemtagfield 
-                && $subf[$i][0] ne $itemtagsubfield));
+                && $subf[$i][0]   ne $itemtagsubfield));
 
         $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
 		if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10) {
@@ -247,7 +249,7 @@ for (my $i=0;$i<=$#big_array; $i++) {
     $row_data{item_value} = $items_data;
     $row_data{itemnumber} = $big_array[$i]->{itemnumber};
     #reporting this_row values
-    $row_data{'nomod'} = $big_array[$i]{'nomod'};
+    $row_data{'nomod'}    = $big_array[$i]{'nomod'};
     push(@item_value_loop,\%row_data);
 }
 foreach my $subfield_code (sort keys(%witness)) {
@@ -261,37 +263,39 @@ my @loop_data =();
 my $i=0;
 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
 
+my $onlymine = C4::Context->preference('IndependantBranches') && 
+               C4::Context->userenv                           && 
+               C4::Context->userenv->{flags}!=1               && 
+               C4::Context->userenv->{branch};
+my $branches = GetBranches($onlymine);  # build once ahead of time, instead of multiple times later.
+
 foreach my $tag (sort keys %{$tagslib}) {
-  my $previous_tag = '';
 # loop through each subfield
   foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
     next if subfield_is_koha_internal_p($subfield);
-    next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
+    next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
     my %subfield_data;
  
-    my $index_subfield= int(rand(1000000)); 
-    if($subfield eq '@'){
+    my $index_subfield = int(rand(1000000)); 
+    if ($subfield eq '@'){
         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
     } else {
-         $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
+        $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
     }
-    $subfield_data{tag}=$tag;
-    $subfield_data{subfield}=$subfield;
-    $subfield_data{random}=int(rand(1000000)); 
-#        $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
-    $subfield_data{marc_lib}  ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
-    $subfield_data{mandatory} =$tagslib->{$tag}->{$subfield}->{mandatory};
-    $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
+    $subfield_data{tag}        = $tag;
+    $subfield_data{subfield}   = $subfield;
+    $subfield_data{random}     = int(rand(1000000));    # why do we need 2 different randoms?
+#   $subfield_data{marc_lib}   = $tagslib->{$tag}->{$subfield}->{lib};
+    $subfield_data{marc_lib}   ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
+    $subfield_data{mandatory}  = $tagslib->{$tag}->{$subfield}->{mandatory};
+    $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
     my ($x,$value);
     ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
     $value =~ s/"/&quot;/g;
     unless ($value) {
         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
-
         # get today date & replace YYYY, MM, DD if provided in the default value
-        my ( $year, $month, $day ) = Today();
-        $month = sprintf( "%02d", $month );
-        $day   = sprintf( "%02d", $day );
+        my ( $year, $month, $day ) = split ',', $today_iso;
         $value =~ s/YYYY/$year/g;
         $value =~ s/MM/$month/g;
         $value =~ s/DD/$day/g;
@@ -303,69 +307,52 @@ foreach my $tag (sort keys %{$tagslib}) {
     #          (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
     # $test and print $input->redirect(".pl?biblionumber=$biblionumber") and exit;
         # search for itemcallnumber if applicable
-    if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
-        my $CNtag       = substr(C4::Context->preference('itemcallnumber'),0,3);
-        my $CNsubfield  = substr(C4::Context->preference('itemcallnumber'),3,1);
-        my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
+    my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
+    if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
+        my $CNtag       = substr($pref_itemcallnumber, 0, 3);
+        my $CNsubfield  = substr($pref_itemcallnumber, 3, 1);
+        my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
         my $temp2 = $temp->field($CNtag);
         if ($temp2) {
             $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
             #remove any trailing space incase one subfield is used
-            $value=~s/^\s+|\s+$//g;
+            $value =~ s/^\s+|\s+$//g;
         }
     }
+
+    my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
+    my $attributes          = qq($attributes_no_value value="$value" );
     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
       my @authorised_values;
       my %authorised_lib;
-      my $dbh=C4::Context->dbh;   
-  
       # builds list, depending on authorised value...
   
-      #---- branch
-      if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
-          #Use GetBranches($onlymine)
-          my $onlymine=C4::Context->preference('IndependantBranches') && 
-                  C4::Context->userenv && 
-                  C4::Context->userenv->{flags}!=1 && 
-                  C4::Context->userenv->{branch};
-          my $branches = GetBranches($onlymine);
-          my @branchloop;
+      if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
           foreach my $thisbranch ( sort keys %$branches ) {
               push @authorised_values, $thisbranch;
               $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
           }
-          
-          #----- itemtypes
       }
       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
-          my $sth =
-            $dbh->prepare(
-              "select itemtype,description from itemtypes order by description");
+          push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
+          my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
           $sth->execute;
-          push @authorised_values, ""
-            unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
-            
-          my $itemtype;
-          
+          my $itemtype;     # FIXME: double declaration of $itemtype
           while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
               push @authorised_values, $itemtype;
               $authorised_lib{$itemtype} = $description;
           }
           $value = $itemtype unless ($value);
-  
-          #---- class_sources
       }
       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
-          push @authorised_values, ""
-            unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
+          push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
             
           my $class_sources = GetClassSources();
-
           my $default_source = C4::Context->preference("DefaultClassificationSource");
           
           foreach my $class_source (sort keys %$class_sources) {
               next unless $class_sources->{$class_source}->{'used'} or
-                          ($value and $class_source eq $value) or
+                          ($value and $class_source eq $value)      or
                           ($class_source eq $default_source);
               push @authorised_values, $class_source;
               $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
@@ -375,12 +362,8 @@ foreach my $tag (sort keys %{$tagslib}) {
           #---- "true" authorised value
       }
       else {
-          $authorised_values_sth->execute(
-              $tagslib->{$tag}->{$subfield}->{authorised_value} );
-  
-          push @authorised_values, ""
-            unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
-  
+          push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
+          $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
           while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
               push @authorised_values, $value;
               $authorised_lib{$value} = $lib;
@@ -401,140 +384,67 @@ foreach my $tag (sort keys %{$tagslib}) {
     # it's a thesaurus / authority field
     }
     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
-        $subfield_data{marc_value} =
-            "<input type=\"text\"
-                    id=\"".$subfield_data{id}."\"
-                    name=\"field_value\"
-                    value=\"$value\"
-                    class=\"input_marceditor\"
-                    tabindex=\"1\"
-                    size=\"67\"
-                    maxlength=\"255\" 
-                    \/>
-                    <a href=\"#\" class=\"buttonDot\"
-                        onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
+        $subfield_data{marc_value} = "<input type=\"text\" $attributes />
+            <a href=\"#\" class=\"buttonDot\"
+                onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
     ";
     # it's a plugin field
     }
-    elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
-
+    elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
         # opening plugin
-        my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
+        my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
         if (do $plugin) {
             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
-        
-            $subfield_data{marc_value} =
-                    "<input tabindex=\"1\"
-                            type=\"text\"
-                            id=\"".$subfield_data{id}."\"
-                            name=\"field_value\"
-                            value=\"$value\"
-                            class=\"input_marceditor\"
-                            onfocus=\"Focus$function_name(".$subfield_data{random}.")\"
-                            size=\"67\"
-                            maxlength=\"255\" 
-                            onblur=\"Blur$function_name(".$subfield_data{random}."); \" \/>
-                            <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
-                    $javascript";
+            $subfield_data{marc_value} = qq[<input $attributes
+                onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
+                 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
+                <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
+                $javascript];
         } else {
             warn "Plugin Failed: $plugin";
-            # supply default input form
-            $subfield_data{marc_value} =
-                "<input type=\"text\"
-                        id=\"".$subfield_data{id}."\"
-                        name=\"field_value\"
-                        value=\"$value\"
-                        tabindex=\"1\"
-                        size=\"67\"
-                        maxlength=\"255\" 
-                        class=\"input_marceditor\"
-                \/>
-                ";
+            $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
         }
-        # it's an hidden field
-    }
-    elsif ( $tag eq '' ) {
-        $subfield_data{marc_value} =
-            "<input tabindex=\"1\"
-                    type=\"hidden\"
-                    id=\"".$subfield_data{id}."\"
-                    name=\"field_value\"
-                    size=\"67\"
-                    maxlength=\"255\" 
-                    value=\"$value\" \/>
-            ";
     }
-    elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
-        $subfield_data{marc_value} =
-            "<input type=\"text\"
-                    id=\"".$subfield_data{id}."\"
-                    name=\"field_value\"
-                    class=\"input_marceditor\"
-                    tabindex=\"1\"
-                    size=\"67\"
-                    maxlength=\"255\" 
-                    value=\"$value\"
-            \/>";
-
-        # it's a standard field
+    elsif ( $tag eq '' ) {       # it's an hidden field
+        $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
     }
-    else {
-        if (
-            length($value) > 100
-            or
-            ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
-                and $tag < 400 && $subfield eq 'a' )
-            or (    $tag >= 500
-                and $tag < 600
-                && C4::Context->preference("marcflavour") eq "MARC21" )
-          )
-        {
-            $subfield_data{marc_value} =
-                "<textarea cols=\"70\"
-                           rows=\"4\"
-                           id=\"".$subfield_data{id}."\"
-                           name=\"field_value\"
-                           class=\"input_marceditor\"
-                           tabindex=\"1\"
-                            size=\"67\"
-                            maxlength=\"255\" 
-                           >$value</textarea>
-                ";
-        }
-        else {
-            $subfield_data{marc_value} =
-                "<input type=\"text\"
-                        id=\"".$subfield_data{id}."\"
-                        name=\"field_value\"
-                        value=\"$value\"
-                        tabindex=\"1\"
-                        size=\"67\"
-                        maxlength=\"255\" 
-                        class=\"input_marceditor\"
-                \/>
-                ";
-        }
+    elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {   # FIXME: shouldn't input type be "hidden" ?
+        $subfield_data{marc_value} = qq(<input type="text" $attributes />);
     }
-#        $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
-        push(@loop_data, \%subfield_data);
-        $i++
+    elsif ( length($value) > 100
+            or (C4::Context->preference("marcflavour") eq "UNIMARC" and
+                  300 <= $tag && $tag < 400 && $subfield eq 'a' )
+            or (C4::Context->preference("marcflavour") eq "MARC21"  and
+                  500 <= $tag && $tag < 600                     )
+          ) {
+        # oversize field (textarea)
+        $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
+    } else {
+        # it's a standard field
+         $subfield_data{marc_value} = "<input $attributes />";
     }
+#   $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
+    push (@loop_data, \%subfield_data);
+    $i++
+  }
 }
 
 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
 $template->param( title => $record->title() ) if ($record ne "-1");
-$template->param(item_loop => \@item_value_loop,
-                        item_header_loop => \@header_value_loop,
-                        biblionumber => $biblionumber,
-                        title => $oldrecord->{title},
-                        author => $oldrecord->{author},
-                        item => \@loop_data,
-                        itemnumber => $itemnumber,
-                        itemtagfield => $itemtagfield,
-                        itemtagsubfield =>$itemtagsubfield,
-                        op => $nextop,
-                        opisadd => ($nextop eq "saveitem")?0:1);
+$template->param(
+    biblionumber => $biblionumber,
+    title        => $oldrecord->{title},
+    author       => $oldrecord->{author},
+    item_loop        => \@item_value_loop,
+    item_header_loop => \@header_value_loop,
+    item             => \@loop_data,
+    itemnumber       => $itemnumber,
+    itemtagfield     => $itemtagfield,
+    itemtagsubfield  => $itemtagsubfield,
+    op      => $nextop,
+    opisadd => ($nextop eq "saveitem") ? 0 : 1,
+);
 foreach my $error (@errors) {
     $template->param($error => 1);
 }
diff --git a/cataloguing/value_builder/barcode.pl b/cataloguing/value_builder/barcode.pl
index cfa3b15..5a42742 100755
--- a/cataloguing/value_builder/barcode.pl
+++ b/cataloguing/value_builder/barcode.pl
@@ -21,7 +21,7 @@
 
 
 use C4::Context;
-
+require C4::Dates;
 my $DEBUG = 0;
 
 =head1
@@ -30,8 +30,8 @@ plugin_parameters : other parameters added when the plugin is called by the dopo
 
 =cut
 sub plugin_parameters {
-my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
-return "";
+#   my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
+    return "";
 }
 
 =head1
@@ -53,146 +53,110 @@ sub plugin_javascript {
 	my $function_name= "barcode".(int(rand(100000))+1);
 
 	# find today's date
-	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
-                                                               localtime(time);
-	$year +=1900;
-	$mon +=1;
-	if (length($mon)==1) {
-		$mon = "0".$mon;
-	}
-	if (length($mday)==1) {
-		$mday = "0".$mday;
-	}
-	if (length($hour)==1) {
-   	     $hour = "0".$hour;
-	}
-	if (length($min)==1) {
-        $min = "0".$min;
-	}
-	if (length($sec)==1) {
-        $hour = "0".$sec;
-	}
-
-	my $date = "$year";
-
-	my ($tag,$subfield) =  GetMarcFromKohaField("items.barcode");
+	my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
+	my ($tag,$subfield)       =  GetMarcFromKohaField("items.barcode");
 	my ($loctag,$locsubfield) =  GetMarcFromKohaField("items.homebranch");
 
 	my $nextnum;
 	my $query;
-        my $scr;
+    my $scr;
 	my $autoBarcodeType = C4::Context->preference("autoBarcode");
-        warn "Barcode type = $autoBarcodeType" if $DEBUG;
-	unless ($autoBarcodeType eq 'OFF' or !$autoBarcodeType) {
-
+    warn "Barcode type = $autoBarcodeType" if $DEBUG;
+	if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
+        # don't return a value unless we have the appropriate syspref set
+		return ($function_name, 
+        "<script type=\"text/javascript\">
+        // autoBarcodeType OFF (or not defined)
+        function Focus$function_name() { return 0;}
+        function  Clic$function_name() { return 0;}
+        function  Blur$function_name() { return 0;}
+        </script>");
+    }
 	if ($autoBarcodeType eq 'annual') {
 		$query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
 		my $sth=$dbh->prepare($query);
 		$sth->execute("$year%");
 		while (my ($count)= $sth->fetchrow_array) {
-                    warn "Examining Record: $count" if $DEBUG;
+            warn "Examining Record: $count" if $DEBUG;
     		$nextnum = $count if $count;
 		}
 		$nextnum++;
 		$nextnum = sprintf("%0*d", "4",$nextnum);
 		$nextnum = "$year-$nextnum";
-                $scr = " 
-		for (i=0 ; i<document.f.field_value.length ; i++) {
-			if (document.f.tag[i].value == '$tag' && document.f.subfield[i].value == '$subfield') {
-				if (document.f.field_value[i].value == '') {
-					document.f.field_value[i].value = '$nextnum';
-				}
-			}
-		}";
 	}
 	elsif ($autoBarcodeType eq 'incremental') {
 		# not the best, two catalogers could add the same barcode easily this way :/
 		$query = "select max(abs(barcode)) from items";
-        my $sth=$dbh->prepare($query);
+        my $sth = $dbh->prepare($query);
 		$sth->execute();
 		while (my ($count)= $sth->fetchrow_array) {
 			$nextnum = $count;
 		}
 		$nextnum++;
-                $scr = " 
-		for (i=0 ; i<document.f.field_value.length ; i++) {
-			if (document.f.tag[i].value == '$tag' && document.f.subfield[i].value == '$subfield') {
-				if (document.f.field_value[i].value == '') {
-					document.f.field_value[i].value = '$nextnum';
-				}
-			}
-		}";
-	}
-        elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
-            $year = substr($year, -2);
-	    $query = "SELECT MAX(CAST(SUBSTRING(barcode,7,4) AS signed)) FROM items WHERE barcode REGEXP ?";
-	    my $sth=$dbh->prepare($query);
-	    $sth->execute("^[a-zA-Z]{1,}$year");
-	    while (my ($count)= $sth->fetchrow_array) {
-    	        $nextnum = $count if $count;
-                warn "Existing incremental number = $nextnum" if $DEBUG;
-	    }
-	    $nextnum++;
-            $nextnum = sprintf("%0*d", "4",$nextnum);
-            $nextnum = $year . $mon . $nextnum;
-            warn "New Barcode = $nextnum" if $DEBUG;
-            $scr = " 
-		for (i=0 ; i<document.f.field_value.length ; i++) {
-			if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
-				fnum = i;
-			}
-		}
-		for (i=0 ; i<document.f.field_value.length ; i++) {
-			if (document.f.tag[i].value == '$tag' && document.f.subfield[i].value == '$subfield') {
-				if (document.f.field_value[i].value == '') {
-					document.f.field_value[i].value = document.f.field_value[fnum].value + '$nextnum';
-				}
-			}
-		}";
+    }
+    elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
+        $year = substr($year, -2);
+        $query = "SELECT MAX(CAST(SUBSTRING(barcode,7,4) AS signed)) FROM items WHERE barcode REGEXP ?";
+        my $sth = $dbh->prepare($query);
+        $sth->execute("^[a-zA-Z]{1,}$year");
+        while (my ($count)= $sth->fetchrow_array) {
+            $nextnum = $count if $count;
+            warn "Existing incremental number = $nextnum" if $DEBUG;
+        }
+        $nextnum++;
+        $nextnum = sprintf("%0*d", "4",$nextnum);
+        $nextnum = $year . $mon . $nextnum;
+        warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
+        $scr = " 
+        for (i=0 ; i<document.f.field_value.length ; i++) {
+            if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
+                fnum = i;
+            }
+        }
+        if (\$('#' + id).val() == '' || force) {
+            \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
         }
+        ";
+    }
 
+    # default js body (if not filled by hbyymmincr)
+    $scr or $scr = <<END_OF_JS;
+if (\$('#' + id).val() == '' || force) {
+    \$('#' + id).val('$nextnum');
+}
+END_OF_JS
 
-		my $res  = "
-<script type=\"text/javascript\">
+    my $js  = <<END_OF_JS;
+<script type="text/javascript">
 //<![CDATA[
 
-//function Blur$function_name(index) {
-//need this?
-//}
-
-function Focus$function_name(subfield_managed) {";
-
-$res .= $scr;
-$res .= "
-return 0;
+function Blur$function_name(index) {
+    //barcode validation might go here
 }
 
-function Clic$function_name(subfield_managed) {";
+function Focus$function_name(subfield_managed, id, force) {
+$scr
+    return 0;
+}
 
-$res .= $scr;
-$res .= "
-return 0;
+function Clic$function_name(id) {
+    return Focus$function_name('not_relavent', id, 1);
 }
 //]]>
 </script>
-";
-	# don't return a value unless we have the appropriate syspref set
-	return ($function_name,$res);
-	}
-	else {
-		return ($function_name,"<script type=\"text/javascript\">function Focus$function_name() { return 0;}</script>");
-	}
+END_OF_JS
+    return ($function_name, $js);
 }
 
 =head1
 
-plugin : the true value_builded. The screen that is open in the popup window.
+plugin: useless here
 
 =cut
 
 sub plugin {
-my ($input) = @_;
-return "";
+    # my ($input) = @_;
+    return "";
 }
 
 1;
diff --git a/cataloguing/value_builder/dateaccessioned.pl b/cataloguing/value_builder/dateaccessioned.pl
index 19f7227..a0a3034 100755
--- a/cataloguing/value_builder/dateaccessioned.pl
+++ b/cataloguing/value_builder/dateaccessioned.pl
@@ -19,15 +19,13 @@
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-
-
 =head1
 
-plugin_parameters : other parameters added when the plugin is called by the dopop function
+plugin_parameters : useless here
 
 =cut
 sub plugin_parameters {
-	my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
+	# my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
 	return "";
 }
 
@@ -35,9 +33,9 @@ sub plugin_parameters {
 
 plugin_javascript : the javascript function called when the user enters the subfield.
 contain 3 javascript functions :
-* one called when the field is entered (OnFocus). Named FocusXXX
-* one called when the field is leaved (onBlur). Named BlurXXX
-* one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
+* one called when the   field  is entered (OnFocus) named FocusXXX
+* one called when the   field  is  left   (onBlur ) named BlurXXX
+* one called when the ... link is clicked (onClick) named ClicXXX
 
 returns :
 * XXX
@@ -46,56 +44,52 @@ the 3 scripts are inserted after the <input> in the html code
 
 =cut
 sub plugin_javascript {
-	my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
-	my $function_name= "dateaccessioned".(int(rand(100000))+1);
+	# my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
+	my $function_name = "dateaccessioned".(int(rand(100000))+1);
 
-	# find today's date
-	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
-                                                               localtime(time);
-	$year +=1900;
-	$mon +=1;
-	$pmon = sprintf("%0*d", "2",$mon);
-
-	$pmday = sprintf("%0*d", "2",$mday);
-	my $date = "$year-$pmon-$pmday";
+    require C4::Dates;
+	my $date = C4::Dates->today('iso');
 
 	# find the tag/subfield mapped to items.dateaccessioned
-	my ($tag,$subfield) =  GetMarcFromKohaField("items.dateaccessioned");
-	my $res  = "
-<script type=\"text/javascript\">
+	my $res  = <<END_OF_JS;
+<script type="text/javascript">
 //<![CDATA[
+//  
+// from: cataloguing/value_builder/dateaccessioned.pl
+
 function Blur$function_name(index) {
-//need this?
+    //date validation could go here
 }
 
-function Focus$function_name(subfield_managed) {
-	for (i=0 ; i<document.f.field_value.length ; i++) {
-                if (document.f.tag[i].value == '$tag' && document.f.subfield[i].value == '$subfield') {
-						if (document.f.field_value[i].value == '') {
-							document.f.field_value[i].value = '$date';
-						}
-                }
-        }
-return 0;
+function Focus$function_name(subfield_managed, id, force) {
+    //var summary = "";
+    //for (i=0 ; i<document.f.field_value.length ; i++) {
+    //  summary += i + ": " + document.f.tag[i].value + " " + document.f.subfield[i].value + ": " + document.f.field_value[i].value + "\\n"; 
+    //}
+    //alert("Got focus, subfieldmanaged: " + subfield_managed + "\\n" + summary);
+    set_to_today(id); // defined in additem.pl HEAD
+    return 0;
 }
 
-function Clic$function_name(subfield_managed) {
+function Clic$function_name(id) {
+    set_to_today(id, 1); // defined in additem.pl HEAD
+    return 0;
 }
 //]]>
 </script>
-";
-	return ($function_name,$res);
+END_OF_JS
+	return ($function_name, $res);
 }
 
 =head1
 
-plugin : the true value_builded. The screen that is open in the popup window.
+plugin: useless here.
 
 =cut
 
 sub plugin {
-my ($input) = @_;
-return "";
+#    my ($input) = @_;
+    return "";
 }
 
 1;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
index 9eacb7a..cd97b17 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
@@ -133,24 +133,31 @@ function CreateKey(){
     return parseInt(Math.random() * 100000);
 }
 
-	 $(document).ready(function() {
+$(document).ready(function() {
     $("#cataloguing_additem_itemlist  tr").hover(
-      function () {
-        $(this).attr("class","highlight");
-      }, 
-      function () {
-        $(this).attr("class","");
-      }
+        function () {$(this).attr("class","highlight");},
+        function () {$(this).attr("class",""         );}
     );
-	 });
+});
 
+function set_to_today(id, force) {
+    if (! id) { alert("Bad id " + id + " sent to set_to_today()"); return 0; }
+    if ($("#" + id).val() == '' || $("#" + id).val() == '0000-00-00' || force) {
+        $("#" + id).val("<!-- TMPL_VAR NAME="today_iso" -->");
+    }
+}
 //]]>
 </script>
 <link type="text/css" rel="stylesheet" href="<!-- TMPL_VAR NAME="themelang" -->/css/addbiblio.css" />
 </head>
 <body>
 <!-- TMPL_INCLUDE NAME="header.inc" -->
-<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/cataloguing/addbooks.pl">Cataloging</a> &rsaquo; <a href="/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=<!-- TMPL_VAR NAME="biblionumber" -->">Edit <!-- TMPL_VAR name="title" escape="html" --> <!-- TMPL_IF NAME="author" --> by <!-- TMPL_VAR name="author" --><!-- /TMPL_IF --> (Record #<!-- TMPL_VAR NAME="biblionumber" -->)</a>  &rsaquo; Items</div>
+<div id="breadcrumbs">
+          <a href="/cgi-bin/koha/mainpage.pl">Home</a>
+ &rsaquo; <a href="/cgi-bin/koha/cataloguing/addbooks.pl">Cataloging</a>
+ &rsaquo; Edit <a href="/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=<!-- TMPL_VAR NAME="biblionumber" -->"><!-- TMPL_VAR name="title" escape="html" --> <!-- TMPL_IF NAME="author" --> by <!-- TMPL_VAR name="author" --><!-- /TMPL_IF --> (Record #<!-- TMPL_VAR NAME="biblionumber" -->)</a>
+ &rsaquo; <a href="/cgi-bin/koha/cataloguing/additem.pl?biblionumber=<!-- TMPL_VAR NAME="biblionumber" -->">Items</a>
+</div>
 
 <div id="doc3" class="yui-t2">
    
@@ -196,7 +203,7 @@ function CreateKey(){
     <!-- TMPL_IF name="opisadd" -->
         <h2 id="additema">Add Item</h2>
     <!-- TMPL_ELSE -->
-        <h2>Edit Item</h2>
+        <h2>Edit Item #<!-- TMPL_VAR NAME="itemnumber" --></h2>
     <!-- /TMPL_IF -->	
 	<fieldset class="rows">
 	<ol>
@@ -205,8 +212,8 @@ function CreateKey(){
    
                <label><!-- TMPL_VAR NAME="subfield" --> - <!-- TMPL_IF name="mandatory" --><b><!-- /TMPL_IF --><!-- TMPL_VAR NAME="marc_lib" --><!-- TMPL_IF name="mandatory" --> *</b><!-- /TMPL_IF --></label>
                 <!-- TMPL_VAR NAME="marc_value" -->
-                <input type="hidden" name="tag" value="<!-- TMPL_VAR NAME="tag" -->" />
-                <input type="hidden" name="subfield" value="<!-- TMPL_VAR NAME="subfield" -->" />
+                <input type="hidden" name="tag"       value="<!-- TMPL_VAR NAME="tag" -->" />
+                <input type="hidden" name="subfield"  value="<!-- TMPL_VAR NAME="subfield" -->" />
                 <input type="hidden" name="mandatory" value="<!-- TMPL_VAR NAME="mandatory" -->" />
                 <!-- TMPL_IF NAME="repeatable" -->
                     <span class="buttonPlus" onclick="CloneSubfield('subfield<!-- TMPL_VAR NAME='tag' --><!-- TMPL_VAR NAME='subfield' --><!-- TMPL_VAR name="random" -->')">+</span>
@@ -214,8 +221,8 @@ function CreateKey(){
    
             </div></li>
         <!-- /tmpl_loop -->
-		</ol>
-		</fieldset>
+    </ol>
+    </fieldset>
     <input type="hidden" name="indicator" value=" " />
     <input type="hidden" name="indicator" value=" " />
     <input type="hidden" name="itemnumber" value="<!-- TMPL_VAR NAME="itemnumber" -->" />
-- 
1.5.5.GIT




More information about the Koha-patches mailing list