public override IList <SearchDocument> GetModifiedSearchDocuments(ModuleInfo modInfo, DateTime beginDateUtc)
        {
            App.Services.Logger.Trace($"Indexing content Module {modInfo.ModuleID} - Tab {modInfo.TabID} - Culture {modInfo.CultureCode}- indexing from {beginDateUtc}");
            var searchDocuments = new List <SearchDocument>();

            //If module is marked as "don't index" then return no results
            if (modInfo.ModuleSettings.GetValue("AllowIndex", "True") == "False")
            {
                App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} - NOT - MODULE Indexing disabled");
                return(searchDocuments);
            }

            //If tab of the module is marked as "don't index" then return no results
            if (modInfo.ParentTab.TabSettings.GetValue("AllowIndex", "True") == "False")
            {
                App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} - NOT - TAB Indexing disabled");
                return(searchDocuments);
            }

            //If tab is marked as "inactive" then return no results
            if (modInfo.ParentTab.DisableLink)
            {
                App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} - NOT - TAB is inactive");
                return(searchDocuments);
            }

            var module = OpenContentModuleConfig.Create(modInfo, PortalSettings.Current);

            if (module.Settings.Template?.Main == null || !module.Settings.Template.Main.DnnSearch)
            {
                return(searchDocuments);
            }
            if (module.Settings.IsOtherModule)
            {
                return(searchDocuments);
            }

            IDataSource ds        = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
            var         dsContext = OpenContentUtils.CreateDataContext(module);

            IDataItems contentList = ds.GetAll(dsContext, null);

            if (!contentList.Items.Any())
            {
                App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} - NOT - No content found");
            }
            foreach (IDataItem content in contentList.Items)
            {
                if (content == null)
                {
                    App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} - NOT - Content is Null");
                }
                else if (content.LastModifiedOnDate.ToUniversalTime() > beginDateUtc &&
                         content.LastModifiedOnDate.ToUniversalTime() < DateTime.UtcNow)
                {
                    SearchDocument searchDoc;
                    if (DnnLanguageUtils.IsMultiLingualPortal(modInfo.PortalID))
                    {
                        // first process the default language module
                        var culture       = modInfo.CultureCode;
                        var localizedData = GetLocalizedContent(content.Data, culture);
                        searchDoc = CreateSearchDocument(modInfo, module.Settings, localizedData, content.Id, culture, content.Title, content.LastModifiedOnDate.ToUniversalTime());
                        searchDocuments.Add(searchDoc);
                        App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{culture} -  OK!  {searchDoc.Title} ({modInfo.TabID}) of {content.LastModifiedOnDate.ToUniversalTime()}");

                        // now do the same with any linked localized instances of this module
                        if (modInfo.LocalizedModules != null)
                        {
                            foreach (var localizedModule in modInfo.LocalizedModules)
                            {
                                culture       = localizedModule.Value.CultureCode;
                                localizedData = GetLocalizedContent(content.Data, culture);
                                searchDoc     = CreateSearchDocument(modInfo, module.Settings, localizedData, content.Id, culture, content.Title, content.LastModifiedOnDate.ToUniversalTime());
                                searchDocuments.Add(searchDoc);
                                App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{culture} -  OK!  {searchDoc.Title} ({modInfo.TabID}) of {content.LastModifiedOnDate.ToUniversalTime()}");
                            }
                        }
                    }
                    else
                    {
                        searchDoc = CreateSearchDocument(modInfo, module.Settings, content.Data, content.Id, "", content.Title, content.LastModifiedOnDate.ToUniversalTime());
                        searchDocuments.Add(searchDoc);
                        App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} -  OK!  {searchDoc.Title} ({modInfo.TabID}) of {content.LastModifiedOnDate.ToUniversalTime()}");
                    }
                }
                else
                {
                    App.Services.Logger.Trace($"Indexing content {modInfo.ModuleID}|{modInfo.CultureCode} - NOT - No need to index: lastmod {content.LastModifiedOnDate.ToUniversalTime()} ");
                }
            }
            return(searchDocuments);
        }
        public HttpResponseMessage Action(SubmitDTO req)
        {
            try
            {
                var    module          = new OpenContentModuleInfo(ActiveModule);
                string editRole        = module.Settings.Template.Manifest.GetEditRole();
                int    createdByUserid = -1;

                IDataSource ds        = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
                var         dsContext = OpenContentUtils.CreateDataContext(module, UserInfo.UserID);

                IDataItem dsItem = null;
                if (module.IsListMode())
                {
                    if (req.id != null)
                    {
                        var itemId = req.id;
                        dsItem = ds.Get(dsContext, itemId);
                        if (dsItem != null)
                        {
                            createdByUserid = dsItem.CreatedByUserId;
                        }
                    }
                }
                else
                {
                    dsContext.Single = true;
                    dsItem           = ds.Get(dsContext, null);
                    if (dsItem != null)
                    {
                        createdByUserid = dsItem.CreatedByUserId;
                    }
                }

                //todo: can't we do some of these checks at the beginning of this method to fail faster?
                if (!OpenContentUtils.HasEditPermissions(PortalSettings, ActiveModule, editRole, createdByUserid))
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized));
                }
                try
                {
                    var res = ds.Action(dsContext, req.action, dsItem, req.form);
                    return(Request.CreateResponse(HttpStatusCode.OK, new
                    {
                        isValid = true,
                        result = res
                    }));
                }
                catch (DataNotValidException ex)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, new
                    {
                        isValid = false,
                        validMessage = ex.Message
                    }));
                }
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
        public HttpResponseMessage Edit(string id)
        {
            try
            {
                var         moduleInfo = new OpenContentModuleInfo(ActiveModule);
                IDataSource ds         = DataSourceManager.GetDataSource(moduleInfo.Settings.Manifest.DataSource);
                var         dsContext  = OpenContentUtils.CreateDataContext(moduleInfo);
                IDataItem   dsItem     = null;
                if (moduleInfo.IsListMode())
                {
                    if (!string.IsNullOrEmpty(id)) // not a new item
                    {
                        dsItem = ds.Get(dsContext, id);
                    }
                }
                else
                {
                    dsContext.Single = true;
                    dsItem           = ds.Get(dsContext, null);
                }
                int createdByUserid = -1;
                var json            = ds.GetAlpaca(dsContext, true, true, true);
                if (ds is IDataActions)
                {
                    var actions = ((IDataActions)ds).GetActions(dsContext, dsItem);
                    if (json["options"] == null)
                    {
                        json["options"] = new JObject();
                    }
                    if (json["options"]["form"] == null)
                    {
                        json["options"]["form"] = new JObject();
                    }
                    if (json["options"]["form"]["buttons"] == null)
                    {
                        json["options"]["form"]["buttons"] = new JObject();
                    }
                    var buttons    = json["options"]["form"]["buttons"] as JObject;
                    var newButtons = new JObject();
                    foreach (var act in actions)
                    {
                        var but = buttons[act.Name];
                        if (but == null)
                        {
                            but = new JObject();
                        }
                        but["after"]         = act.AfterExecute;
                        newButtons[act.Name] = but;
                    }
                    json["options"]["form"]["buttons"] = newButtons;
                }
                if (dsItem != null)
                {
                    json["data"] = dsItem.Data;
                    if (json["schema"]["properties"]["ModuleTitle"] is JObject)
                    {
                        if (json["data"]["ModuleTitle"] != null && json["data"]["ModuleTitle"].Type == JTokenType.String)
                        {
                            json["data"]["ModuleTitle"] = ActiveModule.ModuleTitle;
                        }
                        else if (json["data"]["ModuleTitle"] != null && json["data"]["ModuleTitle"].Type == JTokenType.Object)
                        {
                            json["data"]["ModuleTitle"][DnnLanguageUtils.GetCurrentCultureCode()] = ActiveModule.ModuleTitle;
                        }
                    }
                    var versions = ds.GetVersions(dsContext, dsItem);
                    if (versions != null)
                    {
                        json["versions"] = versions;
                    }
                    createdByUserid = dsItem.CreatedByUserId;
                }

                var context       = new JObject();
                var currentLocale = DnnLanguageUtils.GetCurrentLocale(PortalSettings.PortalId);
                context["culture"]                = currentLocale.Code; //todo why not use  DnnLanguageUtils.GetCurrentCultureCode() ???
                context["defaultCulture"]         = LocaleController.Instance.GetDefaultLocale(PortalSettings.PortalId).Code;
                context["numberDecimalSeparator"] = currentLocale.Culture.NumberFormat.NumberDecimalSeparator;
                context["rootUrl"]                = System.Web.VirtualPathUtility.ToAbsolute(string.Concat(System.Web.HttpRuntime.AppDomainAppVirtualPath, "/"));
                context["alpacaCulture"]          = AlpacaEngine.AlpacaCulture(currentLocale.Code);
                context["bootstrap"]              = OpenContentControllerFactory.Instance.OpenContentGlobalSettingsController(PortalSettings.PortalId).GetEditLayout() != AlpacaLayoutEnum.DNN;
                context["horizontal"]             = OpenContentControllerFactory.Instance.OpenContentGlobalSettingsController(PortalSettings.PortalId).GetEditLayout() == AlpacaLayoutEnum.BootstrapHorizontal;
                json["context"] = context;

                //todo: can't we do some of these checks at the beginning of this method to fail faster?
                if (!OpenContentUtils.HasEditPermissions(PortalSettings, moduleInfo.ViewModule, moduleInfo.Settings.Manifest.GetEditRole(), createdByUserid))
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, json));
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
        public HttpResponseMessage Update(JObject json)
        {
            try
            {
                var    module          = new OpenContentModuleInfo(ActiveModule);
                string editRole        = module.Settings.Template.Manifest.GetEditRole();
                int    createdByUserid = -1;

                IDataSource ds        = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
                var         dsContext = OpenContentUtils.CreateDataContext(module, UserInfo.UserID);

                IDataItem dsItem = null;
                if (module.IsListMode())
                {
                    if (json["id"] != null)
                    {
                        var itemId = json["id"].ToString();
                        dsItem = ds.Get(dsContext, itemId);
                        if (dsItem != null)
                        {
                            createdByUserid = dsItem.CreatedByUserId;
                        }
                    }
                }
                else
                {
                    dsContext.Single = true;
                    dsItem           = ds.Get(dsContext, null);
                    if (dsItem != null)
                    {
                        createdByUserid = dsItem.CreatedByUserId;
                    }
                }

                //todo: can't we do some of these checks at the beginning of this method to fail faster?
                if (!OpenContentUtils.HasEditPermissions(PortalSettings, ActiveModule, editRole, createdByUserid))
                {
                    return(Request.CreateResponse(HttpStatusCode.Unauthorized));
                }

                AddNotifyInfo(dsContext);
                try
                {
                    if (dsItem == null)
                    {
                        ds.Add(dsContext, json["form"] as JObject);
                    }
                    else
                    {
                        ds.Update(dsContext, dsItem, json["form"] as JObject);
                    }
                }
                catch (DataNotValidException ex)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, new
                    {
                        isValid = false,
                        validMessage = ex.Message
                    }));
                }

                if (json["form"]["ModuleTitle"] != null && json["form"]["ModuleTitle"].Type == JTokenType.String)
                {
                    string moduleTitle = json["form"]["ModuleTitle"].ToString();
                    ActiveModule.UpdateModuleTitle(moduleTitle);
                }
                else if (json["form"]["ModuleTitle"] != null && json["form"]["ModuleTitle"].Type == JTokenType.Object)
                {
                    if (json["form"]["ModuleTitle"][DnnLanguageUtils.GetCurrentCultureCode()] != null)
                    {
                        string moduleTitle = json["form"]["ModuleTitle"][DnnLanguageUtils.GetCurrentCultureCode()].ToString();
                        ActiveModule.UpdateModuleTitle(moduleTitle);
                    }
                }
                return(Request.CreateResponse(HttpStatusCode.OK, new
                {
                    isValid = true
                }));
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
        public HttpResponseMessage Lookup(LookupRequestDTO req)
        {
            int moduleid = req.moduleid > 0 ? req.moduleid : ActiveModule.ModuleID;
            int tabid    = req.tabid > 0 ? req.tabid : ActiveModule.TabID;
            var module   = new OpenContentModuleInfo(moduleid, tabid);

            if (module == null)
            {
                throw new Exception($"Can not find ModuleInfo (tabid:{req.tabid}, moduleid:{req.moduleid})");
            }

            List <LookupResultDTO> res = new List <LookupResultDTO>();

            try
            {
                IDataSource ds        = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
                var         dsContext = OpenContentUtils.CreateDataContext(module, UserInfo.UserID);

                if (module.IsListMode())
                {
                    var items = ds.GetAll(dsContext, null).Items;
                    if (items != null)
                    {
                        foreach (var item in items)
                        {
                            var json = item.Data;

                            if (!string.IsNullOrEmpty(req.dataMember) && json[req.dataMember] != null)
                            {
                                json = json[req.dataMember];
                            }

                            json = json.DeepClone();
                            JsonUtils.SimplifyJson(json, DnnLanguageUtils.GetCurrentCultureCode());

                            var array = json as JArray;
                            if (array != null)
                            {
                                res.AddRange(array.Select(childItem =>
                                                          new LookupResultDTO
                                {
                                    value = string.IsNullOrEmpty(req.valueField) || childItem[req.valueField] == null ? "" : childItem[req.valueField].ToString(),
                                    text  = string.IsNullOrEmpty(req.textField) || childItem[req.textField] == null ? "" : childItem[req.textField].ToString()
                                }
                                                          )
                                             );
                            }
                            else
                            {
                                res.Add(new LookupResultDTO
                                {
                                    value = string.IsNullOrEmpty(req.valueField) || json[req.valueField] == null ? item.Id : json[req.valueField].ToString(),
                                    text  = string.IsNullOrEmpty(req.textField) || json[req.textField] == null ? item.Title : json[req.textField].ToString()
                                });
                            }
                        }
                    }
                }
                else
                {
                    dsContext.Single = true;
                    var struc = ds.Get(dsContext, null);
                    if (struc != null)
                    {
                        JToken json = struc.Data;
                        if (!string.IsNullOrEmpty(req.dataMember))
                        {
                            json = json[req.dataMember];
                            json = json.DeepClone();
                            JsonUtils.SimplifyJson(json, DnnLanguageUtils.GetCurrentCultureCode());
                            if (json is JArray)
                            {
                                foreach (JToken item in (JArray)json)
                                {
                                    res.Add(new LookupResultDTO()
                                    {
                                        value = item[req.valueField] == null ? "" : item[req.valueField].ToString(),
                                        text  = item[req.textField] == null ? "" : item[req.textField].ToString()
                                    });
                                }
                            }
                        }
                    }
                }
                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }