00001 #include "page_control.hpp"
00002 #include "ocTypes.h"
00003 #include "lookup.hpp"
00004 #include "../admin/Category.hpp"
00005 #include "page_enhancements.hpp"
00006 #include "shoppingCart.hpp"
00007 #include "../admin/ItemDetail.hpp"
00008 #include "../admin/Style.hpp"
00009 #include "../admin/Size_Price.hpp"
00010 #include "../admin/Promotion.hpp"
00011 #include "Session.hpp"
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 class virtualStore : public read_base
00025 {
00026 protected:
00027
00028 string c,p;
00029 identifier crtid;
00030 ocString sql;
00031
00032 public:
00033 double discount;
00034 bool freeShipping;
00035
00036 virtualStore():read_base(),crtid(0),
00037 discount(0.0),freeShipping(false)
00038 {
00039 ;
00040 }
00041 ~virtualStore()
00042 {
00043 ;
00044 }
00045 void setArgs( cgiScript & script )
00046 {
00047 cgiInput & inputArgs = script.ClientArguments();
00048 if( inputArgs.count("c") )
00049 {
00050 c = inputArgs["c"].c_str();
00051 }
00052 if( inputArgs.count("p") )
00053 {
00054 p = inputArgs["p"].c_str();
00055 }
00056 if( inputArgs.count("crtid") )
00057 {
00058 crtid = atoll(inputArgs["crtid"].c_str());
00059 }
00060
00061
00062 }
00063 bool baseCategoryFixup( cgiScript & script )
00064 {
00065 setArgs(script);
00066 if( c.length() == 0 && p.length() == 0 )
00067 {
00068
00069 sql = "select Id from Category where Base_URL = '";
00070 sql += baseUrl;
00071 sql += script.PathInfo().c_str();
00072 sql +="'";
00073
00074 if( rs.open(sql) )
00075 {
00076 c = rs.getField(0).format();
00077 rs.close();
00078 if( script.QueryString().length() == 0 )
00079 {
00080
00081 script.QueryString() = "c=";
00082 script.QueryString() += c.c_str();
00083 }
00084 }
00085 }
00086 }
00087
00088 void showCategories( cgiScript & script, ocString & mitem )
00089 {
00090 Category_Obj cat_obj;
00091
00092 if( c.length())
00093 {
00094
00095 cat_obj.key(atoll(c.c_str()));
00096
00097 if( cat_obj.get_data() )
00098 {
00099 sql = "select c.Id, c.Description, c.Display_Order, c.Display_Level ";
00100 sql += " from Category c where Display_Order > ";
00101 sql.append( cat_obj.Display_Order );
00102 sql += " order by c.Display_Order ";
00103 bool isExpanding = false;
00104
00105 bool opened = rs.open(sql);
00106 int iFirstLevel = -1;
00107
00108 while( opened )
00109 {
00110
00111 int iLevel = atol(rs.getField(3).format().c_str());
00112 if( iLevel <= cat_obj.Display_Level ) break;
00113 if( iFirstLevel == -1 ) iFirstLevel = iLevel;
00114 if( iFirstLevel == iLevel )
00115 {
00116
00117 string current_c = rs.getField(0).format();
00118
00119 string level=rs.getField(3).format();
00120 string classTag = "pcat"+level;
00121
00122 script << mitem.replace( "$c$", current_c.c_str() )
00123 .replace( "$id", current_c )
00124 .replace( "$cat", classTag )
00125 .replace( "$label", rs.getField(1).format().c_str() );
00126 }
00127 opened = rs.next();
00128
00129 }
00130 rs.close();
00131 }
00132 }
00133 }
00134
00135 void showProducts( page & pg, cgiScript & script, ocString & prodTemplate,
00136 ocString listTemplate, ocString & prodListWrap,
00137 string contentTag, ocString headingTemplate)
00138 {
00139 setArgs(script);
00140
00141 if( p.length() )
00142 {
00143 showProduct( script, prodTemplate );
00144 }
00145 else if( c.length() )
00146 {
00147 showProducts( pg, script, listTemplate, prodListWrap, headingTemplate );
00148 }
00149 else
00150 {
00151 pg.emitContent( contentTag, "$content" );
00152 }
00153 }
00154
00155 void showDesigner( cgiScript & script )
00156 {
00157 cgiTemplates design;
00158 if(design.load("Templates/designer.html"))
00159 {
00160 string Id = rs.getField(0).format();
00161 string Name = rs.getField(2).format();
00162 string Lines_Custom_Text = rs.getField(3).format();
00163
00164 cgiInput & inputArgs = script.ClientArguments();
00165 string sstyle = inputArgs["style"];
00166 string ssize = inputArgs["size"];
00167 string orientation = inputArgs["orientation"];
00168
00169 rs.close();
00170 ocString sql;
00171 Style_Obj style;
00172 style.key( atoll(sstyle.c_str()) );
00173 style.get_data();
00174
00175 Size_Price_Obj size;
00176 size.key( atoll(ssize.c_str()) );
00177 size.get_data();
00178
00179 ocString Real_Height, Real_Width, Design_No;
00180 if( orientation == "Horizontal" )
00181 {
00182 Real_Width.append( size.X * 12 );
00183 Real_Height.append( size.Y * 12 );
00184 }
00185 else
00186 {
00187 Real_Width.append( size.Y * 12 );
00188 Real_Height.append( size.X * 12 );
00189 }
00190 Design_No = style.Manufacturer_Design_No;
00191
00192
00193
00194
00195 sql = "select Id , Pixel_Height , Pixel_Width , "
00196 "Text_Top_Left_X , Text_Top_Left_Y , Text_Bot_Right_X , Text_Bot_Right_Y , "
00197 "Pattern_Image from Designer where Product_Id = ";
00198 sql += Id;
00199 sql += " and Size = " + ssize + " and Style = " + sstyle + " and Orientation = '" +
00200 orientation + "'";
00201 bool opened = rs.open(sql);
00202 if( opened )
00203 {
00204 ocString form = design.getParagraph("end");
00205 script << design.getParagraph("top");
00206 script << "var myDesigner = new designer(" + Lines_Custom_Text + ");" << endl;
00207
00208 script << "myDesigner.addDesign('" <<
00209 rs.getField("Id").format() << "', '" <<
00210 Design_No << "','" <<
00211 rs.getField("Pattern_Image").format() << "'," <<
00212 Real_Width << "," <<
00213 Real_Height << "," <<
00214 rs.getField("Pixel_Width").format() << "," <<
00215 rs.getField("Pixel_Height").format() << "," <<
00216 rs.getField("Text_Top_Left_X").format() << "," <<
00217 rs.getField("Text_Top_Left_Y").format() << "," <<
00218 rs.getField("Text_Bot_Right_X").format() << "," <<
00219 rs.getField("Text_Bot_Right_Y").format() << ");" << endl;
00220 rs.close();
00221
00222
00223
00224
00225
00226
00227 script << "myDesigner.addParameter( 'style' , '" << sstyle << "' );" << endl;
00228 script << "myDesigner.addParameter( 'size' , '" << ssize << "' );" << endl;
00229 script << "myDesigner.addParameter( 'orientation' , '" << orientation << "' );" << endl;
00230 script << "myDesigner.addParameter( 'p' , '" << Id << "' );" << endl;
00231
00232
00233 sql = "select Name, External_Id from "
00234 "Font f inner join Product_Fonts pf on f.Id = pf.Font where pf.Product = ";
00235 sql += Id;
00236 script << endl << "//" << sql << endl;
00237 opened = rs.open(sql);
00238 while(opened)
00239 {
00240 script << "myDesigner.addFont('"
00241 << rs.getField("Name").format() << "','"
00242 << rs.getField("External_Id").format() << "');" << endl;
00243 opened = rs.next();
00244 }
00245
00246 sql = "select Name,RGB,Image "
00247 "from Color c inner join Product_Backgrounds pb on pb.Color = c.Id where pb.Product = ";
00248 sql += Id;
00249 opened = rs.open(sql);
00250
00251 while(opened)
00252 {
00253 script << "myDesigner.addBackground('"
00254 << rs.getField("RGB").format()
00255 << "','"
00256 << rs.getField("Image").format()
00257 << "','"
00258 << rs.getField("Name").format()
00259 << "');" << endl;
00260
00261 opened = rs.next();
00262 }
00263
00264 sql = "select Name,RGB,Image "
00265 "from Color c inner join Product_Colors pc on pc.Color = c.Id where pc.Product = ";
00266 sql += Id;
00267 opened = rs.open(sql);
00268
00269 while(opened)
00270 {
00271 script << "myDesigner.addColor('" << rs.getField("RGB").format() << "','"
00272 << rs.getField("Image").format() << "','"
00273 << rs.getField("Name").format() << "');" << endl;
00274 opened = rs.next();
00275 }
00276 script << "myDesigner.setDefaults();";
00277 script << form.replaceAll("$id$",Id).replace("$label",Name.c_str());
00278 }
00279 }
00280 }
00281 void showProduct( cgiScript & script, ocString & prodTemplate )
00282 {
00283
00284
00285
00286
00287
00288
00289
00290 ocString productsSQL = "select p.Id, p.Product_Line, p.Name, p.Lines_Custom_Text, "
00291 "p.Designer, p.Description from Product p ";
00292 string criteria = " where p.Id = ";
00293 criteria += p;
00294 sql = productsSQL + criteria;
00295
00296
00297
00298
00299 bool opened = rs.open(sql);
00300 if( opened )
00301 {
00302 bool isDesignerMode = atol(rs.getField(4).format().c_str()) != 0;
00303
00304
00305 if( isDesignerMode && crtid == 0 )
00306 {
00307 showDesigner(script);
00308 }
00309 else
00310 {
00311
00312
00313 ShoppingCart crt;
00314 crt.key( crtid );
00315 if(crt.get_data() )
00316 {
00317
00318 xmlParser parser( crt.XML );
00319 parser.parse();
00320 node_vector & xnodes = parser.nodeList();
00321 ItemDetail details;
00322 details = xnodes.back();
00324 string Price;
00325 string Shipping;
00326 if( discount > 0.0 && discount < 100.0 )
00327 {
00328 money reducedPrice;
00329 reducedPrice.amount() = details.Price().amount();
00330 reducedPrice.amount() *= (100-discount);
00331 reducedPrice.amount() /=100.0;
00332 Price = "<del>";
00333 Price += details.Price().format("%n");
00334 Price += "</del> <ins>";
00335 Price += reducedPrice.format("%n");
00336 Price += "</ins>";
00337 details.Price().amount() = reducedPrice.amount();
00338 }
00339 else
00340 {
00341 Price = details.Price().format("%n");
00342 }
00343 if( freeShipping )
00344 {
00345 Shipping = "<del>";
00346 Shipping += details.Shipping().format("%n");
00347 Shipping += "</del> <ins>Free Shipping!</ins>";
00348 details.Shipping().amount() = 0.0;
00349 }
00350 else
00351 {
00352 Shipping = details.Shipping().format("%n");
00353 }
00354
00355
00356 script << prodTemplate.replace("$PRD$", rs.getField("Name").format())
00357 .replace("$IMG$", details.StyleImage())
00358 .replace("$TXT$", details.CustomText().replaceAll("\n","<br>\n"))
00359 .replace("$SZ$", details.strSize())
00360 .replace("$ORNT$", details.Orientation())
00361 .replace("$PRC$", Price )
00362 .replace("$SHP$", Shipping )
00363 .replace("$QNT$", details.strCount())
00364 .replace("$TOT$", details.Total().format("%n"));
00365
00366 }
00367 }
00368 rs.close();
00369 }
00370 }
00371 void showProducts( page & pg, cgiScript & script,
00372 ocString & listTemplate,
00373 ocString & prodListWrap,
00374 ocString & prodHeading )
00375 {
00376
00377
00378
00379
00380
00381 ocString productsSQL = "select p.Id, "
00382 "p.Description, "
00383 "p.Name, "
00384 "p.Lines_Custom_Text, "
00385 "p.Designer, "
00386 "pc.Category, "
00387 "pc.Leading_Text "
00388 "from Product p inner join Product_Categories pc on pc.Product = p.Id ";
00389 string criteria;
00390 criteria = " where pc.Category = ";
00391 criteria += c;
00392 string order = " order by pc.Position ";
00393 sql = productsSQL + criteria + order;
00394
00395 bool opened = rs.open(sql);
00396 int i=0;
00397 while( opened )
00398 {
00399 string PID = rs.getField(0).format().c_str();
00400 string Description = rs.getField(1).format().c_str();
00401 string Name = rs.getField(2).format();
00402 int LinesOfText = atol(rs.getField(3).format().c_str());
00403 bool Designer = rs.getField(4).format() != "0";
00404 string Category = rs.getField(5).format();
00405 string LeadingText = rs.getField(6).format();
00406 if( LeadingText.length() )
00407 {
00408 i=0;
00409 script << prodHeading.replace("$content",LeadingText.c_str());
00410 }
00411 script << listTemplate.replaceAll("$title",Name.c_str())
00412 .replaceAll("$description", Description.c_str());
00413 putForm( pg, script, PID, LinesOfText, Designer );
00414 i++;
00415 opened = rs.next();
00416 }
00417 }
00418
00419
00420
00421 void putForm( page & pg, cgiScript & script, string & PID, int LinesOfText, bool Designer )
00422 {
00423
00424 paragraphMap & paras = pg.get_template().getParagraphs();
00425 ocString prodstart;
00426 if( Designer ) prodstart = paras["dpushstart"];
00427 else prodstart = paras["prodstart"];
00428 ocString style = paras["style"];
00429 ocString style_end = paras["style_end"];
00430 ocString texthead = paras["texthead"];
00431 ocString textentry;
00432 if( !Designer ) textentry= paras["textentry"];
00433 ocString sizehead;
00434 if( Designer ) sizehead = paras["dpushsizehead"];
00435 else sizehead = paras["sizehead"];
00436 ocString sizeval = paras["sizeval"];
00437 ocString prodend;
00438 if( Designer ) prodend = paras["dpushend"];
00439 else prodend = paras["prodend"];
00440 string check;
00441
00442
00443 script << prodstart.replaceAll("$P$",PID.c_str());
00444
00445 ocString query = "select Id, Name, Image from Style where Product = " + PID;
00446
00447 quickQuery qqy;
00448 openRS & rs = qqy.getData ( query );
00449 if( qqy.opened )
00450 {
00451 bool first = true;
00452 do {
00453 if( first )
00454 {
00455 check="checked=\"yes_please\"";
00456 first = false;
00457 }
00458 else
00459 {
00460 check="";
00461 }
00462 script << style.replace("$STL$",rs.getField(0).format().c_str())
00463 .replace("$IMG$",rs.getField(2).format().c_str())
00464 .replace("$NAME$",rs.getField(1).format().c_str())
00465 .replace("$CHECK$",check.c_str());
00466 } while( rs.next() );
00467 }
00468 rs.close();
00469 script << style_end;
00470
00471 query = "select p.Id, p.X, p.Y, u.Abbreviation, p.Price, p.Max_Chars_X, Max_Chars_Y from Size_Price p "
00472 "inner join Product_Sizes ps on p.Id = ps.Size "
00473 "inner join Unit u on u.Id = p.Units "
00474 "where ps.Product = " + PID;
00475
00476
00477
00478 if( rs.open(query) )
00479 {
00480
00481 if( LinesOfText && !Designer )
00482 {
00483 script << texthead
00484 .replaceAll("$XMAX$",rs.getField("Max_Chars_X").format())
00485 .replaceAll("$YMAX$",rs.getField("Max_Chars_Y").format());
00486 for( int line=1; line <= LinesOfText; line++ )
00487 {
00488 ocString line_no;
00489 line_no.append(line);
00490 script << textentry.replace("$NUM$",line_no.c_str() ).replaceAll("$XMAX$",rs.getField("Max_Chars_X").format().c_str());
00491 }
00492 }
00493
00494 script << sizehead;
00495 bool first = true;
00496 do {
00497 if( first )
00498 {
00499 check="checked=\"yes_please\"";
00500 first = false;
00501 }
00502 else
00503 {
00504 check="";
00505 }
00506 money mny = atof(rs.getField("Price").format().c_str());
00507 script << sizeval.replace("$ID$",rs.getField("Id").format().c_str())
00508 .replace("$X$",rs.getField("X").format().c_str())
00509 .replace("$Y$",rs.getField("Y").format().c_str())
00510 .replaceAll("$XMAX$",rs.getField("Max_Chars_X").format().c_str())
00511 .replaceAll("$YMAX$",rs.getField("Max_Chars_Y").format().c_str())
00512 .replaceAll("$U$",rs.getField("Abbreviation").format().c_str())
00513 .replace("$AMT$",mny.format("%n").c_str())
00514 .replace("$CHECK$",check.c_str());
00515 } while( rs.next() );
00516 }
00517
00518
00519 script << prodend;
00520
00521 }
00522 };
00523
00524 class detail_functor: public base_functor
00525 {
00526 protected:
00527 cgiScript & script;
00528 virtualStore store;
00529 Promotion_Obj pro;
00530 public:
00531 detail_functor(page & ipg,cgiScript & scriptIn):base_functor(ipg),script(scriptIn)
00532 {
00533
00534 Session_Obj sess(false);
00535 string proId = sess.GetData( "Promotion");
00536 if( proId.length() )
00537 {
00538 pro.key( atoll(proId.c_str() ) );
00539 if( pro.get_data() )
00540 {
00541 store.discount = pro.Percent_Discount;
00542 store.freeShipping = pro.Free_Shipping;
00543 }
00544 }
00545 store.baseCategoryFixup( script );
00546 }
00547 virtual ~detail_functor(){;}
00548
00549 virtual bool operator()( xmlNode & node )
00550 {
00551 bool bRet = false;
00552
00553 ocString defContentName = getXmlAttribute(node,"items");
00554 ocString prodListName = getXmlAttribute(node,"products");
00555 ocString prodListWrapName = getXmlAttribute(node,"productsWrapped");
00556 ocString prodHeading = getXmlAttribute(node,"productsWrapped");
00557 ocString prodDetailName = getXmlAttribute(node,"aproduct");
00558 string heading = getXmlAttribute(node,"heading");
00559
00560 if( prodDetailName.length() )
00561 {
00562 ocString prodDetail = pg.paragraph_string(prodDetailName);
00563 ocString prodList = pg.paragraph_string(prodListName);
00564 ocString prodListWrap = pg.paragraph_string(prodListWrapName);
00565 ocString pHeading = pg.paragraph_string(heading);
00566 store.showProducts( pg, script, prodDetail, prodList, prodListWrap, defContentName, pHeading);
00567 bRet = true;
00568 }
00569 else
00570 {
00571 script << "no items" << endl;
00572 }
00573 return bRet;
00574 }
00575 };
00576
00577 class index_functor: public base_functor
00578 {
00579 protected:
00580 cgiScript & script;
00581 virtualStore store;
00582 public:
00583 index_functor(page & ipg,cgiScript & scriptIn):base_functor(ipg),script(scriptIn){;}
00584 virtual ~index_functor(){;}
00585
00586 virtual bool operator()( xmlNode & node )
00587 {
00588 bool bRet = false;
00589
00590 string mitemName = getXmlAttribute(node,"navitems");
00591
00592 if( mitemName.length() > 0 )
00593 {
00594 ocString mitem = pg.paragraph_string(mitemName);
00595 store.showCategories( script, mitem );
00596 bRet = true;
00597 }
00598 else
00599 {
00600 script << "no items" << endl;
00601 }
00602 return bRet;
00603 }
00604
00605 bool baseCategoryFixup( void )
00606 {
00607 store.baseCategoryFixup( script );
00608 }
00609 };
00610
00611 #include "read_write_base.cpp"