public void AddPortlet(ContentItem projectDashboard, ContentItem portletTemplate, int position) { ContentItem newPortlet = null; if (portletTemplate.ContentType == ContentTypes.ProjectDashboardProjectionPortletTemplateContentType) { newPortlet = this.services.ContentManager.Create(ContentTypes.ProjectDashboardProjectionPortletContentType); ProjectionWithDynamicSortPart destinationProjectionPart = newPortlet.As <ProjectionWithDynamicSortPart>(); ProjectionWithDynamicSortPart sourceProjectionPart = portletTemplate.As <ProjectionWithDynamicSortPart>(); CRMHelper.Copy(layoutRepository, sourceProjectionPart.Record, destinationProjectionPart.Record); } else if (portletTemplate.ContentType == ContentTypes.ProjectDashboardReportViewerPortletTemplateContentType) { newPortlet = this.services.ContentManager.Create(ContentTypes.ProjectDashboardReportViewerPortletContentType); DataReportViewerPart destinationReportViewerPart = newPortlet.As <DataReportViewerPart>(); DataReportViewerPart sourceReportViewerPart = portletTemplate.As <DataReportViewerPart>(); CRMHelper.Copy(sourceReportViewerPart.Record, destinationReportViewerPart.Record); } else if (portletTemplate.ContentType == ContentTypes.ProjectLastActivityStreamTemplateContentType) { newPortlet = this.services.ContentManager.Create(ContentTypes.ProjectLastActivityStreamContentType); var sourceActivityStreamPart = portletTemplate.As <ActivityStreamPart>(); var destinationActivityStreamPart = newPortlet.As <ActivityStreamPart>(); destinationActivityStreamPart.QueryId = sourceActivityStreamPart.QueryId; } else { throw new ArgumentOutOfRangeException("The ContentType of the portletTemplate is not supported."); } // store templateId newPortlet.As <InfosetPart>().Store <int>(FieldNames.ProjectDashboardPortletTemplateId, portletTemplate.Id); // copy Title TitlePart titlePart = newPortlet.As <TitlePart>(); TitlePart templateTitlePart = portletTemplate.As <TitlePart>(); titlePart.Title = templateTitlePart.Title; ContainablePart containablePart = newPortlet.As <ContainablePart>(); containablePart.Position = position; var projectDetailContainerPart = projectDashboard.As <ContainerPart>(); var newPortletCommon = newPortlet.As <CommonPart>(); newPortletCommon.Container = projectDashboard; this.services.ContentManager.Publish(newPortlet); }
public ContentItem CreateProjectMenu(ProjectPart project) { var editUrl = this.urlHelper.Action("Edit", "Project", new { id = project.Id, area = "Orchard.CRM.Project" }); // if editUrl is null, it means the routes are not existed yet, so all menu items will br crap, then the creation of the menu must be postponed to the first load of the project if (string.IsNullOrEmpty(editUrl)) { return(null); } // create menu var menu = this.menuService.Create(string.Format("Project-{0} --'{1}'", project.Id.ToString(CultureInfo.InvariantCulture), project.Record.Title)); project.MenuId = menu.Id; var contentManger = this.services.ContentManager; ContentItem menuContentItem = contentManger.Create(ContentTypes.ProjectMenuContentType); // update AttachToProjectPart AttachToProjectPart attachToProjectPart = menuContentItem.As <AttachToProjectPart>(); attachToProjectPart.Record.Project = project.Record; // update MenuWidgetPart MenuWidgetPart menuWidgetPart = menuContentItem.As <MenuWidgetPart>(); menuWidgetPart.AddHomePage = false; menuWidgetPart.Breadcrumb = false; menuWidgetPart.MenuContentItemId = menu.Id; // create menu items Action <string, LocalizedString> createMenu = (url, localizedText) => { var menuItemPart = contentManger.Create <MenuItemPart>("MenuItem"); menuItemPart.Url = url; var menuPart = menuItemPart.As <MenuPart>(); menuPart.MenuPosition = Position.GetNext(this.navigationManager.BuildMenu(menu)); menuPart.MenuText = localizedText.Text; menuPart.Menu = menu; contentManger.Publish(menuItemPart.ContentItem); }; Action <ContentItem, string> createProjectSubPartListUrl = (contentItem, title) => { var url = this.urlHelper.Action("Display", "Item", new { id = contentItem.Id, area = "Orchard.CRM.Core" }); createMenu(url, T(title)); }; var targetContentItems = contentManger.HqlQuery().ForType(new[] { ContentTypes.ProjectWikiContentType, ContentTypes.ProjectTicketsContentType, ContentTypes.ProjectDiscussionsContentType, ContentTypes.ProjectActivityStreamType, ContentTypes.ProjectProjectionContentType }).Where(c => c.ContentPartRecord <AttachToProjectPartRecord>(), d => d.Eq("Project.Id", project.Id)).List(); foreach (var contentItem in targetContentItems) { string title = string.Empty; if (contentItem.ContentType == ContentTypes.ProjectWikiContentType) { title = "Wiki"; } else { TitlePart titlePart = contentItem.As <TitlePart>(); title = titlePart.Title; if (string.IsNullOrEmpty(title)) { switch (contentItem.ContentType) { case ContentTypes.ProjectTicketsContentType: title = T("Tickets").Text; break; case ContentTypes.ProjectDiscussionsContentType: title = T("Discussion").Text; // version 2.1 creates two discussion for each project. Because of it, we have to apply this hack to prevent creation of two menus. ProjectionWithDynamicSortPart projectionWithDynamicSortPart = contentItem.As <ProjectionWithDynamicSortPart>(); if (projectionWithDynamicSortPart.Record.QueryPartRecord == null) { continue; } break; case ContentTypes.ProjectActivityStreamType: title = T("Activity Stream").Text; break; } } } createProjectSubPartListUrl(contentItem, title); } // Create link to backlog var backLogItem = contentManger .HqlQuery() .ForType(ContentTypes.MilestoneContentType) .Where(c => c.ContentPartRecord <AttachToProjectPartRecord>(), d => d.Eq("Project.Id", project.Id)) .List() .FirstOrDefault(c => c.As <MilestonePart>().IsBacklog); if (backLogItem != null) { createProjectSubPartListUrl(backLogItem, T("Backlog").Text); } // edit project editUrl = this.urlHelper.Action("Edit", "Project", new { id = project.Id, area = "Orchard.CRM.Project" }); createMenu(editUrl, T("Edit")); // Project People var peopleUrl = this.urlHelper.Action("Edit", "ProjectItemsOwnership", new { ids = project.Id, area = "Orchard.CRM.Project" }); createMenu(peopleUrl, T("People")); this.services.ContentManager.Publish(menuContentItem); return(menuContentItem); }