public IElementContent GetContent(IElementSettings settings, IPageContext pageContext, IUnitOfWork unitOfWork = null)
        {
            // Construct nav bar content
            NavBarContent navBarContent = new NavBarContent {
                PartialViewName = "NavBar", PageTabs = new List <NavBarPageTab>()
            };

            // Get full page information for pages identified in nav bar tabs
            NavBarSettings navBarSettings = (NavBarSettings)settings;

            foreach (NavBarTab tab in navBarSettings.Tabs)
            {
                Page page = _pageService.Read(tab.TenantId, tab.PageId, unitOfWork);
                if (page != null)
                {
                    Page pageHierarchy = pageContext.Hierarchy;
                    bool active        = false;
                    while (!active && pageHierarchy != null)
                    {
                        active        = pageHierarchy.PageId == page.PageId;
                        pageHierarchy = pageHierarchy.ChildPages.Count == 0 ? null : pageHierarchy.ChildPages[0];
                    }
                    navBarContent.PageTabs.Add(new NavBarPageTab {
                        Page = page, Name = tab.Name, Active = active
                    });
                }
            }

            // Return nav bar content
            return(navBarContent);
        }
        public void AddNavigationPages(long tenantId, long elementId, List <Page> navigationPages, IUnitOfWork unitOfWork = null)
        {
            // Get nav bar settings
            NavBarSettings navBarSettings = (NavBarSettings)New(tenantId);

            navBarSettings.ElementId = elementId;

            // Populate nav bar settings
            _navBarRepository.Read(navBarSettings, unitOfWork);

            // Add navigation pages
            int sortOrder = navBarSettings.Tabs.Count;

            foreach (Page page in navigationPages)
            {
                sortOrder++;
                navBarSettings.Tabs.Add(new NavBarTab
                {
                    ElementId   = elementId,
                    Name        = page.Name,
                    NavBarTabId = 0,
                    PageId      = page.PageId,
                    TenantId    = page.TenantId,
                    SortOrder   = sortOrder
                });
            }

            // Update nav bar settings
            _navBarRepository.Update(navBarSettings, unitOfWork);
        }
        private NavBarTabCollection GetNavBarTabCollection(NavBarSettings settings)
        {
            NavBarTabCollection navBarTabCollection = new NavBarTabCollection();

            foreach (NavBarTab tab in settings.Tabs)
            {
                navBarTabCollection.Add(tab);
            }
            return(navBarTabCollection);
        }
        public Form GetForm(string context)
        {
            // Check permissions
            _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements);

            // Get page and element identifiers
            string[] parts     = context.Split('|');
            long     pageId    = Convert.ToInt64(parts[0]);
            long     elementId = Convert.ToInt64(parts[1]);

            // Get current nav bar settings
            Guid elementTypeId = FormId;
            IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(elementTypeId);
            NavBarSettings          navBarSettings = (NavBarSettings)elementService.New(_authenticationService.TenantId);

            navBarSettings.ElementId = elementId;
            elementService.Read(navBarSettings);

            // Construct form
            Form form = new Form {
                Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context
            };

            form.Fields.Add("navBarName", new TextField
            {
                Name  = "navBarName",
                Label = ElementResource.NavBarNameLabel,
                Value = navBarSettings.NavBarName
            });
            form.Fields.Add("showLoggedOffUserOptions", new BooleanField
            {
                Name  = "showLoggedOffUserOptions",
                Label = ElementResource.ShowLoggedOffUserOptionsLabel,
                Value = navBarSettings.ShowLoggedOffUserOptions
            });
            form.Fields.Add("showLoggedOnUserOptions", new BooleanField
            {
                Name  = "showLoggedOnUserOptions",
                Label = ElementResource.ShowLoggedOnUserOptionsLabel,
                Value = navBarSettings.ShowLoggedOnUserOptions
            });
            form.SubmitLabel = ElementResource.NavBarButtonLabel;

            // Get form field sets
            IEnumerable <Page> foldersAndDocuments = GetFoldersAndDocuments();

            form.FieldSets      = GetFieldSets(navBarSettings, foldersAndDocuments);
            form.NamedFieldSets = GetNamedFieldSets(foldersAndDocuments);

            // Return result
            return(form);
        }
        private IList <FormFieldSet> GetFieldSets(NavBarSettings settings, IEnumerable <Page> foldersAndDocuments)
        {
            List <FormFieldSet> fieldSets = new List <FormFieldSet>();

            foreach (NavBarTab tab in settings.Tabs)
            {
                FormFieldSet fieldSet = new FormFieldSet {
                    Fields = new Dictionary <string, IFormField>()
                };
                fieldSet.Fields.Add("name", GetNameTextField(tab.NavBarTabId, tab.Name));
                fieldSet.Fields.Add("tab", GetPageSelectListField(tab.NavBarTabId, tab.PageId, foldersAndDocuments));
                fieldSets.Add(fieldSet);
            }
            return(fieldSets);
        }
        public FormResult PostForm(Form form)
        {
            try
            {
                // Check permissions
                _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements);

                // Get page and element identifiers
                string[] parts     = form.Context.Split('|');
                long     pageId    = Convert.ToInt64(parts[0]);
                long     elementId = Convert.ToInt64(parts[1]);

                // Get website identifier
                long tenantId = _authenticationService.TenantId;

                // Get the nav bar element service
                IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(FormId);

                // Get existing nav bar settings
                NavBarSettings navBarSettings = (NavBarSettings)elementService.New(_authenticationService.TenantId);
                navBarSettings.ElementId = elementId;

                // Update nav bar settings
                navBarSettings.Tabs       = GetTabs(tenantId, elementId, form);
                navBarSettings.NavBarName = string.IsNullOrWhiteSpace(((TextField)form.Fields["navBarName"]).Value) ? null : ((TextField)form.Fields["navBarName"]).Value;
                navBarSettings.ShowLoggedOffUserOptions = ((BooleanField)form.Fields["showLoggedOffUserOptions"]).Value;
                navBarSettings.ShowLoggedOnUserOptions  = ((BooleanField)form.Fields["showLoggedOnUserOptions"]).Value;

                // Perform the update
                elementService.Update(navBarSettings);

                // Return form result with no errors
                return(_formHelperService.GetFormResult());
            }
            catch (ValidationErrorException ex)
            {
                // Return form result containing errors
                return(_formHelperService.GetFormResultWithValidationErrors(ex.Errors));
            }
            catch (Exception)
            {
                // Return form result containing unexpected error message
                return(_formHelperService.GetFormResultWithErrorMessage(ApplicationResource.UnexpectedErrorMessage));
            }
        }
        public void Create(NavBarSettings settings, IUnitOfWork unitOfWork = null)
        {
            NavBarTabCollection navBarTabCollection = GetNavBarTabCollection(settings);
            IUnitOfWork         localUnitOfWork     = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork ?? localUnitOfWork);
                string           sql = _sqlManager.GetSql("Sql.CreateNavBar.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.AddParameter("@Name", FieldType.NVarChar, 50, settings.NavBarName ?? (object)DBNull.Value);
                dbm.AddParameter("@ShowLoggedOnUserOptions", FieldType.Bit, settings.ShowLoggedOnUserOptions);
                dbm.AddParameter("@ShowLoggedOffUserOptions", FieldType.Bit, settings.ShowLoggedOffUserOptions);
                dbm.AddTypedParameter("@NavBarTabs", FieldType.Structured, navBarTabCollection.Count == 0 ? null : navBarTabCollection, "element.NavBarTabTableType");
                dbm.ExecuteNonQuery();
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
            }
            catch
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
        public void Read(NavBarSettings settings, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                string sql = _sqlManager.GetSql("Sql.ReadNavBar.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.ExecuteReader();
                dbm.Read();
                settings.NavBarName = dbm.DataReaderValue("Name") == DBNull.Value ? null : (string)dbm.DataReaderValue("Name");
                settings.ShowLoggedOnUserOptions  = (bool)dbm.DataReaderValue("ShowLoggedOnUserOptions");
                settings.ShowLoggedOffUserOptions = (bool)dbm.DataReaderValue("ShowLoggedOffUserOptions");
                settings.Tabs = new List <NavBarTab>();
                dbm.Read();
                while (dbm.Read())
                {
                    settings.Tabs.Add(new NavBarTab
                    {
                        ElementId   = (long)dbm.DataReaderValue("ElementId"),
                        NavBarTabId = (long)dbm.DataReaderValue("NavBarTabId"),
                        Name        = (string)dbm.DataReaderValue("Name"),
                        SortOrder   = (int)dbm.DataReaderValue("SortOrder"),
                        TenantId    = (long)dbm.DataReaderValue("TenantId"),
                        PageId      = (long)dbm.DataReaderValue("PageId")
                    });
                }
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }