コード例 #1
0
        public ReturnObject GetFieldValues(RequestObject r)
        {
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            currentPage  = new PageSettings(siteSettings.SiteId, r.PageId);
            bool allowed = false;

            if (currentPage != null)
            {
                allowed = WebUser.IsInRoles(currentPage.AuthorizedRoles);
            }
            module = new Module(r.ModuleId);
            if (module != null)
            {
                allowed = WebUser.IsInRoles(module.ViewRoles);
            }
            if (!allowed)
            {
                return(new ReturnObject()
                {
                    Status = "error",
                    ExtraData = new Dictionary <string, string>
                    {
                        ["ErrorCode"] = "100",
                        ["ErrorMessage"] = "Not Allowed"
                    }
                });
            }

            config = new ModuleConfiguration(module);

            int totalPages = 0;
            int totalRows  = 0;

            var fieldValues = ItemFieldValue.GetPageOfValues(
                module.ModuleGuid,
                config.FieldDefinitionGuid,
                r.Field,
                r.PageNumber,
                r.PageSize,
                out totalPages,
                out totalRows);

            //much of the below is temporary, we needed to implement in a hurry
            //to-do: implement distinct on sql side
            List <string> values = new List <string>();

            var dbField = new Field(fieldValues.Select(fv => fv.FieldGuid).FirstOrDefault());

            switch (dbField.ControlType)
            {
            case "DynamicCheckBoxList":
                foreach (var val in fieldValues)
                {
                    values.AddRange(val.FieldValue.SplitOnCharAndTrim(';'));
                }
                break;

            default:
                values = fieldValues.Select(fv => fv.FieldValue).ToList();
                break;
                //we will add the other cases later
            }

            values = values.Distinct().OrderBy(v => v).ToList();

            totalRows = values.Count();

            return(new ReturnObject()
            {
                Status = "success",
                Data = values,
                TotalPages = totalPages,
                TotalRows = totalRows
            });
        }
コード例 #2
0
        public ReturnObject GetModuleItems(RequestObject r)
        {
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            currentPage  = new PageSettings(siteSettings.SiteId, r.PageId);
            bool allowed = false;
            bool canEdit = false;

            if (currentPage != null)
            {
                allowed = WebUser.IsInRoles(currentPage.AuthorizedRoles);
            }
            module = new Module(r.ModuleId);
            if (module != null)
            {
                allowed = WebUser.IsInRoles(module.ViewRoles);
            }
            if (!allowed)
            {
                return(new ReturnObject()
                {
                    Status = "error",
                    ExtraData = new Dictionary <string, string>
                    {
                        ["ErrorCode"] = "100",
                        ["ErrorMessage"] = "Not Allowed"
                    }
                });
            }

            config = new ModuleConfiguration(module);

            int         totalPages = 0;
            int         totalRows  = 0;
            List <Item> items      = new List <Item>();

            if (r.SearchObject != null && r.SearchObject.Count > 0)
            {
                foreach (var set in r.SearchObject)
                {
                    if (set.Value.Contains(";"))
                    {
                        foreach (var setA in set.Value.SplitOnCharAndTrim(';'))
                        {
                            items.AddRange(GetItems(
                                               module.ModuleGuid,
                                               r.PageNumber,
                                               r.PageSize,
                                               out totalPages,
                                               out totalRows,
                                               setA,
                                               set.Key,
                                               r.GetAllForSolution
                                               ));
                        }
                    }
                    else
                    {
                        items.AddRange(GetItems(
                                           module.ModuleGuid,
                                           r.PageNumber,
                                           r.PageSize,
                                           out totalPages,
                                           out totalRows,
                                           set.Value,
                                           set.Key,
                                           r.GetAllForSolution
                                           ));
                    }
                    //we have to figure out paging with this
                }
                items = items.Distinct(new SimpleItemComparer()).ToList();
            }
            else
            {
                items.AddRange(GetItems(
                                   module.ModuleGuid,
                                   r.PageNumber,
                                   r.PageSize,
                                   out totalPages,
                                   out totalRows,
                                   r.SearchTerm,
                                   r.SearchField,
                                   r.GetAllForSolution,
                                   r.SortDescending));
            }

            List <PopulatedItem> popItems = new List <PopulatedItem>();
            SuperFlexiObject     sfObject = new SuperFlexiObject()
            {
                FriendlyName    = config.ModuleFriendlyName,
                ModuleTitle     = module.ModuleTitle,
                GlobalSortOrder = config.GlobalViewSortOrder,
                Items           = popItems
            };

            if (items != null && items.Count > 0)
            {
                List <Field>          fields            = Field.GetAllForDefinition(config.FieldDefinitionGuid).Where(f => f.ControlType != "InstructionBlock").ToList();
                var                   itemGuids         = items.Select(x => x.ItemGuid).ToList();
                List <ItemFieldValue> values            = ItemFieldValue.GetByItemGuids(itemGuids);
                Module                itemModule        = null;
                Guid                  currentModuleGuid = Guid.Empty;
                foreach (Item item in items.OrderBy(x => x.ModuleID).ToList())
                {
                    if (item.ModuleGuid != currentModuleGuid)
                    {
                        currentModuleGuid = item.ModuleGuid;
                        itemModule        = new Module(item.ModuleGuid);
                    }
                    if (itemModule == null)
                    {
                        continue;
                    }
                    if (!WebUser.IsInRoles(itemModule.ViewRoles))
                    {
                        continue;
                    }
                    var populatedItem = new PopulatedItem(item, fields, values.Where(v => v.ItemGuid == item.ItemGuid).ToList(), canEdit);
                    if (populatedItem != null)
                    {
                        if (r.SearchObject != null && r.SearchObject.Count > 0)
                        {
                            int matchCount = 0;
                            foreach (var searchItem in r.SearchObject)
                            {
                                var           value              = populatedItem.Values[searchItem.Key];
                                List <string> itemValArray       = value as List <string>;
                                List <string> searchItemValArray = searchItem.Value.SplitOnCharAndTrim(';');
                                //log.Info($"[{searchItem.Key}]={searchItem.Value}");

                                /*  Check if itemValArray == null because if it is, that means the value is just a plain value, not a List<string>.
                                 *  If we try to do a comparison on value.ToString() when value is a List<string>, .ToString() returns System.Collections.Generic...
                                 *  and then our comparison is actually looking for matches in "System.Collections.Generic...". We had that happen with the word
                                 *  "Collections". Oops.
                                 */

                                if ((itemValArray == null && value.ToString().ToLower().IndexOf(searchItem.Value.ToLower()) >= 0) ||
                                    (itemValArray != null && itemValArray.Any(s => s.Equals(searchItem.Value, StringComparison.OrdinalIgnoreCase))) ||
                                    (searchItemValArray != null && searchItemValArray.Any(s => s.Equals(value.ToString(), StringComparison.OrdinalIgnoreCase))))
                                {
                                    matchCount++;
                                }
                            }

                            if (matchCount == r.SearchObject.Count)
                            {
                                popItems.Add(populatedItem);
                            }
                        }
                        else
                        {
                            popItems.Add(populatedItem);
                        }
                    }
                }
            }

            return(new ReturnObject()
            {
                Status = "success",
                Data = sfObject,
                TotalPages = totalPages,
                TotalRows = totalRows > popItems.Count ? totalRows : popItems.Count,
                AllowEdit = ShouldAllowEdit(),
                CmsModuleId = module.ModuleId,
                CmsPageId = module.PageId
            });
        }