00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef Shopping_Cart_hpp
00018 #define Shopping_Cart_hpp
00019
00020 #include "connectstring"
00021 #include "read_write_base.hpp"
00022 #include "cgiClass.h"
00023 #include "cgiCookie.h"
00024 #include "ocXML.h"
00025 #include "ocString.h"
00026
00027 class ShoppingCart: public read_write_base
00028 {
00029 protected:
00030 changeMap chMap;
00031 public:
00032 identifier Id;
00033 string XML;
00034 time_date Last_Access;
00035 bool Checked_Out;
00036
00037
00038 ShoppingCart():read_write_base(),Id(0LL),Checked_Out(false)
00039 {
00040 data_name("ShoppingCart");
00041
00042 addDXMap( new llongXfer("Id", &Id) );
00043 addDXMap( new stringXfer("XML", &XML) );
00044 addDXMap( new time_dateXfer("Last_Access", &Last_Access ) );
00045 addDXMap( new boolXfer("Checked_Out", &Checked_Out) );
00046
00047 chMap["XML"]="XML";
00048 chMap["Last_Access"]="Last_Access";
00049 chMap["Checked_Out"]="Checked_Out";
00050
00051 Last_Access.now();
00052 }
00053
00054 virtual ~ShoppingCart()
00055 {
00056 ;
00057 }
00058 };
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 class uiShoppingCart: public ShoppingCart
00079 {
00080 private:
00081 bool addNewCart( void )
00082 {
00083
00084 db_action( "i", chMap );
00085 if( Id )
00086 {
00087
00088 cgiCookie cookie;
00089 ocString tempId;
00090 tempId.append(Id);
00091 cookie.setPath("/");
00092 cookie.set("crtid",tempId.c_str());
00093 }
00094 return true;
00095 }
00096
00097 public:
00098
00099 cgiScript & script;
00100 cgiCookie cookie;
00101 int items;
00102
00103 uiShoppingCart(cgiScript & in ): ShoppingCart(),script(in),items(0)
00104 {
00105 string id = cookie.get("crtid");
00106 if( id.length() == 0 )
00107 {
00108
00109 if( script.ClientArguments()["crtid"].length() )
00110 {
00111 id = script.ClientArguments()["crtid"].c_str();
00112 }
00113 }
00114 if( id.length() == 0 )
00115 {
00116
00117 addNewCart();
00118 }
00119 else
00120 {
00121
00122 key( atoll( id.c_str() ) );
00123 get_data();
00124
00125
00126
00127
00128 if(Checked_Out)
00129 {
00130 cookie.setPath("/");
00131 cookie.set("crtid","");
00132 }
00133 }
00134 }
00135
00136 virtual ~uiShoppingCart()
00137 {
00138 ;
00139 }
00140
00141
00142 void stats( void )
00143 {
00144 items = 0;
00145 xmlParser parser( XML );
00146 parser.parse();
00147 node_vector & xnodes = parser.nodeList();
00148 int i;
00149 for(i=0;i<xnodes.size();i++)
00150 {
00151 xmlNode & node = xnodes[i];
00152 if( node.name == "product" )
00153 {
00154
00155
00156 items += atol(node.attr["count"].c_str());
00157 }
00158 }
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 bool UpdateProduct( string productId,
00175 string productCount = "1",
00176 string productOptions = "",
00177 string productData = "",
00178 int node=0 )
00179 {
00180 bool bret = false;
00181
00182 if( Id )
00183 {
00184 int prodCount = atol(productCount.c_str());
00185
00186
00187 xmlParser parser( XML );
00188 parser.parse();
00189
00190
00191 Last_Access.now();
00192
00193
00194 node_vector::iterator & xIt = parser.findFirstNodeByAttribute("id",productId);
00195 if( xIt != parser.nodeList().end() )
00196 {
00197
00198 if( node > 0 )
00199 {
00200
00201
00202 for( int idx=1; idx<=node; ++idx)
00203 {
00204
00205 node_vector::iterator & xIt2 =
00206 parser.findNextNodeByAttribute("id",productId);
00207
00208 if( xIt2 != parser.nodeList().end() )
00209 {
00210 xIt=xIt2;
00211 }
00212 }
00213 }
00214 if( prodCount == 0 )
00215 {
00216 parser.nodeList().erase(xIt);
00217 }
00218 else
00219 {
00220 ocString newCount;
00221 newCount.append(prodCount);
00222 xmlNode & xmlItem = (*xIt);
00223 xmlItem.attr["count"] = newCount;
00224 if( productOptions.length() )
00225 {
00226 xmlItem.attr["options"] = productOptions;
00227 }
00228 }
00229 XML = parser.emit();
00230 bret=db_action( "u", chMap );
00231 }
00232 }
00233 return bret;
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 bool AlwaysAddProduct( string productId,
00247 string productCount = "1",
00248 string productOptions = "",
00249 string productData = "",
00250 int stock=1 )
00251 {
00252 bool bret = false;
00253
00254 if( Id )
00255 {
00256 int prodCount = atol(productCount.c_str());
00257
00258
00259 xmlParser parser( XML );
00260 parser.parse();
00261
00262
00263 Last_Access.now();
00264
00265
00266
00267 if(script.ClientArguments().count("stock"))
00268 stock = atol(script.ClientArguments()["stock"].c_str());
00269
00270 xmlNode xmlItem;
00271 xmlItem.name="product";
00272 xmlItem.monopole=true;
00273 xmlItem.attr["id"]=productId;
00274 if( productOptions.length() )
00275 {
00276 xmlItem.attr["options"] = productOptions;
00277 }
00278 if( productData.length() )
00279 {
00280 xmlItem.data = productData;
00281 xmlItem.monopole = false;
00282 }
00283
00284
00285 int iCan = prodCount<stock?prodCount:stock;
00286 ocString canCount;
00287 canCount.append(iCan);
00288 xmlItem.attr["count"] = canCount;
00289
00290
00291 if( iCan > 0 ) parser.addNode( xmlItem );
00292 XML = parser.emit();
00293 bret = db_action( "u", chMap );
00294 }
00295 return bret;
00296 }
00308 bool AddProduct( string productId,
00309 string productCount = "1",
00310 string productOptions = "",
00311 string productData = "",
00312 int stock=1 )
00313 {
00314 bool bret = false;
00315
00316 if( Id )
00317 {
00318
00319 xmlParser parser( XML );
00320 parser.parse();
00321
00322
00323 node_vector::iterator & xIt = parser.findFirstNodeByAttribute("id",productId);
00324 if( xIt != parser.nodeList().end() )
00325 {
00326
00327 Last_Access.now();
00328 int prodCount = atol(productCount.c_str());
00329
00330
00331 if(script.ClientArguments().count("stock"))
00332 stock = atol(script.ClientArguments()["stock"].c_str());
00333
00334 int existingCount = atol( (*xIt).attr["count"].c_str() );
00335 existingCount += prodCount;
00336
00337
00338 if( existingCount > stock )
00339 {
00340 existingCount = stock;
00341 }
00342 ocString newCount;
00343 newCount.append(existingCount);
00344 (*xIt).attr["count"] = newCount;
00345 if( productOptions.length() )
00346 {
00347 (*xIt).attr["options"] = productOptions;
00348 }
00349 if( productData.length() )
00350 {
00351 (*xIt).data = productData;
00352 (*xIt).monopole = false;
00353
00354 }
00355 XML = parser.emit();
00356 bret = db_action( "u", chMap );
00357 }
00358 else
00359 {
00360
00361 AlwaysAddProduct( productId, productCount, productOptions, productData, stock );
00362 }
00363
00364 }
00365 return bret;
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375 bool RemoveProduct( string productId )
00376 {
00377 bool bret=false;
00378
00379 if( Id )
00380 {
00381
00382 xmlParser parser( XML );
00383 parser.parse();
00384
00385
00386 Last_Access.now();
00387
00388 node_vector::iterator & xIt = parser.findFirstNodeByAttribute("id",productId);
00389 if( xIt != parser.nodeList().end() )
00390 {
00391 script << " found node <br>" << endl;
00392 int existingCount = atol((*xIt).attr["count"].c_str());
00393 if( existingCount > 1 )
00394 {
00395 script << " decrement count <br>" << endl;
00396
00397 ocString newCount;
00398 newCount.append((existingCount-1));
00399 (*xIt).attr["count"] = newCount;
00400 }
00401 else
00402 {
00403
00404 parser.nodeList().erase(xIt);
00405 }
00406 }
00407 XML = parser.emit();
00408 bret=db_action( "u", chMap );
00409 }
00410 return bret;
00411 }
00412
00413
00414 bool CheckOut( void )
00415 {
00416
00417 Checked_Out = true;
00418 db_action( "u", chMap );
00419 }
00420
00421 bool CancelOrder( void )
00422 {
00423
00424 XML = "";
00425 db_action( "u", chMap );
00426 }
00427 };
00428
00429 #endif
00430