| 1 | # | ||
| 2 | # $Id: Report.pm,v 1.12 2006/04/17 00:19:04 dennisl Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | |||
| 6 | package DBIx::Knowledge::Report; | ||
| 7 | |||
| 8 | use vars qw(%allowed_crit_fields %np_allowed_crit_fields); | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use CGI::AutoForm; | ||
| 12 | use DBIx::IO::Search; | ||
| 13 | use DBIx::IO::GenLib (); | ||
| 14 | use POSIX (); | ||
| 15 | use DBIx::Knowledge; | ||
| 16 | use DBIx::Knowledge::Output; | ||
| 17 | use CGI::CRUD::TableIO; | ||
| 18 | |||
| 19 | use constant OK => 0; | ||
| 20 | |||
| 21 | $DBIx::Knowledge::Report::REPORT_SELECT_LIST_TABLE_NAME = 'dbix_knowledge_data_point'; | ||
| 22 | |||
| 23 | my $potential_select_fields; | ||
| 24 | ##at anyway to make this readonly? | ||
| 25 | my %potential_select_fields_by_id; | ||
| 26 | ##at anyway to make this readonly? | ||
| 27 | my %potential_select_fields_by_order_legend; | ||
| 28 | |||
| 29 | my $title_field = { | ||
| 30 | FIELD_NAME => 'REPORT_TITLE', | ||
| 31 | INPUT_CONTROL_TYPE => 'TEXT', | ||
| 32 | SEARCH_CONTROL_TYPE => 'TEXT', | ||
| 33 | HEADING => 'Report title', | ||
| 34 | DATATYPE => 'CHAR', | ||
| 35 | REQUIRED => 'N', | ||
| 36 | INSERTABLE => 'Y', | ||
| 37 | SEARCHABLE => 'Y', | ||
| 38 | HELP_SUMMARY => 'Title of this report', | ||
| 39 | }; | ||
| 40 | |||
| 41 | # I've been working on some Business Intelligence libraries for data sets that exhibit Linear Convergence and was able to acheive such a convergence temporally (over an axis of time) for much of the data in OP with the help of some key data aggregation | ||
| 42 | ##at should use NULLIF(poa.preopt_priority,0), NULLIF(poa.opt_priority_override,0), NULLIF(o.opt_priority_override,0), etc... | ||
| 43 | my $select_field = { | ||
| 44 | FIELD_NAME => 'SELECT_FIELD', | ||
| 45 | INPUT_CONTROL_TYPE => 'SELECT', | ||
| 46 | SEARCH_CONTROL_TYPE => 'SELECT', | ||
| 47 | SEARCH_MULT_SELECT => 7, | ||
| 48 | HEADING => 'Report on', | ||
| 49 | DATATYPE => 'CHAR', | ||
| 50 | REQUIRED => 'Y', | ||
| 51 | INSERTABLE => 'Y', | ||
| 52 | SEARCHABLE => 'Y', | ||
| 53 | HELP_SUMMARY => <<EOH, | ||
| 54 | Choose the data points to include | ||
| 55 | in this report. | ||
| 56 | EOH | ||
| 57 | }; | ||
| 58 | |||
| 59 | ##at anyway to make this readonly? | ||
| 60 | my @select_field_picklist = (); | ||
| 61 | ##at anyway to make this readonly? | ||
| 62 | my @np_select_field_picklist = (); | ||
| 63 | |||
| 64 | my $subtotal_field = { | ||
| 65 | FIELD_NAME => 'SUBTOTAL_FIELD', | ||
| 66 | INPUT_CONTROL_TYPE => 'SELECT', | ||
| 67 | SEARCH_CONTROL_TYPE => 'SELECT', | ||
| 68 | SEARCH_MULT_SELECT => 4, | ||
| 69 | HEADING => 'Summary totals on', | ||
| 70 | DATATYPE => 'CHAR', | ||
| 71 | REQUIRED => 'N', | ||
| 72 | INSERTABLE => 'Y', | ||
| 73 | SEARCHABLE => 'Y', | ||
| 74 | HELP_SUMMARY => <<EOH, | ||
| 75 | Choose which data points will have | ||
| 76 | subtotal summary lines. Must be a | ||
| 77 | subset of the fields selected above. | ||
| 78 | EOH | ||
| 79 | }; | ||
| 80 | |||
| 81 | ##at anyway to make this readonly? | ||
| 82 | my @subtotal_field_picklist; | ||
| 83 | |||
| 84 | ##at anyway to make this readonly? | ||
| 85 | my @np_subtotal_field_picklist = (); | ||
| 86 | |||
| 87 | my $aux_info_field = { | ||
| 88 | FIELD_NAME => 'AUX_INFO_FIELD', | ||
| 89 | INPUT_CONTROL_TYPE => 'SELECT', | ||
| 90 | SEARCH_CONTROL_TYPE => 'SELECT', | ||
| 91 | SEARCH_MULT_SELECT => 4, | ||
| 92 | HEADING => 'Auxiliary info fields', | ||
| 93 | DATATYPE => 'CHAR', | ||
| 94 | REQUIRED => 'N', | ||
| 95 | INSERTABLE => 'Y', | ||
| 96 | SEARCHABLE => 'Y', | ||
| 97 | HELP_SUMMARY => <<EOH, | ||
| 98 | For advanced use only. | ||
| 99 | EOH | ||
| 100 | }; | ||
| 101 | |||
| 102 | my $head_repeat_field = { | ||
| 103 | FIELD_NAME => 'HEADER_REPEAT', | ||
| 104 | INPUT_CONTROL_TYPE => 'RADIO', | ||
| 105 | SEARCH_CONTROL_TYPE => 'RADIO', | ||
| 106 | HEADING => 'Repeat header on subtotals', | ||
| 107 | DATATYPE => 'CHAR', | ||
| 108 | DEFAULT_VALUE => '0', | ||
| 109 | REQUIRED => 'N', | ||
| 110 | INSERTABLE => 'Y', | ||
| 111 | SEARCHABLE => 'Y', | ||
| 112 | HELP_SUMMARY => <<EOH, | ||
| 113 | Repeat field headers for | ||
| 114 | every subtotal row? | ||
| 115 | EOH | ||
| 116 | }; | ||
| 117 | |||
| 118 | my $head_repeat_picklist = [ | ||
| 119 | { ID => 0, MASK => 'No', }, | ||
| 120 | { ID => 1, MASK => 'Yes', }, | ||
| 121 | ]; | ||
| 122 | |||
| 123 | my $orderby_field = { | ||
| 124 | FIELD_NAME => 'ORDER_FIELD', | ||
| 125 | INPUT_CONTROL_TYPE => 'TEXT', | ||
| 126 | SEARCH_CONTROL_TYPE => 'TEXT', | ||
| 127 | SEARCH_MULT_SELECT => 4, | ||
| 128 | HEADING => 'Order by', | ||
| 129 | DATATYPE => 'CHAR', | ||
| 130 | REQUIRED => 'N', | ||
| 131 | INSERTABLE => 'Y', | ||
| 132 | SEARCHABLE => 'Y', | ||
| 133 | INPUT_SIZE => 50, | ||
| 134 | INPUT_MAXLENGTH => 200, | ||
| 135 | HELP_SUMMARY => <<EOH, | ||
| 136 | For each data point selected above, | ||
| 137 | there is an abbreviation of the field in | ||
| 138 | parenthesis. Use those abbreviations | ||
| 139 | here separated by commas to | ||
| 140 | override the default ordering of | ||
| 141 | this report. The word "desc" | ||
| 142 | may be appended to each abbreviation | ||
| 143 | to specify order polarity. | ||
| 144 | EOH | ||
| 145 | }; | ||
| 146 | |||
| 147 | my $limit_field = { | ||
| 148 | FIELD_NAME => 'RESULT_LIMIT', | ||
| 149 | INPUT_CONTROL_TYPE => 'TEXT', | ||
| 150 | SEARCH_CONTROL_TYPE => 'TEXT', | ||
| 151 | HEADING => 'Limit result set', | ||
| 152 | DATATYPE => 'INT UNSIGNED', | ||
| 153 | REQUIRED => 'N', | ||
| 154 | INSERTABLE => 'Y', | ||
| 155 | SEARCHABLE => 'Y', | ||
| 156 | INPUT_SIZE => 6, | ||
| 157 | INPUT_MAXLENGTH => 10, | ||
| 158 | HELP_SUMMARY => <<EOH, | ||
| 159 | Limit the result set to this number | ||
| 160 | of rows. | ||
| 161 | EOH | ||
| 162 | }; | ||
| 163 | |||
| 164 | =head1 NAME | ||
| 165 | |||
| 166 | DBIx::Knowledge::Report - Handler routines to serve report builder form and report execution | ||
| 167 | |||
| 168 | =head1 SYNOPSIS | ||
| 169 | |||
| 170 | use DBIx::Knowledge::Report; | ||
| 171 | use CGI::CRUD::SkipperOutput; | ||
| 172 | |||
| 173 | # Example implementation in mod_perl | ||
| 174 | $crud = new CGI::CRUD::SkipperOutput($r) or return OK; | ||
| 175 | $report = new DBIx::Knowledge::Report($crud,'PROCESS_LOAD_LOG') or return OK; | ||
| 176 | $report->select_list_from_table() or return OK; | ||
| 177 | return $report->take_action(); | ||
| 178 | |||
| 179 | =head1 DESCRIPTION | ||
| 180 | |||
| 181 | Provides methods to display a report builder form and to execute the reports. To be run in the context | ||
| 182 | of a webserver. | ||
| 183 | |||
| 184 | See SmartCruddy! L<http://www.thesmbexchange.com/smartcruddy/> for more info and an implementation. | ||
| 185 | |||
| 186 | =head1 DETAILS | ||
| 187 | |||
| 188 | =head2 Linear convergent data sets | ||
| 189 | |||
| 190 | Your data set must be in a form that can be grouped by a set of fields. An example | ||
| 191 | may be as simple as a table populated with webserver log records. This table can be aggregated by the timestamp field | ||
| 192 | of the webserver request (say you want to report on the number of hits to /index.html from 9am to 10am) | ||
| 193 | and will fit nicely into a model that can readily be used by this abstraction. | ||
| 194 | |||
| 195 | This may be accomplished by creating views or semi-aggregated tables (materialized views) usually along | ||
| 196 | an axis of time (temporal linear convergence). Semi-aggregated tables are useful for reducing the size of very large data sets | ||
| 197 | aggregated by key fields analysts might be interested in so they can be queried efficiently in real-time. | ||
| 198 | |||
| 199 | =head2 Data point definitions | ||
| 200 | |||
| 201 | Once you have an appropriate table or view to build reports from (see L<Linear convergent data sets>) you'll need to define | ||
| 202 | a set of data points the report builder can present. | ||
| 203 | A data point may be a field in the table or view or a derivative of field(s), essentially any valid expression in a | ||
| 204 | select list of an SQL SELECT statement. Example: | ||
| 205 | |||
| 206 | SELECT request_date, TO_CHAR(request_date, 'YYYY') FROM... | ||
| 207 | |||
| 208 | represents two data points. | ||
| 209 | |||
| 210 | CAREFUL! the terms "field" and "data point" may be used interchangeably below but they are not the same thing (the author | ||
| 211 | will try to avoid such confusion). | ||
| 212 | |||
| 213 | These data point | ||
| 214 | definitions may exist in a table C<DBIX_KNOWLEDGE_DATA_POINT> so they can be managed easily (probably | ||
| 215 | by data analysts, preferably someone who knows SQL). Each record in the table defines a data point and | ||
| 216 | each column is explained below: | ||
| 217 | |||
| 218 | =over 2 | ||
| 219 | |||
| 220 | =item TABLE_NAME | ||
| 221 | |||
| 222 | Name of table or view. Name doesn't actually have to refer to an object in the database, this is a way to identify a group | ||
| 223 | of data points and this name will be passed to methods in this class. Syntax rules for general identifiers apply - no whitespace, alphanumeric characters only, | ||
| 224 | may want to use ALL CAPS for consistency. | ||
| 225 | E.g. APACHE_LOG | ||
| 226 | |||
| 227 | =item FIELD_NAME | ||
| 228 | |||
| 229 | Name of the data point - don't confuse this with an actual column name in a table or view, although they may be the same. The | ||
| 230 | syntax rules for general identifiers apply - no whitespace, alphanumeric characters only, may want to use ALL CAPS for consistency. | ||
| 231 | E.g. REQUEST_YEAR | ||
| 232 | |||
| 233 | =item APPEAR_ORDER | ||
| 234 | |||
| 235 | Ordering of the data points as they will appear in the report builder form and the final report. I recommend spacing by 10 (10, 20, 30, etc). | ||
| 236 | |||
| 237 | =item HEADER | ||
| 238 | |||
| 239 | Friendly name of C<FIELD_NAME> that will appear in the report builder and reports. | ||
| 240 | |||
| 241 | =item SELECT_SQL | ||
| 242 | |||
| 243 | Valid SQL that will appear in the select list of a SELECT statement (example above). | ||
| 244 | This is how the data point will appear in the report so any formatting should happen here. | ||
| 245 | |||
| 246 | =item ORDER_LEGEND | ||
| 247 | |||
| 248 | Another alias for the data point name, this will appear in the report builder and allows the report ordering | ||
| 249 | to be modified by specifying a comma-separated list of these aliases in the "Order by" box of the report builder. The reason for another set of aliases | ||
| 250 | is an abbreviation of C<HEADER> with stricter syntax that can be easily parsed when comma-separated. | ||
| 251 | |||
| 252 | The syntax rules for general identifiers apply - no whitespace, alphabetic characters only. Additionally, THESE MUST BE IN ALL CAPS. | ||
| 253 | |||
| 254 | When using these aliases in the "Order by" box, each may be appended with "desc" to specify order polarity. | ||
| 255 | |||
| 256 | =item ORDER_BY_SQL | ||
| 257 | |||
| 258 | Allows you to achieve an ordering in the report for this data point different from how it would | ||
| 259 | order by in its appearance in the report (C<SELECT_SQL>). Example is for formatted dates where | ||
| 260 | |||
| 261 | SELECT_SQL = TO_CHAR(base_hour,'MM/DD/YY') | ||
| 262 | |||
| 263 | You'll want to order this field chronologically: | ||
| 264 | |||
| 265 | ORDER_BY_SQL = TO_CHAR(base_hour,'YYYYMMDD') | ||
| 266 | |||
| 267 | or simply | ||
| 268 | |||
| 269 | ORDER_BY_SQL = base_hour | ||
| 270 | |||
| 271 | =item GROUP_BY_EXPR | ||
| 272 | |||
| 273 | Evaluated as a perl boolean as to whether or not this data point (C<SELECT_SQL>) is a group by expression (e.g. SUM(error_count) IS a group by expression). | ||
| 274 | |||
| 275 | =item GROUP_BY_SQL | ||
| 276 | |||
| 277 | If this data point is a group by expression and the C<SELECT_SQL> expression references fields that are not part of the group by expression, they must appear here | ||
| 278 | so they are included in the GROUP BY clause. The field name(s) must be enclosed in quotes; multiple field names may be entered separated by commas. Example: | ||
| 279 | |||
| 280 | SELECT_SQL = TO_CHAR((payin_amt - payout_amt) * SUM(export_count),'L99,990.99') | ||
| 281 | |||
| 282 | The payin_amt and payout_amt fields are not part of any group by functions, so: | ||
| 283 | |||
| 284 | GROUP_BY_SQL = 'payin_amt', 'payout_amt' | ||
| 285 | |||
| 286 | =item NO_DEFAULT_ORDER_BY | ||
| 287 | |||
| 288 | The default ordering of the report is used if the "Order by" box is empty in the report builder. The default ordering uses the list of data points | ||
| 289 | that are NOT group by expressions, (non-group by expressions are the ones that will appear in the GROUP BY clause) unless C<NO_DEFAULT_ORDER_BY> evaluates | ||
| 290 | true as a perl boolean. | ||
| 291 | |||
| 292 | =item NO_REPEAT | ||
| 293 | |||
| 294 | To make the report look cleaner, within subgroups the name of this data point will only appear in the first line of many. | ||
| 295 | |||
| 296 | =item HTML_CLASS | ||
| 297 | |||
| 298 | HTML classes that will be prepended to those assigned to the E<lt>tdE<gt> element (see L<CSS>). | ||
| 299 | Class name(s) must be enclosed in quotes; multiple class names may be entered separated by commas. Example: | ||
| 300 | |||
| 301 | HTML_CLASS = 'revenue', 'data_align_right' | ||
| 302 | |||
| 303 | =item NP_ALLOW | ||
| 304 | |||
| 305 | Advanced: For authenticated users in the "np" group, this data point will appear | ||
| 306 | in reports if C<NP_ALLOW> evaluates to true as a perl boolean. | ||
| 307 | |||
| 308 | =item NP_HEADER | ||
| 309 | |||
| 310 | Advanced: For authenticated users in the "np" group, an alternate friendly name that will appear in reports. | ||
| 311 | |||
| 312 | =back | ||
| 313 | |||
| 314 | You may also pass an array of hash references to the constructor instead of managing records in C<DBIX_KNOWLEDGE_DATA_POINT>. | ||
| 315 | Each hash having key/value pairs corresponding to a record as described above with the following | ||
| 316 | differences: | ||
| 317 | |||
| 318 | TABLE_NAME is not needed (the array is the group). Rename `FIELD_NAME' to `ID' (UPPER CASE). All other field names must be lower case | ||
| 319 | as keys in each hash. This array is passed to the constructor. | ||
| 320 | |||
| 321 | =head2 Report Builder | ||
| 322 | |||
| 323 | Once the data points have been defined, you will be able to build a report. The first screen presented by the L<take_action> method will be | ||
| 324 | a report builder form. The first section lets you add a title, | ||
| 325 | pick the fields to report on, which fields to subtotal, specify repeated headings, auxiliary fields (advanced, see the code), | ||
| 326 | override ordering and set result | ||
| 327 | limits. The next section of the form allows you to set criteria on the result set. | ||
| 328 | |||
| 329 | =head2 CSS | ||
| 330 | |||
| 331 | Class attributes are hooked into the HTML that is generated to allow custom styling with CSS. | ||
| 332 | The following is a list of class names and how/where they influence the presentation. | ||
| 333 | |||
| 334 | =over | ||
| 335 | |||
| 336 | =item report_data | ||
| 337 | |||
| 338 | All C<table> elements are tagged with this class. | ||
| 339 | |||
| 340 | =item report_summary | ||
| 341 | |||
| 342 | The final C<tr> element is tagged with this class if a grand total report summary is requested. | ||
| 343 | |||
| 344 | =item data_subtotalX | ||
| 345 | |||
| 346 | Where X is an integer indicating the subtotal rank or nesting level. C<tr> elements on summary (subtotal) rows are tagged with this | ||
| 347 | class, the name of which varies depending on the nesting level or subtotal rank. Example: if you subtotal on year/month/day, the | ||
| 348 | yearly subtotal row C<tr> element will be tagged with a class named C<data_subtotal1>, month with C<data_subtotal2>, etc. | ||
| 349 | |||
| 350 | =item subtotal_label | ||
| 351 | |||
| 352 | C<td> elements are tagged with this class only if they follow C<tr> elements tagged with a C<data_subtotalX> or C<report_summary> classes. | ||
| 353 | These fields normally won't contain data, just a label indicating this row is a summary row. | ||
| 354 | |||
| 355 | =item custom HTML_CLASS | ||
| 356 | |||
| 357 | C<td> elements of data points with C<HTML_CLASS> populated (see above) are tagged with a set of classes prepended with the class(es) appearing in C<HTML_CLASS>. | ||
| 358 | |||
| 359 | =back | ||
| 360 | |||
| 361 | =head2 Saving and editing reports | ||
| 362 | |||
| 363 | At the bottom of every report generated are two links. | ||
| 364 | |||
| 365 | =over | ||
| 366 | |||
| 367 | =item Save report URL for later | ||
| 368 | |||
| 369 | If you right click this link in most browsers you should have the option to copy the URL link location. You can then past it in an HTML | ||
| 370 | document and anchor it, etc. | ||
| 371 | |||
| 372 | =item Further customize this report | ||
| 373 | |||
| 374 | This link will take you to the report builder with all parameters saved and pre-selected. | ||
| 375 | |||
| 376 | =back | ||
| 377 | |||
| 378 | |||
| 379 | =head1 METHOD DETAILS | ||
| 380 | |||
| 381 | =over 4 | ||
| 382 | |||
| 383 | =item C<new> (constructor) | ||
| 384 | |||
| 385 | $report = new DBIx::Knowledge::Report($crud_output, $report_table_name[, $data_points, $cache_list]); | ||
| 386 | |||
| 387 | Create a $report object where $crud_output is a L<CGI::CRUD> output object and $report_table_name | ||
| 388 | is the name of the table or view to be reported upon. | ||
| 389 | An optional array reference $data_points may be given as described in L<Data point definitions>, | ||
| 390 | otherwise you must invoke C<select_list_from_table()>. | ||
| 391 | Optional boolean $cache_list will cache the data point list. | ||
| 392 | |||
| 393 | Upon error, C<server_error()> will be called on $crud_output and undef will be returned. | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | sub new | ||
| 398 | { | ||
| 399 | my $caller = shift; | ||
| 400 | |||
| 401 | my ($output,$report_table_name,$potential_select_fields_in,$cache_list) = @_; | ||
| 402 | |||
| 403 | my $self = { | ||
| 404 | report_table_name => $report_table_name, | ||
| 405 | output => $output, | ||
| 406 | }; | ||
| 407 | |||
| 408 | create_select_list($potential_select_fields_in,$cache_list) if defined($potential_select_fields_in); | ||
| 409 | |||
| 410 | my $class = ref($caller) || $caller; | ||
| 411 | bless($self,$class); | ||
| 412 | |||
| 413 | my $sqlclass = $self->_pull_driver($output->dbh()); | ||
| 414 | ($output->server_error(),return undef) unless defined($sqlclass); | ||
| 415 | |||
| 416 | $self->{sql_gen} = $sqlclass->new(); | ||
| 417 | |||
| 418 | return $self; | ||
| 419 | } | ||
| 420 | |||
| 421 | sub _pull_driver | ||
| 422 | { | ||
| 423 | my ($caller,$dbh) = @_; | ||
| 424 | |||
| 425 | return $caller->{sqlclass} if ref($caller) && defined($caller->{sqlclass}); | ||
| 426 | |||
| 427 | # SQL classes must be named after the DBI driver name | ||
| 428 | my $sqlclass = "DBIx::Knowledge::$dbh->{Driver}{Name}SQL"; | ||
| 429 | eval qq(require $sqlclass) || (warn("Database driver not supported"),return undef); | ||
| 430 | |||
| 431 | $caller->{sqlclass} = $sqlclass if ref($caller); | ||
| 432 | return $sqlclass; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub create_select_list | ||
| 436 | { | ||
| 437 | my ($potential_select_fields_in,$cache_list) = @_; | ||
| 438 | return 1 if $cache_list && $potential_select_fields; | ||
| 439 | |||
| 440 | $potential_select_fields = $potential_select_fields_in; | ||
| 441 | |||
| 442 | %potential_select_fields_by_id = (); | ||
| 443 | %potential_select_fields_by_order_legend = (); | ||
| 444 | @select_field_picklist = (); | ||
| 445 | @np_select_field_picklist = (); | ||
| 446 | @subtotal_field_picklist = (); | ||
| 447 | @np_subtotal_field_picklist = (); | ||
| 448 | |||
| 449 | map { $potential_select_fields_by_id{$_->{ID}} = $_ } @$potential_select_fields; | ||
| 450 | |||
| 451 | map { $potential_select_fields_by_order_legend{$_->{order_legend}} = $_ } @$potential_select_fields; | ||
| 452 | @select_field_picklist = map({ ID => $_->{ID}, MASK => "$_->{header} ($_->{order_legend})" },@$potential_select_fields); | ||
| 453 | map { push(@np_select_field_picklist,{ ID => $_->{ID}, MASK => ($_->{np_header} ? $_->{np_header} : $_->{header}) . ' ($_->{order_legend})' }) if $_->{np_allow} } @$potential_select_fields; | ||
| 454 | map { push(@subtotal_field_picklist, | ||
| 455 | { | ||
| 456 | ID => $_->{ID}, | ||
| 457 | MASK => "$_->{header} ($_->{order_legend})" | ||
| 458 | } | ||
| 459 | ) | ||
| 460 | unless $_->{group_by_expr} | ||
| 461 | } | ||
| 462 | reverse @$potential_select_fields; | ||
| 463 | push(@subtotal_field_picklist,{ ID => $DBIx::Knowledge::REPORT_TOTAL_KEY, MASK => 'Report grand total (GRANDTOT)' }); | ||
| 464 | map { push(@np_subtotal_field_picklist, | ||
| 465 | { | ||
| 466 | ID => $_->{ID}, | ||
| 467 | MASK => "$_->{header} ($_->{order_legend})" | ||
| 468 | } | ||
| 469 | ) | ||
| 470 | if $_->{np_allow} && !$_->{group_by_expr} | ||
| 471 | } | ||
| 472 | reverse @$potential_select_fields; | ||
| 473 | push(@np_subtotal_field_picklist,{ ID => $DBIx::Knowledge::REPORT_TOTAL_KEY, MASK => 'Report grand total (GRANDTOT)' }); | ||
| 474 | } | ||
| 475 | |||
| 476 | =pod | ||
| 477 | |||
| 478 | =item C<select_list_from_table> | ||
| 479 | |||
| 480 | $bool = $report->select_list_from_table([$report_table_name, $cache_list, $dbh, $select_field_table_name]); | ||
| 481 | |||
| 482 | Retrieve the list of data points for report building from a database table. | ||
| 483 | Optional $report_table_name overrides the same parameter given to the constructor only for the purpose of identifying the group of datapoints by matching the C<TABLE_NAME> | ||
| 484 | column in the data points table. | ||
| 485 | Boolean $cache_list will cache the data point list, $dbh is an appropriate DBI database handle. | ||
| 486 | The table that stores the data points may be given by | ||
| 487 | $select_field_table_name table, which defaults to $DBIx::Knowledge::Report::REPORT_SELECT_LIST_TABLE_NAME. | ||
| 488 | |||
| 489 | Upon error, C<server_error()> or C<perror> will be called on $crud_output and false will be returned, otherwise return true. | ||
| 490 | |||
| 491 | =cut | ||
| 492 | |||
| 493 | sub select_list_from_table | ||
| 494 | { | ||
| 495 | my ($self,$report_table_name,$cache_list,$dbh,$select_field_table_name) = @_; | ||
| 496 | return 1 if $cache_list && $potential_select_fields; | ||
| 497 | |||
| 498 | my @select_list; | ||
| 499 | |||
| 500 | $dbh ||= $self->{output}->dbh(); | ||
| 501 | $report_table_name ||= $self->{report_table_name}; | ||
| 502 | $select_field_table_name ||= $DBIx::Knowledge::Report::REPORT_SELECT_LIST_TABLE_NAME; | ||
| 503 | |||
| 504 | my $searcher = new DBIx::IO::Search($dbh,$select_field_table_name); | ||
| 505 | my $recs; | ||
| 506 | if (ref($searcher)) | ||
| 507 | { | ||
| 508 | $searcher->build_scalar_crit('TABLE_NAME','=',$report_table_name); | ||
| 509 | $recs = $searcher->search(undef,[ 'APPEAR_ORDER' ]) || return undef; | ||
| 510 | } | ||
| 511 | elsif (!defined($searcher)) | ||
| 512 | { | ||
| 513 | $self->{output}->server_error(); | ||
| 514 | return undef; | ||
| 515 | } | ||
| 516 | elsif (!$searcher) | ||
| 517 | { | ||
| 518 | warn("$report_table_name does not seem to exist so no select list could be found"); | ||
| 519 | return 1; | ||
| 520 | } | ||
| 521 | else | ||
| 522 | { | ||
| 523 | die("A horrible death"); | ||
| 524 | } | ||
| 525 | |||
| 526 | foreach my $rec (@$recs) | ||
| 527 | { | ||
| 528 | my ($k,$v); | ||
| 529 | my %select_attrs; | ||
| 530 | while (($k,$v) = each(%$rec)) | ||
| 531 | { | ||
| 532 | if ($k eq 'GROUP_BY_SQL' || $k eq 'HTML_CLASS') | ||
| 533 | { | ||
| 534 | eval("\$select_attrs{lc(\$k)} = [ $v ];") if length($v); | ||
| 535 | if ($@) | ||
| 536 | { | ||
| 537 | $self->{output}->perror("parse error for value found in $report_table_name.$k [$v]"); | ||
| 538 | return 0; | ||
| 539 | } | ||
| 540 | } | ||
| 541 | elsif ($k eq 'FIELD_NAME') | ||
| 542 | { | ||
| 543 | $select_attrs{ID} = $v; | ||
| 544 | } | ||
| 545 | else | ||
| 546 | { | ||
| 547 | $select_attrs{lc($k)} = $v; | ||
| 548 | } | ||
| 549 | } | ||
| 550 | push(@select_list,\%select_attrs); | ||
| 551 | } | ||
| 552 | |||
| 553 | create_select_list(\@select_list); | ||
| 554 | |||
| 555 | return 1; | ||
| 556 | } | ||
| 557 | |||
| 558 | =pod | ||
| 559 | |||
| 560 | =item C<take_action> | ||
| 561 | |||
| 562 | $status = $report->take_action(); | ||
| 563 | |||
| 564 | Call within a webserver context to display a report builder form or execute and display report results. | ||
| 565 | May return an error page if there are any problems along the way. | ||
| 566 | |||
| 567 | Return 0 (equivalent to Apache status code `OK'). | ||
| 568 | |||
| 569 | =cut | ||
| 570 | |||
| 571 | sub take_action | ||
| 572 | { | ||
| 573 | my ($self) = @_; | ||
| 574 | my $r = $self->{output}; | ||
| 575 | my $action = $r->param('__SDAT_TAB_ACTION.ACTION') || ''; | ||
| 576 | |||
| 577 | if ($action eq 'RR') | ||
| 578 | { | ||
| 579 | $self->report_results($r); | ||
| 580 | } | ||
| 581 | else | ||
| 582 | { | ||
| 583 | $self->report_defn_request($r); | ||
| 584 | } | ||
| 585 | return OK; | ||
| 586 | } | ||
| 587 | |||
| 588 | sub report_defn_form | ||
| 589 | { | ||
| 590 | my ($self,$r) = @_; | ||
| 591 | my $form = $r->form($r->dbh()); | ||
| 592 | $form->action($self->{action}); | ||
| 593 | $form->heading($self->{form_heading} || 'Custom Reports'); | ||
| 594 | $form->submit_value('Report'); | ||
| 595 | |||
| 596 | $form->add_group('SEARCHABLE',undef,'Data Points','FORMAT'); | ||
| 597 | my $select_pick; | ||
| 598 | my $subtotal_pick; | ||
| 599 | if (length($r->{np_uname})) | ||
| 600 | { | ||
| 601 | $select_pick = \@np_select_field_picklist; | ||
| 602 | $subtotal_pick = \@np_subtotal_field_picklist; | ||
| 603 | } | ||
| 604 | else | ||
| 605 | { | ||
| 606 | $select_pick = \@select_field_picklist; | ||
| 607 | $subtotal_pick = \@subtotal_field_picklist; | ||
| 608 | } | ||
| 609 | |||
| 610 | $form->add_field($title_field); | ||
| 611 | |||
| 612 | $form->add_field($select_field,$select_pick); | ||
| 613 | my $field = $form->field_by_name('SELECT_FIELD'); | ||
| 614 | $field->{REQUIRED} = 'Y'; | ||
| 615 | $form->add_field($subtotal_field,$subtotal_pick); | ||
| 616 | |||
| 617 | $form->add_field($head_repeat_field,$head_repeat_picklist); | ||
| 618 | my $q = $r->query(); | ||
| 619 | $q->{'FORMAT.HEADER_REPEAT'} = '0' unless length($q->{'FORMAT.HEADER_REPEAT'}); | ||
| 620 | |||
| 621 | $form->add_field($aux_info_field,$select_pick); | ||
| 622 | $form->add_field($orderby_field); | ||
| 623 | $form->add_field($limit_field); | ||
| 624 | |||
| 625 | $r->graceful_add_form_group($form,'SEARCHABLE',$self->{report_table_name},'Result Criteria','CRITERIA') or return undef; | ||
| 626 | |||
| 627 | # disallow some criteria fields for network partner report | ||
| 628 | if (length($r->{np_uname})) | ||
| 629 | { | ||
| 630 | my $fields = $form->field_list(); | ||
| 631 | my @fields_s = @$fields; | ||
| 632 | foreach my $field (@fields_s) | ||
| 633 | { | ||
| 634 | $form->delete_field($field->{FIELD_NAME}) unless $np_allowed_crit_fields{$field->{FIELD_NAME}}; | ||
| 635 | } | ||
| 636 | } | ||
| 637 | $form->reset_group(); | ||
| 638 | |||
| 639 | return $form; | ||
| 640 | } | ||
| 641 | |||
| 642 | sub report_defn_request | ||
| 643 | { | ||
| 644 | my ($self,$r) = @_; | ||
| 645 | my $form = $self->report_defn_form($r) or return undef; | ||
| 646 | my $q = $r->query(); | ||
| 647 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'RR'; | ||
| 648 | $r->output($form->prepare($q),($ENV{SMARTCRUDDY_FAST_TEMPLATE_MAIN} || 'smartcruddy.tpl')); | ||
| 649 | } | ||
| 650 | |||
| 651 | sub report_results | ||
| 652 | { | ||
| 653 | my ($self,$r) = @_; | ||
| 654 | |||
| 655 | # keep in mind this is NOT normalized or unescaped until later | ||
| 656 | my $q = $r->query(); | ||
| 657 | |||
| 658 | $r->{tpl_vars}{REPORT_TITLE} = $q->{'FORMAT.REPORT_TITLE'} || 'Custom Report'; | ||
| 659 | ##at should use $r->{apache}->request_time() | ||
| 660 | $r->{tpl_vars}{REPORT_DATE} = POSIX::strftime('%c',localtime()); | ||
| 661 | |||
| 662 | my $form = $self->report_defn_form($r) || return undef; | ||
| 663 | unless ($form->validate_query($q)) | ||
| 664 | { | ||
| 665 | $r->output($form->prepare($q),($ENV{SMARTCRUDDY_FAST_TEMPLATE_MAIN} || 'smartcruddy.tpl')); | ||
| 666 | return OK; | ||
| 667 | } | ||
| 668 | |||
| 669 | $q = $form->format_query($r->query()); | ||
| 670 | |||
| 671 | my $tq = $r->query(); | ||
| 672 | $r->{tpl_vars}{FULL_SAVED_QUERY} = CGI::CRUD::TableIO::stringify_query($tq); | ||
| 673 | $tq = { %$tq }; | ||
| 674 | delete($tq->{'__SDAT_TAB_ACTION.ACTION'}); | ||
| 675 | $r->{tpl_vars}{CUSTOM_SAVED_QUERY} = CGI::CRUD::TableIO::stringify_query($tq); | ||
| 676 | |||
| 677 | my $select_fields_q = $q->{FORMAT}{SELECT_FIELD}; | ||
| 678 | $select_fields_q = [ $select_fields_q ] unless ref($select_fields_q); | ||
| 679 | my %select_fields_q; | ||
| 680 | @select_fields_q{ @$select_fields_q } = (1) x @$select_fields_q; | ||
| 681 | my @select_fields = (); | ||
| 682 | |||
| 683 | if (length($r->{np_uname})) | ||
| 684 | { | ||
| 685 | foreach my $item (@np_select_field_picklist) | ||
| 686 | { | ||
| 687 | if ($select_fields_q{$item->{ID}}) | ||
| 688 | { | ||
| 689 | my %f = %{$potential_select_fields_by_id{$item->{ID}}}; | ||
| 690 | # overwrite header with np_header for np report | ||
| 691 | $f{header} = $f{np_header} if ($f{np_header}); | ||
| 692 | push(@select_fields, \%f); | ||
| 693 | } | ||
| 694 | } | ||
| 695 | } | ||
| 696 | else | ||
| 697 | { | ||
| 698 | map { push(@select_fields,$potential_select_fields_by_id{$_->{ID}}) if $select_fields_q{$_->{ID}} } @select_field_picklist; | ||
| 699 | } | ||
| 700 | |||
| 701 | my $subtotal_fields_q = $q->{FORMAT}{SUBTOTAL_FIELD}; | ||
| 702 | my @subtotal_fields = (); | ||
| 703 | if ($subtotal_fields_q) | ||
| 704 | { | ||
| 705 | $subtotal_fields_q = [ $subtotal_fields_q ] unless ref($subtotal_fields_q); | ||
| 706 | my %subtotal_fields_q; | ||
| 707 | @subtotal_fields_q{ @$subtotal_fields_q } = (1) x @$subtotal_fields_q; | ||
| 708 | map { ($r->perror("'Summary totals on' fields must be a subset of the 'Report on' fields [$_ is not], please try again"),return undef) | ||
| 709 | unless exists($select_fields_q{$_}) || $_ eq $DBIx::Knowledge::REPORT_TOTAL_KEY } @$subtotal_fields_q; | ||
| 710 | map { push(@subtotal_fields,$_->{ID}) if $subtotal_fields_q{$_->{ID}} } @subtotal_field_picklist; | ||
| 711 | ##at on freeform field, please mind the LEGEND key 'GRANDTOT' | ||
| 712 | } | ||
| 713 | |||
| 714 | ##at by default, results will be sorted in the same order as the order of the select list | ||
| 715 | my $orderby_q = $q->{FORMAT}{ORDER_FIELD}; | ||
| 716 | my @order_fields = (); | ||
| 717 | if (length($orderby_q)) | ||
| 718 | { | ||
| 719 | foreach my $order_term (split(/\s*,\s*/,$orderby_q)) | ||
| 720 | { | ||
| 721 | my $desc_needed = $order_term =~ s/\s+desc//i; | ||
| 722 | $order_term = uc($order_term); | ||
| 723 | ##at should be done in validate_query() | ||
| 724 | ($r->perror("Invalid term entered in 'Order by' sequence [$order_term], please go back and correct"),return undef) | ||
| 725 | unless exists($potential_select_fields_by_order_legend{$order_term}); | ||
| 726 | my $term_id = $potential_select_fields_by_order_legend{$order_term}; | ||
| 727 | ($r->perror("'Ordered by' fields must be a subset of the 'Report on' fields [$order_term is not], please try again"),return undef) | ||
| 728 | unless exists($select_fields_q{$term_id->{ID}}); | ||
| 729 | my %order_field = ( %{$term_id} ); | ||
| 730 | $order_field{descending_order} = $desc_needed; | ||
| 731 | push(@order_fields,\%order_field) unless length($r->{np_uname}) && !$term_id->{np_allow}; | ||
| 732 | } | ||
| 733 | } | ||
| 734 | |||
| 735 | my $result_limit = $q->{FORMAT}{RESULT_LIMIT}; | ||
| 736 | |||
| 737 | my $where_sql = ''; | ||
| 738 | if ($q->{CRITERIA}) | ||
| 739 | { | ||
| 740 | my $crit = $q->{CRITERIA}; | ||
| 741 | my @keys = keys(%$crit); | ||
| 742 | foreach my $key (@keys) | ||
| 743 | { | ||
| 744 | delete($crit->{$key}) if length($r->{np_uname}) && !$np_allowed_crit_fields{$key}; | ||
| 745 | } | ||
| 746 | $crit->{NP_USERNAME} = $r->{np_uname} if length($r->{np_uname}); | ||
| 747 | my $where = new CGI::CRUD::TableIO($r->dbh()); | ||
| 748 | $where_sql = $where->where_sql($crit,$self->{report_table_name}); | ||
| 749 | defined($where_sql) or ($r->server_error(),return undef); | ||
| 750 | } | ||
| 751 | |||
| 752 | my %want_subtotal_fields = (); | ||
| 753 | |||
| 754 | my $sql = $self->{sql_gen}->sql_rollup($self->{report_table_name},\@select_fields,$where_sql,\@subtotal_fields,\@order_fields,\%want_subtotal_fields) or | ||
| 755 | ($r->perror("Report definition is invalid:<br>$self->{sql_gen}->{errstr}<br>Please go back and try again"),return undef); | ||
| 756 | |||
| 757 | my $dbh = $r->dbh(); | ||
| 758 | my $report_sth = $dbh->prepare($sql) or ($r->perror("Report definition is invalid, please go back and try again"),return undef); | ||
| 759 | $report_sth->execute() or ($r->server_error(),return undef); | ||
| 760 | |||
| 761 | my $title = CGI::AutoForm->escape($r->{tpl_vars}{REPORT_TITLE}); | ||
| 762 | my $tdate = CGI::AutoForm->escape($r->{tpl_vars}{REPORT_DATE}); | ||
| 763 | |||
| 764 | my $html = <<HTML; | ||
| 765 | <H3 style="padding-bottom:0px;margin-bottom:0px;margin-top: 10px;">$title</H3> | ||
| 766 | <p style="padding-top:0px;margin-top:0px;">Report created $tdate</p> | ||
| 767 | HTML | ||
| 768 | |||
| 769 | my $aux_info_fields = $q->{FORMAT}{AUX_INFO_FIELD}; | ||
| 770 | $aux_info_fields = [ $aux_info_fields ] unless ref $aux_info_fields; | ||
| 771 | |||
| 772 | mark_aux_fields(\@select_fields, $aux_info_fields); | ||
| 773 | |||
| 774 | my $out = new DBIx::Knowledge::Output($q->{FORMAT}{HEADER_REPEAT}); | ||
| 775 | #OPDebugUtil::ddump(\@select_fields); | ||
| 776 | my $outp = $out->generate_html($report_sth,\@select_fields,\%want_subtotal_fields) or ($r->server_error(),return undef); | ||
| 777 | |||
| 778 | $html .= $$outp; | ||
| 779 | |||
| 780 | $html .= <<HTML; | ||
| 781 | <p style="margin: 30px;"> | ||
| 782 | <a href="$r->{action}?$r->{tpl_vars}{FULL_SAVED_QUERY}">Save report URL for later</a><br> | ||
| 783 | <a href="$r->{action}?$r->{tpl_vars}{CUSTOM_SAVED_QUERY}">Further customize this report</a> | ||
| 784 | </p> | ||
| 785 | HTML | ||
| 786 | |||
| 787 | $r->{tpl_vars}{CSS_MAIN} = '/smartcruddy.css'; | ||
| 788 | $r->output($html,($ENV{SMARTCRUDDY_FAST_TEMPLATE_MAIN} || 'smartcruddy.tpl')); | ||
| 789 | } | ||
| 790 | |||
| 791 | # AUX_INFO_FIELD columns are not displayed in the report body but are | ||
| 792 | # shown in the summary rows. We will mark all AUX_INFO_FIELDS here in | ||
| 793 | # this sub and the DBIx::Knowledge module will then act appropriately. | ||
| 794 | # Arguments are a reference to an array of query select fields and a | ||
| 795 | # reference to an array of the aux info field names. | ||
| 796 | sub mark_aux_fields | ||
| 797 | { | ||
| 798 | my ($select_fields, $aux_fields) = @_; | ||
| 799 | |||
| 800 | # clear aux_info flags. they could have been cached previously by mod_perl | ||
| 801 | for my $se (@$select_fields) { | ||
| 802 | $se->{aux_info} = 0; | ||
| 803 | } | ||
| 804 | |||
| 805 | # mark aux_info flags | ||
| 806 | for my $aux (@$aux_fields) { | ||
| 807 | for my $sel (@$select_fields) { | ||
| 808 | if ($aux eq $sel->{ID}) { | ||
| 809 | $sel->{aux_info} = 1; | ||
| 810 | } | ||
| 811 | } | ||
| 812 | } | ||
| 813 | } | ||
| 814 | |||
| 815 | |||
| 816 | =pod | ||
| 817 | |||
| 818 | =back | ||
| 819 | |||
| 820 | =cut | ||
| 821 | |||
| 822 | 1; | ||
| 823 | |||
| 824 | __END__ |