Update longoverdue.pl cron job to manage setting lost values.
[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 =head2 ($results, $total) = execute_query($sql, $type, $offset, $limit, $format, $id)
335
336     When passed C<$sql>, this function returns an array ref containing a result set
337     suitably formatted for display in html or for output as a flat file when passed in
338     C<$format> and C<$id>. It also returns the C<$total> records available for the
339     supplied query. If passed any query other than a SELECT, or if there is a db error,
340     C<$errors> an array ref is returned containing the error after this manner:
341
342     C<$error->{'sqlerr'}> contains the offending SQL keyword.
343     C<$error->{'queryerr'}> contains the native db engine error returned for the query.
344     
345     Valid values for C<$format> are 'text,' 'tab,' 'csv,' or 'url. C<$sql>, C<$type>,
346     C<$offset>, and C<$limit> are required parameters. If a valid C<$format> is passed
347     in, C<$offset> and C<$limit> are ignored for obvious reasons. A LIMIT specified by
348     the user in a user-supplied SQL query WILL apply in any case.
349
350 =cut
351
352 sub execute_query ($$$$;$$) {
353     my ( $sql, $type, $offset, $limit, $format, $id ) = @_;
354     my @params;
355     my $total = 0;
356     my ($useroffset, $userlimit);
357     my @errors = ();
358     my $error = {};
359     my $sqlerr = 0;
360     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
361         $sqlerr = 1;
362         $error->{'sqlerr'} = $1;
363         push @errors, $error;
364     } elsif ($sql !~ /^(SELECT)/i) {
365         $sqlerr = 1;
366         $error->{'queryerr'} = 'Missing SELECT';
367         push @errors, $error;
368     }
369     if ($sqlerr == 0) {
370         my $dbh = C4::Context->dbh();
371         unless ($format eq 'text' || $format eq 'tab' || $format eq 'csv' || $format eq 'url'){
372             # Grab offset/limit from user supplied LIMIT and drop the LIMIT so we can control pagination
373             if ($sql =~ /LIMIT/i) {
374                 $sql =~ s/LIMIT\W?(\d+)?\,?\W+?(\d+)//ig;
375                 $debug and warn "User has supplied LIMIT\n";
376                 $useroffset = $1;
377                 $userlimit = $2;
378                 $debug and warn "User supplied offset = $useroffset, limit = $userlimit\n";
379                 $offset += $useroffset if $useroffset;
380                 # keep track of where we are if there is a user supplied LIMIT
381                 if ( $offset + $limit > $userlimit ) {
382                     $limit = $userlimit - $offset;
383                 }
384             }
385             my $countsql = $sql;
386             $sql .= " LIMIT ?, ?";
387             $debug and warn "Passing query with params offset = $offset, limit = $limit\n";
388             @params = ($offset, $limit);
389             # Modify the query passed in to create a count query... (I think this covers all cases -crn)
390             $countsql =~ s/\bSELECT\W+(?:\w+\W+){1,}?FROM\b|\bSELECT\W\*\WFROM\b/SELECT count(*) FROM /ig;
391             $debug and warn "original query: $sql\n";
392             $debug and warn "count query: $countsql\n";
393             my $sth1 = $dbh->prepare($countsql);
394             $sth1->execute();
395             $total = $sth1->fetchrow();
396             $debug and warn "total records for this query: $total\n";
397             $total = $userlimit if defined($userlimit) and $userlimit < $total;     # we will never exceed a user defined LIMIT and...
398             $userlimit = $total if defined($userlimit) and $userlimit > $total;     # we will never exceed the total number of records available to satisfy the query
399         }
400         my $sth = $dbh->prepare($sql);
401         $sth->execute(@params);
402         my $colnames=$sth->{'NAME'};
403         my @results;
404         my $row;
405         my %temphash;
406         $row = join ('</th><th>',@$colnames);
407         $row = "<tr><th>$row</th></tr>";
408         $temphash{'row'} = $row;
409         push @results, \%temphash;
410         my $string;
411         if ($format eq 'tab') {
412             $string = join("\t",@$colnames);
413         }
414         if ($format eq 'csv') {
415             $string = join(",",@$colnames);
416         }
417         my @xmlarray;
418         while ( my @data = $sth->fetchrow_array() ) {
419             # if the field is a date field, it needs formatting
420             foreach my $data (@data) {
421                 next unless $data =~ C4::Dates->regexp("iso");
422                 my $date = C4::Dates->new($data, "iso");
423                 $data = $date->output();
424             }
425             # tabular
426             my %temphash;
427             my $row = join( '</td><td>', @data );
428             $row = "<tr><td>$row</td></tr>";
429             $temphash{'row'} = $row;
430             if ( $format eq 'text' ) {
431                 $string .= "\n" . $row;
432             }
433             if ($format eq 'tab' ){
434                 $row = join("\t",@data);
435                 $string .="\n" . $row;
436             }
437             if ($format eq 'csv' ){
438                 $row = join(",",@data);
439                 $string .="\n" . $row;
440             }
441             if ($format eq 'url'){
442                 my $temphash;
443                 @$temphash{@$colnames}=@data;
444                 push @xmlarray,$temphash;
445             }
446             push @results, \%temphash;
447         }
448         if (defined($sth->errstr)) {
449             $error->{'queryerr'} = $sth->errstr;
450             push @errors, $error;
451             warn "Database returned: $sth->errstr";
452         }
453         if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
454             return $string, $total, \@errors;
455         }
456         elsif ($format eq 'url') {
457             my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
458             my $dump = new XML::Dumper;
459             my $xml = $dump->pl2xml( \@xmlarray );
460             store_results($id,$xml);
461             return $url, $total, \@errors;
462         }
463         else {
464             return \@results, $total, \@errors;
465         }
466     } else {
467         return undef, undef, \@errors;
468     }
469 }
470
471 =item save_report($sql,$name,$type,$notes)
472
473 Given some sql and a name this will saved it so that it can resued
474
475 =cut
476
477 sub save_report {
478     my ( $sql, $name, $type, $notes ) = @_;
479     my $dbh = C4::Context->dbh();
480     my $query =
481 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes)  VALUES (?,now(),now(),?,?,?,?)";
482     my $sth = $dbh->prepare($query);
483     $sth->execute( 0, $sql, $name, $type, $notes );
484     $sth->finish();
485
486 }
487
488 sub store_results {
489         my ($id,$xml)=@_;
490         my $dbh = C4::Context->dbh();
491         my $query = "SELECT * FROM saved_reports WHERE report_id=?";
492         my $sth = $dbh->prepare($query);
493         $sth->execute($id);
494         if (my $data=$sth->fetchrow_hashref()){
495                 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
496                 my $sth2 = $dbh->prepare($query2);
497             $sth2->execute($xml,$id);
498                 $sth2->finish();
499         }
500         else {
501                 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
502                 my $sth2 = $dbh->prepare($query2);
503                 $sth2->execute($id,$xml);
504                 $sth2->finish();
505         }
506         $sth->finish();
507 }
508
509 sub format_results {
510         my ($id) = @_;
511         my $dbh = C4::Context->dbh();
512         my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
513         my $sth = $dbh->prepare($query);
514         $sth->execute($id);
515         my $data = $sth->fetchrow_hashref();
516         my $dump = new XML::Dumper;
517         my $perl = $dump->xml2pl( $data->{'report'} );
518         foreach my $row (@$perl) {
519                 my $htmlrow="<tr>";
520                 foreach my $key (keys %$row){
521                         $htmlrow .= "<td>$row->{$key}</td>";
522                 }
523                 $htmlrow .= "</tr>";
524                 $row->{'row'} = $htmlrow;
525         }
526         $sth->finish;
527         $query = "SELECT * FROM saved_sql WHERE id = ?";
528         $sth = $dbh->prepare($query);
529         $sth->execute($id);
530         $data = $sth->fetchrow_hashref();
531     $sth->finish();
532         return ($perl,$data->{'report_name'},$data->{'notes'}); 
533 }       
534
535 sub delete_report {
536         my ( $id ) = @_;
537         my $dbh = C4::Context->dbh();
538         my $query = "DELETE FROM saved_sql WHERE id = ?";
539         my $sth = $dbh->prepare($query);
540         $sth->execute($id);
541         $sth->finish();
542 }       
543
544 sub get_saved_reports {
545     my $dbh   = C4::Context->dbh();
546     my $query = "SELECT *,saved_sql.id AS id FROM saved_sql 
547     LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
548     ORDER by date_created";
549     my $sth   = $dbh->prepare($query);
550     $sth->execute();
551     my @reports;
552     while ( my $data = $sth->fetchrow_hashref() ) {
553         push @reports, $data;
554     }
555     $sth->finish();
556     return ( \@reports );
557 }
558
559 sub get_saved_report {
560     my ($id)  = @_;
561     my $dbh   = C4::Context->dbh();
562     my $query = " SELECT * FROM saved_sql WHERE id = ?";
563     my $sth   = $dbh->prepare($query);
564     $sth->execute($id);
565     my $data = $sth->fetchrow_hashref();
566     $sth->finish();
567     return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
568 }
569
570 =item create_compound($masterID,$subreportID)
571
572 This will take 2 reports and create a compound report using both of them
573
574 =cut
575
576 sub create_compound {
577         my ($masterID,$subreportID) = @_;
578         my $dbh = C4::Context->dbh();
579         # get the reports
580         my ($mastersql,$mastertype) = get_saved_report($masterID);
581         my ($subsql,$subtype) = get_saved_report($subreportID);
582         
583         # now we have to do some checking to see how these two will fit together
584         # or if they will
585         my ($mastertables,$subtables);
586         if ($mastersql =~ / from (.*) where /i){ 
587                 $mastertables = $1;
588         }
589         if ($subsql =~ / from (.*) where /i){
590                 $subtables = $1;
591         }
592         return ($mastertables,$subtables);
593 }
594
595 =item get_column_type($column)
596
597 This takes a column name of the format table.column and will return what type it is
598 (free text, set values, date)
599
600 =cut
601
602 sub get_column_type {
603         my ($tablecolumn) = @_;
604         my ($table,$column) = split(/\./,$tablecolumn);
605         my $dbh = C4::Context->dbh();
606         my $catalog;
607         my $schema;
608
609         # mysql doesnt support a column selection, set column to %
610         my $tempcolumn='%';
611         my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
612         while (my $info = $sth->fetchrow_hashref()){
613                 if ($info->{'COLUMN_NAME'} eq $column){
614                         #column we want
615                         if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
616                                 $info->{'TYPE_NAME'} = 'distinct';
617                         }
618                         return $info->{'TYPE_NAME'};            
619                 }
620         }
621         $sth->finish();
622 }
623
624 =item get_distinct_values($column)
625
626 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop 
627 with the distinct values of the column
628
629 =cut
630
631 sub get_distinct_values {
632         my ($tablecolumn) = @_;
633         my ($table,$column) = split(/\./,$tablecolumn);
634         my $dbh = C4::Context->dbh();
635         my $query =
636           "SELECT distinct($column) as availablevalues FROM $table";
637         my $sth = $dbh->prepare($query);
638         $sth->execute();
639         my @values;
640         while ( my $row = $sth->fetchrow_hashref() ) {
641                 push @values, $row;
642         }
643         $sth->finish();
644         return \@values;
645 }       
646
647 sub save_dictionary {
648         my ($name,$description,$sql,$area) = @_;
649         my $dbh = C4::Context->dbh();
650         my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
651   VALUES (?,?,?,?,now(),now())";
652     my $sth = $dbh->prepare($query);
653     $sth->execute($name,$description,$sql,$area) || return 0;
654     $sth->finish();
655     return 1;
656 }
657
658 sub get_from_dictionary {
659         my ($area,$id) = @_;
660         my $dbh = C4::Context->dbh();
661         my $query = "SELECT * FROM reports_dictionary";
662         if ($area){
663                 $query.= " WHERE area = ?";
664         }
665         elsif ($id){
666                 $query.= " WHERE id = ?"
667         }
668         my $sth = $dbh->prepare($query);
669         if ($id){
670                 $sth->execute($id);
671         }
672         elsif ($area) {
673                 $sth->execute($area);
674         }
675         else {
676                 $sth->execute();
677         }
678         my @loop;
679         my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
680         while (my $data = $sth->fetchrow_hashref()){
681                 $data->{'areaname'}=$reports[$data->{'area'}-1];
682                 push @loop,$data;
683                 
684         }
685         $sth->finish();
686         return (\@loop);
687 }
688
689 sub delete_definition {
690         my ($id) = @_;
691         my $dbh = C4::Context->dbh();
692         my $query = "DELETE FROM reports_dictionary WHERE id = ?";
693         my $sth = $dbh->prepare($query);
694         $sth->execute($id);
695         $sth->finish();
696 }
697
698 sub get_sql {
699         my ($id) = @_;
700         my $dbh = C4::Context->dbh();
701         my $query = "SELECT * FROM saved_sql WHERE id = ?";
702         my $sth = $dbh->prepare($query);
703         $sth->execute($id);
704         my $data=$sth->fetchrow_hashref();
705         $sth->finish(); 
706         return $data->{'savedsql'};
707 }
708
709 sub _get_column_defs {
710         my ($cgi) = @_;
711         my %columns;
712         my $columns_def_file = "columns.def";
713         my $htdocs = C4::Context->config('intrahtdocs');                       
714         my $section='intranet';
715         my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section,$cgi);
716
717         my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";    
718         open (COLUMNS,$full_path_to_columns_def_file);
719         while (my $input = <COLUMNS>){
720                 my @row =split(/\t/,$input);
721                 $columns{$row[0]}=$row[1];
722         }
723
724         close COLUMNS;
725         return \%columns;
726 }
727 1;
728 __END__
729
730 =head1 AUTHOR
731
732 Chris Cormack <crc@liblime.com>
733
734 =cut