Esempio n. 1
0
        /// <summary>
        /// Create a module-content block
        /// </summary>
        /// <param name="moduleInfo">the dnn module-info</param>
        /// <param name="parentLog">a parent-log; can be null but where possible you should wire one up</param>
        /// <param name="overrideParams">optional override parameters</param>
        public ModuleContentBlock(ModuleInfo moduleInfo, Log parentLog, IEnumerable <KeyValuePair <string, string> > overrideParams = null) : base(parentLog, "CB.Mod")
        {
            ModuleInfo     = moduleInfo ?? throw new Exception("Need valid ModuleInfo / ModuleConfiguration of runtime");
            ParentId       = moduleInfo.ModuleID;
            ContentBlockId = ParentId;

            // url-params
            _urlParams = overrideParams ?? DnnWebForms.Helpers.SystemWeb.GetUrlParams();


            // Ensure we know what portal the stuff is coming from
            // PortalSettings is null, when in search mode
            PortalSettings = PortalSettings.Current == null || moduleInfo.OwnerPortalID != moduleInfo.PortalID
                ? new PortalSettings(moduleInfo.OwnerPortalID)
                : PortalSettings.Current;

            // important: don't use the SxcInstance.Environment, as it would try to init the Sxc-object before the app is known, causing various side-effects
            ZoneId = new Environment.DnnEnvironment(Log).ZoneMapper.GetZoneId(moduleInfo.OwnerPortalID); // ZoneHelpers.GetZoneId(moduleInfo.OwnerPortalID) ?? 0; // new

            AppId = AppHelpers.GetAppIdFromModule(moduleInfo, ZoneId) ?? 0;                              // fallback/undefined YET

            Log.Add($"parent#{ParentId}, content-block#{ContentBlockId}, z#{ZoneId}, a#{AppId}");

            if (AppId == Settings.DataIsMissingInDb)
            {
                _dataIsMissing = true;
                Log.Add("data is missing, will stop here");
                return;
            }

            if (AppId != 0)
            {
                Log.Add("real app, will load data");
                // try to load the app - if possible
                App = new App(ZoneId, AppId, PortalSettings, parentLog: Log);

                Configuration = ConfigurationProvider.GetConfigProviderForModule(moduleInfo.ModuleID, App, SxcInstance);

                // maybe ensure that App.Data is ready
                App.InitData(SxcInstance.Environment.Permissions.UserMayEditContent,
                             SxcInstance.Environment.PagePublishing
                             /*new Environment.Dnn7.PagePublishing(Log)*/.IsEnabled(moduleInfo.ModuleID),
                             Configuration);

                var res = App.ContentGroupManager.GetContentGroupForModule(moduleInfo.ModuleID, moduleInfo.TabID);
                var contentGroupGuid    = res.Item1;
                var previewTemplateGuid = res.Item2;
                ContentGroup = App.ContentGroupManager.GetContentGroupOrGeneratePreview(contentGroupGuid, previewTemplateGuid);

                //ContentGroup = App.ContentGroupManager.GetContentGroupForModule(moduleInfo.ModuleID, moduleInfo.TabID);

                if (ContentGroup.DataIsMissing)
                {
                    _dataIsMissing = true;
                    App            = null;
                    return;
                }

                // use the content-group template, which already covers stored data + module-level stored settings
                //Template = ContentGroup.Template;

                SxcInstance.SetTemplateOrOverrideFromUrl(ContentGroup.Template);

                //SxcInstance.CheckTemplateOverrides();

                // 2017-09-14 2dm disabled, as the TemplateChooser State isn't a thing any more
                // set show-status of the template/view picker
                //var showStatus = moduleInfo.ModuleSettings[Settings.SettingsShowTemplateChooser];
                //bool show;
                //if (bool.TryParse((showStatus ?? true).ToString(), out show))
                //    ShowTemplateChooser = show;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get search info for each dnn module containing 2sxc data
        /// </summary>
        /// <param name="moduleInfo"></param>
        /// <param name="beginDate"></param>
        /// <returns></returns>
        public IList <SearchDocument> GetModifiedSearchDocuments(ModuleInfo moduleInfo, DateTime beginDate)
        {
            // always log with method, to ensure errors are cought
            Log.Add(() => $"start search for mod#{moduleInfo.ModuleID}");
            var searchDocuments = new List <SearchDocument>();
            var isContentModule = moduleInfo.DesktopModule.ModuleName == "2sxc";

            // New Context because PortalSettings.Current is null
            var zoneId = new Environment.DnnEnvironment(Log).ZoneMapper.GetZoneId(moduleInfo.OwnerPortalID);

            //if (!zoneId.HasValue)
            //    return searchDocuments;

            int?appId = new ZoneRuntime(zoneId, Log).DefaultAppId;

            if (!isContentModule)
            {
                appId = AppHelpers.GetAppIdFromModule(moduleInfo, zoneId);
                if (!appId.HasValue)
                {
                    return(searchDocuments);
                }
            }

            // new 2016-03-27
            var mcb  = new ModuleContentBlock(moduleInfo, Log);
            var sexy = mcb.SxcInstance;

            // old 2016-03-27
            var language = moduleInfo.CultureCode;
            //var contentGroup = sexy.App.ContentGroupManager./*AppContentGroups.*/GetContentGroupForModule(moduleInfo.ModuleID, moduleInfo.TabID);
            var res = sexy.App.ContentGroupManager.GetContentGroupForModule(moduleInfo.ModuleID, moduleInfo.TabID);
            var contentGroupGuid    = res.Item1;
            var previewTemplateGuid = res.Item2;
            var contentGroup        = sexy.App.ContentGroupManager.GetContentGroupOrGeneratePreview(contentGroupGuid, previewTemplateGuid);

            var template = contentGroup.Template;

            // This list will hold all EAV entities to be indexed
            // before 2016-02-27 2dm: var dataSource = sexy.GetViewDataSource(moduleInfo.ModuleID, false, template);
            var dataSource = sexy.Data;// ViewDataSource.ForModule(moduleInfo.ModuleID, false, template, sexy);

            if (template == null)
            {
                return(searchDocuments);
            }

            var engine = EngineFactory.CreateEngine(template);

            engine.Init(template, sexy.App, moduleInfo, dataSource, InstancePurposes.IndexingForSearch, sexy, Log);

            // see if data customization inside the cshtml works
            try
            {
                engine.CustomizeData();
            }
            catch (Exception e) // Catch errors here, because of references to Request etc.
            {
                Exceptions.LogException(new SearchIndexException(moduleInfo, e));
            }


            var searchInfoDictionary = new Dictionary <string, List <ISearchInfo> >();

            // Get DNN SearchDocuments from 2Sexy SearchInfos
            foreach (var stream in dataSource.Out.Where(p => p.Key != AppConstants.Presentation && p.Key != AppConstants.ListPresentation))
            {
                var entities       = stream.Value.List.Select(p => p.Value);
                var searchInfoList = searchInfoDictionary[stream.Key] = new List <ISearchInfo>();

                searchInfoList.AddRange(entities.Select(entity =>
                {
                    var searchInfo = new SearchInfo
                    {
                        Entity          = entity,
                        Url             = "",
                        Description     = "",
                        Body            = GetJoinedAttributes(entity, language),
                        Title           = entity.Title != null && entity.Title[language] != null ? entity.Title[language].ToString() : "(no title)",
                        ModifiedTimeUtc = (entity.Modified == DateTime.MinValue ? DateTime.Now.Date.AddHours(DateTime.Now.Hour) : entity.Modified).ToUniversalTime(),
                        UniqueKey       = "2sxc-" + moduleInfo.ModuleID + "-" + (entity.EntityGuid != new Guid() ? entity.EntityGuid.ToString() : (stream.Key + "-" + entity.EntityId)),
                        IsActive        = true,
                        TabId           = moduleInfo.TabID,
                        PortalId        = moduleInfo.PortalID
                    };

                    // Take the newest value (from ContentGroupItem and Entity)
                    if (entity is IHasEditingData)
                    {
                        var contentGroupItemModifiedUtc = ((IHasEditingData)entity).ContentGroupItemModified.ToUniversalTime();
                        searchInfo.ModifiedTimeUtc      = searchInfo.ModifiedTimeUtc > contentGroupItemModifiedUtc
                            ? searchInfo.ModifiedTimeUtc
                            : contentGroupItemModifiedUtc;
                    }

                    return(searchInfo);
                }));
            }

            // check if the cshtml has search customizations
            try
            {
                engine.CustomizeSearch(searchInfoDictionary, moduleInfo, beginDate);
            }
            catch (Exception e)
            {
                Exceptions.LogException(new SearchIndexException(moduleInfo, e));
            }

            // reduce load by only keeping recently modified ites
            foreach (var searchInfoList in searchInfoDictionary)
            {
                // Filter by Date - take only SearchDocuments that changed since beginDate
                var searchDocumentsToAdd = searchInfoList.Value.Where(p => p.ModifiedTimeUtc >= beginDate.ToUniversalTime()).Select(p => (SearchDocument)p);

                searchDocuments.AddRange(searchDocumentsToAdd);
            }

            return(searchDocuments);
        }