Fixed an encoding bug in the documentation
[koha.git] / C4 / SQLHelper.pm
index d2df0a5..4470742 100644 (file)
@@ -13,9 +13,9 @@ package C4::SQLHelper;
 # 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;
@@ -97,7 +97,7 @@ $searchtype is string Can be "start_with" or "exact"
 
 sub SearchInTable{
     my ($tablename,$filters,$orderby, $limit, $columns_out, $filter_columns,$searchtype) = @_; 
-#      $searchtype||="start_with";
+       $searchtype||="exact";
     my $dbh      = C4::Context->dbh; 
        $columns_out||=["*"];
     my $sql      = do { local $"=', '; 
@@ -116,10 +116,13 @@ sub SearchInTable{
        }
     if ($orderby){ 
                #Order by desc by default
-        my @orders=map{ "$_".($$orderby{$_}? " DESC" : "") } keys %$orderby; 
-        $sql.= do { local $"=', '; 
-                qq{ ORDER BY @orders} 
-               }; 
+               my @orders;
+               foreach my $order (@$orderby){
+                       push @orders,map{ "$_".($order->{$_}? " DESC " : "") } keys %$order; 
+               }
+               $sql.= do { local $"=', '; 
+                               qq{ ORDER BY @orders} 
+        }; 
     } 
        if ($limit){
                $sql.=qq{ LIMIT }.join(",",@$limit);
@@ -137,7 +140,7 @@ sub SearchInTable{
 
 =over 4
 
-  $data_id_in_table = &InsertInTable($tablename,$data_hashref);
+  $data_id_in_table = &InsertInTable($tablename,$data_hashref,$withprimarykeys);
 
 =back
 
@@ -146,9 +149,9 @@ sub SearchInTable{
 =cut
 
 sub InsertInTable{
-    my ($tablename,$data) = @_;
+    my ($tablename,$data,$withprimarykeys) = @_;
     my $dbh      = C4::Context->dbh;
-    my ($keys,$values)=_filter_hash($tablename,$data,0);
+    my ($keys,$values)=_filter_hash($tablename,$data,($withprimarykeys?"exact":0));
     my $query = qq{ INSERT INTO $tablename SET  }.join(", ",@$keys);
 
        $debug && warn $query, join(",",@$values);
@@ -233,7 +236,7 @@ sub DeleteInTable{
 sub GetPrimaryKeys($) {
        my $tablename=shift;
        my $hash_columns=_get_columns($tablename);
-       return  grep { $$hash_columns{$_}{'Key'} =~/PRI/i}  keys %$hash_columns;
+       return  grep { $hash_columns->{$_}->{'Key'} =~/PRI/i}  keys %$hash_columns;
 }
 
 =head2 _get_columns
@@ -341,6 +344,7 @@ sub _filter_fields{
                }
        } 
        else{
+        $debug && warn "filterstring : $filter_input";
                my ($keys, $values) = _filter_string($tablename,$filter_input, $searchtype,$filtercolumns);
                if ($keys){
                my $stringkey="(".join (") AND (",@$keys).")";
@@ -364,8 +368,8 @@ sub _filter_hash{
     my $elements=join "|",@columns_filtered;
        foreach my $field (grep {/\b($elements)\b/} keys %$filter_input){
                ## supposed to be a hash of simple values, hashes of arrays could be implemented
-               $$filter_input{$field}=format_date_in_iso($$filter_input{$field}) if ($$columns{$field}{Type}=~/date/ && $$filter_input{$field} !~C4::Dates->regexp("iso"));
-               my ($tmpkeys, $localvalues)=_Process_Operands($$filter_input{$field},"$tablename.$field",$searchtype,$columns);
+               $filter_input->{$field}=format_date_in_iso($filter_input->{$field}) if ($columns->{$field}{Type}=~/date/ && $filter_input->{$field} !~C4::Dates->regexp("iso"));
+               my ($tmpkeys, $localvalues)=_Process_Operands($filter_input->{$field},"$tablename.$field",$searchtype,$columns);
                if (@$tmpkeys){
                        push @values, @$localvalues;
                        push @keys, @$tmpkeys;
@@ -414,28 +418,28 @@ sub _Process_Operands{
        push @tmpkeys, " $field = ? ";
        push @values, $operand;
        #By default, exact search
-       unless ($searchtype){
+       if (!$searchtype ||$searchtype eq "exact"){
                return \@tmpkeys,\@values;
        }
-       if ($searchtype eq "contain"){
-                       my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field);
-                       if ($field=~/(?<!zip)code|(?<!card)number/ ){
-                               push @tmpkeys,(" $field= '' ","$field IS NULL");
-                       } elsif ($$columns{$col_field}{Type}=~/varchar|text/i){
-                               push @tmpkeys,(" $field LIKE ? ");
-                               my @localvaluesextended=("\%$operand\%") ;
-                               push @values,@localvaluesextended;
-                       }
+       my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field);
+       if ($field=~/(?<!zip)code|(?<!card)number/ && $searchtype ne "exact"){
+               push @tmpkeys,(" $field= '' ","$field IS NULL");
        }
-       if ($searchtype eq "start_with"){
-                       my $col_field=(index($field,".")>0?substr($field, index($field,".")+1):$field);
-                       if ($field=~/(?<!zip)code|(?<!card)number/ ){
-                               push @tmpkeys,(" $field= '' ","$field IS NULL");
-                       } elsif ($$columns{$col_field}{Type}=~/varchar|text/i){
-                               push @tmpkeys,(" $field LIKE ? ","$field LIKE ?");
-                               my @localvaluesextended=("\% $operand\%","$operand\%") ;
-                               push @values,@localvaluesextended;
-                       }
+       if ($columns->{$col_field}->{Type}=~/varchar|text/i){
+               my @localvaluesextended;
+               if ($searchtype eq "contain"){
+                       push @tmpkeys,(" $field LIKE ? ");
+                       push @localvaluesextended,("\%$operand\%") ;
+               }
+               if ($searchtype eq "field_start_with"){
+                       push @tmpkeys,("$field LIKE ?");
+                       push @localvaluesextended, ("$operand\%") ;
+               }
+               if ($searchtype eq "start_with"){
+                       push @tmpkeys,("$field LIKE ?","$field LIKE ?");
+                       push @localvaluesextended, ("$operand\%", " $operand\%") ;
+               }
+               push @values,@localvaluesextended;
        }
        push @localkeys,qq{ (}.join(" OR ",@tmpkeys).qq{) };
        return (\@localkeys,\@values);