void FormStorage_Created(object sender, FormEventArgs e) { var form = e.Form; // Was this form created in a folder? var sessionId = UmbracoContext.Current.Security.GetSessionId(); var folderId = HttpContext.Current.Cache[sessionId + "_folderId"]; if (folderId == null) { return; } var folder = PerplexFolder.Get(folderId.ToString()); if (folder == null) { return; } folder.Forms.Add(form.Id.ToString()); PerplexFolder.SaveAll(); ClearFormsCache(folderId.ToString()); }
public void Move(PerplexFolder newParent) { // Remove from current parent PerplexFolder currentParent = GetParent(); // Should not be possible, but just in case (root folder can never be moved) if (currentParent == null) { return; } // Trying to "move" to the same parent? Nope. if (currentParent.Id == newParent.Id) { return; } // Remove from current parent currentParent.Folders.Remove(this); // Move to new parent ParentId = newParent.Id; newParent.Folders.Add(this); SaveAll(); }
/// <summary> /// Adds a new folder /// </summary> /// <param name="folder"></param> /// <param name="parentId"></param> public static void Add(PerplexFolder folder, string parentId) { var parentFolder = Get(parentId); if (parentFolder == null) { throw new ArgumentException("Folder with parentId " + parentId + " not found.", "parentId"); } parentFolder.AddFolder(folder); }
void FormStorage_Deleted(object sender, Umbraco.Forms.Core.FormEventArgs e) { // If this Form was stored in a Folder, remove it. var form = e.Form; var folder = PerplexFolder.Get(f => f.Forms.Any(fid => fid == form.Id.ToString())); if (folder != null) { folder.Forms.Remove(form.Id.ToString()); PerplexFolder.SaveAll(); } }
/// <summary> /// Updates this folder with all properties of the given folder, except for id. /// </summary> /// <param name="folder"></param> public void Update(PerplexFolder folder) { // Disabled folders cannot be updated if (Disabled) { return; } ParentId = folder.ParentId; Name = folder.Name; Forms = folder.Forms; Folders = folder.Folders; SaveAll(); }
/// <summary> /// Returns the first common ancestor of the list of folders /// </summary> /// <param name="folders"></param> /// <returns></returns> public static PerplexFolder GetCommonAncestor(List <PerplexFolder> folders) { if (!folders.Any()) { return(null); } // The common ancestor is found by simply taking the intersection of all folder paths // If the resulting list contains more than 1 folder, we take the last one (= deepest in the tree). var folderPaths = folders.Select(folder => folder.Path); // This list will always be at least of length 1, as all folders share at least the root folder. var commonAncestors = folderPaths.Aggregate((intersected, list) => intersected.Intersect(list).ToList()); return(PerplexFolder.Get(commonAncestors.Last())); }
/// <summary> /// Returns the root folder. /// Any other folders are nested inside the root folder. /// </summary> public static PerplexFolder GetRootFolder() { if (rootFolder == null) { var jsonFile = GetFilePath(); if (formsFileSystem.FileExists(jsonFile)) { try { using (StreamReader sr = new StreamReader(formsFileSystem.OpenFile(jsonFile))) { string json = sr.ReadToEnd(); if (!string.IsNullOrEmpty(json)) { PerplexFolder folder = null; try { folder = JsonConvert.DeserializeObject <PerplexFolder>(json); } catch (JsonSerializationException) { } // Only set as rootfolder if deserialization was succesful if (folder != null) { rootFolder = folder; } } } } catch (Exception) { } // TODO: Handle errors? } // Something went wrong retrieving the rootFolder // Create a new instance and write it to disk. // We also create a new root folder when the Id is missing. // This can occur when the JSON was simply "{}" which will deserialize properly // but will yield a completely empty PerplexFolder with no data whatsoever, whereas // an Id is required to function properly. if (rootFolder == null || string.IsNullOrEmpty(rootFolder.Id)) { rootFolder = CreateNewRoot(); SaveAll(); } } return(rootFolder); }
/// <summary> /// Returns the start folders for the given user /// </summary> /// <param name="user"></param> /// <returns></returns> public static List <PerplexFolder> GetStartFoldersForUser(IUser user) { if (user == null) { return(new List <PerplexFolder>()); } // We cache the result by user id, // to prevent unnecessary SQL queries string cacheKey = "_Perplex_StartFolders_" + user.Id; List <PerplexFolder> folders = HttpContext.Current.Cache[cacheKey] as List <PerplexFolder>; if (folders == null) { var sqlHelper = Helper.SqlHelper; try { var reader = sqlHelper.ExecuteReader( "SELECT formsStartNode FROM [perplexUmbracoUser] WHERE userId = @userId", sqlHelper.CreateParameter("@userId", user.Id) ); folders = new List <PerplexFolder>(); while (reader.Read()) { string folderId = reader.Get <string>("formsStartNode"); PerplexFolder folder = PerplexFolder.Get(folderId); if (folder != null) { folders.Add(folder); } } } catch (Exception) { folders = new List <PerplexFolder>(); } HttpContext.Current.Cache[cacheKey] = folders; } return(folders); }
/// <summary> /// Returns the start folders for the given user /// </summary> /// <param name="user"></param> /// <returns></returns> public static List <PerplexFolder> GetStartFoldersForUser(IUser user) { if (user == null) { return(new List <PerplexFolder>()); } // We cache the result by user id, // to prevent unnecessary SQL queries string cacheKey = "_Perplex_StartFolders_" + user.Id; List <PerplexFolder> folders = HttpContext.Current.Cache[cacheKey] as List <PerplexFolder>; if (folders == null) { try { folders = new List <PerplexFolder>(); var records = Sql.CreateSqlDataEnumerator( "SELECT formsStartNode FROM [perplexUmbracoUser] WHERE userId = @userId", System.Data.CommandType.Text, new { userId = user.Id }); foreach (IDataRecord record in records) { string folderId = record["formsStartNode"].ToString(); PerplexFolder folder = PerplexFolder.Get(folderId); if (folder != null) { folders.Add(folder); } } } catch (Exception) { folders = new List <PerplexFolder>(); } HttpContext.Current.Cache[cacheKey] = folders; } return(folders); }
/// <summary> /// Returns the root folder. /// Any other folders are nested inside the root folder. /// </summary> public static PerplexFolder GetRootFolder() { if (rootFolder == null) { var jsonFile = GetFilePath(); if (File.Exists(jsonFile)) { try { string json = File.ReadAllText(jsonFile); if (!string.IsNullOrEmpty(json)) { PerplexFolder folder = null; try { folder = JsonConvert.DeserializeObject <PerplexFolder>(json); } catch (JsonSerializationException) { } // Only set as rootfolder if deserialization was succesful if (folder != null) { rootFolder = folder; } } } catch (Exception) { } // TODO: Handle errors? } // Something went wrong retrieving the rootFolder // Create a new instance and write it to disk if (rootFolder == null) { rootFolder = CreateNewRoot(); SaveAll(); } } return(rootFolder); }
public void AddFolder(PerplexFolder folder) { folder.ParentId = Id; Folders.Add(folder); SaveAll(); }
public HttpResponseMessage Update(PerplexFolder folder) { // Update the folder var folderToUpdate = PerplexFolder.Get(folder.Id); if(folderToUpdate == null) { return new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = "Folder with id " + folder.Id + " not found" }; } // If this folder is disabled, it means the user does not have access so it cannot be updated either if (folderToUpdate.Disabled) { return new HttpResponseMessage(HttpStatusCode.Unauthorized) { ReasonPhrase = "Insufficient permission to update folder " + folder.Name }; } folderToUpdate.Update(folder); // Return the updated folder return Request.CreateResponse(HttpStatusCode.OK, folderToUpdate); }
public void Move(PerplexFolder newParent) { // Remove from current parent PerplexFolder currentParent = GetParent(); // Should not be possible, but just in case (root folder can never be moved) if (currentParent == null) return; // Trying to "move" to the same parent? Nope. if (currentParent.Id == newParent.Id) return; // Remove from current parent currentParent.Folders.Remove(this); // Move to new parent ParentId = newParent.Id; newParent.Folders.Add(this); SaveAll(); }
public void AddFolder(PerplexFolder folder) { folder.ParentId = Id; Folders.Add(folder); SaveAll(); }
/// <summary> /// Adds a new folder /// </summary> /// <param name="folder"></param> /// <param name="parentId"></param> public static void Add(PerplexFolder folder, string parentId) { var parentFolder = Get(parentId); if (parentFolder == null) { throw new ArgumentException("Folder with parentId " + parentId + " not found.", "parentId"); } parentFolder.AddFolder(folder); }
/// <summary> /// Returns the root folder. /// Any other folders are nested inside the root folder. /// </summary> public static PerplexFolder GetRootFolder() { if (rootFolder == null) { var jsonFile = GetJsonFilePath(); if (File.Exists(jsonFile)) { try { string json = File.ReadAllText(jsonFile); if (!string.IsNullOrEmpty(json)) { PerplexFolder folder = null; try { folder = JsonConvert.DeserializeObject<PerplexFolder>(json); } catch (JsonSerializationException) { } // Only set as rootfolder if deserialization was succesful if (folder != null) { rootFolder = folder; } } } catch (Exception) { } // TODO: Handle errors? } // Something went wrong retrieving the rootFolder // Create a new instance and write it to disk if (rootFolder == null) { rootFolder = CreateNewRoot(); SaveAll(); } } return rootFolder; }
/// <summary> /// Updates this folder with all properties of the given folder, except for id. /// </summary> /// <param name="folder"></param> public void Update(PerplexFolder folder) { // Disabled folders cannot be updated if (Disabled) return; ParentId = folder.ParentId; Name = folder.Name; Forms = folder.Forms; Folders = folder.Folders; SaveAll(); }
public HttpResponseMessage CreateFolder(string parentId, string name) { if (PerplexFolder.Get(parentId) == null) return new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = "Parent folder with id " + parentId + " not found" }; // Add folder var folder = new PerplexFolder { Id = Guid.NewGuid().ToString(), ParentId = parentId, // Do not allow a name as null, transform that to empty string instead (UI related, would display as "null" otherwise). // If users want an empty folder name, so be it, so we are not going to prevent them from inputting an empty name. Name = name == null ? "" : name, Forms = new List<string>(), Folders = new List<PerplexFolder>() }; PerplexFolder.Add(folder, parentId); return Request.CreateResponse(HttpStatusCode.OK, folder); }