Finalizing main components. All koha modules are now working with the new XML API
[koha-ffzg.git] / C4 / Context.pm
index d7cdf9f..be8e868 100644 (file)
@@ -45,8 +45,8 @@ C4::Context - Maintain and manipulate the context of a Koha script
 =head1 DESCRIPTION
 
 When a Koha script runs, it makes use of a certain number of things:
-configuration settings in F</etc/koha.conf>, a connection to the Koha
-database, and so forth. These things make up the I<context> in which
+configuration settings in F</etc/koha.xml>, a connection to the Koha
+databases, and so forth. These things make up the I<context> in which
 the script runs.
 
 This module takes care of setting up the context for a script:
@@ -66,7 +66,7 @@ different contexts to search both databases. Such scripts should use
 the C<&set_context> and C<&restore_context> functions, below.
 
 By default, C4::Context reads the configuration from
-F</etc/koha.conf>. This may be overridden by setting the C<$KOHA_CONF>
+F</etc/koha.xml>. This may be overridden by setting the C<$KOHA_CONF>
 environment variable to the pathname of a configuration file to use.
 
 =head1 METHODS
@@ -156,11 +156,11 @@ sub import
 =item new
 
   $context = new C4::Context;
-  $context = new C4::Context("/path/to/koha.conf");
+  $context = new C4::Context("/path/to/koha.xml");
 
 Allocates a new context. Initializes the context from the specified
 file, which defaults to either the file given by the C<$KOHA_CONF>
-environment variable, or F</etc/koha.conf>.
+environment variable, or F</etc/koha.xml>.
 
 C<&new> does not set this context as the new default context; for
 that, use C<&set_context>.
@@ -200,6 +200,7 @@ sub new
        $self->{"Zconnauth"} = undef;   # Zebra Connection for updating
        $self->{"stopwords"} = undef; # stopwords list
        $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
+       $self->{"attrfromkohafield"} = undef; # the hash with relations between koha table fields and Bib1-attributes
        $self->{"userenv"} = undef;             # User env
        $self->{"activeuser"} = undef;          # current active user
 
@@ -397,27 +398,22 @@ creates one and connects.
 sub Zconn {
         my $self = shift;
 my $server=shift;
+my $syntax=shift;
        my $Zconn;
-      if (defined($context->{"Zconn"})) {
-           $Zconn = $context->{"Zconn"};
-                   return $context->{"Zconn"};
-       } else { 
-               $context->{"Zconn"} = &new_Zconn($server);
-               return $context->{"Zconn"};
-        }
+       $context->{"Zconn"} = &new_Zconn($server,$syntax);
+       return $context->{"Zconn"};
+  
 }
 
 sub Zconnauth {
         my $self = shift;
 my $server=shift;
+my $syntax=shift;
        my $Zconnauth;
-        if (defined($context->{"Zconnauth"})) {
-           $Zconnauth = $context->{"Zconnauth"};
-                   return $context->{"Zconnauth"};
-       } else {
-               $context->{"Zconnauth"} = &new_Zconnauth($server);
+##We destroy each connection made so create a new one  
+               $context->{"Zconnauth"} = &new_Zconnauth($server,$syntax);
                return $context->{"Zconnauth"};
-       }       
+               
 }
 
 
@@ -432,28 +428,19 @@ the data given in the current context and returns it.
 sub new_Zconn {
 use ZOOM;
 my $server=shift;
-my $tried==0;
+my $syntax=shift;
+$syntax="xml" unless $syntax;
 my $Zconn;
 my ($tcp,$host,$port)=split /:/,$context->{"listen"}->{$server}->{"content"};
+my $o = new ZOOM::Options();
+$o->option(async => 1);
+$o->option(preferredRecordSyntax => $syntax); ## in case we use MARC
+$o->option(databaseName=>$context->{"config"}->{$server});
 
-retry:
-       eval {
-               $Zconn=new ZOOM::Connection($context->config("hostname"),$port,databaseName=>$context->{"config"}->{$server},
-               preferredRecordSyntax => "USmarc",elementSetName=> "F");
-       };
-       if ($@){
-###Uncomment the lines below if you want to automatically restart your zebra if its stop
-###The system call is for Windows it should be changed to unix deamon starting for Unix platforms      
-#              if ($@->code==10000 && $tried==0){ ##No connection try restarting Zebra
-#              $tried==1;
-#              my $res=system('sc start "Z39.50 Server" >c:/zebraserver/error.log');
-#              goto "retry";
-#              }else{
-               warn "Error ", $@->code(), ": ", $@->message(), "\n";
-               $Zconn="error";
-               return $Zconn;
-#              }
-       }
+my $o2= new ZOOM::Options();
+
+ $Zconn=create ZOOM::Connection($o);
+       $Zconn->connect($context->{"config"}->{"hostname"},$port);
        
        return $Zconn;
 }
