Bug 8015: (follow-up) fix various issues (squashed patch)
[koha.git] / C4 / Koha.pm
index 3648dcc..bb3efc7 100644 (file)
@@ -25,8 +25,8 @@ use strict;
 
 use C4::Context;
 use C4::Branch qw(GetBranchesCount);
+use Koha::DateUtils qw(dt_from_string);
 use Memoize;
-use DateTime;
 use DateTime::Format::MySQL;
 use autouse 'Data::Dumper' => qw(Dumper);
 
@@ -41,7 +41,6 @@ BEGIN {
                &subfield_is_koha_internal_p
                &GetPrinters &GetPrinter
                &GetItemTypes &getitemtypeinfo
-               &GetCcodes
                &GetSupportName &GetSupportList
                &get_itemtypeinfos_of
                &getframeworks &getframeworkinfo
@@ -174,16 +173,16 @@ build a HTML select with the following code :
 
 =head3 in TEMPLATE
 
-    <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
-        <select name="itemtype">
-            <option value="">Default</option>
-        <!-- TMPL_LOOP name="itemtypeloop" -->
-            <option value="<!-- TMPL_VAR name="itemtype" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->> <!--TMPL_IF Name="imageurl"--><img alt="<!-- TMPL_VAR name="description" -->" src="<!--TMPL_VAR Name="imageurl"-->><!--TMPL_ELSE-->"<!-- TMPL_VAR name="description" --><!--/TMPL_IF--></option>
-        <!-- /TMPL_LOOP -->
-        </select>
-        <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
-        <input type="submit" value="OK" class="button">
-    </form>
+    <select name="itemtype" id="itemtype">
+        <option value=""></option>
+        [% FOREACH itemtypeloo IN itemtypeloop %]
+             [% IF ( itemtypeloo.selected ) %]
+                <option value="[% itemtypeloo.itemtype %]" selected="selected">[% itemtypeloo.description %]</option>
+            [% ELSE %]
+                <option value="[% itemtypeloo.itemtype %]">[% itemtypeloo.description %]</option>
+            [% END %]
+       [% END %]
+    </select>
 
 =cut
 
@@ -206,10 +205,15 @@ sub GetSupportList{
 }
 =head2 GetItemTypes
 
-  $itemtypes = &GetItemTypes();
+  $itemtypes = &GetItemTypes( style => $style );
 
 Returns information about existing itemtypes.
 
+Params:
+    style: either 'array' or 'hash', defaults to 'hash'.
+           'array' returns an arrayref,
+           'hash' return a hashref with the itemtype value as the key
+
 build a HTML select with the following code :
 
 =head3 in PERL SCRIPT
@@ -242,6 +246,8 @@ build a HTML select with the following code :
 =cut
 
 sub GetItemTypes {
+    my ( %params ) = @_;
+    my $style = defined( $params{'style'} ) ? $params{'style'} : 'hash';
 
     # returns a reference to a hash of references to itemtypes...
     my %itemtypes;
@@ -252,10 +258,15 @@ sub GetItemTypes {
     |;
     my $sth = $dbh->prepare($query);
     $sth->execute;
-    while ( my $IT = $sth->fetchrow_hashref ) {
-        $itemtypes{ $IT->{'itemtype'} } = $IT;
+
+    if ( $style eq 'hash' ) {
+        while ( my $IT = $sth->fetchrow_hashref ) {
+            $itemtypes{ $IT->{'itemtype'} } = $IT;
+        }
+        return ( \%itemtypes );
+    } else {
+        return $sth->fetchall_arrayref({});
     }
-    return ( \%itemtypes );
 }
 
 sub get_itemtypeinfos_of {
@@ -274,27 +285,6 @@ END_SQL
     return get_infos_of( $query, 'itemtype', undef, \@itemtypes );
 }
 
-# this is temporary until we separate collection codes and item types
-sub GetCcodes {
-    my $count = 0;
-    my @results;
-    my $dbh = C4::Context->dbh;
-    my $sth =
-      $dbh->prepare(
-        "SELECT * FROM authorised_values ORDER BY authorised_value");
-    $sth->execute;
-    while ( my $data = $sth->fetchrow_hashref ) {
-        if ( $data->{category} eq "CCODE" ) {
-            $count++;
-            $results[$count] = $data;
-
-            #warn "data: $data";
-        }
-    }
-    $sth->finish;
-    return ( $count, @results );
-}
-
 =head2 getauthtypes
 
   $authtypes = &getauthtypes();
@@ -718,6 +708,11 @@ sub getFacets {
                 tags  => [ qw/ 225a / ],
                 sep   => ', ',
             },
+            {
+                idx  => 'location',
+                label => 'Location',
+                tags        => [ qw/ 995c / ],
+            }
             ];
 
             my $library_facet;
@@ -727,12 +722,6 @@ sub getFacets {
                     label => 'Libraries',
                     tags        => [ qw/ 995b / ],
                 };
-            } else {
-                $library_facet = {
-                    idx  => 'location',
-                    label => 'Location',
-                    tags        => [ qw/ 995c / ],
-                };
             }
             push( @$facets, $library_facet );
     }
@@ -780,6 +769,11 @@ sub getFacets {
                 tags  => [ qw/ 952y 942c / ],
                 sep   => ', ',
             },
+            {
+                idx => 'location',
+                label => 'Location',
+                tags => [ qw / 952c / ],
+            },
             ];
 
             my $library_facet;
@@ -789,12 +783,6 @@ sub getFacets {
                     label => 'Libraries',
                     tags        => [ qw / 952b / ],
                 };
-            } else {
-                $library_facet = {
-                    idx => 'location',
-                    label => 'Location',
-                    tags => [ qw / 952c / ],
-                };
             }
             push( @$facets, $library_facet );
     }
@@ -1064,7 +1052,11 @@ sub GetAuthorisedValues {
     if(@where_strings > 0) {
         $query .= " WHERE " . join(" AND ", @where_strings);
     }
-    $query .= " GROUP BY lib ORDER BY category, lib, lib_opac";
+    $query .= " GROUP BY lib";
+    $query .= ' ORDER BY category, ' . (
+                $opac ? 'COALESCE(lib_opac, lib)'
+                      : 'lib, lib_opac'
+              );
 
     my $sth = $dbh->prepare($query);
 
@@ -1130,7 +1122,7 @@ sub IsAuthorisedValueCategory {
 
 =head2 GetAuthorisedValueByCode
 
-$authhorised_value = GetAuthorisedValueByCode( $category, $authvalcode );
+$authorised_value = GetAuthorisedValueByCode( $category, $authvalcode, $opac );
 
 Return the lib attribute from authorised_values from the row identified
 by the passed category and code
@@ -1466,7 +1458,10 @@ sub GetDailyQuote {
         # update the timestamp for that quote
         $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
         $sth = C4::Context->dbh->prepare($query);
-        $sth->execute(DateTime::Format::MySQL->format_datetime(DateTime->now), $quote->{'id'});
+        $sth->execute(
+            DateTime::Format::MySQL->format_datetime( dt_from_string() ),
+            $quote->{'id'}
+        );
     }
     return $quote;
 }
@@ -1491,6 +1486,22 @@ sub _isbn_cleanup {
     return;
 }
 
+=head2 Log( $message );
+
+  Writes data to /tmp/koha.log.
+
+  This is useful for debugging forked processes
+  that do not write to the apache error log
+
+=cut
+
+sub Log {
+  my ($data) = @_;
+  warn $data;
+  open my $fh, '>>/tmp/koha.log';
+  print $fh "$data\n";
+  close $fh;
+}
 1;
 
 __END__