public static List <CustomBlock> GetAll(PortalSettings PortalSettings)
            {
                List <CustomBlock> CustomBlocks = BlockFactory.GetAll(PortalSettings.PortalId).Where(c => c.Locale == null).ToList();
                string             Locale       = PageManager.GetCultureCode(PortalSettings);

                if (!string.IsNullOrEmpty(Locale))
                {
                    List <CustomBlock> CustomBlocksByLocale = BlockFactory.GetAll(PortalSettings.PortalId).Where(c => c.Locale == Locale).ToList();
                    if (CustomBlocksByLocale == null || CustomBlocksByLocale.Count <= 0)
                    {
                        return(CustomBlocks);
                    }
                    else
                    {
                        foreach (CustomBlock item in CustomBlocks)
                        {
                            if (CustomBlocksByLocale.Where(c => c.Guid.ToLower() == item.Guid.ToLower()).FirstOrDefault() == null)
                            {
                                CustomBlocksByLocale.Add(item);
                            }
                        }
                        return(CustomBlocksByLocale);
                    }
                }
                else
                {
                    return(CustomBlocks);
                }
            }
            public static CustomBlock GetByLocale(int PortalID, string GUID, string Locale)
            {
                CustomBlock cb = BlockFactory.GetAll(PortalID).Where(b => b.Guid.ToLower() == GUID.ToLower() && b.Locale == Locale).FirstOrDefault();

                if (cb == null)
                {
                    cb = BlockFactory.GetAll(PortalID).Where(b => b.Guid.ToLower() == GUID.ToLower() && b.Locale == null).FirstOrDefault();
                }

                return(cb);
            }
            public static dynamic Add(PortalSettings PortalSettings, CustomBlock CustomBlock, int ForceCount = 0)
            {
                dynamic Result = new ExpandoObject();

                try
                {
                    if (BlockFactory.Get(PortalSettings.PortalId, CustomBlock.Name) == null)
                    {
                        if (ForceCount == 0)
                        {
                            CustomBlock.Guid = Guid.NewGuid().ToString().ToLower();
                        }

                        CustomBlock.PortalID  = PortalSettings.PortalId;
                        CustomBlock.CreatedBy = PortalSettings.UserId;
                        CustomBlock.UpdatedBy = PortalSettings.UserId;
                        CustomBlock.CreatedOn = DateTime.UtcNow;
                        CustomBlock.UpdatedOn = DateTime.UtcNow;
                        CustomBlock.Locale    = PageManager.GetCultureCode(PortalSettings);
                        BlockFactory.AddUpdate(CustomBlock);
                        CustomBlock cb = BlockFactory.GetAll(PortalSettings.PortalId).Where(b => b.Guid.ToLower() == CustomBlock.Guid.ToLower() && b.Locale == null).FirstOrDefault();
                        if (cb == null)
                        {
                            cb        = CustomBlock;
                            cb.Locale = null;
                            cb.ID     = 0;
                            BlockFactory.AddUpdate(cb);
                        }
                        Result.Status = "Success";
                        Result.Guid   = CustomBlock.Guid;
                    }
                    else
                    {
                        if (ForceCount > 0)
                        {
                            CustomBlock.Name = CustomBlock.Name + ForceCount;
                            ForceCount++;
                            Result = Add(PortalSettings, CustomBlock, ForceCount);
                        }
                        else
                        {
                            Result.Status = "Exist";
                        }
                    }
                }
                catch (Exception ex)
                {
                    Result.Status = ex.Message.ToString();
                    DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                }
                return(Result);
            }
            public static dynamic Edit(PortalSettings PortalSettings, CustomBlock CustomBlock)
            {
                dynamic Result = new ExpandoObject();

                try
                {
                    CustomBlock cb = BlockFactory.GetAll(PortalSettings.PortalId).Where(b => b.Guid.ToLower() == CustomBlock.Guid.ToLower() && b.Locale == PageManager.GetCultureCode(PortalSettings)).FirstOrDefault();
                    if (cb == null)
                    {
                        cb = BlockFactory.GetAll(PortalSettings.PortalId).Where(b => b.Guid.ToLower() == CustomBlock.Guid.ToLower() && b.Locale == null).FirstOrDefault();
                        if (cb != null)
                        {
                            cb.Locale = PageManager.GetCultureCode(PortalSettings);
                            cb.ID     = 0;
                            BlockFactory.AddUpdate(cb);
                        }
                    }
                    if (cb != null)
                    {
                        cb.Name     = CustomBlock.Name;
                        cb.Category = CustomBlock.Category;

                        if (CustomBlock.IsGlobal && !string.IsNullOrEmpty(CustomBlock.Html))
                        {
                            HtmlDocument html = new HtmlDocument();
                            html.LoadHtml(CustomBlock.Html);
                            IEnumerable <HtmlNode> query = html.DocumentNode.Descendants("div");
                            foreach (HtmlNode item in query.ToList())
                            {
                                if (item.Attributes.Where(a => a.Name == "dmid").FirstOrDefault() != null && item.Attributes.Where(a => a.Name == "mid").FirstOrDefault() != null && !string.IsNullOrEmpty(item.Attributes.Where(a => a.Name == "mid").FirstOrDefault().Value))
                                {
                                    int        mid   = int.Parse(item.Attributes.Where(a => a.Name == "mid").FirstOrDefault().Value);
                                    ModuleInfo minfo = ModuleController.Instance.GetModule(mid, Null.NullInteger, false);
                                    if (minfo != null && !minfo.AllTabs)
                                    {
                                        minfo.AllTabs = true;
                                        ModuleController.Instance.UpdateModule(minfo);
                                        List <TabInfo> listTabs = TabController.GetPortalTabs(minfo.PortalID, Null.NullInteger, false, true);
                                        foreach (TabInfo destinationTab in listTabs)
                                        {
                                            ModuleInfo module = ModuleController.Instance.GetModule(minfo.ModuleID, destinationTab.TabID, false);
                                            if (module != null)
                                            {
                                                if (module.IsDeleted)
                                                {
                                                    ModuleController.Instance.RestoreModule(module);
                                                }
                                            }
                                            else
                                            {
                                                if (!PortalSettings.Current.ContentLocalizationEnabled || (minfo.CultureCode == destinationTab.CultureCode))
                                                {
                                                    ModuleController.Instance.CopyModule(minfo, destinationTab, minfo.PaneName, true);
                                                }
                                            }
                                        }
                                    }
                                    item.InnerHtml = "<div vjmod=\"true\"><app id=\"" + mid + "\"></app>";
                                }
                                else if (item.Attributes.Where(a => a.Name == "data-block-type").FirstOrDefault() != null)
                                {
                                    if (item.Attributes.Where(a => a.Name == "data-block-type").FirstOrDefault().Value.ToLower() != "global")
                                    {
                                        item.InnerHtml = item.Attributes.Where(a => a.Name == "data-block-type").FirstOrDefault().Value;
                                    }
                                }
                            }
                            CustomBlock.Html = html.DocumentNode.OuterHtml;
                        }

                        cb.Html      = CustomBlock.Html;
                        cb.Css       = CustomBlock.Css;
                        cb.IsGlobal  = CustomBlock.IsGlobal;
                        cb.UpdatedBy = PortalSettings.Current.UserId;
                        cb.UpdatedOn = DateTime.UtcNow;
                        BlockFactory.AddUpdate(cb);
                        Result.Status      = "Success";
                        Result.Guid        = cb.Guid;
                        Result.CustomBlock = cb;
                    }
                    else
                    {
                        Result.Status = "Not exist";
                    }
                }
                catch (Exception ex)
                {
                    Result.Status = ex.Message.ToString();
                    DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                }
                return(Result);
            }