/// <summary> /// get a list of upcoming events /// </summary> /// <returns></returns> public ActionResult GetUpcomingEvents() { //creae the initial model var model = new UpcomingEventsListModel(); //get the current context var contextItem = RenderingContext.Current.ContextItem; //get the events landing page id from the config, to use to get the logo image from var eventsLandingPageId = _standardHelpers.GetItemIdFromConfig("EventsLandingPageID", contextItem); if (eventsLandingPageId != ID.Null) { var eventsLandingPage = _database.GetItem(eventsLandingPageId); if (eventsLandingPage != null) { //get the events landing template id from the config var eventsLandingTemplateId = _standardHelpers.GetTemplateIdFromConfig("EventsLandingTemplateID", contextItem); //check if the events landing page uses that template var isEventsLandingTemplate = eventsLandingPage.TemplateID == eventsLandingTemplateId; if (isEventsLandingTemplate) { //set the url for the events landing page model.EventsLandingUrl = LinkManager.GetItemUrl(eventsLandingPage); //get the index to use var index = GetEventsSearchIndex(); //check the index if (index != null) { //use the indexes search using (var context = index.CreateSearchContext()) { // get the results var results = context.GetQueryable <EventDetails>() .Where(eventDetailItem => eventDetailItem.Paths.Contains(eventsLandingPage.ID) && eventDetailItem.Date != DateTime.MinValue && eventDetailItem.Date >= DateTime.Now) .OrderBy(eventDetailItem => eventDetailItem.Date) .Take(PageSize) .GetResults(); // assing them to our model model.Events = results.Hits.Select(hit => hit.Document).ToList(); } } } } } return(View(model)); }
public async Task <ViewResult> AttributesImporter(string attributeType, AttributesModel model) { //create a the counter for the imported attributes var attributeCounter = 0; //check if we have a selected attribute type if (string.IsNullOrWhiteSpace(model.SelectedAttributeType)) { //prefill the model with dropdown items PrepareAttributesModel(model); model.ErrorMessage = "Please select the attribute type to import"; return(View(model)); } //get the selected attribute type var selectedAttributeType = model.SelectedAttributeType; //create the http client to request the json with var httpClient = new HttpClient(); //create the initial response attributes IEnumerable <AttributesJsonModel> responseAttributes; // create the api response var response = await httpClient.GetAsync( "http://atlas.atdw-online.com.au/api/atlas/attributes?key=996325374125&types=" + selectedAttributeType + "&out=json"); //check if we get a successs status back if (response.IsSuccessStatusCode) { //trya and read the contents and convert them into a json object try { var responseStream = response.Content.ReadAsStringAsync(); //assign the json response to our attributes model responseAttributes = JsonConvert .DeserializeObject <IEnumerable <AttributesJsonModel> >(responseStream.Result).ToList(); } catch (Exception ex) { //catch the error //prefill the model with dropdown items PrepareAttributesModel(model); model.ErrorMessage = "There was an error reading the data from ATDW, please try again, see :" + ex.Message; return(View(model)); } } else { //get the response status code var errorStatusCode = response.StatusCode; var responseError = errorStatusCode.ToString(); PrepareAttributesModel(model); model.ErrorMessage = "There was an error reading the data from ATDW, please try again, see :" + responseError; return(View(model)); } //now get the attributes from the model if (responseAttributes.Any()) { //get the attribute templates to use late var atdwSettingsItemId = _standardHelper.GetItemIdFromConfig("ATDWSettingsItemID"); var atdwAttributeTemplateId = _standardHelper.GetTemplateIdFromConfig("ATDWAttributeTemplateID"); var atdwAttributeParentTemplateId = _standardHelper.GetTemplateIdFromConfig("ATDWAttributeParentTemplateID"); //check if the templates are valid if (atdwAttributeTemplateId.ID != ID.Null && atdwAttributeParentTemplateId.ID != ID.Null) { var database = Sitecore.Configuration.Factory.GetDatabase("master"); //get the atdw settings item to look for the parent with var atdwSettingsItem = database.GetItem(atdwSettingsItemId); if (atdwSettingsItem != null) { var atdwAttributeParentItem = atdwSettingsItem.Children .FirstOrDefault(item => item["AttributeTypeId"].ToString() == selectedAttributeType && item.TemplateID == atdwAttributeParentTemplateId); //check if the parent to insert on is not null if (atdwAttributeParentItem != null) { var attributeTemplateId = new TemplateID(atdwAttributeTemplateId); // use the security disabler to creae or update items using (new SecurityDisabler()) { //go through the attributes to add foreach (var attributeTypeItem in responseAttributes) { try { //initiate the editing atdwAttributeParentItem.Editing.BeginEdit(); // fill in the parents type and description atdwAttributeParentItem["AttributeTypeId"] = attributeTypeItem.AttributeTypeId; atdwAttributeParentItem["Description"] = attributeTypeItem.Description; //terminate the editing atdwAttributeParentItem.Editing.EndEdit(); //get the attribute items from the parent if (attributeTypeItem.Attributes.Any()) { foreach (var attribute in attributeTypeItem.Attributes) { //check if the parent doesnt alread have the attribute var attributeCode = attribute.AttributeId; var hasAttribute = atdwAttributeParentItem.Children .Any(attributeItem => attributeItem.Fields["Attribute-Id"].Value == attributeCode); if (!hasAttribute) { //get a valid name from the description var name = ItemUtil.ProposeValidItemName(attribute.Description); //create the new attribute to add Item attributeItem = atdwAttributeParentItem.Add(name, attributeTemplateId); //initiate the editing attributeItem.Editing.BeginEdit(); // fill in the attribute properties attributeItem["Attribute-Type-Id"] = attributeTypeItem.AttributeTypeId; attributeItem["Attribute-Id"] = attribute.AttributeId; attributeItem["Attribute-Description"] = attribute.Description; attributeItem["Attribute-ATDW-Id"] = attribute.AttributeId; //initiate the editing attributeItem.Editing.EndEdit(); attributeCounter++; } } } } catch (Exception ex) { atdwAttributeParentItem.Editing.CancelEdit(); //prefill the model with dropdown items PrepareAttributesModel(model); model.ErrorMessage = "There was an error reading the data from ATDW, please try again, see :" + ex.Message; return(View(model)); } } } } } } } PrepareAttributesModel(model); model.SuccessMessage = attributeCounter + " attributes for the type: " + model.SelectedAttributeType + " have been imported successfully"; return(View(model)); }
public ActionResult GetPageCommentsList(int page = 1) { // get the current context item var contextItem = RenderingContext.Current.ContextItem; //create the default model var model = new EventCommentsListModel(); //get the database name, should be lower anyway but just in case //var databaseName = _database.Name.ToLower(); //get the index name //var indexName = string.Format("sitecore_{0}_index", databaseName); //var indexName = $"sitecore_{databaseName}_index"; //get the index to use //var sitecoreIndex = ContentSearchManager.GetIndex(indexName); //get the comments template id var eventCommentsTemplateId = _standardHelpers.GetTemplateIdFromConfig("EventCommentsTemplateID", contextItem); //check the index and the template id, testing how to use the standard index //if (sitecoreIndex != null && eventCommentsTemplateId != new TemplateID().ID) //{ // //use the indexes search to get the comments, need to adjust the index to have the properties we need to populate comment list item model // using (var context = sitecoreIndex.CreateSearchContext()) // { // // get the results // var results = context.GetQueryable<CommentListItemModel>() // .Where(item => item.Paths.Contains(contextItem.ID) // && item.Language == contextItem.Language.Name // && item.TemplateId == eventCommentsTemplateId.ID) // .Page(page - 1, PageSize) // .GetResults(); // // assing them to our model // model.PageComments = results.Hits.Select(hit => hit.Document).ToList(); // } //} //use the standard get children method if (eventCommentsTemplateId != new TemplateID().ID) { //get any child comments var eventComments = contextItem.GetChildren() .Where(item => item.TemplateID == eventCommentsTemplateId.ID).ToList(); // add them to the model if (eventComments.Any()) { model.PageComments = eventComments.Select( comment => new CommentListItemModel() { CommentorName = comment.Fields["Name"].ToString(), CommentText = comment.Fields["Comment"].ToString(), CommentDate = comment.Created }).ToList(); } } //retunr the model with any comments return(View(model)); }
/// <summary> /// Get the default menu items and other settings /// </summary> /// <returns></returns> public ActionResult Index() { //get the current context var contextItem = RenderingContext.Current.ContextItem; //get the master database to use to get items from var database = contextItem.Database; //create a new model var model = new PageHeaderModel(); //get the project settings id from the config, to use to get the logo image from var projectSettingsPageId = _standardHelpers.GetItemIdFromConfig("ProjectSettingsPageID", contextItem); if (projectSettingsPageId != ID.Null) { var projectSettingsPage = database.GetItem(projectSettingsPageId); if (projectSettingsPage != null) { //get the project settings template id from the config var projectSettingsTemplateId = _standardHelpers.GetTemplateIdFromConfig("ProjectSettingsTemplateID", contextItem); //check if the project settings page uses that template var isProjectSettingsTemplate = projectSettingsPage.TemplateID == projectSettingsTemplateId; if (isProjectSettingsTemplate) { model.LogoImage = new HtmlString(FieldRenderer.Render(projectSettingsPage, "Project_Logo", "mw=400")); } } } // get the homepage id set in the config to use to get the menu items var homePageId = _standardHelpers.GetItemIdFromConfig("HomePageID", contextItem); if (homePageId != ID.Null) { //get the home item from the config id var homePage = database.GetItem(homePageId); //check if the home item in not null if (homePage != null) { //check if the homepage's template is the same as the one set in our config var homeTemplateId = _standardHelpers.GetTemplateIdFromConfig("HomeTemplateID", contextItem); var isHomePageTemplate = homePage.TemplateID == homeTemplateId; //if the homepage template matches then we can now use it to get the navigation items if (isHomePageTemplate) { //add the verified home page to the model model.HomePageUrl = LinkManager.GetItemUrl(homePage); //get the home page item and add it to the menu items from the verified home page model.MenuItems.Add(_navigationHelpers.CreateSiteMenuModel(homePage, false)); //get the home page children and add them to the menu items if (homePage.HasChildren) { foreach (Item childPage in homePage.Children) { //get the child page items from the verified home page model.MenuItems.Add(_navigationHelpers.CreateSiteMenuModel(childPage, true)); } } } } } //get the site's languages for the dropdown language selector model.CurrentLanguage = _languageHelpers.GetActiveLanguageModel(); model.Languages = _languageHelpers.GetAllLanguages(); return(View(model)); }
/// <summary> /// get the page footer with the model populated /// </summary> /// <returns></returns> public ActionResult Index() { //get the current context var contextItem = RenderingContext.Current.ContextItem; //get the master database to use to get items from var database = contextItem.Database; //create the intial model var model = new PageFooterModel(); //get the project settings id from the config, to use to get the logo image from var projectSettingsPageId = _standardHelpers.GetItemIdFromConfig("ProjectSettingsPageID", contextItem); if (projectSettingsPageId != ID.Null) { var projectSettingsPage = database.GetItem(projectSettingsPageId); if (projectSettingsPage != null) { //get the project settings template id from the config var projectSettingsTemplateId = _standardHelpers.GetTemplateIdFromConfig("ProjectSettingsTemplateID", contextItem); //check if the project settings page uses that template var isProjectSettingsTemplate = projectSettingsPage.TemplateID == projectSettingsTemplateId; if (isProjectSettingsTemplate) { //save the project settings path to use for the editor model.EditorDatasourcePath = projectSettingsPage.Paths.FullPath; //get the copyright text model.CopyrightText = new HtmlString(FieldRenderer.Render(projectSettingsPage, "Copyright_Text")); //get the page set for the details var detailsPageId = projectSettingsPage.Fields["Info_Page"]; if (detailsPageId != null) { //get the details page var detailsPage = database.GetItem(detailsPageId.ToString()); if (detailsPage != null) { model.FooterHeading = new HtmlString(FieldRenderer.Render(detailsPage, "ContentHeading")); model.FooterIntro = new HtmlString(FieldRenderer.Render(detailsPage, "ContentIntro")); } } //get the footer links MultilistField footerLinks = projectSettingsPage.Fields["Page-Links"]; if (footerLinks != null) { var footerLinksPages = footerLinks.GetItems(); if (footerLinksPages.Any()) { model.FooterLinks = footerLinksPages.Select(linkPage => new NavigationItemModel() { Title = linkPage.DisplayName, Url = LinkManager.GetItemUrl(linkPage) }) .ToList(); } } } } } // return the view with the model return(View(model)); }
// GET: Carousel public ActionResult Index() { //get the master database to use to get items from //var database = RenderingContext.Current.ContextItem.Database; var database = Sitecore.Context.Database; //create the default model var model = new SiteCarouselModel(); //get the current context var contextItem = RenderingContext.Current.ContextItem; //get the site settings id from the config, to use to get the carousel items var siteSettingsPageId = _standardHelpers.GetItemIdFromConfig("SiteSettingsPageID", contextItem); if (siteSettingsPageId != ID.Null) { var siteSettingsPage = database.GetItem(siteSettingsPageId); if (siteSettingsPage != null) { //get the site settings template id from the config var siteSettingsTemplateId = _standardHelpers.GetTemplateIdFromConfig("SiteSettingsTemplateID", contextItem); //check if the site settings page uses that template var isSiteSettingsTemplate = siteSettingsPage.TemplateID == siteSettingsTemplateId; if (isSiteSettingsTemplate) { //get the tree list of carousel images MultilistField carouselImagesList = siteSettingsPage.Fields["Carousel"]; if (carouselImagesList != null) { //get the carousel images from the tree list var carouselImages = carouselImagesList.GetItems(); //create the carousel models if we have any images if (carouselImages.Any()) { foreach (var image in carouselImages) { var carouselImageModel = new CarouselImageModel() { CarouselHeading = image.Fields["Title"].ToString(), CarouselIntro = image.Fields["Description"].ToString() }; //get the carousel image MediaItem carouselMedia = image; if (carouselMedia != null) { var options = new MediaUrlOptions { Height = 1920, Width = 660 }; carouselImageModel.CarouselImage = MediaManager.GetMediaUrl(carouselMedia, options); } //get the content link var contentLinkField = image.Fields["ContentLink"].ToString(); if (!string.IsNullOrWhiteSpace(contentLinkField)) { var contentLink = database.GetItem(contentLinkField); if (contentLink != null) { carouselImageModel.CarouselLink = LinkManager.GetItemUrl(contentLink); } } //add it to the model items model.CarouselItems.Add(carouselImageModel); } } } } } } return(View(model)); }