Ejemplo n.º 1
0
        /// <summary>
        /// Builds the mobile package that can be archived for deployment.
        /// </summary>
        /// <param name="applicationId">The application identifier.</param>
        /// <param name="deviceType">The type of device to build for.</param>
        /// <param name="versionId">The version identifier to use on this package.</param>
        /// <returns>An update package for the specified application and device type.</returns>
        public static UpdatePackage BuildMobilePackage(int applicationId, DeviceType deviceType, int versionId)
        {
            var    site               = SiteCache.Get(applicationId);
            string applicationRoot    = GlobalAttributesCache.Value("PublicApplicationRoot");
            var    additionalSettings = site.AdditionalSettings.FromJsonOrNull <AdditionalSiteSettings>();

            if (additionalSettings == null)
            {
                throw new Exception("Invalid or non-existing AdditionalSettings property on site.");
            }

            //
            // Get all the system phone formats.
            //
            var phoneFormats = DefinedTypeCache.Get(SystemGuid.DefinedType.COMMUNICATION_PHONE_COUNTRY_CODE)
                               .DefinedValues
                               .Select(dv => new MobilePhoneFormat
            {
                CountryCode      = dv.Value,
                MatchExpression  = dv.GetAttributeValue("MatchRegEx"),
                FormatExpression = dv.GetAttributeValue("FormatRegEx")
            })
                               .ToList();

            //
            // Get all the defined values.
            //
            var definedTypeGuids = new[]
            {
                SystemGuid.DefinedType.LOCATION_COUNTRIES,
                SystemGuid.DefinedType.LOCATION_ADDRESS_STATE,
                SystemGuid.DefinedType.PERSON_MARITAL_STATUS
            };
            var definedValues = new List <MobileDefinedValue>();

            foreach (var definedTypeGuid in definedTypeGuids)
            {
                var definedType = DefinedTypeCache.Get(definedTypeGuid);
                definedValues.AddRange(definedType.DefinedValues
                                       .Select(a => new MobileDefinedValue
                {
                    Guid            = a.Guid,
                    DefinedTypeGuid = a.DefinedType.Guid,
                    Value           = a.Value,
                    Description     = a.Description,
                    Attributes      = GetMobileAttributeValues(a, a.Attributes.Select(b => b.Value))
                }));
            }

            //
            // Build CSS Styles
            //
            var settings = additionalSettings.DownhillSettings;

            settings.Platform = DownhillPlatform.Mobile;           // ensure the settings are set to mobile

            var cssStyles = CssUtilities.BuildFramework(settings); // append custom css but parse it for downhill variables

            if (additionalSettings.CssStyle.IsNotNullOrWhiteSpace())
            {
                cssStyles += CssUtilities.ParseCss(additionalSettings.CssStyle, settings);
            }

            // Run Lava on CSS to enable color utilities
            cssStyles = cssStyles.ResolveMergeFields(Lava.LavaHelper.GetCommonMergeFields(null, null, new Lava.CommonMergeFieldsOptions {
                GetLegacyGlobalMergeFields = false
            }));

            // Get the Rock organization time zone. If not found back to the
            // OS time zone. If not found just use Greenwich.
            var timeZoneMapping = NodaTime.TimeZones.TzdbDateTimeZoneSource.Default.WindowsMapping.PrimaryMapping;
            var timeZoneName    = timeZoneMapping.GetValueOrNull(RockDateTime.OrgTimeZoneInfo.Id)
                                  ?? timeZoneMapping.GetValueOrNull(TimeZoneInfo.Local.Id)
                                  ?? "GMT";

            //
            // Initialize the base update package settings.
            //
            var package = new UpdatePackage
            {
                ApplicationType                  = additionalSettings.ShellType ?? ShellType.Blank,
                ApplicationVersionId             = versionId,
                CssStyles                        = cssStyles,
                LoginPageGuid                    = site.LoginPageId.HasValue ? PageCache.Get(site.LoginPageId.Value)?.Guid : null,
                ProfileDetailsPageGuid           = additionalSettings.ProfilePageId.HasValue ? PageCache.Get(additionalSettings.ProfilePageId.Value)?.Guid : null,
                PhoneFormats                     = phoneFormats,
                DefinedValues                    = definedValues,
                TabsOnBottomOnAndroid            = additionalSettings.TabLocation == TabLocation.Bottom,
                HomepageRoutingLogic             = additionalSettings.HomepageRoutingLogic,
                DoNotEnableNotificationsAtLaunch = !additionalSettings.EnableNotificationsAutomatically,
                TimeZone             = timeZoneName,
                PushTokenUpdateValue = additionalSettings.PushTokenUpdateValue
            };

            //
            // Setup the appearance settings.
            //
            package.AppearanceSettings.BarBackgroundColor     = additionalSettings.BarBackgroundColor;
            package.AppearanceSettings.MenuButtonColor        = additionalSettings.MenuButtonColor;
            package.AppearanceSettings.ActivityIndicatorColor = additionalSettings.ActivityIndicatorColor;
            package.AppearanceSettings.FlyoutXaml             = additionalSettings.FlyoutXaml;
            package.AppearanceSettings.ToastXaml = additionalSettings.ToastXaml;
            package.AppearanceSettings.NavigationBarActionsXaml = additionalSettings.NavigationBarActionXaml;
            package.AppearanceSettings.LockedPhoneOrientation   = additionalSettings.LockedPhoneOrientation;
            package.AppearanceSettings.LockedTabletOrientation  = additionalSettings.LockedTabletOrientation;
            package.AppearanceSettings.PaletteColors.Add("text-color", additionalSettings.DownhillSettings.TextColor);
            package.AppearanceSettings.PaletteColors.Add("heading-color", additionalSettings.DownhillSettings.HeadingColor);
            package.AppearanceSettings.PaletteColors.Add("background-color", additionalSettings.DownhillSettings.BackgroundColor);
            package.AppearanceSettings.PaletteColors.Add("app-primary", additionalSettings.DownhillSettings.ApplicationColors.Primary);
            package.AppearanceSettings.PaletteColors.Add("app-secondary", additionalSettings.DownhillSettings.ApplicationColors.Secondary);
            package.AppearanceSettings.PaletteColors.Add("app-success", additionalSettings.DownhillSettings.ApplicationColors.Success);
            package.AppearanceSettings.PaletteColors.Add("app-info", additionalSettings.DownhillSettings.ApplicationColors.Info);
            package.AppearanceSettings.PaletteColors.Add("app-danger", additionalSettings.DownhillSettings.ApplicationColors.Danger);
            package.AppearanceSettings.PaletteColors.Add("app-warning", additionalSettings.DownhillSettings.ApplicationColors.Warning);
            package.AppearanceSettings.PaletteColors.Add("app-light", additionalSettings.DownhillSettings.ApplicationColors.Light);
            package.AppearanceSettings.PaletteColors.Add("app-dark", additionalSettings.DownhillSettings.ApplicationColors.Dark);
            package.AppearanceSettings.PaletteColors.Add("app-brand", additionalSettings.DownhillSettings.ApplicationColors.Brand);

            if (site.FavIconBinaryFileId.HasValue)
            {
                package.AppearanceSettings.LogoUrl = $"{applicationRoot}/GetImage.ashx?Id={site.FavIconBinaryFileId.Value}";
            }

            //
            // Load all the layouts.
            //
            foreach (var layout in LayoutCache.All().Where(l => l.SiteId == site.Id))
            {
                var mobileLayout = new MobileLayout
                {
                    LayoutGuid = layout.Guid,
                    Name       = layout.Name,
                    LayoutXaml = deviceType == DeviceType.Tablet ? layout.LayoutMobileTablet : layout.LayoutMobilePhone
                };

                package.Layouts.Add(mobileLayout);
            }

            //
            // Load all the pages.
            //
            var blockIds = new List <int>();

            using (var rockContext = new RockContext())
            {
                AddPagesToUpdatePackage(package, applicationRoot, rockContext, new[] { PageCache.Get(site.DefaultPageId.Value) });

                blockIds = new BlockService(rockContext).Queryable()
                           .Where(b => b.Page != null && b.Page.Layout.SiteId == site.Id && b.BlockType.EntityTypeId.HasValue)
                           .OrderBy(b => b.Order)
                           .Select(b => b.Id)
                           .ToList();
            }

            //
            // Load all the blocks.
            //
            foreach (var blockId in blockIds)
            {
                var block           = BlockCache.Get(blockId);
                var blockEntityType = block?.BlockType.EntityType.GetEntityType();

                if (blockEntityType != null && typeof(Rock.Blocks.IRockMobileBlockType).IsAssignableFrom(blockEntityType))
                {
                    var additionalBlockSettings = block.AdditionalSettings.FromJsonOrNull <AdditionalBlockSettings>() ?? new AdditionalBlockSettings();

                    var mobileBlockEntity = (Rock.Blocks.IRockMobileBlockType)Activator.CreateInstance(blockEntityType);

                    mobileBlockEntity.BlockCache     = block;
                    mobileBlockEntity.PageCache      = block.Page;
                    mobileBlockEntity.RequestContext = new Net.RockRequestContext();

                    var attributes = block.Attributes
                                     .Select(a => a.Value)
                                     .Where(a => a.Categories.Any(c => c.Name == "custommobile"));

                    var mobileBlock = new MobileBlock
                    {
                        PageGuid            = block.Page.Guid,
                        Zone                = block.Zone,
                        BlockGuid           = block.Guid,
                        RequiredAbiVersion  = mobileBlockEntity.RequiredMobileAbiVersion,
                        BlockType           = mobileBlockEntity.MobileBlockType,
                        ConfigurationValues = mobileBlockEntity.GetBlockInitialization(Blocks.RockClientType.Mobile),
                        Order               = block.Order,
                        AttributeValues     = GetMobileAttributeValues(block, attributes),
                        PreXaml             = block.PreHtml,
                        PostXaml            = block.PostHtml,
                        CssClasses          = block.CssClass,
                        CssStyles           = additionalBlockSettings.CssStyles,
                        ShowOnTablet        = additionalBlockSettings.ShowOnTablet,
                        ShowOnPhone         = additionalBlockSettings.ShowOnPhone,
                        RequiresNetwork     = additionalBlockSettings.RequiresNetwork,
                        NoNetworkContent    = additionalBlockSettings.NoNetworkContent,
                        AuthorizationRules  = string.Join(",", GetOrderedExplicitAuthorizationRules(block))
                    };

                    package.Blocks.Add(mobileBlock);
                }
            }

            //
            // Load all the campuses.
            //
            foreach (var campus in CampusCache.All().Where(c => c.IsActive ?? true))
            {
                var mobileCampus = new MobileCampus
                {
                    Guid = campus.Guid,
                    Name = campus.Name
                };

                if (campus.Location != null)
                {
                    if (campus.Location.Latitude.HasValue && campus.Location.Longitude.HasValue)
                    {
                        mobileCampus.Latitude  = campus.Location.Latitude;
                        mobileCampus.Longitude = campus.Location.Longitude;
                    }

                    if (!string.IsNullOrWhiteSpace(campus.Location.Street1))
                    {
                        mobileCampus.Street1    = campus.Location.Street1;
                        mobileCampus.City       = campus.Location.City;
                        mobileCampus.State      = campus.Location.State;
                        mobileCampus.PostalCode = campus.Location.PostalCode;
                    }
                }

                // Get the campus time zone, If not found try the Rock
                // organization time zone. If not found back to the
                // OS time zone. If not found just use Greenwich.
                mobileCampus.TimeZone = timeZoneMapping.GetValueOrNull(campus.TimeZoneId ?? string.Empty)
                                        ?? timeZoneMapping.GetValueOrNull(RockDateTime.OrgTimeZoneInfo.Id)
                                        ?? timeZoneMapping.GetValueOrNull(TimeZoneInfo.Local.Id)
                                        ?? "GMT";

                package.Campuses.Add(mobileCampus);
            }

            return(package);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds the mobile package that can be archived for deployment.
        /// </summary>
        /// <param name="applicationId">The application identifier.</param>
        /// <param name="deviceType">The type of device to build for.</param>
        /// <returns>An update package for the specified application and device type.</returns>
        public static UpdatePackage BuildMobilePackage(int applicationId, DeviceType deviceType)
        {
            var    site               = SiteCache.Get(applicationId);
            string applicationRoot    = GlobalAttributesCache.Value("PublicApplicationRoot");
            var    additionalSettings = site.AdditionalSettings.FromJsonOrNull <AdditionalSiteSettings>();

            if (additionalSettings.IsNull())
            {
                throw new Exception("Invalid or non-existing AdditionalSettings property on site.");
            }

            //
            // Get all the system phone formats.
            //
            var phoneFormats = DefinedTypeCache.Get(SystemGuid.DefinedType.COMMUNICATION_PHONE_COUNTRY_CODE)
                               .DefinedValues
                               .Select(dv => new MobilePhoneFormat
            {
                CountryCode      = dv.Value,
                MatchExpression  = dv.GetAttributeValue("MatchRegEx"),
                FormatExpression = dv.GetAttributeValue("FormatRegEx")
            })
                               .ToList();

            //
            // Build CSS Styles
            //
            var settings = additionalSettings.DownhillSettings;

            settings.Platform = DownhillPlatform.Mobile;           // ensure the settings are set to mobile

            var cssStyles = CssUtilities.BuildFramework(settings); // append custom css but parse it for downhill variables

            if (additionalSettings.CssStyle.IsNotNullOrWhiteSpace())
            {
                cssStyles += CssUtilities.ParseCss(additionalSettings.CssStyle, settings);
            }

            //
            // Initialize the base update package settings.
            //
            var package = new UpdatePackage
            {
                ApplicationType        = additionalSettings.ShellType ?? ShellType.Blank,
                ApplicationVersionId   = ( int )(RockDateTime.Now.ToJavascriptMilliseconds() / 1000),
                CssStyles              = cssStyles,
                LoginPageGuid          = site.LoginPageId.HasValue ? PageCache.Get(site.LoginPageId.Value)?.Guid : null,
                ProfileDetailsPageGuid = additionalSettings.ProfilePageId.HasValue ? PageCache.Get(additionalSettings.ProfilePageId.Value)?.Guid : null,
                PhoneFormats           = phoneFormats,
                TabsOnBottomOnAndroid  = additionalSettings.TabLocation == TabLocation.Bottom
            };

            //
            // Setup the appearance settings.
            //
            package.AppearanceSettings.BarBackgroundColor      = additionalSettings.BarBackgroundColor;
            package.AppearanceSettings.MenuButtonColor         = additionalSettings.MenuButtonColor;
            package.AppearanceSettings.ActivityIndicatorColor  = additionalSettings.ActivityIndicatorColor;
            package.AppearanceSettings.FlyoutXaml              = additionalSettings.FlyoutXaml;
            package.AppearanceSettings.LockedPhoneOrientation  = additionalSettings.LockedPhoneOrientation;
            package.AppearanceSettings.LockedTabletOrientation = additionalSettings.LockedTabletOrientation;

            if (site.FavIconBinaryFileId.HasValue)
            {
                package.AppearanceSettings.LogoUrl = $"{applicationRoot}/GetImage.ashx?Id={site.FavIconBinaryFileId.Value}";
            }

            //
            // Load all the layouts.
            //
            foreach (var layout in LayoutCache.All().Where(l => l.SiteId == site.Id))
            {
                var mobileLayout = new MobileLayout
                {
                    LayoutGuid = layout.Guid,
                    Name       = layout.Name,
                    LayoutXaml = deviceType == DeviceType.Tablet ? layout.LayoutMobileTablet : layout.LayoutMobilePhone
                };

                package.Layouts.Add(mobileLayout);
            }

            //
            // Load all the pages.
            //
            using (var rockContext = new RockContext())
            {
                AddPagesToUpdatePackage(package, applicationRoot, rockContext, new[] { PageCache.Get(site.DefaultPageId.Value) });
            }

            //
            // Load all the blocks.
            //
            foreach (var block in BlockCache.All().Where(b => b.Page != null && b.Page.SiteId == site.Id && b.BlockType.EntityTypeId.HasValue).OrderBy(b => b.Order))
            {
                var blockEntityType = block.BlockType.EntityType.GetEntityType();

                if (typeof(Rock.Blocks.IRockMobileBlockType).IsAssignableFrom(blockEntityType))
                {
                    var additionalBlockSettings = block.AdditionalSettings.FromJsonOrNull <AdditionalBlockSettings>() ?? new AdditionalBlockSettings();

                    var mobileBlockEntity = (Rock.Blocks.IRockMobileBlockType)Activator.CreateInstance(blockEntityType);

                    mobileBlockEntity.BlockCache     = block;
                    mobileBlockEntity.PageCache      = block.Page;
                    mobileBlockEntity.RequestContext = new Net.RockRequestContext();

                    var attributes = block.Attributes
                                     .Select(a => a.Value)
                                     .Where(a => a.Categories.Any(c => c.Name == "custommobile"));

                    var mobileBlock = new MobileBlock
                    {
                        PageGuid            = block.Page.Guid,
                        Zone                = block.Zone,
                        BlockGuid           = block.Guid,
                        RequiredAbiVersion  = mobileBlockEntity.RequiredMobileAbiVersion,
                        BlockType           = mobileBlockEntity.MobileBlockType,
                        ConfigurationValues = mobileBlockEntity.GetMobileConfigurationValues(),
                        Order               = block.Order,
                        AttributeValues     = GetMobileAttributeValues(block, attributes),
                        PreXaml             = block.PreHtml,
                        PostXaml            = block.PostHtml,
                        CssClasses          = block.CssClass,
                        ShowOnTablet        = additionalBlockSettings.ShowOnTablet,
                        ShowOnPhone         = additionalBlockSettings.ShowOnPhone,
                        RequiresNetwork     = additionalBlockSettings.RequiresNetwork,
                        NoNetworkContent    = additionalBlockSettings.NoNetworkContent,
                        AuthorizationRules  = string.Join(",", GetOrderedExplicitAuthorizationRules(block))
                    };

                    package.Blocks.Add(mobileBlock);
                }
            }

            //
            // Load all the campuses.
            //
            foreach (var campus in CampusCache.All().Where(c => c.IsActive ?? true))
            {
                var mobileCampus = new MobileCampus
                {
                    Guid = campus.Guid,
                    Name = campus.Name
                };

                if (campus.Location != null)
                {
                    if (campus.Location.Latitude.HasValue && campus.Location.Longitude.HasValue)
                    {
                        mobileCampus.Latitude  = campus.Location.Latitude;
                        mobileCampus.Longitude = campus.Location.Longitude;
                    }

                    if (!string.IsNullOrWhiteSpace(campus.Location.Street1))
                    {
                        mobileCampus.Street1    = campus.Location.Street1;
                        mobileCampus.City       = campus.Location.City;
                        mobileCampus.State      = campus.Location.State;
                        mobileCampus.PostalCode = campus.Location.PostalCode;
                    }
                }

                package.Campuses.Add(mobileCampus);
            }

            return(package);
        }