using System; using System.Collections.Generic; using System.Text; using ScrewTurn.Wiki.PluginFramework; namespace ScrewTurn.Wiki { /// /// Manages content templates. /// public static class Templates { /// /// Gets all the content templates. /// /// The content templates, sorted by name. public static List GetTemplates() { List result = new List(20); // Retrieve templates from all providers foreach(IPagesStorageProviderV30 prov in Collectors.PagesProviderCollector.AllProviders) { result.AddRange(prov.GetContentTemplates()); } result.Sort(new ContentTemplateNameComparer()); return result; } /// /// Finds a content template. /// /// The name of the template to find. /// The content template, or null. public static ContentTemplate Find(string name) { List templates = GetTemplates(); int index = templates.BinarySearch(new ContentTemplate(name, "", null), new ContentTemplateNameComparer()); if(templates.Count > 0 && index >= 0) return templates[index]; else return null; } /// /// Adds a new content template. /// /// The name of the template. /// The content of the template. /// The target provider (null for the default provider). /// true if the template is added, false otherwise. public static bool AddTemplate(string name, string content, IPagesStorageProviderV30 provider) { if(Find(name) != null) return false; if(provider == null) provider = Collectors.PagesProviderCollector.GetProvider(Settings.DefaultPagesProvider); ContentTemplate result = provider.AddContentTemplate(name, content); if(result != null) Log.LogEntry("Content Template " + name + " created", EntryType.General, Log.SystemUsername); else Log.LogEntry("Creation failed for Content Template " + name, EntryType.Error, Log.SystemUsername); return result != null; } /// /// Removes a content template. /// /// The template to remove. /// true if the template is removed, false otherwise. public static bool RemoveTemplate(ContentTemplate template) { bool done = template.Provider.RemoveContentTemplate(template.Name); if(done) Log.LogEntry("Content Template " + template.Name + " deleted", EntryType.General, Log.SystemUsername); else Log.LogEntry("Deletion failed for Content Template " + template.Name, EntryType.Error, Log.SystemUsername); return done; } /// /// Modifies a content template. /// /// The template to modify. /// The new content of the template. /// true if the template is modified, false otherwise. public static bool ModifyTemplate(ContentTemplate template, string content) { ContentTemplate result = template.Provider.ModifyContentTemplate(template.Name, content); if(result != null) Log.LogEntry("Content Template " + template.Name + " updated", EntryType.General, Log.SystemUsername); else Log.LogEntry("Update failed for Content Template " + template.Name, EntryType.Error, Log.SystemUsername); return result != null; } } }