/// <summary> /// Returns a new instance of a page with the page name set, or a clone of an existing one, if a page with than name already exists /// </summary> /// <param name="page"></param> /// <param name="parent"></param> /// <returns></returns> public static T GetOrCreatePageWithName <T>(this PageReference parent, string pageName) where T : BasePageData { if (parent == null) { return(null); } var page = parent.GetChildWithName(pageName); if (page != null) { var clone = (page.CreateWritableClone() as T); var assetsFolderForPage = _contentAssetHelper.GetOrCreateAssetFolder(clone.ContentLink); var children = _repo.GetChildren <IContent>(assetsFolderForPage.ContentLink); foreach (var child in children) { _repo.Delete(child.ContentLink, true, AccessLevel.Read); } return(clone); } var newPage = _repo.GetDefault <T>(parent); newPage.PageName = pageName; return(newPage); }
private ContentReference GetAssetFolder() { var folder = _helper.GetOrCreateAssetFolder(Parent); if (folder == null) { return(Parent); } return(folder.ContentLink); }
/// <summary> /// Creates a new block of type T, and saves it to the ContentReference's assets folder /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parentPageReference"></param> /// <param name="newBlockName"></param> /// <returns></returns> public static T CreateGenericBlockForPage <T>(this ContentReference parentPageReference, string newBlockName) where T : BaseBlockData { if (parentPageReference.IsNullOrEmpty()) { return(null); } var assetsFolderForPage = _contentAssetHelper.GetOrCreateAssetFolder(parentPageReference); var blockInstance = _repo.GetDefault <T>(assetsFolderForPage.ContentLink); var blockForPage = blockInstance as IContent; blockForPage.Name = newBlockName; return(blockInstance); }
/// <summary> /// Downloads an image from the url and saves to parents assets folder /// </summary> /// <param name="parent"></param> /// <param name="url"></param> /// <param name="title"></param> /// <returns></returns> public static ImageFile DownloadImage(ContentReference parent, string url, string title) { var imageFile = Repo.GetDefault <ImageFile>(ContentAssetHelper.GetOrCreateAssetFolder(parent).ContentLink); imageFile.Name = $"image-{title}"; var blob = ImageDownloaderHelper.DownloadImageBlob(url); if (blob != null) { imageFile.BinaryData = blob; Repo.Save(imageFile, SaveAction.Publish); return(imageFile); } return(null); }
// POST api/<controller> public ReviewData Post(ReviewData reviewData) { string language = Language; ContentReference contentLink = _referenceConverter.GetContentLink(reviewData.ContentId, CatalogContentType.CatalogEntry, 0); ContentAssetFolder assetFolder = _contentAssetHelper.GetOrCreateAssetFolder(contentLink); EntryContentBase product = _contentRepository.Get <EntryContentBase>(contentLink, new CultureInfo(language)); Review newReview = _contentRepository.GetDefault <Review>(assetFolder.ContentLink, new CultureInfo(language)); newReview.Rating = reviewData.Rating; newReview.Heading = reviewData.Heading; newReview.Text = reviewData.Text; //TODO: Get currentuser, need to be logedin to post review newReview.UserDisplayName = reviewData.UserName; newReview.Name = newReview.UserDisplayName + "(" + DateTime.Now.ToShortDateString() + ")"; newReview.ReviewDate = DateTime.Now; ContentReference cf = _contentRepository.Save(newReview, SaveAction.Publish, AccessLevel.NoAccess); Review postedReview = _contentRepository.Get <Review>(cf, new CultureInfo(language)); if (postedReview != null) { ReviewData review = new ReviewData { ContentId = postedReview.ContentId, Rating = postedReview.Rating, Heading = postedReview.Heading, Text = postedReview.Text, UserName = postedReview.UserDisplayName, ReviewDate = postedReview.ReviewDate }; UpdateProductWithAverageReview(product, language); return(review); } return(null); }
/// <summary> /// Update content based on row values. Using property indicator to find matching property to update /// Syntax: propertyName.propertyName or contentAreaName.blockname.propertyname /// </summary> /// <param name="rowData"></param> /// <param name="rowNumber"></param> /// <param name="content"></param> /// <param name="configuration"></param> /// <returns></returns> public void SaveProperty(IContent content, ExcelDefaultValueMapping columnMapping, object excelValue) { var saveWithoutPublish = columnMapping.Configuration.SaveWithoutPublish; string propertyIndicator = columnMapping.PropertyIndicator; var processingQueue = new Queue <IContent>(); processingQueue.Enqueue(content); var segments = propertyIndicator.Split('.').ToList(); for (int i = 0; i < segments.Count; i++) { var processingContent = processingQueue.Peek(); if (processingContent == null) { continue; } var segment = segments[i]; if (!IsPropertyExist(processingContent, segment)) { if ((excelValue != null && !string.IsNullOrEmpty(excelValue + ""))) { ContentArea contentArea; PropertyInfo contentAreaProperty; if (i > 0 && IsContainerProperty(processingContent, segments[i - 1], out contentArea, out contentAreaProperty)) { var allowedBlocks = GetAllowedBlocks(contentAreaProperty); var blockType = allowedBlocks.FirstOrDefault(c => c.Name.Equals(segment, StringComparison.OrdinalIgnoreCase)); if (blockType != null) { var contentType = _contentTypeRepository.Load(blockType.Name); if (contentArea?.Items != null) { var blocks = contentArea.Items.Select(x => _contentRepository.Get <IContent>(x.ContentLink.ToReferenceWithoutVersion())).ToList(); var existingBlock = blocks.FirstOrDefault(x => x != null && x.ContentTypeID == contentType.ID); if (existingBlock != null) { processingQueue.Enqueue((IContent)(existingBlock as ContentData).CreateWritableClone()); processingQueue.Dequeue(); continue; } } var assetFolder = _contentAssetHelper.GetOrCreateAssetFolder(processingContent.ContentLink); var newBlock = _contentRepository.GetDefault <IContent>(assetFolder.ContentLink, contentType.ID); newBlock.Name = blockType.Name; var savedNewBlock = _contentRepository.Save(newBlock, saveBlockAction, AccessLevel.NoAccess); contentArea.Items.Add(new ContentAreaItem() { ContentLink = savedNewBlock }); SaveContent(processingContent, segments[i - 1], contentArea, saveWithoutPublish); processingQueue.Enqueue(newBlock); processingQueue.Dequeue(); } } } continue; } else { object epiValue = null; if (columnMapping.Parser != null) { epiValue = columnMapping.Parser.Parse(processingContent, excelValue, columnMapping.Configuration); } else { epiValue = excelValue; } ContentArea contentArea; PropertyInfo contentAreaProperty; if (IsContainerProperty(processingContent, segment, out contentArea, out contentAreaProperty)) { if (contentArea == null) { contentArea = new ContentArea(); } if (i == segments.Count - 1 && epiValue is ContentReference) { contentArea.Items.Clear(); contentArea.Items.Add(new ContentAreaItem() { ContentLink = (ContentReference)epiValue }); } SaveContent(processingContent, segment, contentArea, saveWithoutPublish); continue; } string blockContentTypeName; if (IsLocalBlockProperty(processingContent, segment, out blockContentTypeName) && i == segments.Count - 2) { var localBlockObject = ((ContentData)processingContent)[segment]; PropertyInfo prop = localBlockObject.GetType().GetProperty(segments[i + 1], BindingFlags.Public | BindingFlags.Instance); if (null != prop && prop.CanWrite) { if (epiValue != null || !columnMapping.IgnoreIfNull) { prop.SetValue(localBlockObject, epiValue, null); SaveContent(processingContent, segment, localBlockObject, saveWithoutPublish); } } break; } if (epiValue != null || !columnMapping.IgnoreIfNull) { SaveContent(processingContent, segment, epiValue, saveWithoutPublish); } } } }