Bug 8945: Update help files for 3.10
[koha.git] / reports / dictionary.pl
blobaae96c98d8a4539a6125b4a2bec9c26c7882a4fe
1 #!/usr/bin/perl
3 # Copyright 2007 Liblime ltd
5 # This file is part of Koha.
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.
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
20 use strict;
21 use warnings;
22 use C4::Auth;
23 use CGI;
24 use C4::Output;
25 use C4::Reports::Guided;
26 use C4::Dates;
28 =head1 NAME
30 Script to control the guided report creation
32 =head1 DESCRIPTION
34 =cut
36 my $input = new CGI;
37 my $referer = $input->referer();
39 my $phase = $input->param('phase') || 'View Dictionary';
40 my $definition_name = $input->param('definition_name');
41 my $definition_description = $input->param('definition_description');
42 my $area = $input->param('area') || '';
43 my $no_html = 0; # this will be set if we dont want to print out an html::template
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 { template_name => "reports/dictionary.tmpl",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { reports => '*' },
50 debug => 1,
54 if ($phase eq 'View Dictionary'){
55 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
56 my $definitions = get_from_dictionary($area);
57 $template->param(
58 'areas'=> areas(),
59 'start_dictionary' => 1,
60 'definitions' => $definitions,
62 } elsif ( $phase eq 'Add New Definition' ) {
64 # display form allowing them to add a new definition
65 $template->param( 'new_dictionary' => 1, );
68 elsif ( $phase eq 'New Term step 2' ) {
70 # Choosing the area
71 $template->param(
72 'step_2' => 1,
73 'areas' => areas(),
74 'definition_name' => $definition_name,
75 'definition_description' => $definition_description,
79 elsif ( $phase eq 'New Term step 3' ) {
81 # Choosing the columns
82 my $columns = get_columns( $area, $input );
83 $template->param(
84 'step_3' => 1,
85 'area' => $area,
86 'columns' => $columns,
87 'definition_name' => $definition_name,
88 'definition_description' => $definition_description,
92 elsif ( $phase eq 'New Term step 4' ) {
94 # Choosing the values
95 my @columns = $input->param('columns');
96 my $columnstring = join( ',', @columns );
97 my @column_loop;
98 foreach my $column (@columns) {
99 my %tmp_hash;
100 $tmp_hash{'name'} = $column;
101 my $type = get_column_type($column);
102 if ( $type eq 'distinct' ) {
103 my $values = get_distinct_values($column);
104 $tmp_hash{'values'} = $values;
105 $tmp_hash{'distinct'} = 1;
108 if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
109 $tmp_hash{'date'} = 1;
111 if ( $type eq 'TEXT' ) {
112 $tmp_hash{'text'} = 1;
115 # else {
116 # warn $type;#
118 push @column_loop, \%tmp_hash;
121 $template->param( 'step_4' => 1,
122 'area' => $area,
123 'definition_name' => $definition_name,
124 'definition_description' => $definition_description,
125 'columns' => \@column_loop,
126 'columnstring' => $columnstring,
127 'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
131 elsif ( $phase eq 'New Term step 5' ) {
132 # Confirmation screen
133 my $columnstring = $input->param('columnstring');
134 my @criteria = $input->param('criteria_column');
135 my $query_criteria;
136 my @criteria_loop;
138 foreach my $crit (@criteria) {
139 my $value = $input->param( $crit . "_value" );
140 if ($value) {
141 my %tmp_hash;
142 $tmp_hash{'name'} = $crit;
143 $tmp_hash{'value'} = $value;
144 push @criteria_loop, \%tmp_hash;
145 if ( $value =~ C4::Dates->regexp( C4::Context->preference('dateformat') ) ) {
146 my $date = C4::Dates->new($value);
147 $value = $date->output("iso");
149 $query_criteria .= " AND $crit='$value'";
151 $value = $input->param( $crit . "_start_value" );
152 if ($value) {
153 my %tmp_hash;
154 $tmp_hash{'name'} = "$crit Start";
155 $tmp_hash{'value'} = $value;
156 push @criteria_loop, \%tmp_hash;
157 if ( $value =~ C4::Dates->regexp( C4::Context->preference('dateformat') ) ) {
158 my $date = C4::Dates->new($value);
159 $value = $date->output("iso");
161 $query_criteria .= " AND $crit >= '$value'";
163 $value = $input->param( $crit . "_end_value" );
164 if ($value) {
165 my %tmp_hash;
166 $tmp_hash{'name'} = "$crit End";
167 $tmp_hash{'value'} = $value;
168 push @criteria_loop, \%tmp_hash;
169 if ( $value =~ C4::Dates->regexp( C4::Context->preference('dateformat') ) ) {
170 my $date = C4::Dates->new($value);
171 $value = $date->output("iso");
173 $query_criteria .= " AND $crit <= '$value'";
176 my %report_areas = map @$_, get_report_areas();
177 $template->param(
178 'step_5' => 1,
179 'area' => $area,
180 'areaname' => $report_areas{$area},
181 'definition_name' => $definition_name,
182 'definition_description' => $definition_description,
183 'query' => $query_criteria,
184 'columnstring' => $columnstring,
185 'criteria_loop' => \@criteria_loop,
189 elsif ( $phase eq 'New Term step 6' ) {
190 # Saving
191 my $area = $input->param('area');
192 my $sql = $input->param('sql');
193 save_dictionary( $definition_name, $definition_description, $sql, $area );
194 $no_html = 1;
195 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
197 } elsif ( $phase eq 'Delete Definition' ) {
198 $no_html = 1;
199 my $id = $input->param('id');
200 delete_definition($id);
201 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
204 $template->param( 'referer' => $referer );
207 if (!$no_html){
208 output_html_with_http_headers $input, $cookie, $template->output;
211 sub areas {
212 my $areas = get_report_areas();
213 my @a;
214 foreach (@$areas) {
215 push @a, {
216 id => $_->[0],
217 name => $_->[1],
218 selected => ($_->[0] eq $area),
221 return \@a;