Plugin for field 124e
[koha.git] / C4 / Context.pm
index 4b88245..78c817f 100644 (file)
@@ -226,10 +226,12 @@ sub new
 
        # Load the desired config file.
        $self->{"config"} = &read_config_file($conf_fname);
+       warn "read_config_file($conf_fname) returned undef" if !defined($self->{"config"});
        return undef if !defined($self->{"config"});
 
        $self->{"dbh"} = undef;         # Database handle
        $self->{"stopwords"} = undef; # stopwords list
+       $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
 
        bless $self, $class;
        return $self;
@@ -433,8 +435,6 @@ sub dbh
            return $context->{"dbh"} if (defined($sth->execute));
        }
 
-       warn "Database died";
-
        # No database handle or it died . Create one.
        $context->{"dbh"} = &_new_dbh();
 
@@ -517,6 +517,46 @@ sub restore_dbh
        # return something, then this function should, too.
 }
 
+=item marcfromkohafield
+
+  $dbh = C4::Context->marcfromkohafield;
+
+Returns a hash with marcfromkohafield.
+
+This hash is cached for future use: if you call
+C<C4::Context-E<gt>marcfromkohafield> twice, you will get the same hash without real DB access
+
+=cut
+#'
+sub marcfromkohafield
+{
+       my $retval = {};
+
+       # If the hash already exists, return it.
+       return $context->{"marcfromkohafield"} if defined($context->{"marcfromkohafield"});
+
+       # No hash. Create one.
+       $context->{"marcfromkohafield"} = &_new_marcfromkohafield();
+
+       return $context->{"marcfromkohafield"};
+}
+
+# _new_marcfromkohafield
+# Internal helper function (not a method!). This creates a new
+# hash with stopwords
+sub _new_marcfromkohafield
+{
+       my $dbh = C4::Context->dbh;
+       my $marcfromkohafield;
+       my $sth = $dbh->prepare("select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''");
+       $sth->execute;
+       while (my ($frameworkcode,$kohafield,$tagfield,$tagsubfield) = $sth->fetchrow) {
+               my $retval = {};
+               $marcfromkohafield->{$frameworkcode}->{$kohafield} = [$tagfield,$tagsubfield];
+       }
+       return $marcfromkohafield;
+}
+
 =item stopwords
 
   $dbh = C4::Context->stopwords;
@@ -557,6 +597,8 @@ sub _new_stopwords
        return $stopwordlist;
 }
 
+
+
 1;
 __END__