Plugin for field 124e
[koha.git] / C4 / Authorities.pm
index 0cca31c..8af184b 100644 (file)
@@ -51,8 +51,9 @@ It contains every functions to manage/find authorities.
 @EXPORT = qw(  &newauthority
                                                &searchauthority
                                                &delauthority
+                                               &modauthority
+                                               &SearchDeeper
                                        );
-# FIXME - This is never used
 
 =item newauthority
 
@@ -85,6 +86,7 @@ Note :
 sub newauthority  {
        my ($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy)=@_;
        exit unless ($stdlib);
+       $level=1 unless $level;
        $freelib = $stdlib unless ($freelib);
        my $dbh = C4::Context->dbh;
        my $sth1b=$dbh->prepare("select id from bibliothesaurus where freelib=? and hierarchy=? and category=?");
@@ -109,6 +111,7 @@ sub newauthority  {
        my $id;
        if ($#Thierarchy >=0) {
                # free form
+               $hierarchy='' unless $hierarchy;
                $sth1b->execute($freelib,$hierarchy,$category);
                ($id) = $sth1b->fetchrow;
                unless ($id) {
@@ -118,6 +121,7 @@ sub newauthority  {
                        $Fhierarchy[$#Fhierarchy] =~ s/\s+$// if ($#Fhierarchy>=0);
                        $freelib =~ s/\s+$//;
                        $sth2->execute($category,$Thierarchy[$#Thierarchy],$#Fhierarchy==$#Thierarchy?$Fhierarchy[$#Fhierarchy]:$freelib,$father,$level,$hierarchy);
+               } else {
                }
                # authority form
                $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
@@ -125,17 +129,36 @@ sub newauthority  {
                unless ($id) {
                        $Thierarchy[$#Thierarchy] =~ s/^\s+//;
                        $Thierarchy[$#Thierarchy] =~ s/\s+$//;
-                       $sth2->execute($category,$Thierarchy[$#Thierarchy],$Thierarchy[$#Thierarchy],$father,$level,$hierarchy);
                        $sth1b->execute($stdlib,$hierarchy,$category);
                        ($id) = $sth1b->fetchrow;
+                       unless ($id) {
+                               $sth2->execute($category,$Thierarchy[$#Thierarchy],$Thierarchy[$#Thierarchy],$father,$level,$hierarchy);
+                       }
                }
        }
        return $id;
 }
 
+=item ModAuthority
+
+  $id = &ModAuthority($dbh,$id,$freelib);
+
+  modify a free lib
+
+ C<$dbh> is a DBI::db handle for the Koha database.
+ C<$id> is the entry id
+ C<$freelib> is the new freelib
+
+=cut
+sub modauthority {
+       my ($dbh,$id,$freelib) = @_;
+       my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
+       $sth->execute($freelib,$id);
+}
+
 =item SearchAuthority
 
-  $id = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
+  ($count, \@array) = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
 
   searches for an authority
 
@@ -148,6 +171,9 @@ entries beginning by 1024|2345
 
 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
 
+return :
+C<$count> : the number of authorities found
+C<\@array> : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
 
 =cut
 sub searchauthority  {
@@ -155,30 +181,82 @@ sub searchauthority  {
        $offset=0 unless ($offset);
 #      warn "==> ($env,$category,$branch,$searchstring,$offset,$pagesize)";
        my $dbh = C4::Context->dbh;
-       $searchstring=~ s/\'/\\\'/g;
-       my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where (category =\"$category\")";
-       $query .= " and hierarchy='$branch'" if ($branch);
-       $query .= " and match (category,freelib) AGAINST ('$searchstring')" if ($searchstring);
+       my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where category=?";
+       my @bind=($category);
+       if ($branch) {
+               $query .= " and hierarchy=?";
+               push(@bind,$branch);
+               }
+       if ($searchstring) {
+               $query .= " and match (category,freelib) AGAINST (?)";
+               push(@bind,$searchstring);
+               }
 #      $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
-       $query .= " order by category,freelib limit $offset,".($pagesize*4);
-#      warn "q : $query";
+       $query .= " limit ?,?";
+       push(@bind,$offset,($pagesize*4));
+       warn "q : $query".@bind;
        my $sth=$dbh->prepare($query);
-       $sth->execute;
+       $sth->execute(@bind);
        my @results;
        my $old_stdlib="";
        while (my $data=$sth->fetchrow_hashref){
                        push(@results,$data);
        }
        $sth->finish;
-       $query="Select count(*) from bibliothesaurus where (category =\"$category\")";
-       $query .= " and hierarchy='$branch'" if ($branch);
-       $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
-       $query .= "";
+       $query="Select count(*) from bibliothesaurus where category =?";
+       @bind=($category);
+       if ($branch) {
+               $query .= " and hierarchy=?";
+               push(@bind,$branch);
+               }
+       if ($searchstring) {
+               $query .= " and stdlib like ?";
+               push(@bind,"$searchstring%");
+               }
        $sth=$dbh->prepare($query);
-       $sth->execute;
+       $sth->execute(@bind);
        my ($cnt) = $sth->fetchrow;
        $cnt = $pagesize+1 if ($cnt>$pagesize);
-       return ($cnt,\@results);
+       $sth->finish();
+       return ($#results,\@results);
+}
+
+=item SearchDeeper
+
+ @array = &SearchAuthority($dbh,$category,$father);
+
+  Finds everything depending on the parameter.
+
+C<$dbh> is a DBI::db handle for the Koha database.
+
+C<$category> is the category of the authority
+
+C<$father> Is the string "father".
+
+return :
+@array : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
+
+For example :
+Geography -- Europe is the father and the result is : France and Germany if there is
+Geography -- Europe -- France and Geography -- Europe -- Germany in the thesaurus
+
+
+=cut
+sub SearchDeeper  {
+       my ($category,$father)=@_;
+       my $dbh = C4::Context->dbh;
+       my $sth=$dbh->prepare("Select distinct level,stdlib,father from bibliothesaurus where category =? and father =? order by category,stdlib");
+       $sth->execute($category,"$father --");
+       my @results;
+       while (my ($level,$stdlib,$father)=$sth->fetchrow){
+                       my %line;
+                       $line{level} = $level;
+                       $line{stdlib}= $stdlib;
+                       $line{father} = $father;
+                       push(@results,\%line);
+       }
+       $sth->finish;
+       return (@results);
 }