@@ -462,29 +449,20 @@ retry:
 sub new_Zconnauth {
 use ZOOM;
 my $server=shift;
-my $tried==0;
+my $syntax=shift;
+$syntax="xml" unless $syntax;
 my $Zconnauth;
 my ($tcp,$host,$port)=split /:/,$context->{"listen"}->{$server}->{"content"};
-retry:
-eval{
- $Zconnauth=new ZOOM::Connection($context->config("hostname"),$port,databaseName=>$context->{"config"}->{$server},
-                                               user=>$context->{"config"}->{"zebrauser"},
-                                               password=>$context->{"config"}->{"zebrapass"},preferredRecordSyntax => "USmarc",elementSetName=> "F");
-};
-       if ($@){
-###Uncomment the lines below if you want to automatically restart your zebra if its stop
-###The system call is for Windows it should be changed to unix deamon starting for Unix platforms      
-#              if ($@->code==10000 && $tried==0){ ##No connection try restarting Zebra
-#              $tried==1;
-#              my $res=system('sc start "Z39.50 Server" >c:/zebraserver/error.log');
-#              goto "retry";
-#              }else{
-               warn "Error ", $@->code(), ": ", $@->message(), "\n";
-               $Zconnauth="error";
-               return $Zconnauth;
-#              }
-       }
-       return $Zconnauth;
+my $o = new ZOOM::Options();
+#$o->option(async => 1);
+$o->option(preferredRecordSyntax => $syntax);
+$o->option(user=>$context->{"config"}->{"zebrauser"});
+$o->option(password=>$context->{"config"}->{"zebrapass"});
+$o->option(databaseName=>$context->{"config"}->{$server});
+ $o->option(charset=>"UTF8");
+ $Zconnauth=create ZOOM::Connection($o);
+$Zconnauth->connect($context->config("hostname"),$port);
+return $Zconnauth;
 }
 
 
@@ -650,22 +628,50 @@ sub marcfromkohafield
        return $context->{"marcfromkohafield"};
 }
 
+
 # _new_marcfromkohafield
-# Internal helper function (not a method!). This creates a new
-# hash with stopwords
+# Internal helper function (not a method!). 
 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 > ''");
+       my $sth = $dbh->prepare("select kohafield,tagfield,tagsubfield,recordtype from koha_attr where tagfield is not null  ");
        $sth->execute;
-       while (my ($frameworkcode,$kohafield,$tagfield,$tagsubfield) = $sth->fetchrow) {
+       while (my ($kohafield,$tagfield,$tagsubfield,$recordtype) = $sth->fetchrow) {
                my $retval = {};
-               $marcfromkohafield->{$frameworkcode}->{$kohafield} = [$tagfield,$tagsubfield];
+               $marcfromkohafield->{$recordtype}->{$kohafield} = [$tagfield,$tagsubfield];
        }
+       
        return $marcfromkohafield;
 }
 
+
+#item attrfromkohafield
+#To use as a hash of koha to z3950 attributes
+sub _new_attrfromkohafield
+{
+       my $dbh = C4::Context->dbh;
+       my $attrfromkohafield;
+       my $sth2 = $dbh->prepare("select kohafield,attr,extraattr from koha_attr" );
+       $sth2->execute;
+       while (my ($kohafield,$attr,$extra) = $sth2->fetchrow) {
+               my $retval = {};
+               $attrfromkohafield->{$kohafield} = "\@attr 1=".$attr." ".$extra;
+       }
+       return $attrfromkohafield;
+}
+sub attrfromkohafield
+{
+       my $retval = {};
+
+       # If the hash already exists, return it.
+       return $context->{"attrfromkohafield"} if defined($context->{"attrfromkohafield"});
+
+       # No hash. Create one.
+       $context->{"attrfromkohafield"} = &_new_attrfromkohafield();
+
+       return $context->{"attrfromkohafield"};
+}
 =item stopwords
 
   $dbh = C4::Context->stopwords;
@@ -826,6 +832,18 @@ Andrew Arensburger <arensb at ooblick dot com>
 
 =cut
 # $Log$
+# Revision 1.47  2006/09/27 19:53:52  tgarip1957
+# Finalizing main components. All koha modules are now working with the new XML API
+#
+# Revision 1.46  2006/09/06 16:21:03  tgarip1957
+# Clean up before final commits
+#
+# Revision 1.43  2006/08/10 12:49:37  toins
+# sync with dev_week.
+#
+# Revision 1.42  2006/07/04 14:36:51  toins
+# Head & rel_2_2 merged
+#
 # Revision 1.41  2006/05/20 14:36:09  tgarip1957
 # Typo error. Missing '>'
 #