Patch security hole in vestigial report.
[koha.git] / C4 / Reports.pm
1 package C4::Reports;
2
3 # Copyright 2007 Liblime Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
24 use C4::Context;
25 use C4::Output;
26 use XML::Simple;
27 use XML::Dumper;
28 use C4::Debug;
29 # use Smart::Comments;
30 # use Data::Dumper;
31
32 BEGIN {
33         # set the version for version checking
34         $VERSION = 0.12;
35         require Exporter;
36         @ISA = qw(Exporter);
37         @EXPORT = qw(
38                 get_report_types get_report_areas get_columns build_query get_criteria
39                 save_report get_saved_reports execute_query get_saved_report create_compound run_compound
40                 get_column_type get_distinct_values save_dictionary get_from_dictionary
41                 delete_definition delete_report format_results get_sql
42         );
43 }
44
45 our %table_areas;
46 $table_areas{'1'} =
47   [ 'borrowers', 'statistics','items', 'biblioitems' ];    # circulation
48 $table_areas{'2'} = [ 'items', 'biblioitems', 'biblio' ];   # catalogue
49 $table_areas{'3'} = [ 'borrowers' ];        # patrons
50 $table_areas{'4'} = ['aqorders', 'biblio', 'items'];        # acquisitions
51 $table_areas{'5'} = [ 'borrowers', 'accountlines' ];        # accounts
52 our %keys;
53 $keys{'1'} = [
54     'statistics.borrowernumber=borrowers.borrowernumber',
55     'items.itemnumber = statistics.itemnumber',
56     'biblioitems.biblioitemnumber = items.biblioitemnumber'
57 ];
58 $keys{'2'} = [
59     'items.biblioitemnumber=biblioitems.biblioitemnumber',
60     'biblioitems.biblionumber=biblio.biblionumber'
61 ];
62 $keys{'3'} = [ ];
63 $keys{'4'} = [
64         'aqorders.biblionumber=biblio.biblionumber',
65         'biblio.biblionumber=items.biblionumber'
66 ];
67 $keys{'5'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
68
69 # have to do someting here to know if its dropdown, free text, date etc
70
71 our %criteria;
72 $criteria{'1'} = [
73     'statistics.type',   'borrowers.categorycode',
74     'statistics.branch',
75     'biblioitems.publicationyear|date',
76     'items.dateaccessioned|date'
77 ];
78 $criteria{'2'} =
79   [ 'items.holdingbranch', 'items.homebranch' ,'items.itemlost', 'items.location', 'items.ccode'];
80 $criteria{'3'} = ['borrowers.branchcode'];
81 $criteria{'4'} = ['aqorders.datereceived|date'];
82 $criteria{'5'} = ['borrowers.branchcode'];
83
84 if (C4::Context->preference('item-level_itypes')) {
85     unshift @{ $criteria{'1'} }, 'items.itype';
86     unshift @{ $criteria{'2'} }, 'items.itype';
87 } else {
88     unshift @{ $criteria{'1'} }, 'biblioitems.itemtype';
89     unshift @{ $criteria{'2'} }, 'biblioitems.itemtype';
90 }
91
92 =head1 NAME
93    
94 C4::Reports - Module for generating reports 
95
96 =head1 SYNOPSIS
97
98   use C4::Reports;
99
100 =head1 DESCRIPTION
101
102
103 =head1 METHODS
104
105 =over 2
106
107 =cut
108
109 =item get_report_types()
110
111 This will return a list of all the available report types
112
113 =cut
114
115 sub get_report_types {
116     my $dbh = C4::Context->dbh();
117
118     # FIXME these should be in the database perhaps
119     my @reports = ( 'Tabular', 'Summary', 'Matrix' );
120     my @reports2;
121     for ( my $i = 0 ; $i < 3 ; $i++ ) {
122         my %hashrep;
123         $hashrep{id}   = $i + 1;
124         $hashrep{name} = $reports[$i];
125         push @reports2, \%hashrep;
126     }
127     return ( \@reports2 );
128
129 }
130
131 =item get_report_areas()
132
133 This will return a list of all the available report areas
134
135 =cut
136
137 sub get_report_areas {
138     my $dbh = C4::Context->dbh();
139
140     # FIXME these should be in the database
141     my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
142     my @reports2;
143     for ( my $i = 0 ; $i < 5 ; $i++ ) {
144         my %hashrep;
145         $hashrep{id}   = $i + 1;
146         $hashrep{name} = $reports[$i];
147         push @reports2, \%hashrep;
148     }
149     return ( \@reports2 );
150
151 }
152
153 =item get_all_tables()
154
155 This will return a list of all tables in the database 
156
157 =cut
158
159 sub get_all_tables {
160     my $dbh   = C4::Context->dbh();
161     my $query = "SHOW TABLES";
162     my $sth   = $dbh->prepare($query);
163     $sth->execute();
164     my @tables;
165     while ( my $data = $sth->fetchrow_arrayref() ) {
166         push @tables, $data->[0];
167     }
168     $sth->finish();
169     return ( \@tables );
170
171 }
172
173 =item get_columns($area)
174
175 This will return a list of all columns for a report area
176
177 =cut
178
179 sub get_columns {
180
181     # this calls the internal fucntion _get_columns
182     my ($area,$cgi) = @_;
183     my $tables = $table_areas{$area};
184     my @allcolumns;
185     my $first = 1;
186     foreach my $table (@$tables) {
187         my @columns = _get_columns($table,$cgi, $first);
188         $first = 0;
189         push @allcolumns, @columns;
190     }
191     return ( \@allcolumns );
192 }
193
194 sub _get_columns {
195     my ($tablename,$cgi, $first) = @_;
196     my $dbh         = C4::Context->dbh();
197     my $sth         = $dbh->prepare("show columns from $tablename");
198     $sth->execute();
199     my @columns;
200         my $column_defs = _get_column_defs($cgi);
201         my %tablehash;
202         $tablehash{'table'}=$tablename;
203     $tablehash{'__first__'} = $first;
204         push @columns, \%tablehash;
205     while ( my $data = $sth->fetchrow_arrayref() ) {
206         my %temphash;
207         $temphash{'name'}        = "$tablename.$data->[0]";
208         $temphash{'description'} = $column_defs->{"$tablename.$data->[0]"};
209         push @columns, \%temphash;
210     }
211     $sth->finish();
212     return (@columns);
213 }
214
215 =item build_query($columns,$criteria,$orderby,$area)
216
217 This will build the sql needed to return the results asked for, 
218 $columns is expected to be of the format tablename.columnname.
219 This is what get_columns returns.
220
221 =cut
222
223 sub build_query {
224     my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
225 ### $orderby
226     my $keys   = $keys{$area};
227     my $tables = $table_areas{$area};
228
229     my $sql =
230       _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
231     return ($sql);
232 }
233
234 sub _build_query {
235     my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
236 ### $orderby
237     # $keys is an array of joining constraints
238     my $dbh           = C4::Context->dbh();
239     my $joinedtables  = join( ',', @$tables );
240     my $joinedcolumns = join( ',', @$columns );
241     my $joinedkeys    = join( ' AND ', @$keys );
242     my $query =
243       "SELECT $totals $joinedcolumns FROM $tables->[0] ";
244         for (my $i=1;$i<@$tables;$i++){
245                 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
246         }
247
248     if ($criteria) {
249                 $criteria =~ s/AND/WHERE/;
250         $query .= " $criteria";
251     }
252         if ($definition){
253                 my @definitions = split(',',$definition);
254                 my $deftext;
255                 foreach my $def (@definitions){
256                         my $defin=get_from_dictionary('',$def);
257                         $deftext .=" ".$defin->[0]->{'saved_sql'};
258                 }
259                 if ($query =~ /WHERE/i){
260                         $query .= $deftext;
261                 }
262                 else {
263                         $deftext  =~ s/AND/WHERE/;
264                         $query .= $deftext;                     
265                 }
266         }
267     if ($totals) {
268         my $groupby;
269         my @totcolumns = split( ',', $totals );
270         foreach my $total (@totcolumns) {
271             if ( $total =~ /\((.*)\)/ ) {
272                 if ( $groupby eq '' ) {
273                     $groupby = " GROUP BY $1";
274                 }
275                 else {
276                     $groupby .= ",$1";
277                 }
278             }
279         }
280         $query .= $groupby;
281     }
282     if ($orderby) {
283         $query .= $orderby;
284     }
285     return ($query);
286 }
287
288 =item get_criteria($area,$cgi);
289
290 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
291
292 =cut
293
294 sub get_criteria {
295     my ($area,$cgi) = @_;
296     my $dbh    = C4::Context->dbh();
297     my $crit   = $criteria{$area};
298         my $column_defs = _get_column_defs($cgi);
299     my @criteria_array;
300     foreach my $localcrit (@$crit) {
301         my ( $value, $type )   = split( /\|/, $localcrit );
302         my ( $table, $column ) = split( /\./, $value );
303         if ( $type eq 'date' ) {
304                         my %temp;
305             $temp{'name'}   = $value;
306             $temp{'date'}   = 1;
307                         $temp{'description'} = $column_defs->{$value};
308             push @criteria_array, \%temp;
309         }
310         else {
311
312             my $query =
313               "SELECT distinct($column) as availablevalues FROM $table";
314             my $sth = $dbh->prepare($query);
315             $sth->execute();
316             my @values;
317             while ( my $row = $sth->fetchrow_hashref() ) {
318                 push @values, $row;
319                 ### $row;
320             }
321             $sth->finish();
322             my %temp;
323             $temp{'name'}   = $value;
324                         $temp{'description'} = $column_defs->{$value};
325             $temp{'values'} = \@values;
326             push @criteria_array, \%temp;
327         }
328     }
329     return ( \@criteria_array );
330 }
331
332 =item execute_query
333
334 =over
335
336 ($results, $total) = execute_query($sql, $type, $offset, $limit, $format, $id)
337
338 =back
339
340     When passed C<$sql>, this function returns an array ref containing a result set
341     suitably formatted for display in html or for output as a flat file when passed in
342     C<$format> and C<$id>. It also returns the C<$total> records available for the
343     supplied query. If passed any query other than a SELECT, or if there is a db error,
344     C<$errors> an array ref is returned containing the error after this manner:
345
346     C<$error->{'sqlerr'}> contains the offending SQL keyword.
347     C<$error->{'queryerr'}> contains the native db engine error returned for the query.
348     
349     Valid values for C<$format> are 'text,' 'tab,' 'csv,' or 'url. C<$sql>, C<$type>,
350     C<$offset>, and C<$limit> are required parameters. If a valid C<$format> is passed
351     in, C<$offset> and C<$limit> are ignored for obvious reasons. A LIMIT specified by
352     the user in a user-supplied SQL query WILL apply in any case.
353
354 =cut
355
356 sub execute_query ($$$$;$$) {
357     my ( $sql, $type, $offset, $limit, $format, $id ) = @_;
358     my @params;
359     my $total = 0;
360     my ($useroffset, $userlimit);
361     my @errors = ();
362     my $error = {};
363     my $sqlerr = 0;
364     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
365         $sqlerr = 1;
366         $error->{'sqlerr'} = $1;
367         push @errors, $error;
368     } elsif ($sql !~ /^(SELECT)/i) {
369         $sqlerr = 1;
370         $error->{'queryerr'} = 'Missing SELECT';
371         push @errors, $error;
372     }
373     if ($sqlerr == 0) {
374         my $dbh = C4::Context->dbh();
375         unless ($format eq 'text' || $format eq 'tab' || $format eq 'csv' || $format eq 'url'){
376             # Grab offset/limit from user supplied LIMIT and drop the LIMIT so we can control pagination
377             if ($sql =~ /LIMIT/i) {
378                 $sql =~ s/LIMIT\W?(\d+)?\,?\W+?(\d+)//ig;
379                 $debug and warn "User has supplied LIMIT\n";
380                 $useroffset = $1;
381                 $userlimit = $2;
382                 $debug and warn "User supplied offset = $useroffset, limit = $userlimit\n";
383                 $offset += $useroffset if $useroffset;
384                 # keep track of where we are if there is a user supplied LIMIT
385                 if ( $offset + $limit > $userlimit ) {
386                     $limit = $userlimit - $offset;
387                 }
388             }
389             my $countsql = $sql;
390             $sql .= " LIMIT ?, ?";
391             $debug and warn "Passing query with params offset = $offset, limit = $limit\n";
392             @params = ($offset, $limit);
393             # Modify the query passed in to create a count query... (I think this covers all cases -crn)
394             $countsql =~ s/\bSELECT\W+(?:\w+\W+){1,}?FROM\b|\bSELECT\W\*\WFROM\b/SELECT count(*) FROM /ig;
395             $debug and warn "original query: $sql\n";
396             $debug and warn "count query: $countsql\n";
397             my $sth1 = $dbh->prepare($countsql);
398             $sth1->execute();
399             $total = $sth1->fetchrow();
400             $debug and warn "total records for this query: $total\n";
401             $total = $userlimit if defined($userlimit) and $userlimit < $total;     # we will never exceed a user defined LIMIT and...
402             $userlimit = $total if defined($userlimit) and $userlimit > $total;     # we will never exceed the total number of records available to satisfy the query
403         }
404         my $sth = $dbh->prepare($sql);
405         $sth->execute(@params);
406         my $colnames=$sth->{'NAME'};
407         my @results;
408         my $row;
409         my %temphash;
410         $row = join ('</th><th>',@$colnames);
411         $row = "<tr><th>$row</th></tr>";
412         $temphash{'row'} = $row;
413         push @results, \%temphash;
414         my $string;
415         if ($format eq 'tab') {
416             $string = join("\t",@$colnames);
417         }
418         if ($format eq 'csv') {
419             $string = join(",",@$colnames);
420         }
421         my @xmlarray;
422         while ( my @data = $sth->fetchrow_array() ) {
423             # if the field is a date field, it needs formatting
424             foreach my $data (@data) {
425                 next unless $data =~ C4::Dates->regexp("iso");
426                 my $date = C4::Dates->new($data, "iso");
427                 $data = $date->output();
428             }
429             # tabular
430             my %temphash;
431             my $row = join( '</td><td>', @data );
432             $row = "<tr><td>$row</td></tr>";
433             $temphash{'row'} = $row;
434             if ( $format eq 'text' ) {
435                 $string .= "\n" . $row;
436             }
437             if ($format eq 'tab' ){
438                 $row = join("\t",@data);
439                 $string .="\n" . $row;
440             }
441             if ($format eq 'csv' ){
442                 $row = join(",",@data);
443                 $string .="\n" . $row;
444             }
445             if ($format eq 'url'){
446                 my $temphash;
447                 @$temphash{@$colnames}=@data;
448                 push @xmlarray,$temphash;
449             }
450             push @results, \%temphash;
451         }
452         if (defined($sth->errstr)) {
453             $error->{'queryerr'} = $sth->errstr;
454             push @errors, $error;
455             warn "Database returned: $sth->errstr";
456         }
457         if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
458             return $string, $total, \@errors;
459         }
460         elsif ($format eq 'url') {
461             my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
462             my $dump = new XML::Dumper;
463             my $xml = $dump->pl2xml( \@xmlarray );
464             store_results($id,$xml);
465             return $url, $total, \@errors;
466         }
467         else {
468             return \@results, $total, \@errors;
469         }
470     } else {
471         return undef, undef, \@errors;
472     }
473 }
474
475 =item save_report($sql,$name,$type,$notes)
476
477 Given some sql and a name this will saved it so that it can resued
478
479 =cut
480
481 sub save_report {
482     my ( $sql, $name, $type, $notes ) = @_;
483     my $dbh = C4::Context->dbh();
484     my $query =
485 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes)  VALUES (?,now(),now(),?,?,?,?)";
486     my $sth = $dbh->prepare($query);
487     $sth->execute( 0, $sql, $name, $type, $notes );
488     $sth->finish();
489
490 }
491
492 sub store_results {
493         my ($id,$xml)=@_;
494         my $dbh = C4::Context->dbh();
495         my $query = "SELECT * FROM saved_reports WHERE report_id=?";
496         my $sth = $dbh->prepare($query);
497         $sth->execute($id);
498         if (my $data=$sth->fetchrow_hashref()){
499                 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
500                 my $sth2 = $dbh->prepare($query2);
501             $sth2->execute($xml,$id);
502                 $sth2->finish();
503         }
504         else {
505                 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
506                 my $sth2 = $dbh->prepare($query2);
507                 $sth2->execute($id,$xml);
508                 $sth2->finish();
509         }
510         $sth->finish();
511 }
512
513 sub format_results {
514         my ($id) = @_;
515         my $dbh = C4::Context->dbh();
516         my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
517         my $sth = $dbh->prepare($query);
518         $sth->execute($id);
519         my $data = $sth->fetchrow_hashref();
520         my $dump = new XML::Dumper;
521         my $perl = $dump->xml2pl( $data->{'report'} );
522         foreach my $row (@$perl) {
523                 my $htmlrow="<tr>";
524                 foreach my $key (keys %$row){
525                         $htmlrow .= "<td>$row->{$key}</td>";
526                 }
527                 $htmlrow .= "</tr>";
528                 $row->{'row'} = $htmlrow;
529         }
530         $sth->finish;
531         $query = "SELECT * FROM saved_sql WHERE id = ?";
532         $sth = $dbh->prepare($query);
533         $sth->execute($id);
534         $data = $sth->fetchrow_hashref();
535     $sth->finish();
536         return ($perl,$data->{'report_name'},$data->{'notes'}); 
537 }       
538
539 sub delete_report {
540         my ( $id ) = @_;
541         my $dbh = C4::Context->dbh();
542         my $query = "DELETE FROM saved_sql WHERE id = ?";
543         my $sth = $dbh->prepare($query);
544         $sth->execute($id);
545         $sth->finish();
546 }       
547
548 sub get_saved_reports {
549     my $dbh   = C4::Context->dbh();
550     my $query = "SELECT *,saved_sql.id AS id FROM saved_sql 
551     LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
552     ORDER by date_created";
553     my $sth   = $dbh->prepare($query);
554     $sth->execute();
555     my @reports;
556     while ( my $data = $sth->fetchrow_hashref() ) {
557         push @reports, $data;
558     }
559     $sth->finish();
560     return ( \@reports );
561 }
562
563 sub get_saved_report {
564     my ($id)  = @_;
565     my $dbh   = C4::Context->dbh();
566     my $query = " SELECT * FROM saved_sql WHERE id = ?";
567     my $sth   = $dbh->prepare($query);
568     $sth->execute($id);
569     my $data = $sth->fetchrow_hashref();
570     $sth->finish();
571     return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
572 }
573
574 =item create_compound($masterID,$subreportID)
575
576 This will take 2 reports and create a compound report using both of them
577
578 =cut
579
580 sub create_compound {
581         my ($masterID,$subreportID) = @_;
582         my $dbh = C4::Context->dbh();
583         # get the reports
584         my ($mastersql,$mastertype) = get_saved_report($masterID);
585         my ($subsql,$subtype) = get_saved_report($subreportID);
586         
587         # now we have to do some checking to see how these two will fit together
588         # or if they will
589         my ($mastertables,$subtables);
590         if ($mastersql =~ / from (.*) where /i){ 
591                 $mastertables = $1;
592         }
593         if ($subsql =~ / from (.*) where /i){
594                 $subtables = $1;
595         }
596         return ($mastertables,$subtables);
597 }
598
599 =item get_column_type($column)
600
601 This takes a column name of the format table.column and will return what type it is
602 (free text, set values, date)
603
604 =cut
605
606 sub get_column_type {
607         my ($tablecolumn) = @_;
608         my ($table,$column) = split(/\./,$tablecolumn);
609         my $dbh = C4::Context->dbh();
610         my $catalog;
611         my $schema;
612
613         # mysql doesnt support a column selection, set column to %
614         my $tempcolumn='%';
615         my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
616         while (my $info = $sth->fetchrow_hashref()){
617                 if ($info->{'COLUMN_NAME'} eq $column){
618                         #column we want
619                         if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
620                                 $info->{'TYPE_NAME'} = 'distinct';
621                         }
622                         return $info->{'TYPE_NAME'};            
623                 }
624         }
625         $sth->finish();
626 }
627
628 =item get_distinct_values($column)
629
630 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop 
631 with the distinct values of the column
632
633 =cut
634
635 sub get_distinct_values {
636         my ($tablecolumn) = @_;
637         my ($table,$column) = split(/\./,$tablecolumn);
638         my $dbh = C4::Context->dbh();
639         my $query =
640           "SELECT distinct($column) as availablevalues FROM $table";
641         my $sth = $dbh->prepare($query);
642         $sth->execute();
643         my @values;
644         while ( my $row = $sth->fetchrow_hashref() ) {
645                 push @values, $row;
646         }
647         $sth->finish();
648         return \@values;
649 }       
650
651 sub save_dictionary {
652         my ($name,$description,$sql,$area) = @_;
653         my $dbh = C4::Context->dbh();
654         my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
655   VALUES (?,?,?,?,now(),now())";
656     my $sth = $dbh->prepare($query);
657     $sth->execute($name,$description,$sql,$area) || return 0;
658     $sth->finish();
659     return 1;
660 }
661
662 sub get_from_dictionary {
663         my ($area,$id) = @_;
664         my $dbh = C4::Context->dbh();
665         my $query = "SELECT * FROM reports_dictionary";
666         if ($area){
667                 $query.= " WHERE area = ?";
668         }
669         elsif ($id){
670                 $query.= " WHERE id = ?"
671         }
672         my $sth = $dbh->prepare($query);
673         if ($id){
674                 $sth->execute($id);
675         }
676         elsif ($area) {
677                 $sth->execute($area);
678         }
679         else {
680                 $sth->execute();
681         }
682         my @loop;
683         my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
684         while (my $data = $sth->fetchrow_hashref()){
685                 $data->{'areaname'}=$reports[$data->{'area'}-1];
686                 push @loop,$data;
687                 
688         }
689         $sth->finish();
690         return (\@loop);
691 }
692
693 sub delete_definition {
694         my ($id) = @_;
695         my $dbh = C4::Context->dbh();
696         my $query = "DELETE FROM reports_dictionary WHERE id = ?";
697         my $sth = $dbh->prepare($query);
698         $sth->execute($id);
699         $sth->finish();
700 }
701
702 sub get_sql {
703         my ($id) = @_;
704         my $dbh = C4::Context->dbh();
705         my $query = "SELECT * FROM saved_sql WHERE id = ?";
706         my $sth = $dbh->prepare($query);
707         $sth->execute($id);
708         my $data=$sth->fetchrow_hashref();
709         $sth->finish(); 
710         return $data->{'savedsql'};
711 }
712
713 sub _get_column_defs {
714         my ($cgi) = @_;
715         my %columns;
716         my $columns_def_file = "columns.def";
717         my $htdocs = C4::Context->config('intrahtdocs');                       
718         my $section='intranet';
719         my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section,$cgi);
720
721         my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";    
722         open (COLUMNS,$full_path_to_columns_def_file);
723         while (my $input = <COLUMNS>){
724                 my @row =split(/\t/,$input);
725                 $columns{$row[0]}=$row[1];
726         }
727
728         close COLUMNS;
729         return \%columns;
730 }
731 1;
732 __END__
733
734 =back
735
736 =head1 AUTHOR
737
738 Chris Cormack <crc@liblime.com>
739
740 =cut