public static CanvasModel GetObjectByPage(IContent node) { try { string json = node.GetValue<string>("canvas"); var model = new CanvasModel(); if (!string.IsNullOrEmpty(json)) { model = JsonConvert.DeserializeObject<CanvasModel>(json); } return model; } catch (Exception ex) { Log.Error("Canvas error on GetObjectByPage in Repository.", ex); return null; } }
public static CanvasModel GetObjectByNode(IPublishedContent node) { try { var model = new CanvasModel(); if (node.HasProperty("canvas") && node.HasValue("canvas")) { string json = node.GetPropertyValue<string>("canvas"); model = JsonConvert.DeserializeObject<CanvasModel>(json); } return model; } catch (Exception ex) { Log.Error("Canvas error on GetObjectByNode in Repository.", ex); return null; } }
public static CanvasView Get(string alias, bool isAuthenticated) { try { var View = new CanvasView(); int pageId = UmbracoContext.Current.PageId.Value; if (isAuthenticated) { var cs = ApplicationContext.Current.Services.ContentService; var page = cs.GetById(pageId); var model = Repository.GetObjectById(pageId); // Find area recurcivly if (model.Areas.All(x => x.Alias != alias)) { // Did not find area // Check if there is a root area, create it if not. Log.Info("Canvas - Did not find area, try to create it: " + alias); var area = new CanvasArea(); area.Alias = alias; if (model.Areas.Any()) { // Append new model.Areas.Add(area); Repository.SaveJson(model, pageId); page = cs.GetById(pageId); } else { // Create new var areaModel = new CanvasModel(); areaModel.Areas.Add(area); Repository.SaveJson(areaModel, pageId); page = cs.GetById(pageId); } } View.page = page; View.viewName = "/Views/Canvas/Backend.cshtml"; } else { View.node = UmbracoContext.Current.ContentCache.GetById(pageId); View.viewName = "/Views/Canvas/FrontEnd.cshtml"; } View.alias = alias; return View; } catch (Exception ex) { Log.Error("Canvas error on Get in ViewHelper.", ex); return null; } }
public JsonResult AddControl(string areaAlias, string controlType, int pageId) { var controlId = Guid.NewGuid(); string columns = "6:medium:;6:medium:"; var model = Repository.GetObjectById(pageId); if (model != null && !string.IsNullOrEmpty(controlType) && !string.IsNullOrEmpty(areaAlias)) { // Find Area In Grid/Section or Root foreach (var area in model.Areas) { FindArea(area, areaAlias); } if (AreaResult != null) { var control = new CanvasControl { ControlID = controlId, Type = controlType }; // If control is grid then create area inside it if (controlType == "Grid") { control.Columns = columns; int p = 0; foreach (var column in columns.Split(';').Where(x => !string.IsNullOrEmpty(x))) { string col = column.Split(':')[0]; var area = new CanvasArea {Alias = "grid-" + col + "-" + controlId.ToString() + "-" + p}; control.Areas.Add(area); p++; } } // If control is section then create area inside it if (controlType == "Section") { var area = new CanvasArea {Alias = "section-" + controlId.ToString()}; control.Areas.Add(area); } AreaResult.Controls.Add(control); } // Convert model back to json for saving Repository.SaveJson(model, pageId); } else { // If no area is found, create it var m = new CanvasModel(); var area = new CanvasArea {Alias = areaAlias}; var control = new CanvasControl { ControlID = controlId, Type = controlType }; area.Controls.Add(control); m.Areas.Add(area); Repository.SaveJson(m, pageId); } return Json(new { success = true, controlType = controlType, controlId = controlId.ToString(), pageId = pageId }); }
public static bool SaveJson(CanvasModel model, int pageId) { try { var node = cs.GetById(pageId); var newJson = JsonConvert.SerializeObject(model, Formatting.None); node.SetValue("canvas", newJson.ToString()); var currentUser = Authorize.GetCurrentUser(); cs.Save(node, currentUser.Id); return true; } catch (Exception ex) { Log.Error("Canvas error on SaveJson in Repository.", ex); return false; } }