A new Date.pm to use for all date calculations. Mysql date calculations removed from...
[koha-ffzg.git] / C4 / Biblio.pm
index 17d79f3..22ddb90 100644 (file)
@@ -75,14 +75,14 @@ $VERSION = 2.01;
 &XMLmoditemonefield
 &XMLkoha2marc
 &XML_separate
-
+&XML_record_header
 &ZEBRAdelbiblio
 &ZEBRAgetrecord   
 &ZEBRAop 
 &ZEBRAopserver 
 &ZEBRA_readyXML 
 &ZEBRA_readyXML_noheader
-
+&ZEBRAopcommit
 &newbiblio
 &modbiblio
 &DisplayISBN
@@ -235,7 +235,7 @@ $newvalue= Encode::decode('utf8',$newvalue) if $newvalue;
 my $biblio=$xml->{'datafield'};
 my $controlfield=$xml->{'controlfield'};
  ($tag,$subf)=MARCfind_marc_from_kohafield($kohafield,$recordtype) if $kohafield;
-my $updated=0;
+my $updated;
     if ($tag>9){
        foreach my $data (@$biblio){
                        if ($data->{'tag'} eq $tag){
@@ -281,7 +281,7 @@ my $updated=0;
                                            } ;
                   }                                                            
           }## created now
