00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef REPORTS_HPP
00017 #define REPORTS_HPP
00018
00019 #include <iostream>
00020 #include <iomanip>
00021 #include "cgiTemplates.h"
00022 #include "connectstring"
00023 #include "forms.h"
00024 #include "ocTypes.h"
00025 #include "ocString.h"
00026 #include "cgiTemplates.h"
00027 #include "openLogin.h"
00028
00029 #include "Report.hpp"
00030 #include "sublist_base.hpp"
00031
00032
00033
00034 #include "Report_Params_ui.h"
00035
00036 #include "col_handling_rep.hpp"
00037 #include "siteLimit.h"
00038
00039 using namespace std;
00040
00041 class Report_List: public sublist_base
00042 {
00043 string lastCat;
00044 cgiScript & scr;
00045
00046 public:
00047
00048 Report_List(cgiScript&sc):sublist_base(sc), scr(sc)
00049 {
00050 ;
00051 }
00052 ~Report_List(){;}
00053
00054 bool list_display( void )
00055 {
00056 bool breturn = true;
00057
00058 hotCol=-2;
00059 skipCol=0;
00060
00061 editLink = listTemplate.getParagraph("hotcolumn");
00062
00063
00064 string sql = "select r.Id, r.Category, r.Name, t.File_Extension, r.External_Program from Report r "
00065 "left join Report_Type t on t.Id = r.Media_Type where r.Show_In_List > 0 ";
00066 if( addedCriteria.length() )
00067 {
00068 sql += " and ";
00069 sql += addedCriteria;
00070 }
00071 sql += " order by r.Category, r.Name";
00072 webIO << "<!-- " << sql << "-->" << endl;
00073 if( getData( sql ) == false )
00074 {
00075 webIO << "<!-- " << sql << "-->" << endl;
00076 }
00077
00078 webIO << listTemplate.getParagraph("list_begin");
00079
00080 emitData();
00081
00082 emitEnd();
00083 return breturn;
00084 }
00085 virtual void sendHotField( int iField, ocString & td )
00086 {
00087 string cat;
00088 ocString th = listTemplate.getParagraph("th");
00089 ocString editLink2;
00090 string program = rs.getField("External_Program").format();
00091 if( program.length() == 0 ) program = "Reports.cgi";
00092 string key;
00093 string extension;
00094 string qString = scr.QueryString().c_str();
00095 switch(iField)
00096 {
00097 case 1:
00098 cat = rs.getField(iField).format();
00099 if( cat != lastCat )
00100 webIO << th.replace( "$data$", cat.c_str() );
00101 else
00102 webIO << th.replace( "$data$", "" );
00103 lastCat = cat;
00104 break;
00105
00106 case 2:
00107 extension= rs.getField(3).format();
00108 if( extension.length() > 0 )
00109 {
00110 key += "/" + rs.getField(iField).format() + "." + extension;
00111 }
00112 key += "?Id=" + rs.getField(0).format();
00113 if( qString.length() )
00114 {
00115 key += "&" + qString;
00116 }
00117 editLink2 = editLink.replace("$prog$",program);
00118 webIO << td.replace( "$data$",
00119 editLink2.replaceAll( "?Id=$key$",key.c_str())
00120 .replaceAll( "$col$",rs.getField(iField).format().c_str()).c_str());
00121 break;
00122 }
00123 }
00124
00125 };
00126
00127 class ReportEmitter
00128 {
00129 cgiEnvironment cenv;
00130 ocString qs;
00131 bool isPDF;
00132 ocString scriptName;
00133 cgiInput & args;
00134 cgiTemplates pgTemplate;
00135 string mediaType;
00136 Report_Obj report;
00137 ocString sql;
00138 bool addPDFLink;
00139 public:
00140 ReportEmitter():cenv(),qs(cenv.QueryString().c_str()),args(cenv.ClientArguments()),addPDFLink(false)
00141 {
00142 isPDF = qs.regExMatch("isPDF");
00143 scriptName = cenv.ScriptName().c_str();
00144 #ifdef REPORTDOESPDF
00145 addPDFLink = true;
00146 #endif
00147 }
00148 ~ReportEmitter(){;}
00149 void run( void )
00150 {
00151 if( oLogin.testLoginStatus() || isPDF )
00152 {
00153 if( args.count("Id")==0 )
00154 {
00157 listReports();
00158 }
00159 else
00160 {
00161 report.key( atoll(args["Id"].c_str()) );
00162 report.Id = report.key();
00163
00164 if( report.get_data() )
00165 {
00166 mediaType = report.getMediaType();
00167 cgiScript script(mediaType.c_str(),false,"",true);
00168
00169 script.ClientArguments().TheMap() = args.TheMap();
00170 Report_Parameters_form param_form(script);
00171 param_form.Report_Id = report.Id;
00172 param_form.get();
00173
00174 if( param_form.haveFormValues() == false )
00175 {
00176 paramForm(script,param_form);
00177 }
00178 else
00179 {
00180 if( mediaType == "application/pdf" && ! isPDF )
00181 {
00182 redirectToHTMLDoc();
00183 }
00184 else
00185 {
00186 script.openHeader();
00187 script.closeHeader();
00188
00189 genReport(script,param_form);
00190 }
00191 }
00192 }
00193 else
00194 {
00195
00196 noData();
00197 }
00198 }
00199 }
00200 else
00201 {
00202
00203 invalidLogin();
00204 }
00205 }
00206
00207 private:
00208
00209 void genReport(cgiScript & script, Report_Parameters_form ¶m_form)
00210 {
00212 string path = "reportTemplates/" + report.getTemplatePath();
00213 string sql = report.getCompositeQuery();
00214
00216 sql = param_form.paramFilters( sql );
00217
00219 col_handling_rep rpt(script);
00220 rpt.Formatting_Modulus = report.Formatting_Modulus;
00221 rpt. Items_Per_Page = report.Items_Per_Page;
00222 rpt.loadTemplates(path);
00223
00224 rpt.addColumnHandlers( report.Id );
00225 if( args.count("doParamFilter") )
00226 rpt.doParamFilter = true;
00227 if( rpt.getData(sql) )
00228 {
00229 rpt.emitTop(report.Name);
00230 rpt.emitHeader();
00231 rpt.emitData();
00232
00233 ocString extra = param_form.filterList(path);
00234
00235 if( addPDFLink )
00236 {
00237 bool isParameterized = false;
00238 if( !isPDF )
00239 {
00240 extra += "<a href='/htmldoc.cgi" + scriptName;
00241 if( args.TheMap().size() > 0 )
00242 {
00243 extra += "?";
00244 for( queryStringMap::iterator pos = args.TheMap().begin();
00245 pos != args.TheMap().end();
00246 )
00247 {
00248 extra += pos->first.c_str();
00249 extra += "=";
00250 extra += pos->second.c_str();
00251 ++pos;
00252 if( pos != args.TheMap().end() )
00253 {
00254 ocString paramTest = pos->first;
00255 if( paramTest == "xmldirections" )
00256 {
00257 isParameterized = true;
00258 break;
00259 }
00260 extra += "&";
00261 }
00262 }
00263 }
00264 extra += "&isPDF=1'>[As PDF]</a>";
00265 if( isParameterized )
00266 {
00267 extra += " <a href='" + scriptName
00268 + "?" + qs
00269 + "'>[BACK]</a>";
00270 }
00271
00272 rpt.emitEnd(extra);
00273 }
00274 else
00275 {
00276
00277 rpt.emitEnd(extra);
00278 }
00279 }
00280 else
00281 {
00282 rpt.emitEnd(extra);
00283 }
00284 }
00285 else
00286 {
00287 rpt.emitTop(report.Name);
00288 rpt.emitEmptySet();
00289 rpt.emitEnd("<a href='javascript:window.close()'>[Quit]</a>");
00290 }
00291 }
00292 void paramForm(cgiScript & script, Report_Parameters_form & param_form)
00293 {
00294 script.openHeader("text/html");
00295 script.closeHeader();
00296
00297 if( ! pgTemplate.load("Templates/reportPane.htmp") )
00298 {
00299 script << "<h2>could not load Templates/reportPane.htmp</h2>" << endl;
00300 }
00301
00302 script << ocString(pgTemplate.getParagraph("top"))
00303 .replaceAll("$heading$","Reports");
00304
00305 if( !param_form.loadControlTemplates("Templates/repform.htmp") )
00306 {
00307 {
00308 script << "<h2>could not load Templates/repform.htmp</h2>" << endl;
00309 }
00310 }
00311 param_form.reportName = report.Name;
00312 param_form.form_display();
00313 ocString end = pgTemplate.getParagraph("bottom");
00314 script << end;
00315 }
00316
00317
00318 void noData( bool needScript = true )
00319 {
00320 if( needScript )
00321 {
00322 cgiScript script;
00323 }
00324 cgiHtml html;
00325 {
00326 cgiHead head;
00327 head << "<style>"
00328 "body {color:darkred;background:#ddd;font-size:12pt;border:"
00329 "8pt solid #fff;margin:0pt;padding:30pt;text-align:center;} "
00330 "h2 {color:#00a;background:transparent;}"
00331 "</style>" << endl;
00332 }
00333 {
00334 cgiBody body(" onLoad='window.focus()'");
00335 body << "<h2>Sorry!</h2>"
00336 "There is no data for the criteria that you have selected.<br>"
00337 "Please try again with different criteria <!--"
00338 << sql << "-->" << endl;
00339 }
00340 }
00341 void invalidLogin( void )
00342 {
00343 cgiScript script;
00344 cgiHtml html;
00345 {
00346 cgiBody bod;
00347 bod << "Invalid Login Detected!" << endl;
00348 }
00349 }
00350
00351
00352 void redirectToHTMLDoc( void )
00353 {
00354 cgiScript script( "text/html", false );
00355
00356 string redir = "/htmldoc.cgi";
00357 redir += scriptName;
00358 redir += "?isPDF=1";
00359 if( args.TheMap().size() > 0 )
00360 {
00361
00362 for( queryStringMap::iterator pos = args.TheMap().begin();
00363 pos != args.TheMap().end();
00364 )
00365 {
00366 redir += "&";
00367 redir += pos->first;
00368 redir += "=";
00369 redir += pos->second.c_str();
00370 ++pos;
00371 if( pos != args.TheMap().end() )
00372 {
00373 ocString paramTest = pos->first;
00374 if( paramTest == "xmldirections" )
00375 {
00376 break;
00377 }
00378 }
00379 }
00380 }
00381 script.Redirect( redir.c_str() );
00382 script.closeHeader();
00383 }
00390 void addListCriteria( Report_List & repList )
00391 {
00392 if( oLogin.fetchServices() )
00393 {
00394 serviceMap::iterator pos = oLogin.Services().find("ReportListing.meta");
00395 if( pos != oLogin.Services().end() )
00396 {
00397 openService & service = pos->second;
00398 string::size_type idx = service.xml_param1.find("<view>");
00399 if( idx != string::npos )
00400 {
00401 idx += 6;
00402 string::size_type idx2=service.xml_param1.find("</view>");
00403 if( idx2 != string::npos && idx2 > idx )
00404 {
00405 string reps = service.xml_param1.substr(idx, idx2-idx);
00406 repList.addedCriteria = " where r.Id in ( " + reps + " ) ";
00407 }
00408 }
00409 }
00410 }
00411 }
00412 void listReports( void )
00413 {
00414 cgiScript script( "text/html", false );
00415 Report_List mylist(script);
00416 script.closeHeader();
00417 pgTemplate.load("Templates/adminPane.htmp");
00418
00419 script << ocString(pgTemplate.getParagraph("top"))
00420 .replaceAll("$heading$","Report");
00421
00422 mylist.loadListTemplates("Templates/list.htmp");
00423
00424
00425 addListCriteria(mylist);
00426
00427 mylist.list_display();
00428
00429 ocString end = pgTemplate.getParagraph("bottom");
00430
00431 script << "<form id='uiForm' name='uiForm'><input type='hidden' id='xmldirections'></form>" << endl;
00432 script << end;
00433 }
00434 };
00435
00436 #endif
00437
00438
00439