public string ExtraValue(LangTextContentViewModel updatemodel, string FieldName) { if (string.IsNullOrWhiteSpace(FieldName)) { return(null); } FieldName = FieldName.ToLower(); string key = null; foreach (var langitem in updatemodel.Values) { foreach (var fielditem in langitem.Value) { if (fielditem.Key.ToLower() == FieldName) { key = fielditem.Value; break; } } } if (!string.IsNullOrWhiteSpace(key)) { // remove the key foreach (var langitem in updatemodel.Values) { List <string> keysToRemove = new List <string>(); foreach (var fielditem in langitem.Value) { if (fielditem.Key.ToLower() == FieldName) { keysToRemove.Add(fielditem.Key); } } foreach (var item in keysToRemove) { langitem.Value.Remove(item); } } } return(key); }
public Guid LangUpdate(ApiCall call) { var sitedb = call.WebSite.SiteDb(); LangTextContentViewModel updatemodel = call.Context.Request.Model as LangTextContentViewModel; Guid folderId = GetFolderId(call); var contenttype = sitedb.ContentTypes.GetByFolder(folderId); if (contenttype == null || folderId == default(Guid)) { return(default(Guid)); } Guid parentId = call.GetValue <Guid>("ParentId"); string userkey = ExtraValue(updatemodel, "userkey"); if (!string.IsNullOrEmpty(userkey)) { userkey = Kooboo.Sites.Contents.UserKeyHelper.ToSafeUserKey(userkey); } string stronline = ExtraValue(updatemodel, "online"); bool online = true; if (!string.IsNullOrEmpty(stronline) && stronline.ToLower() == "false") { online = false; } int sequence = 0; var strsequence = ExtraValue(updatemodel, "sequence"); if (!string.IsNullOrEmpty(strsequence)) { int.TryParse(strsequence, out sequence); } TextContent newcontent = sitedb.TextContent.Get(call.ObjectId); if (newcontent == null) { newcontent = new TextContent() { FolderId = folderId, ContentTypeId = contenttype.Id, UserKey = userkey, ParentId = parentId, Order = sequence }; if (!string.IsNullOrEmpty(userkey) && sitedb.TextContent.IsUserKeyExists(userkey)) { throw new Exception(Data.Language.Hardcoded.GetValue("UserKey has been taken", call.Context)); } } if (!string.IsNullOrEmpty(userkey) && newcontent.UserKey != userkey) { var existings = sitedb.TextContent.Get(userkey); if (existings != null && existings.Id != newcontent.Id) { throw new Exception(Data.Language.Hardcoded.GetValue("UserKey has been taken", call.Context)); } sitedb.TextContent.Delete(newcontent.Id); newcontent.UserKey = userkey; } newcontent.Online = online; newcontent.Order = sequence; newcontent.Embedded = updatemodel.Embedded; foreach (var item in contenttype.Properties.Where(o => !o.IsSystemField && !o.MultipleLanguage)) { var value = ExtraValue(updatemodel, item.Name); if (!string.IsNullOrWhiteSpace(value)) { newcontent.SetValue(item.Name, value, call.WebSite.DefaultCulture); } } foreach (var langDict in updatemodel.Values) { string lang = langDict.Key; foreach (var item in langDict.Value) { string value = item.Value == null ? string.Empty : item.Value.ToString(); newcontent.SetValue(item.Key, value, lang); } } // sitedb.TextContent.EusureNonLangContent(newcontent, contenttype); sitedb.TextContent.AddOrUpdate(newcontent, call.Context.User.Id); if (updatemodel.Categories.Count > 0) { foreach (var item in updatemodel.Categories) { sitedb.ContentCategories.UpdateCategory(newcontent.Id, item.Key, item.Value.ToList(), call.Context.User.Id); } } return(newcontent.Id); }