コード例 #1
0
        /// <summary>
        /// Hàm trợ giúp lấy mã của Category trong Tab hiện thời khi người sử dụng chọn xem một Category
        /// </summary>
        /// <returns>Mã tham chiếu đến Category hiện thời</returns>
        public static string GetCurrentCategoryID()
        {
            Cache cache = new Cache(HttpContext.Current.Application);
            // Biến lưu trữ mã của chuyên san hiện thời
            int    _intCurrentCategoryID = 0, _intCurrParent = 0;
            string _strCurrentTabRef = HttpContext.Current.Request.QueryString["TabRef"];

            if (HttpContext.Current.Request.QueryString["RealRef"] != null)
            {
                _strCurrentTabRef = HttpContext.Current.Request.QueryString["RealRef"];
            }
            string mrs = Config.GetPortalUniqueCacheKey() + "CurrentCategoryID_" + (_strCurrentTabRef == null ? "" : _strCurrentTabRef);
            string nrs = Config.GetPortalUniqueCacheKey() + "CurrentParentID_" + (_strCurrentTabRef == null ? "" : _strCurrentTabRef);

            // Tìm kiếm trong Cache
            if (cache[mrs] != null && cache[nrs] != null)
            {
                _intCurrentCategoryID = (int)cache[mrs];
                _intCurrParent        = (int)cache[nrs];
            }
            if (_intCurrentCategoryID > 0)
            {
                return(_intCurrParent + "," + _intCurrentCategoryID);
            }

            // Lấy thông tin về Tab hiện thời
            PortalDefinition.Tab _objCurrentTab = PortalDefinition.getTabByRef(_strCurrentTabRef);//PortalDefinition.GetCurrentTab();

            if (_objCurrentTab != null)
            {
                // Lấy mã tham chiếu của Tab
                string _strTabRef = _objCurrentTab.reference;
                string _strCategoryRef = "", _strParentRef = "";

                // Tìm vị trí dấu chấm cuối
                int _intLastDotPos = _strTabRef.LastIndexOf('.');

                if (_intLastDotPos > 0)
                {
                    // Cắt lấy phần tên đại diện trên URL của Category
                    _strCategoryRef = _objCurrentTab.reference.Substring(_intLastDotPos + 1);
                    _strParentRef   = _objCurrentTab.reference.Substring(0, _intLastDotPos);
                    // Lấy thông tin về Category đã chọn
                    CategoryRow _objCurrentCategory = null;
                    using (MainDB _objDB = new MainDB()) {
                        _objCurrentCategory = _objDB.CategoryCollection.GetRow("Cat_DisplayURL = '" + _strCategoryRef + "'");
                    }

                    if (_objCurrentCategory != null)
                    {
                        _intCurrentCategoryID = _objCurrentCategory.Cat_ID;
                        _intCurrParent        = _objCurrentCategory.Cat_ParentID;
                    }
                    else
                    {
                        _intLastDotPos = _strParentRef.LastIndexOf('.');
                        _strParentRef  = _strParentRef.Substring(_intLastDotPos + 1);
                        using (MainDB _objDB = new MainDB()) {
                            _objCurrentCategory = _objDB.CategoryCollection.GetRow("Cat_DisplayURL = '" + _strParentRef + "'");
                        }
                        if (_objCurrentCategory != null)
                        {
                            _intCurrentCategoryID = _objCurrentCategory.Cat_ID;
                            _intCurrParent        = _objCurrentCategory.Cat_ParentID;
                        }
                    }
                }
            }

            // Lưu mã của chuyên san hiện thời vào Cache
            cache[mrs] = _intCurrentCategoryID;
            cache[nrs] = _intCurrParent;
            // Trả về 0 nếu không tìm thấy Category
            return(_intCurrParent + "," + _intCurrentCategoryID);//0
        }