-    }else{
+    }elsif ($tag>0){
        foreach my $control (@$controlfield){
                if ($control->{'tag'} eq $tag){
                        $control->{'content'}=$newvalue;
@@ -348,6 +348,7 @@ return ($biblio,@items);
 sub XML_xml2hash_onerecord{
 ##make a perl hash from xml file
 my ($xml)=@_;
+return undef unless $xml;
   my $hashed = XMLin( $xml ,KeyAttr =>['leader','controlfield','datafield'],ForceArray => ['leader','controlfield','datafield','subfield'],KeepRoot=>0);
 return $hashed;
 }
@@ -435,7 +436,7 @@ if ($related_record eq "biblios" || $related_record eq "" || !$related_record){
                        
                }
        }else{
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where  recordtype like 'biblios' and tagfield is not null" );
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where  recordtype like 'biblios' and tagfield is not null" );
        $sth2->execute();
        my $field;
                while ($field=$sth2->fetchrow) {
@@ -478,7 +479,7 @@ my $itemresult;
            push @items, $itemresult;
           }
        }else{
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where recordtype like 'holdings' and tagfield is not null" );
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where recordtype like 'holdings' and tagfield is not null" );
           foreach my $holding (@$holdings){    
           $sth2->execute();
            my $field;
@@ -509,7 +510,7 @@ sub XMLmarc2koha_onerecord {
                        $result->{$field}=$val if $val;                 
                }
        }else{
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where  recordtype like ? and tagfield is not null" );
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where  recordtype like ? and tagfield is not null" );
        $sth2->execute($related_record);
        my $field;
                while ($field=$sth2->fetchrow) {
@@ -567,14 +568,14 @@ my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
 $year=substr($year,2,2);
        my $accdate=sprintf("%2d%02d%02d",$year,$mon,$mday);
 my ($titletag,$titlesubf)=MARCfind_marc_from_kohafield("title","biblios");
-my $xml="<record><leader>     naa a22     7ar4500</leader><controlfield tag='005'>$timestamp</controlfield><controlfield tag='008'>$accdate</controlfield><datafield ind1='' ind2='' tag='$titletag'></datafield></record>";
+##create a dummy record
+my $xml="<record><leader>     naa a22     7ar4500</leader><controlfield tag='xxx'></controlfield><datafield ind1='' ind2='' tag='$titletag'></datafield></record>";
 ## Now build XML
        my $record = XML_xml2hash($xml);
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where tagfield is not null and recordtype=?");
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where tagfield is not null and recordtype=?");
        $sth2->execute($recordtype);
        my $field;
        while (($field)=$sth2->fetchrow) {
-warn $field;
                $record=XML_writeline($record,$field,$result->{$field},$recordtype) if $result->{$field};
        }
 return $record;
@@ -836,7 +837,7 @@ sub MARChtml2xml {
        $xml=Encode::decode('utf8',$xml);
        return $xml;
 }
-sub marc_record_header {
+sub XML_record_header {
 ####  this one is for <record>
     my $format = shift;
     my $enc = shift || 'UTF-8';
@@ -912,7 +913,6 @@ NEWmoditem ( $dbh, $xmlhash, $biblionumber, $itemnumber);
    
 ##Add biblionumber to $record
 $xmlhash=XML_writeline($xmlhash,"biblionumber",$biblionumber,"holdings");
-#    MARCkoha2marcOnefield($record,"biblionumber",$biblionumber,"holdings");
  my $sth=$dbh->prepare("select notforloan from itemtypes where itemtype='$itemtype'");
 $sth->execute();
 my $notforloan=$sth->fetchrow;
@@ -1202,19 +1202,21 @@ my ($count,@result)=C4::Search::ZEBRAsearch_kohafields(\@kohafield,\@value);
 sub ZEBRAop {
 ### Puts the zebra update in queue writes in zebraserver table
 my ($dbh,$biblionumber,$op,$server)=@_;
-my ($record);
+if (!$biblionumber){
+warn "Zebra received no biblionumber";
+}else{
 my $sth=$dbh->prepare("insert into zebraqueue  (biblio_auth_number ,server,operation) values(?,?,?)");
 $sth->execute($biblionumber,$server,$op);
 }
-
+}
 
 sub ZEBRAopserver{
 
 ###Accepts a $server variable thus we can use it to update  biblios, authorities or other zebra dbs
 my ($record,$op,$server,$biblionumber)=@_;
-my @Zconnbiblio;
+
 my @port;
-my $Zpackage;
+
 my $tried=0;
 my $recon=0;
 my $reconnect=0;
@@ -1222,22 +1224,16 @@ $record=Encode::encode("UTF-8",$record);
 my $shadow=$server."shadow";
 reconnect:
 
-$Zconnbiblio[0]=C4::Context->Zconnauth($server);
+ my $Zconnbiblio=C4::Context->Zconnauth($server);
 if ($record){
-my $Zpackage = $Zconnbiblio[0]->package();
+my $Zpackage = $Zconnbiblio->package();
 $Zpackage->option(action => $op);
        $Zpackage->option(record => $record);
        $Zpackage->option(recordIdOpaque => $biblionumber);
 retry:
                $Zpackage->send("update");
-my $i;
-my $event;
 
-while (($i = ZOOM::event(\@Zconnbiblio)) != 0) {
-    $event = $Zconnbiblio[0]->last_event();
-    last if $event == ZOOM::Event::ZEND;
-}
- my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio[0]->error_x();
+ my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio->error_x();
        if ($error==10007 && $tried<3) {## timeout --another 30 looonng seconds for this update
                sleep 1;        ##  wait a sec!
                $tried=$tried+1;
@@ -1250,39 +1246,41 @@ while (($i = ZOOM::event(\@Zconnbiblio)) != 0) {
                sleep 1;        ##  wait a sec!
                $recon=1;
                $Zpackage->destroy();
-               $Zconnbiblio[0]->destroy();
+               $Zconnbiblio->destroy();
                goto "reconnect";
        }elsif ($error){
        #       warn "Error-$server   $op  /errcode:, $error, /MSG:,$errmsg,$addinfo \n";       
                $Zpackage->destroy();
-               $Zconnbiblio[0]->destroy();
-       #       ZEBRAopfiles($dbh,$biblionumber,$record,$op,$server);
+               $Zconnbiblio->destroy();
                return 0;
        }
-       ## System preference batchMode=1 means wea are bulk importing
-       ## DO NOT COMMIT while in batchMode for faster operation
-       my $batchmode=C4::Context->preference('batchMode');
-        if (C4::Context->$shadow >0 && !$batchmode){
-        $Zpackage->send('commit');
-               while (($i = ZOOM::event(\@Zconnbiblio)) != 0) {
-                $event = $Zconnbiblio[0]->last_event();
-               last if $event == ZOOM::Event::ZEND;
-               }
-            my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio[0]->error_x();
-            if ($error) { ## This is serious ZEBRA server is not updating      
-            $Zpackage->destroy();
-            $Zconnbiblio[0]->destroy();
-            return 0;
-           }
-        }##commit
-#
+       
 $Zpackage->destroy();
-$Zconnbiblio[0]->destroy();
+$Zconnbiblio->destroy();
 return 1;
 }
 return 0;
 }
 
+
+sub ZEBRAopcommit {
+my $server=shift;
+
+my $Zconnbiblio=C4::Context->Zconnauth($server);
+
+my $Zpackage = $Zconnbiblio->package();
+ $Zpackage->send('commit');
+               
+                my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio->error_x();
+                if ($error) { ## This is serious ZEBRA server is not updating  
+            $Zpackage->destroy();
+            $Zconnbiblio->destroy();
+            return 0;
+           }
+$Zpackage->destroy();
+$Zconnbiblio->destroy();
+return 1;
+}
 sub ZEBRA_readyXML{
 my ($dbh,$biblionumber)=@_;
 my $biblioxml=XMLgetbiblio($dbh,$biblionumber);
@@ -1380,7 +1378,7 @@ sub getitemtypes {
 
 sub getkohafields{
 #returns MySQL like fieldnames to emulate searches on sql like fieldnames
-my $type=@_;
+my $type=shift;
 ## Either opac or intranet to select appropriate fields
 ## Assumes intranet
 $type="intra" unless $type;
@@ -1389,7 +1387,7 @@ my $dbh   = C4::Context->dbh;
   my $i=0;
 my @results;
 $type=$type."show";
-my $sth=$dbh->prepare("SELECT  * FROM koha_attr  where $type=1 order by liblibrarian");
+my $sth=$dbh->prepare("SELECT  * FROM koha_attr  where $type=1 order by label");
 $sth->execute();
 while (my $data=$sth->fetchrow_hashref){
        $results[$i]=$data;