System Preference updates
[koha.git] / C4 / Koha.pm
index 290ef40..11c7850 100644 (file)
@@ -13,15 +13,17 @@ package C4::Koha;
 # 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; FIXME - Bug 2505
 use C4::Context;
 use C4::Output;
 use URI::Split qw(uri_split);
+use Memoize;
 
 use vars qw($VERSION @ISA @EXPORT $DEBUG);
 
@@ -36,6 +38,7 @@ BEGIN {
                &GetPrinters &GetPrinter
                &GetItemTypes &getitemtypeinfo
                &GetCcodes
+               &GetSupportName &GetSupportList
                &get_itemtypeinfos_of
                &getframeworks &getframeworkinfo
                &getauthtypes &getauthtype
@@ -51,8 +54,8 @@ BEGIN {
                &GetAuthorisedValues
                &GetAuthorisedValueCategories
                &GetKohaAuthorisedValues
+               &GetKohaAuthorisedValuesFromField
                &GetAuthValCode
-               &GetManagedTagSubfields
                &GetNormalizedUPC
                &GetNormalizedISBN
                &GetNormalizedEAN
@@ -63,6 +66,9 @@ BEGIN {
        $DEBUG = 0;
 }
 
+# expensive functions
+memoize('GetAuthorisedValues');
+
 =head1 NAME
 
     C4::Koha - Perl Module containing convenience functions for Koha scripts
@@ -208,6 +214,87 @@ sub subfield_is_koha_internal_p ($) {
     return length $subfield != 1;
 }
 
+=head2 GetSupportName
+
+  $itemtypename = &GetSupportName($codestring);
+
+Returns a string with the name of the itemtype.
+
+
+=cut
+
+sub GetSupportName{
+       my ($codestring)=@_;
+       return if (! $codestring); 
+       my $resultstring;
+       my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
+       if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {  
+               my $query = qq|
+                       SELECT description
+                       FROM   itemtypes
+                       WHERE itemtype=?
+                       order by description
+               |;
+               my $sth = C4::Context->dbh->prepare($query);
+               $sth->execute($codestring);
+               ($resultstring)=$sth->fetchrow;
+               return $resultstring;
+       } else {
+        my $sth =
+            C4::Context->dbh->prepare(
+                    "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
+                    );
+        $sth->execute( $advanced_search_types, $codestring );
+        my $data = $sth->fetchrow_hashref;
+        return $$data{'lib'};
+       }
+
+}
+=head2 GetSupportList
+
+  $itemtypes = &GetSupportList();
+
+Returns an array ref containing informations about Support (since itemtype is rather a circulation code when item-level-itypes is used).
+
+build a HTML select with the following code :
+
+=head3 in PERL SCRIPT
+
+    my $itemtypes = GetSupportList();
+    $template->param(itemtypeloop => $itemtypes);
+
+=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>
+
+=cut
+
+sub GetSupportList{
+       my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
+       if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {  
+               my $query = qq|
+                       SELECT *
+                       FROM   itemtypes
+                       order by description
+               |;
+               my $sth = C4::Context->dbh->prepare($query);
+               $sth->execute;
+               return $sth->fetchall_arrayref({});
+       } else {
+               my $advsearchtypes = GetAuthorisedValues($advanced_search_types);
+               my @results= map {{itemtype=>$$_{authorised_value},description=>$$_{lib},imageurl=>$$_{imageurl}}} @$advsearchtypes;
+               return \@results;
+       }
+}
 =head2 GetItemTypes
 
   $itemtypes = &GetItemTypes();
@@ -963,20 +1050,6 @@ sub displayServers {
     return \@primaryserverloop;
 }
 
-sub displaySecondaryServers {
-
-#      my $secondary_servers_loop = [
-#              { inner_sup_servers_loop => [
-#              {label => "Google", id=>"GOOG", value=>"google",icon => "google.ico",opensearch => "1"},
-#              {label => "Yahoo", id=>"YAH", value=>"yahoo", icon =>"yahoo.ico", zed => "1"},
-#              {label => "Worldcat", id=>"WCT", value=>"worldcat", icon => "worldcat.gif", zed => "1"},
-#              {label => "Library of Congress", id=>"LOC", name=> "server", value=>"z3950.loc.gov:7090/Voyager", icon =>"loc.ico", zed => "1"},
-#      ],
-#      },
-#      ];
-    return;    #$secondary_servers_loop;
-}
-
 =head2 GetAuthValCode
 
 $authvalcode = GetAuthValCode($kohafield,$frameworkcode);
@@ -993,30 +1066,59 @@ sub GetAuthValCode {
        return $authvalcode;
 }
 
+=head2 GetAuthValCodeFromField
+
+$authvalcode = GetAuthValCodeFromField($field,$subfield,$frameworkcode);
+
+C<$subfield> can be undefined
+
+=cut
+
+sub GetAuthValCodeFromField {
+       my ($field,$subfield,$fwcode) = @_;
+       my $dbh = C4::Context->dbh;
+       $fwcode='' unless $fwcode;
+       my $sth;
+       if (defined $subfield) {
+           $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?');
+           $sth->execute($field,$subfield,$fwcode);
+       } else {
+           $sth = $dbh->prepare('select authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?');
+           $sth->execute($field,$fwcode);
+       }
+       my ($authvalcode) = $sth->fetchrow_array;
+       return $authvalcode;
+}
+
 =head2 GetAuthorisedValues
 
 $authvalues = GetAuthorisedValues([$category], [$selected]);
 
-This function returns all authorised values from the'authosied_value' table in a reference to array of hashrefs.
+This function returns all authorised values from the'authorised_value' table in a reference to array of hashrefs.
 
 C<$category> returns authorised values for just one category (optional).
 
+C<$opac> If set to a true value, displays OPAC descriptions rather than normal ones when they exist.
+
 =cut
 
 sub GetAuthorisedValues {
-    my ($category,$selected) = @_;
+    my ($category,$selected,$opac) = @_;
        my @results;
     my $dbh      = C4::Context->dbh;
     my $query    = "SELECT * FROM authorised_values";
     $query .= " WHERE category = '" . $category . "'" if $category;
-
+    $query .= " ORDER BY category, lib, lib_opac";
     my $sth = $dbh->prepare($query);
     $sth->execute;
        while (my $data=$sth->fetchrow_hashref) {
-               if ($selected eq $data->{'authorised_value'} ) {
-                       $data->{'selected'} = 1;
-               }
-        push @results, $data;
+           if ($selected && $selected eq $data->{'authorised_value'} ) {
+                   $data->{'selected'} = 1;
+           }
+           if ($opac && $data->{'lib_opac'}) {
+               $data->{'lib'} = $data->{'lib_opac'};
+           }
+           push @results, $data;
        }
     #my $data = $sth->fetchall_arrayref({});
     return \@results; #$data;
@@ -1045,6 +1147,7 @@ sub GetAuthorisedValueCategories {
 =head2 GetKohaAuthorisedValues
        
        Takes $kohafield, $fwcode as parameters.
+       If $opac parameter is set to a true value, displays OPAC descriptions rather than normal ones when they exist.
        Returns hashref of Code => description
        Returns undef 
          if no authorised value category is defined for the kohafield.
@@ -1052,16 +1155,16 @@ sub GetAuthorisedValueCategories {
 =cut
 
 sub GetKohaAuthorisedValues {
-  my ($kohafield,$fwcode,$codedvalue) = @_;
+  my ($kohafield,$fwcode,$opac) = @_;
   $fwcode='' unless $fwcode;
   my %values;
   my $dbh = C4::Context->dbh;
   my $avcode = GetAuthValCode($kohafield,$fwcode);
   if ($avcode) {  
-       my $sth = $dbh->prepare("select authorised_value, lib from authorised_values where category=? ");
+       my $sth = $dbh->prepare("select authorised_value, lib, lib_opac from authorised_values where category=? ");
        $sth->execute($avcode);
-       while ( my ($val, $lib) = $sth->fetchrow_array ) { 
-               $values{$val}= $lib;
+       while ( my ($val, $lib, $lib_opac) = $sth->fetchrow_array ) { 
+               $values{$val} = ($opac && $lib_opac) ? $lib_opac : $lib;
        }
        return \%values;
   } else {
@@ -1069,47 +1172,33 @@ sub GetKohaAuthorisedValues {
   }
 }
 
-=head2 GetManagedTagSubfields
-
-=over 4
-
-$res = GetManagedTagSubfields();
-
-=back
-
-Returns a reference to a big hash of hash, with the Marc structure fro the given frameworkcode
-
-NOTE: This function is used only by the (incomplete) bulk editing feature.  Since
-that feature currently does not deal with items and biblioitems changes 
-correctly, those tags are specifically excluded from the list prepared
-by this function.
-
-For future reference, if a bulk item editing feature is implemented at some point, it
-needs some design thought -- for example, circulation status fields should not 
-be changed willy-nilly.
+=head2 GetKohaAuthorisedValuesFromField
+       
+       Takes $field, $subfield $fwcode as parameters.
+       If $opac parameter is set to a true value, displays OPAC descriptions rather than normal ones when they exist.
+       $subfield can be undefined
+       Returns hashref of Code => description
+       Returns undef 
+         if no authorised value category is defined for the given field and subfield 
 
 =cut
 
-sub GetManagedTagSubfields{
-  my $dbh=C4::Context->dbh;
-  my $rq=$dbh->prepare(qq|
-SELECT 
-  DISTINCT CONCAT( marc_subfield_structure.tagfield, tagsubfield ) AS tagsubfield, 
-  marc_subfield_structure.liblibrarian as subfielddesc, 
-  marc_tag_structure.liblibrarian as tagdesc
-FROM marc_subfield_structure
-  LEFT JOIN marc_tag_structure 
-    ON marc_tag_structure.tagfield = marc_subfield_structure.tagfield
-    AND marc_tag_structure.frameworkcode = marc_subfield_structure.frameworkcode
-WHERE marc_subfield_structure.tab>=0
-AND marc_tag_structure.tagfield NOT IN (SELECT tagfield FROM marc_subfield_structure WHERE kohafield like 'items.%')
-AND marc_tag_structure.tagfield NOT IN (SELECT tagfield FROM marc_subfield_structure WHERE kohafield = 'biblioitems.itemtype')
-AND marc_subfield_structure.kohafield <> 'biblio.biblionumber'
-AND marc_subfield_structure.kohafield <>  'biblioitems.biblioitemnumber'
-ORDER BY marc_subfield_structure.tagfield, tagsubfield|);
-  $rq->execute;
-  my $data=$rq->fetchall_arrayref({});
-  return $data;
+sub GetKohaAuthorisedValuesFromField {
+  my ($field, $subfield, $fwcode,$opac) = @_;
+  $fwcode='' unless $fwcode;
+  my %values;
+  my $dbh = C4::Context->dbh;
+  my $avcode = GetAuthValCodeFromField($field, $subfield, $fwcode);
+  if ($avcode) {  
+       my $sth = $dbh->prepare("select authorised_value, lib, lib_opac from authorised_values where category=? ");
+       $sth->execute($avcode);
+       while ( my ($val, $lib, $lib_opac) = $sth->fetchrow_array ) { 
+               $values{$val} = ($opac && $lib_opac) ? $lib_opac : $lib;
+       }
+       return \%values;
+  } else {
+       return undef;
+  }
 }
 
 =head2 display_marc_indicators
@@ -1249,10 +1338,12 @@ sub _normalize_match_point {
 
 sub _isbn_cleanup ($) {
     my $normalized_isbn = shift;
-    $normalized_isbn =~/([0-9]{1,})/;
+    $normalized_isbn =~ s/-//g;
+    $normalized_isbn =~/([0-9x]{1,})/i;
     $normalized_isbn = $1;
     if (
         $normalized_isbn =~ /\b(\d{13})\b/ or
+        $normalized_isbn =~ /\b(\d{12})\b/i or
         $normalized_isbn =~ /\b(\d{10})\b/ or
         $normalized_isbn =~ /\b(\d{9}X)\b/i
     ) {