Пример #1
0
 public TextContentObject(Kooboo.Sites.Contents.Models.TextContent siteobject, RenderContext context)
 {
     this.context     = context;
     this.Culture     = context.Culture;
     this.TextContent = siteobject;
     this.siteDb      = context.WebSite.SiteDb();
 }
Пример #2
0
        public void Add(object SiteObject)
        {
            var sitedb = this.context.WebSite.SiteDb();

            var data = kHelper.GetData(SiteObject);

            var culture = this.context.Culture;

            Kooboo.Sites.Contents.Models.TextContent content = new Kooboo.Sites.Contents.Models.TextContent();

            ContentType   type   = null;
            ContentFolder folder = null;
            Dictionary <string, string> initValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);


            foreach (var item in data)
            {
                var lower = item.Key.ToLower();

                if (lower == "userkey")
                {
                    content.UserKey = item.Value.ToString();
                    continue;
                }
                else if (lower == "id")
                {
                    if (Guid.TryParse(lower, out Guid Id))
                    {
                        content.Id = Id;
                        continue;
                    }
                }
                else if (lower == "Order" || lower == "sequence")
                {
                    int value;
                    if (int.TryParse(item.Value.ToString(), out value))
                    {
                        content.Order = value;
                        continue;
                    }
                }
                else if (lower == "contenttype")
                {
                    type = sitedb.ContentTypes.Get(lower);
                    if (type != null)
                    {
                        content.ContentTypeId = type.Id;
                        folder = sitedb.ContentFolders.All().Find(o => o.ContentTypeId == type.Id);
                        if (folder != null)
                        {
                            content.FolderId = folder.Id;
                            continue;
                        }
                    }
                }
                else if (lower == "contentfolder" || lower == "folder")
                {
                    folder = sitedb.ContentFolders.Get(item.Value.ToString());
                    if (folder != null)
                    {
                        content.FolderId      = folder.Id;
                        content.ContentTypeId = folder.ContentTypeId;
                        continue;
                    }
                }

                else if (lower == "online")
                {
                    if (bool.TryParse(lower, out bool isonline))
                    {
                        content.Online = isonline;
                        continue;
                    }
                }

                // content.SetValue(item.Key, item.Value.ToString(), culture);

                initValues.Add(item.Key, item.Value.ToString());
            }

            if (content.ContentTypeId == default(Guid) && content.FolderId == default(Guid))
            {
                throw new Exception(Kooboo.Data.Language.Hardcoded.GetValue("Content folder undefined"));
            }

            if (content.FolderId == default(Guid))
            {
                var folders = sitedb.ContentFolders.Query.Where(o => o.ContentTypeId == content.ContentTypeId).FirstOrDefault();
                if (folders != null)
                {
                    content.ContentTypeId = folders.ContentTypeId;
                }
                else
                {
                    throw new Exception(Kooboo.Data.Language.Hardcoded.GetValue("Content folder not found"));
                }
            }

            // prepare and set values...
            if (type == null && folder != null)
            {
                type = sitedb.ContentTypes.Get(folder.ContentTypeId);
            }

            PrepareSetValues(initValues, content, culture, type, folder, sitedb);

            sitedb.TextContent.AddOrUpdate(content);
        }