Merge remote-tracking branch 'kc/new/bug_5868' into kcmaster
[koha.git] / admin / preferences.pl
index 146fe3f..d523c75 100755 (executable)
@@ -13,9 +13,9 @@
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
 use warnings;
@@ -28,7 +28,7 @@ use C4::Languages qw(getTranslatedLanguages);
 use C4::ClassSource;
 use C4::Log;
 use C4::Output;
-use C4::Bookfund qw(GetLocalCurrency);
+use C4::Budgets qw(GetCurrency);
 use File::Spec;
 use IO::File;
 use YAML::Syck qw();
@@ -43,8 +43,13 @@ sub GetTab {
 
     my $tab_template = C4::Output::gettemplate( 'admin/preferences/' . $tab . '.pref', 'intranet', $input );
 
+    my $active_currency = GetCurrency();
+    my $local_currency;
+    if ($active_currency) {
+        $local_currency = $active_currency->{currency};
+    }
     $tab_template->param(
-        local_currency => GetLocalCurrency()->{'currency'}, # currency code is used, because we do not know how a given currency is formatted.
+        local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
     );
 
     return YAML::Syck::Load( $tab_template->output() );
@@ -58,6 +63,8 @@ sub _get_chunk {
 
     if ( $options{'class'} && $options{'class'} eq 'password' ) {
         $chunk->{'input_type'} = 'password';
+    } elsif ( $options{'class'} && $options{'class'} eq 'date' ) {
+        $chunk->{'dateinput'} = 1;
     } elsif ( $options{'type'} && ( $options{'type'} eq 'opac-languages' || $options{'type'} eq 'staff-languages' ) ) {
         my $current_languages = { map { +$_, 1 } split( /\s*,\s*/, $value ) };
 
@@ -104,7 +111,7 @@ sub _get_chunk {
 }
 
 sub TransformPrefsToHTML {
-    my ( $data, $highlighted_pref ) = @_;
+    my ( $data, $searchfield ) = @_;
 
     my @lines;
     my $dbh = C4::Context->dbh;
@@ -135,10 +142,20 @@ sub TransformPrefsToHTML {
                         }
                         my $chunk = _get_chunk( $value, %$piece );
 
-                        $chunk->{'highlighted'} = 1 if ( $highlighted_pref && $name =~ /$highlighted_pref/ );
+                        # No highlighting of inputs yet, but would be useful
+                        $chunk->{'highlighted'} = 1 if ( $searchfield && $name =~ /^$searchfield$/i );
 
                         push @chunks, $chunk;
-                        push @names, { name => $name, highlighted => ( $highlighted_pref && ( $name =~ /$highlighted_pref/i ? 1 : 0 ) ) };
+
+                        my $name_entry = { name => $name };
+                        if ( $searchfield ) {
+                            if ( $name =~ /^$searchfield$/i ) {
+                                $name_entry->{'jumped'} = 1;
+                            } elsif ( $name =~ /$searchfield/i ) {
+                                $name_entry->{'highlighted'} = 1;
+                            }
+                        }
+                        push @names, $name_entry;
                     } else {
                         push @chunks, $piece;
                     }
@@ -147,7 +164,7 @@ sub TransformPrefsToHTML {
                 }
             }
 
-            push @lines, { CHUNKS => \@chunks, NAMES => \@names };
+            push @lines, { CHUNKS => \@chunks, NAMES => \@names, is_group_title => 0 };
         }
     }
 
@@ -170,24 +187,6 @@ sub _get_pref_files {
     return %results;
 }
 
-sub JumpPref {
-    my ( $input, $tab, $jumpfield ) = @_;
-
-    return ( $tab ) if ( $jumpfield !~ /^[a-zA-Z_0-9-]+$/ );
-
-    my %tab_files = _get_pref_files( $input, 1 );
-
-    while ( my ( $tab, $tabfile ) = each %tab_files ) {
-        while ( <$tabfile> ) {
-            return ( $tab, $1 ) if ( /pref: ($jumpfield)/i );
-        }
-
-        close $tabfile;
-    }
-
-    return ( "", "" );
-}
-
 sub SearchPrefs {
     my ( $input, $searchfield ) = @_;
     my @tabs;
@@ -222,10 +221,14 @@ sub SearchPrefs {
 
                 foreach my $piece ( @$line ) {
                     if ( ref( $piece ) eq 'HASH' ) {
-                        if ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
-                            $matched = 1;
+                        if ( $piece->{'pref'} =~ /^$searchfield$/i ) {
+                            my ( undef, $LINES ) = TransformPrefsToHTML( $data, $searchfield );
+
+                            return { search_jumped => 1, tab => $tab_name, tab_title => $title, LINES => $LINES };
                         } elsif ( matches( $piece->{'pref'} ) ) {
                             $matched = 1;
+                        } elsif ( ref( $piece->{'choices'} ) eq 'HASH' && grep( { $_ && matches( $_ ) } values( %{ $piece->{'choices'} } ) ) ) {
+                            $matched = 1;
                         }
                     } elsif ( matches( $piece ) ) {
                         $matched = 1;
@@ -265,7 +268,8 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
 $lang = $template->param( 'lang' );
 my $op = $input->param( 'op' ) || '';
 my $tab = $input->param( 'tab' );
-$tab ||= 'local-use';
+$tab ||= 'acquisitions'; # Ideally this should be "local-use" but preferences.pl
+                         # does not presently support local use preferences
 
 my $highlighted;
 
@@ -285,18 +289,6 @@ if ( $op eq 'save' ) {
 
     print $input->redirect( '/cgi-bin/koha/admin/preferences.pl?tab=' . $tab );
     exit;
-} elsif ( $op eq 'jump' ) {
-    my $jumpfield = $input->param( 'jumpfield' );
-    $template->param( jumpfield => $jumpfield );
-
-    my $new_tab;
-    ( $new_tab, $highlighted ) = JumpPref( $input, $tab, $jumpfield );
-
-    if ( $highlighted ) {
-        $tab = $new_tab;
-    } else {
-        $template->param( jump_not_found => 1 );
-    }
 }
 
 my @TABS;
@@ -318,6 +310,7 @@ if ( $op eq 'search' ) {
 
     if ( @TABS ) {
         $tab = ''; # No need to load a particular tab, as we found results
+        $template->param( search_jumped => 1 ) if ( $TABS[0]->{'search_jumped'} );
     } else {
         $template->param(
             search_not_found => 1,
@@ -326,7 +319,7 @@ if ( $op eq 'search' ) {
 }
 
 if ( $tab ) {
-    my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted, ( $op eq 'jump' ) );
+    my ( $tab_title, $LINES ) = TransformPrefsToHTML( GetTab( $input, $tab ), $highlighted );
 
     push @TABS, { tab_title => $tab_title, LINES => $LINES };
     $template->param(