public object ItemEditSave(PluginAddress pluginAddress, PluginEditInput pluginEditInput, bool isEditingCode) { if (isEditingCode && !string.IsNullOrEmpty(pluginEditInput.VirtualPath)) { pluginEditInput.VirtualPath.SaveFileText(pluginEditInput.Code, HttpContext); pluginEngine.ReloadPlugin(p => p.Tag == null && !(p.Tag is Plugin) && string.Compare(p.VirtualPath, pluginEditInput.VirtualPath, true) == 0, p => pluginEditInput.VirtualPath); return(EditNotInstalled(new PluginNotInstalledAddress(pluginEditInput.VirtualPath))); } ModelResult <Plugin> results = null; Plugin plugin = pluginService.GetPlugin(pluginAddress); if (isEditingCode && !string.IsNullOrEmpty(pluginEditInput.Code) && pluginEditInput.Code != plugin.GetFileText()) { results = pluginService.EditPlugin(pluginAddress, pluginEditInput, true); } else if (!isEditingCode) { results = pluginService.EditPlugin(pluginAddress, pluginEditInput, false); } if (results != null && !results.IsValid) { ViewData["ValidationState"] = results.ValidationState; } if (isEditingCode || ViewData.ContainsKey("ValidationState")) { return(ItemEdit(pluginAddress, pluginEditInput)); } return(Redirect(Url.Plugin(plugin))); }
public static Plugin ApplyNew(this Plugin plugin, PluginEditInput input, IEnumerable <ExtendedProperty> extendedProperties) { List <ExtendedProperty> finalExtendedProperties = new List <ExtendedProperty>(); // remove extended properties that don't exist anymore plugin.ExtendedProperties.Where(ep => extendedProperties.Contains(ep, new ExtendedPropertyComparer())).ToList().ForEach(kvp => finalExtendedProperties.Add(kvp)); // set values for existing extended properties and add new extended properties extendedProperties.ToList().ForEach(ep => finalExtendedProperties.First(fep => string.Compare(fep.Name, ep.Name, true) == 0).Value = ep.Value); return(new Plugin(plugin.Site.ID, plugin.ID, plugin.VirtualPath, input.Enabled.GetValueOrDefault(plugin.Enabled), finalExtendedProperties)); }
public OxiteViewModelItem <PluginEditInput> ItemEdit(Plugin plugin, PluginEditInput pluginEditInput) { if (plugin == null) { return(null); } OxiteViewModelItem <PluginEditInput> model = new OxiteViewModelItem <PluginEditInput>(pluginEditInput == null ? new PluginEditInput(plugin.GetFileText(), plugin.Enabled, plugin.ExtendedProperties) : new PluginEditInput(null, pluginEditInput.Code ?? plugin.GetFileText(), pluginEditInput.Enabled, pluginEditInput.PropertyValues)); model.AddModelItem(pluginEngine.GetPlugin(plugin)); return(model); }
public ModelResult <Plugin> EditPlugin(PluginAddress pluginAddress, PluginEditInput pluginEditInput, bool hasCodeChanges) { Plugin originalPlugin = GetPlugin(pluginAddress); bool enabled = originalPlugin.Enabled; bool newlyEnabled = false; Plugin newPlugin; PluginContainer pluginContainer; ValidationStateDictionary validationState; if (!hasCodeChanges) { validationState = validatePluginPropertyValues(originalPlugin.FillContainer(pluginEngine), pluginEditInput.PropertyValues); pluginContainer = pluginEngine.GetPlugin(originalPlugin); pluginContainer.IsValid = validationState.IsValid; newlyEnabled = !enabled && validationState.IsValid; newPlugin = originalPlugin.Apply(pluginEditInput, newlyEnabled ? (bool?)true : null); if (!validationState.IsValid) { return(new ModelResult <Plugin>(newPlugin, validationState)); } } else { validationState = new ValidationStateDictionary(); originalPlugin.SaveFileText(pluginEditInput.Code); pluginContainer = pluginEngine.ReloadPlugin(p => p.Tag != null && p.Tag is Plugin && ((Plugin)p.Tag).ID == originalPlugin.ID, p => originalPlugin.VirtualPath); if (pluginContainer.IsLoaded) { newPlugin = originalPlugin.ApplyNew(pluginEditInput, pluginContainer.GetProperties()); } else { newPlugin = originalPlugin.Apply(pluginEditInput, null); } } newPlugin = repository.Save(newPlugin); newPlugin.Container = pluginContainer; pluginContainer.Tag = newPlugin; if (!hasCodeChanges) { pluginContainer.ApplyProperties(newPlugin.ExtendedProperties); if (newlyEnabled) { pluginTemplateRegistry.Reload(pluginEngine); context.Routes.Reload(modules, this, pluginEngine); pluginStyleRegistry.Reload(pluginEngine); pluginScriptRegistry.Reload(pluginEngine); } else { pluginTemplateRegistry.UpdatePlugin(newPlugin); pluginStyleRegistry.UpdatePlugin(newPlugin); pluginScriptRegistry.UpdatePlugin(newPlugin); } } else { //TODO: (erikpo) Instead fire off a module event saying a plugin was edited and then Oxite.Blogs, Oxite.CMS and Oxite.Comments can subscribe to those events and invalidate cache by their type on their own. //if (pluginContainer.HasMethod("ProcessDisplayOfPost")) // cache.Invalidate<Post>(); //else if (pluginContainer.HasMethod("ProcessDisplayOfPage")) // cache.Invalidate<Page>(); //else if (pluginContainer.HasMethod("ProcessDisplayOfComment")) // cache.Invalidate<Comment>(); pluginTemplateRegistry.Reload(pluginEngine); context.Routes.Reload(modules, this, pluginEngine); pluginStyleRegistry.Reload(pluginEngine); pluginScriptRegistry.Reload(pluginEngine); } return(new ModelResult <Plugin>(newPlugin, validationState)); }
public static Plugin Apply(this Plugin plugin, PluginEditInput input, bool?enabled) { return(new Plugin(plugin.Site.ID, plugin.ID, plugin.VirtualPath, enabled.HasValue ? enabled.Value : input.Enabled.GetValueOrDefault(plugin.Enabled), input.PropertyValues == null ? plugin.ExtendedProperties : input.GetExtendedProperties(plugin.ExtendedProperties))); }
public static string PluginPropertyFieldsets(this HtmlHelper htmlHelper, Plugin plugin, PluginEditInput pluginEditInput, Func <string, string, string> localize) { StringBuilder outputBuilder = new StringBuilder(); IEnumerable <ExtendedProperty> editInputProperties = pluginEditInput.GetExtendedProperties(plugin.ExtendedProperties); IEnumerable <IGrouping <KeyValuePair <string, int>, ExtendedProperty> > extendedPropertyGroupings = plugin.ExtendedProperties.GroupBy( ps => plugin.GetGroup(ps.Name) != null ? new KeyValuePair <string, int>(plugin.GetGroup(ps.Name).Name, plugin.GetGroup(ps.Name).Order) : new KeyValuePair <string, int>(localize("Plugin.OtherPropertyGroup", "Other"), int.MaxValue)); foreach (var extendedPropertyGrouping in extendedPropertyGroupings.OrderBy(psg => psg.Key.Value)) { if (extendedPropertyGroupings.Count(epg => epg.Key.Key != "Other") > 0) { outputBuilder.AppendFormat("<h4>{0}</h4>", extendedPropertyGrouping.Key.Key.CleanText()); } foreach (var extendedProperty in extendedPropertyGrouping.OrderBy(ps => plugin.GetOrder(ps.Name))) { string extendedPropertyName = extendedProperty.Name; ExtendedProperty editInputProperty = editInputProperties.FirstOrDefault(ep => ep.Name == extendedPropertyName); object extendedPropertyValue = editInputProperty != null ? editInputProperty.Value : extendedProperty.Value; bool labelFirst = true; string fieldName = string.Format("ps_{0}", extendedPropertyName.CleanAttribute()); Func <string> fieldRenderField = () => extendedPropertyName.EndsWith("Password") && extendedProperty.Type == typeof(string) ? htmlHelper.Password(fieldName, extendedPropertyValue, new { id = fieldName }) : htmlHelper.TextBox(fieldName, extendedPropertyValue, new { id = fieldName }); var fieldset = new TagBuilder("fieldset"); string extendedPropertyValidationKey = string.Format("PluginPropertiesInput.{0}", extendedProperty.Name); if (htmlHelper.ViewData.ModelState.ContainsKey(extendedPropertyValidationKey) && htmlHelper.ViewData.ModelState[extendedPropertyValidationKey].Errors.Count > 0) { fieldset.AddCssClass("error"); } fieldset.Attributes["id"] = string.Format("psf_{0}", extendedPropertyName.CleanAttribute()); if (extendedPropertyValue is bool) { labelFirst = false; fieldRenderField = () => htmlHelper.CheckBox(fieldName, (bool)extendedPropertyValue, null, null); } else if (extendedPropertyValue is DateTime) { fieldset.AddCssClass("datetime"); } else { fieldset.AddCssClass("textbox"); } PropertyAppearance appearance = plugin.GetAppearance(extendedPropertyName); if (appearance != null) { if (!string.IsNullOrEmpty(appearance.Width)) { fieldset.Attributes["style"] = string.Format("width:{0}", appearance.Width); fieldset.AddCssClass("hasWidth"); } //TODO: (erikpo) Add height (multiline) when appropriate } fieldset.InnerHtml = htmlHelper.fieldWithLabel( fieldName.CleanAttribute(), fieldRenderField, (plugin.GetLabelText(extendedPropertyName) ?? extendedPropertyName).CleanText().AutoAnchor(), labelFirst, true ); outputBuilder.Append(fieldset.ToString()); } } return(outputBuilder.ToString()); }