package WebGUI::Widget::SQLReport; #------------------------------------------------------------------- # WebGUI is Copyright 2001 Plain Black Software. #------------------------------------------------------------------- # 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::Macro; use WebGUI::Privilege; use WebGUI::Session; use WebGUI::SQL; use WebGUI::Utility; use WebGUI::Widget; #------------------------------------------------------------------- sub purge { WebGUI::SQL->write("delete from SQLReport where widgetId=$_[0]",$_[1]); purgeWidget($_[0],$_[1]); } #------------------------------------------------------------------- sub widgetName { return "SQL Report"; } #------------------------------------------------------------------- sub www_add { my ($output); if (WebGUI::Privilege::canEditPage()) { $output = ''; $output .= '

Add '.widgetName().'

'; $output .= '
'; $output .= WebGUI::Form::hidden("widget","SQLReport"); $output .= WebGUI::Form::hidden("func","addSave"); $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= '
Title'.WebGUI::Form::text("title",20,30,'SQL Report').'
Display the title?'.WebGUI::Form::checkbox("displayTitle",1,1).'
Process macros?'.WebGUI::Form::checkbox("processMacros",1,1).'
Description'.WebGUI::Form::textArea("description",'','','',1).'
Template'.WebGUI::Form::textArea("template",'','','',1).'
Query'.WebGUI::Form::textArea("dbQuery",'').'
DSN'.WebGUI::Form::text("DSN",20,255,$session{config}{dsn}).'
Database User'.WebGUI::Form::text("username",20,255,$session{config}{dbuser}).'
Database Password'.WebGUI::Form::password("identifier",20,255).'
Convert carriage returns?'.WebGUI::Form::checkbox("convertCarriageReturns",1).'
'.WebGUI::Form::submit("save").'
'; return $output; } else { return WebGUI::Privilege::insufficient(); } return $output; } #------------------------------------------------------------------- sub www_addSave { my ($widgetId); if (WebGUI::Privilege::canEditPage()) { $widgetId = create(); WebGUI::SQL->write("insert into SQLReport values($widgetId, ".quote($session{form}{template}).", ".quote($session{form}{dbQuery}).", ".quote($session{form}{DSN}).", ".quote($session{form}{username}).", ".quote($session{form}{identifier}).", '$session{form}{convertCarriageReturns}')",$session{dbh}); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- sub www_edit { my ($output, %data); tie %data, 'Tie::CPHash'; if (WebGUI::Privilege::canEditPage()) { %data = WebGUI::SQL->quickHash("select * from widget,SQLReport where widget.widgetId=$session{form}{wid} and widget.widgetId=SQLReport.widgetId",$session{dbh}); $output = ''; $output .= '

Edit '.widgetName().'

'; $output .= '
'; $output .= WebGUI::Form::hidden("wid",$session{form}{wid}); $output .= WebGUI::Form::hidden("func","editSave"); $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= '
Title'.WebGUI::Form::text("title",20,30,$data{title}).'
Display the title?'.WebGUI::Form::checkbox("displayTitle","1",$data{displayTitle}).'
Process macros?'.WebGUI::Form::checkbox("processMacros","1",$data{processMacros}).'
Description'.WebGUI::Form::textArea("description",$data{description},50,10,1).'
Template'.WebGUI::Form::textArea("template",$data{template},50,10,1).'
Query'.WebGUI::Form::textArea("dbQuery",$data{dbQuery},50,10,1).'
DSN'.WebGUI::Form::text("DSN",20,255,$data{DSN}).'
Database User'.WebGUI::Form::text("username",20,255,$data{username}).'
Database Password'.WebGUI::Form::password("identifier",20,255,$data{identifier}).'
Convert carriage returns?'.WebGUI::Form::checkbox("convertCarriageReturns",1,$data{convertCarriageReturns}).'
'.WebGUI::Form::submit("save").'
'; return $output; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- sub www_editSave { my ($widgetId, $displayTitle, $image, $attachment); if (WebGUI::Privilege::canEditPage()) { update(); WebGUI::SQL->write("update SQLReport set template=".quote($session{form}{template}).", dbQuery=".quote($session{form}{dbQuery}).", convertCarriageReturns='$session{form}{convertCarriageReturns}', DSN=".quote($session{form}{DSN}).", username=".quote($session{form}{username}).", identifier=".quote($session{form}{identifier})." where widgetId=$session{form}{wid}",$session{dbh}); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- sub www_view { my (%data, $output, $widgetId, $sth, $dbh, @result, @template, $temp); tie %data, 'Tie::CPHash'; $widgetId = shift; %data = WebGUI::SQL->quickHash("select * from widget,SQLReport where widget.widgetId=$widgetId and widget.widgetId=SQLReport.widgetId",$session{dbh}); if (defined %data) { if ($data{displayTitle} == 1) { $output = "

".$data{title}."

"; } if ($data{description} ne "") { $output .= $data{description}.'

'; } @template = split(/\^\-/,$data{template}); $output .= $template[0]; if ($data{DSN} =~ /\DBI\:\w+\:\w+/) { $dbh = DBI->connect($data{DSN},$data{username},$data{identifier}); } else { $output .= 'Error: The DSN specified is of an improper format.

'; WebGUI::ErrorHandler::warn("SQLReport [$widgetId] The DSN specified is of an improper format."); } if (defined $dbh) { if ($data{dbQuery} =~ /select/i) { $sth = WebGUI::SQL->read($data{dbQuery},$dbh); } else { $output .= 'Error: The SQL query is improperly formatted.

'; WebGUI::ErrorHandler::warn("SQLReport [$widgetId] The SQL query is improperly formatted."); } if (defined $sth) { while (@result = $sth->array) { $temp = $template[1]; $temp =~ s/\^(\d)/$result[$1]/g; if ($data{convertCarriageReturns}) { $temp =~ s/\n/\/g; } $output .= $temp; } $sth->finish; } else { $output .= 'Error: There was a problem with the query.

'; WebGUI::ErrorHandler::warn("SQLReport [$widgetId] There was a problem with the query."); } $dbh->disconnect(); } else { $output .= 'Error: Could not connect to remote database.

'; WebGUI::ErrorHandler::warn("SQLReport [$widgetId] Could not connect to remote database."); } $output .= $template[2]; } if ($data{processMacros} == 1) { $output = WebGUI::Macro::process($output); } return $output; } 1;