public static IList <ContainerMetadata> Load(Page page) { var metadataList = new List <ContainerMetadata>(); var appData = page.LoadApplicationData("ContainerMetadata"); if (appData != null) { // Metadata format: [ID]#[Template ID]=[Count],... // var metadataStr = Encoding.Default.GetString(appData.Data); Log.Info("Read metadata: " + metadataStr); string[] tokens = metadataStr.Split(new string[] { "#", "=", ";" }, StringSplitOptions.RemoveEmptyEntries); int index = 0; while (index < tokens.Length) { ContainerMetadata metadata = new ContainerMetadata(); metadata.Id = tokens[index++]; metadata.TemplateId = tokens[index++]; metadata.Count = Int32.Parse(tokens[index++]); metadataList.Add(metadata); } } return(metadataList); }
/// <summary> /// On Page Save. /// </summary> /// <param name="page"></param> /// <param name="args"></param> /// <param name="phase"></param> public static void OnPageSave(Page page, SaveEventArgs args, EventPhases phase) { if (IsContainerPage(page)) { try { IList <Container> containers = Container.GetContainers(page); if (containers.Count > 0) { var foundComponentInWrongContainer = ProcessComponentPresentationsInWrongContainer(page, containers); if (!foundComponentInWrongContainer) { var containerMetadata = ContainerMetadata.Load(page); if (containerMetadata != null && containerMetadata.Count != containers.Count) { ProcessContainersOnWrongLocation(containers, page, containerMetadata); } } ProcessComponentTemplates(page.ComponentPresentations); ContainerMetadata.Save(page, containers); } } catch (Exception e) { Log.Error("Error when processing container page: " + e); } } }
/// <summary> /// Process containers on wrong location in the page /// </summary> /// <param name="newContainers"></param> /// <param name="page"></param> /// <param name="oldContainers"></param> static public void ProcessContainersOnWrongLocation(IList <Container> newContainers, Page page, IList <ContainerMetadata> containerMetadataList) { for (int i = 0; i < newContainers.Count; i++) { var container = newContainers[i]; if (container.ComponentPresentations.Count == 0 && i + 1 < newContainers.Count) { var nextContainer = newContainers[i + 1]; if (container.PageIndex == nextContainer.PageIndex - 1) // Found two containers lying beside eachother on the page { // Verify so the container items is not in the wrong container. // If the container has been drag&dropped through XPM it will be attached to the container directly. So this needs to be corrected in that case. // ContainerMetadata containerMetadata = null; foreach (var metadata in containerMetadataList) { if (container.Matches(metadata)) { containerMetadata = metadata; break; } } if (containerMetadata != null && containerMetadata.Count > 0) { int newPageIndex = container.PageIndex + containerMetadata.Count + 1; var containerCp = page.ComponentPresentations[nextContainer.PageIndex]; page.ComponentPresentations.RemoveAt(nextContainer.PageIndex); page.ComponentPresentations.Insert(newPageIndex, containerCp); return; } } } } }
public bool Matches(ContainerMetadata containerMetadata) { return(containerMetadata.Id == this.containerComponent.Id && containerMetadata.TemplateId == this.containerTemplate.Id); }