package WebGUI::Wobject::SQLReport; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2002 Plain Black LLC. #------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. #------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com #------------------------------------------------------------------- use strict; use WebGUI::ErrorHandler; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; use WebGUI::Macro; use WebGUI::Paginator; use WebGUI::Privilege; use WebGUI::Session; use WebGUI::SQL; use WebGUI::URL; use WebGUI::Utility; use WebGUI::Wobject; our @ISA = qw(WebGUI::Wobject); our $namespace = "SQLReport"; our $name = WebGUI::International::get(1,$namespace); #------------------------------------------------------------------- sub duplicate { my ($w); $w = $_[0]->SUPER::duplicate($_[1]); $w = WebGUI::Wobject::SQLReport->new({wobjectId=>$w,namespace=>$namespace}); $w->set({ template=>$_[0]->get("template"), dbQuery=>$_[0]->get("dbQuery"), DSN=>$_[0]->get("DSN"), username=>$_[0]->get("username"), identifier=>$_[0]->get("identifier"), convertCarriageReturns=>$_[0]->get("convertCarriageReturns"), paginateAfter=>$_[0]->get("paginateAfter"), preprocessMacros=>$_[0]->get("preprocessMacros"), debugMode=>$_[0]->get("debugMode") }); } #------------------------------------------------------------------- sub new { my ($self, $class, $property); $class = shift; $property = shift; $self = WebGUI::Wobject->new($property); bless $self, $class; } #------------------------------------------------------------------- sub set { $_[0]->SUPER::set($_[1],[qw(template dbQuery DSN username identifier convertCarriageReturns paginateAfter preprocessMacros debugMode)]); } #------------------------------------------------------------------- sub www_copy { if (WebGUI::Privilege::canEditPage()) { $_[0]->duplicate; return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- sub www_edit { my ($output, $f, $dsn, $username, $paginateAfter); if (WebGUI::Privilege::canEditPage()) { $dsn = $_[0]->get("DSN") || $session{config}{dsn}; $username = $_[0]->get("username") || $session{config}{dbuser}; $paginateAfter = $_[0]->get("paginateAfter") || 50; $f = WebGUI::HTMLForm->new; $output = helpIcon(1,$namespace); $output .= '
" if ($_[0]->get("debugMode")); if ($dsn =~ /\DBI\:\w+\:\w+/) { eval{$dbh = DBI->connect($dsn,$_[0]->get("username"),$_[0]->get("identifier"))}; if ($@) { WebGUI::ErrorHandler::warn("SQL Report [".$_[0]->get("wobjectId")."] ".$@); undef $dbh; } } else { $output .= WebGUI::International::get(9,$namespace).'
' if ($_[0]->get("debugMode")); WebGUI::ErrorHandler::warn("SQLReport [".$_[0]->get("wobjectId")."] The DSN specified is of an improper format."); } if (defined $dbh) { if ($query =~ /select/i || $query =~ /show/i || $query =~ /describe/i) { $sth = WebGUI::SQL->unconditionalRead($query,$dbh); unless ($sth->errorCode < 1) { $errorMessage = $sth->errorMessage; $output .= WebGUI::International::get(11,$namespace).' : '.$errorMessage.'
' if ($_[0]->get("debugMode")); WebGUI::ErrorHandler::warn("There was a problem with the query: ".$errorMessage); } else { if ($_[0]->get("template") ne "") { @template = split(/\^\-\;/,$_[0]->get("template")); } else { $i = 0; $template[0] = '
| '.$col.' | '; $template[1] .= '^'.$i.'; | '; $i++; } $template[0] .= '
'; } else { $url = WebGUI::URL::page('&wid='.$_[0]->get("wobjectId").'&func=view'); foreach (keys %{$session{form}}) { unless ($_ eq "pn" || $_ eq "wid" || $_ eq "func") { $url = WebGUI::URL::append($url, WebGUI::URL::escape($_) .'='.WebGUI::URL::escape($session{form}{$_})); } } $p = WebGUI::Paginator->new($url,\@row,$_[0]->get("paginateAfter")); $output .= $p->getPage($session{form}{pn}); $output .= $template[2]; $output .= $p->getBar($session{form}{pn}); } $sth->finish; } } else { $output .= WebGUI::International::get(10,$namespace).'
' if ($_[0]->get("debugMode")); WebGUI::ErrorHandler::warn("SQLReport [".$_[0]->get("wobjectId")."] The SQL query is improperly formatted."); } $dbh->disconnect(); } else { $output .= WebGUI::International::get(12,$namespace).'
' if ($_[0]->get("debugMode")); WebGUI::ErrorHandler::warn("SQLReport [".$_[0]->get("wobjectId")."] Could not connect to database."); } return $_[0]->processMacros($output); } 1;