// // ==================================================================================================== /// <summary> /// Populate the view model from the entity model /// </summary> /// <param name="cp"></param> /// <param name="settings"></param> /// <returns></returns> public static AddonMockViewModel create(CPBaseClass cp, AddonMockDataModel settings) { try { // // -- base fields var result = create <AddonMockViewModel>(cp, settings); // // -- custom result.jsonData = settings.jsondata; var layout = Contensive.Models.Db.DbBaseModel.create <Contensive.Models.Db.LayoutModel>(cp, settings.layoutid); if (layout == null) { throw new ArgumentException("Layout not found."); } result.layout = layout.layout.content; if (string.IsNullOrEmpty(result.layout)) { throw new ArgumentException("Layout is empty."); } // return(result); } catch (Exception ex) { cp.Site.ErrorReport(ex); throw; } }
// // ==================================================================================================== // public override object Execute(CPBaseClass CP) { // // -- read instanceId, guid created uniquely for this instance of the addon on a page var result = string.Empty; AddonMockDataModel settings = null; try { // // -- this is a dev tool -- catch excerptions and display // -- edit tool is outside to let them edit content on exception var settingsGuid = DesignBlockController.getSettingsGuid(CP, "Addon Mock", ref result); if (string.IsNullOrEmpty(settingsGuid)) { return(result); } // // -- locate or create a data record for this guid settings = AddonMockDataModel.createOrAddSettings(CP, settingsGuid); if (settings == null) { throw new ApplicationException("Could not create the design block settings record."); } // // -- translate the Db model to a view model and mustache it into the layout var viewModel = AddonMockViewModel.create(CP, settings); if (viewModel == null) { throw new ApplicationException("Could not create design block view model."); } object dataObject = Newtonsoft.Json.JsonConvert.DeserializeObject(viewModel.jsonData); result = Nustache.Core.Render.StringToString(viewModel.layout, dataObject); } catch (ArgumentException ex) { // // -- argument error result = "<div class=\"ccError\">" + ex.Message + "</div>"; } catch (ApplicationException ex) { // // -- app error result = "<div class=\"ccError\">" + ex.Message + "</div>"; } catch (Exception ex) { // // -- tool, just return the exception result = "<div class=\"ccError\">" + ex.Message + "</div>"; } // // -- if editing enabled, add the link and wrapperwrapper if (settings == null) { return(result); } return(CP.Content.GetEditWrapper(result, AddonMockDataModel.tableMetadata.contentName, settings.id)); }
// // ==================================================================================================== public static AddonMockDataModel createOrAddSettings(CPBaseClass cp, string settingsGuid) { AddonMockDataModel result = create <AddonMockDataModel>(cp, settingsGuid); if ((result == null)) { // // -- create default content result = addDefault <AddonMockDataModel>(cp); result.name = tableMetadata.contentName + " " + result.id; result.ccguid = settingsGuid; result.themeStyleId = 0; result.padTop = false; result.padBottom = false; result.padRight = false; result.padLeft = false; // // -- create custom content result.jsondata = "{\"headline\":\"Hello World\",\"items\":[{\"name\":\"Rock\",\"price\":\"$1.24\"},{\"name\":\"Nice Rock\",\"price\":\"$2.99\"}]}"; // DateTime rightNow = DateTime.Now; var layout = DbBaseModel.create <LayoutModel>(cp, Constants.guidAddonMockLayout); string defaultLayout = "<h2>{{headline}}</h2><ul>{{#items}}<hr><li><div>name: {{name}}</div><div>price: {{price}}</div></li>{{/items}}</ul><hr>"; if (layout == null) { // // -- no layout found for the default guid, create now layout = DbBaseModel.addDefault <LayoutModel>(cp); layout.name = "Addon Mock Default Layout, " + rightNow.Year + rightNow.Month.ToString().PadLeft(2, '0') + rightNow.Day.ToString().PadLeft(2, '0'); layout.ccguid = Constants.guidAddonMockLayout; layout.layout.content = defaultLayout; layout.save(cp); } if (layout.layout.content != defaultLayout) { // // -- layout does not match default content, they edited it so just create a new record layout = DbBaseModel.addDefault <LayoutModel>(cp); layout.name = "Addon Mock Default Layout, " + rightNow.Year + rightNow.Month.ToString().PadLeft(2, '0') + rightNow.Day.ToString().PadLeft(2, '0'); layout.ccguid = cp.Utils.CreateGuid(); layout.layout.content = defaultLayout; layout.save(cp); } result.layoutid = layout.id; // result.save(cp); // // -- track the last modified date cp.Content.LatestContentModifiedDate.Track(result.modifiedDate); } return(result); }