import rt 3.8.7
[freeside.git] / rt / share / html / Search / Chart
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %# 
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %# 
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 <%args>
49 $Query => "id > 0"
50 $PrimaryGroupBy => 'Queue'
51 $SecondaryGroupBy => undef
52 $ChartStyle => 'bars'
53 </%args>
54 <%init>
55 my $chart_class;
56 use GD;
57 use GD::Text;
58
59 if ($ChartStyle eq 'pie') {
60     require GD::Graph::pie;
61     $chart_class = "GD::Graph::pie";
62 } else {
63     require GD::Graph::bars;
64     $chart_class = "GD::Graph::bars";
65 }
66
67 use RT::Report::Tickets;
68 my $tix = RT::Report::Tickets->new( $session{'CurrentUser'} );
69 my ($count_name, $value_name) = $tix->SetupGroupings(
70     Query => $Query, GroupBy => $PrimaryGroupBy,
71 );
72
73 my $chart = $chart_class->new( 600 => 400 );
74
75 my $font = RT->Config->Get('ChartFont') || ['verdana', 'arial', gdMediumBoldFont];
76 $chart->set_title_font( $font, 12 ) if $chart->can('set_title_font');
77 $chart->set_legend_font( $font, 12 ) if $chart->can('set_legend_font');
78 $chart->set_x_label_font( $font, 10 ) if $chart->can('set_x_label_font');
79 $chart->set_y_label_font( $font, 10 ) if $chart->can('set_y_label_font');
80 $chart->set_label_font( $font, 10 ) if $chart->can('set_label_font');
81 $chart->set_x_axis_font( $font, 9 ) if $chart->can('set_x_axis_font');
82 $chart->set_y_axis_font( $font, 9 ) if $chart->can('set_y_axis_font');
83 $chart->set_values_font( $font, 9 ) if $chart->can('set_values_font');
84 $chart->set_value_font( $font, 9 ) if $chart->can('set_value_font');
85
86 # Pie charts don't like having no input, so we show a special image
87 # that indicates an error message. Because this is used in an <img>
88 # context, it can't be a simple error message. Without this check,
89 # the chart will just be a non-loading image.
90 if ($tix->Count == 0) {
91     my $plot = GD::Image->new(600 => 400);
92     $plot->colorAllocate(255, 255, 255); # background
93     my $black = $plot->colorAllocate(0, 0, 0);
94
95     require GD::Text::Wrap;
96     my $error = GD::Text::Wrap->new($plot,
97         color => $black,
98         text  => loc("No tickets found."),
99     );
100     $error->set_font( $font, 12 );
101     $error->draw(0, 0);
102
103     $m->comp( 'SELF:Plot', plot => $plot, %ARGS );
104 }
105
106 if ($chart_class eq "GD::Graph::bars") {
107     $chart->set(
108         x_label => $tix->Label( $PrimaryGroupBy ),
109         x_labels_vertical => 1,
110         y_label => loc('Tickets'),
111         show_values => 1
112     );
113 }
114
115 my %class = (
116     Queue => 'RT::Queue',
117     Owner => 'RT::User',
118     Creator => 'RT::User',
119     LastUpdatedBy => 'RT::User',
120 );
121 my $class = $class{ $PrimaryGroupBy };
122
123 my %data;
124 while ( my $entry = $tix->Next ) {
125     my $key;
126     if ( $class ) {
127         my $q = $class->new( $session{'CurrentUser'} );
128         $q->Load( $entry->__Value( $value_name ) );
129         $key = $q->Name;
130     }
131     else {
132         $key = $entry->__Value($value_name);
133     }
134     $key ||= '(no value)';
135     
136     my $value = $entry->__Value( $count_name );
137     if ($chart_class eq 'GD::Graph::pie') {
138         $key = loc($key) ." - ". $value;
139     } else {
140         $key = loc($key);
141     }
142     $data{ $key } = $value;
143 }
144
145 # XXX: Convert 1970-01-01 date to the 'Not Set'
146 # this code should be generalized!!!
147 if ( $PrimaryGroupBy =~ /(Daily|Monthly|Annually)$/ ) {
148     my $re;
149     $re = qr{1970-01-01} if $PrimaryGroupBy =~ /Daily$/;
150     $re = qr{1970-01} if $PrimaryGroupBy =~ /Monthly$/;
151     $re = qr{1970} if $PrimaryGroupBy =~ /Annually$/;
152     foreach my $k (keys %data) {
153         my $tmp = $k;
154         $tmp =~ s/^$re/loc('Not Set')/e or next;
155         $data{$tmp} = delete $data{$k};
156     }
157 }
158
159 unless (keys %data) {
160     $data{''} = 0;
161 }
162
163 my $plot = $chart->plot( [ [sort keys %data], [map $data{$_}, sort keys %data] ] ) or die $chart->error;
164 $m->comp( 'SELF:Plot', plot => $plot, %ARGS );
165 </%init>
166
167 <%METHOD Plot>
168 <%ARGS>
169 $plot => undef
170 </%ARGS>
171 <%INIT>
172 my @types = ('png', 'gif');
173
174 for my $type (@types) {
175     $plot->can($type)
176         or next;
177
178     $r->content_type("image/$type");
179     $m->out( $plot->$type );
180     $m->abort();
181 }
182
183 die "Your GD library appears to support none of the following image types: " . join(', ', @types);
184 </%INIT>
185
186 </%METHOD>