CartHelper.cs :  » Content-Management-Systems-CMS » mojoPortal » WebStore » UI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » mojoPortal 
mojoPortal » WebStore » UI » CartHelper.cs
#region using statementsSystem;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using mojoPortal.Web;
using mojoPortal.Business;
using WebStore.Business;
using mojoPortal.Web.Framework;

#endregion

namespace WebStore.UI{
    /// <summary>
    /// Author:          Joe Audette
    /// Created:        2007-03-06
    /// Last Modified:      2008-03-09
    /// 
    /// The use and distribution terms for this software are covered by the 
    /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
    /// which can be found in the file CPL.TXT at the root of this distribution.
    /// By using this software in any fashion, you are agreeing to be bound by 
    /// the terms of this license.
    ///
    /// You must not remove this notice, or any other, from this software.
    /// 
    /// </summary>
    public class CartHelper
    {

        public static Cart GetCart(Guid storeGuid)
        {
            

            if (HttpContext.Current != null)
            {
                string cartKey = "cart" + storeGuid.ToString();
                if (HttpContext.Current.Items[cartKey] != null)
                {
                    return (Cart)HttpContext.Current.Items[cartKey];
                }
                else
                {
                    if (UserHasCartCookie(storeGuid))
                    {
                        string cartCookie = GetCartCookie(storeGuid);
                        if (cartCookie.Length == 36)
                        {
                            Guid cartGuid = new Guid(cartCookie);
                            Cart cart = new Cart(cartGuid);
                            if (!cart.Exists)
                            {
                                return CreateCartAndSetCookie(storeGuid);
                            }
                            HttpContext.Current.Items[cartKey] = cart;
                            return cart;

                        }
                        else
                        {
                            // cookie is invalid
                            return CreateCartAndSetCookie(storeGuid);
                        }
                    }
                    else
                    {
                        // TODO: handle use case where user adds to cart on 1 machine
                        // then comes back to site on another machine and has no cart cookie
                        // look for a cart that has the userguid, 
                        // if found set cookie for that cart

                        // new cart
                        return CreateCartAndSetCookie(storeGuid);

                    }

                }

            }

            return null;


        }

        
        private static Cart CreateCartAndSetCookie(Guid storeGuid)
        {
            if (storeGuid == Guid.Empty) return null;

            string cartKey = "cart" + storeGuid.ToString();
            Cart cart = new Cart();
            cart.StoreGuid = storeGuid;
            if (
                (HttpContext.Current != null)
                && (HttpContext.Current.Request.IsAuthenticated)
                )
            {
                SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
                cart.UserGuid = siteUser.UserGuid;
                cart.LoadExistingUserCartIfExists();

            }
            cart.Save();

            SetCartCookie(storeGuid, cart.CartGuid);
            HttpContext.Current.Items[cartKey] = cart;

            return cart;


        }


        public static bool UserHasCartCookie(Guid storeGuid)
        {
            if (storeGuid == Guid.Empty) return false;
            string cartKey = "cart" + storeGuid.ToString();

            return CookieHelper.CookieExists(cartKey);

        }

        public static void SetCartCookie(Guid storeGuid, Guid cartGuid)
        {
            if ((storeGuid != Guid.Empty) && (cartGuid != Guid.Empty))
            {
                string cartKey = "cart" + storeGuid.ToString();

                // TODO encrypt, sign?

                CookieHelper.SetPersistentCookie(cartKey, cartGuid.ToString());


            }

        }

        public static void ClearCartCookie(Guid storeGuid)
        {
            if (storeGuid != Guid.Empty) 
            {
                string cartKey = "cart" + storeGuid.ToString();

                // TODO encrypt, sign?

                CookieHelper.SetPersistentCookie(cartKey, string.Empty);


            }

        }

        public static string GetCartCookie(Guid storeGuid)
        {
            if (storeGuid == Guid.Empty) return string.Empty;

            string cartKey = "cart" + storeGuid.ToString();

            // TODO: decrypt and verify?

            return CookieHelper.GetCookieValue(cartKey);


        }

        public static void InitializeOrderInfo(Cart cart, SiteUser siteUser)
        {
            cart.OrderInfo.CustomerName = siteUser.Name;
            cart.OrderInfo.CustomerEmail = siteUser.Email;
            cart.OrderInfo.Save();

        }

    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.