public ActionResult ManageResources()
        {
            //set up the model/service and fetch data
            var model = new ResourceListViewModel();

            model.ResourceCategories  = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()).OrderBy(c => c.CategoryOrder);
            model.CountryAvailability = GlobalSettings.Markets.AvailableMarkets;
            model.Languages           = ExigoDAL.GetUniqueLanguages().ToList();
            model.ResourceTypes       = ExigoDAL.GetResourceTypes(new GetResourceTypeRequest()).OrderBy(rt => rt.SortOrder).ToList();
            model.CategoryTranslation = ExigoDAL.GetCategoryTranslations(new GetTranslatedCategoryRequest());
            model.Tags = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest());

            //Create ListItems for the Market/Language DropDown
            List <SelectListItem> items = new List <SelectListItem>();

            foreach (var market in model.CountryAvailability)
            {
                SelectListItem item = new SelectListItem()
                {
                    Value    = market.Countries.FirstOrDefault(),
                    Text     = CommonResources.Countries(market.Countries.FirstOrDefault()),
                    Selected = Identity.Current.Country == market.Countries.FirstOrDefault()
                };
                items.Add(item);
            }
            model.CountryList = items;

            return(View(model));
        }
        public JsonNetResult LoadCategories(Guid parentCategoryID)
        {
            try
            {
                var model = new SubcategoryListViewModel();
                model.ParentCategoryID          = parentCategoryID;
                model.ParentCategoryDescription = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    CategoryID = parentCategoryID
                }).FirstOrDefault().CategoryDescription;
                model.ResourceCategories = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    ParentID = parentCategoryID
                }).OrderBy(v => v.CategoryOrder);
                var html = this.RenderPartialViewToString("partials/_categorylist", model);

                return(new JsonNetResult(new
                {
                    success = true,
                    html = html
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        public JsonNetResult CreateCategory(List <TranslatedCategory> transdesc, Guid?parentID)
        {
            try
            {
                //2015-09-08
                //Ivan S.
                //66
                //Sets the initial order for the new category to the maximum number for all the categories
                var lastCategory      = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()).OrderByDescending(rc => rc.CategoryOrder).FirstOrDefault();
                int?lastCategoryOrder = InitialOrderValue - 1;
                if (lastCategory != null)
                {
                    lastCategoryOrder = lastCategory.CategoryOrder;
                }
                var NewOrder   = ++lastCategoryOrder;
                var CategoryID = Guid.NewGuid();
                if (parentID == null)
                {
                    parentID = Guid.Empty;
                }
                foreach (var description in transdesc)
                {
                    //English is the Default Language and will be used as the Description on the Categories Table
                    if (description.Language == "English")
                    {
                        ResourceCategory Category = new ResourceCategory()
                        {
                            CategoryID          = CategoryID,
                            CategoryDescription = description.TranslatedCategoryDescription,
                            CategoryOrder       = NewOrder,
                            ParentID            = (Guid)parentID
                        };
                        ExigoDAL.AddResourceCategory(Category);
                    }
                    //Adds an entry for each language translation provided in the TranslatedCategoryItems table
                    ResourceTranslatedCategoryItem TCategory = new ResourceTranslatedCategoryItem()
                    {
                        TranslatedCategoryID          = Guid.NewGuid(),
                        CategoryID                    = CategoryID,
                        Language                      = description.Language,
                        TranslatedCategoryDescription = description.TranslatedCategoryDescription
                    };
                    ExigoDAL.AddCategoryTranslation(TCategory);
                }

                return(new JsonNetResult(new
                {
                    success = true
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        public ActionResult ResourceItems(Guid?editItemID)
        {
            Guid ID = Guid.NewGuid();

            var model = new ResourceListViewModel();

            model.CountryAvailability = GlobalSettings.Markets.AvailableMarkets;

            model.Languages = ExigoDAL.GetUniqueLanguages().ToList();

            model.ResourceTypes = ExigoDAL.GetResourceTypes(new GetResourceTypeRequest());

            model.Statuses = ExigoDAL.GetResourceStatuses();

            model.ResourceCategories = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest());

            model.Tags = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest());

            model.IsEdit = false;

            model.CurrentTags = new List <string>();

            if (editItemID != null && editItemID != Guid.Empty)
            {
                ID             = editItemID ?? ID;
                model.Resource = ExigoDAL.GetResourceItems(new GetResourcesRequest()
                {
                    ItemID = ID
                }).FirstOrDefault();

                var TagIDs = ExigoDAL.GetResourceItemTags(new GetResourceItemTagsRequest()
                {
                    ItemID = ID
                }).Select(t => t.TagID);
                model.CurrentTags = model.Tags.Where(t => TagIDs.Contains(t.TagID)).Select(t => t.Name).Distinct().ToList();
                model.Resource.ResourceCategoryItems = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                {
                    ItemID = ID
                });
                model.Resource.ResourceAvailabilities = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
                {
                    ItemID = ID
                });
                //SET Default Values for DropDownList on View
                model.TypeID   = model.Resource.TypeID;
                model.StatusID = model.Resource.StatusID;
                model.Language = model.Resource.Language;

                model.IsEdit = true;
            }

            model.EditItemID = editItemID ?? Guid.Empty;

            return(View(model));
        }
        public JsonNetResult EditCategory(List <TranslatedCategory> items, Guid categoryID, Guid?parentID)
        {
            try
            {
                foreach (var item in items.Where(i => !i.Language.IsNullOrEmpty()))
                {
                    if (item.Language == "English")
                    {
                        //Update the Resource
                        var resourceCategory = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                        {
                            CategoryID = categoryID
                        }).FirstOrDefault();
                        resourceCategory.CategoryDescription = item.TranslatedCategoryDescription;
                        resourceCategory.ParentID            = parentID ?? Guid.Empty;
                        ExigoDAL.ModifyResourceCategory(resourceCategory);
                    }
                    //Update the Translations
                    var translatedCategory = ExigoDAL.GetCategoryTranslations(new GetTranslatedCategoryRequest()
                    {
                        Language = item.Language, CategoryID = categoryID
                    }).FirstOrDefault();
                    if (translatedCategory != null)
                    {
                        translatedCategory.TranslatedCategoryDescription = item.TranslatedCategoryDescription;
                        ExigoDAL.ModifyResourceTranslationDescription(translatedCategory);
                    }
                    else
                    {
                        ResourceTranslatedCategoryItem TCategory = new ResourceTranslatedCategoryItem()
                        {
                            TranslatedCategoryID          = Guid.NewGuid(),
                            CategoryID                    = categoryID,
                            Language                      = item.Language,
                            TranslatedCategoryDescription = item.TranslatedCategoryDescription
                        };
                        ExigoDAL.AddCategoryTranslation(TCategory);
                    }
                }

                return(new JsonNetResult(new
                {
                    success = true
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        public ActionResult SaveResourceCategorySort(string parentID, SortValues[] sortValues)
        {
            try
            {
                var categories   = new List <ResourceCategory>();
                var guidParentID = (parentID == "0") ? Guid.Empty : Guid.Parse(parentID);

                categories = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    ParentID = guidParentID
                }).ToList();

                if (categories.Count() == 0)
                {
                    return(new JsonNetResult(new { success = false }));
                }

                foreach (var cat in categories)
                {
                    var valueItem = sortValues.Where(sv => sv.NodeID == cat.CategoryID).FirstOrDefault();
                    if (valueItem != null)
                    {
                        ResourceCategory modifyCategory = new ResourceCategory()
                        {
                            CategoryOrder = valueItem.SortIndex,
                            CategoryID    = cat.CategoryID,
                            ParentID      = guidParentID
                        };
                        ExigoDAL.ModifyResourceCategoryOrder(modifyCategory);
                    }
                }

                return(new JsonNetResult(new
                {
                    success = true
                }));
            }
            catch
            {
                return(new JsonNetResult(new
                {
                    success = false
                }));
            }
        }
        public ActionResult ManageResourceCategories()
        {
            //set up the model/service and fetch data
            var model = new ResourceCategoryListViewModel();

            string ResourceCategoryText;


            model.ResourceCategories = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
            {
                ParentID = Guid.Empty
            }).OrderBy(c => c.CategoryOrder);

            List <SelectListItem> categorySelectItems = new List <SelectListItem>();

            foreach (var category in model.ResourceCategories.ToList())
            {
                category.HasChildren = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    ParentID = category.CategoryID
                }).Any();
                //var childCount = context.ResourceCategories.Where(c => c.ParentID == category.CategoryID).Count();
                //category.HasChildren = (childCount > 0);
                ResourceCategoryText = ExigoDAL.GetCategoryTranslations(new GetTranslatedCategoryRequest()
                {
                    Language = Identity.Current.Language.LanguageDescription, CategoryID = category.CategoryID
                }).FirstOrDefault().TranslatedCategoryDescription;

                if (ResourceCategoryText.IsNullOrEmpty())//#71613 Ivan S. 2015-11-06 Validated when the translation is not available in the resource files
                {
                    ResourceCategoryText = category.CategoryDescription;
                }
                categorySelectItems.Add(new SelectListItem
                {
                    Text  = ResourceCategoryText,
                    Value = category.CategoryID.ToString()
                });
            }
            model.ResourceCategorySelectListItems = categorySelectItems;

            return(View(model));
        }
        public JsonNetResult GetModal(Guid categoryID, Guid parentCategoryID)
        {
            var model = new ResourceCategoryViewModel();

            try
            {
                //context.ResourceCategories.Where(c => c.ParentID == null && c.CategoryID != categoryID)
                List <SelectListItem>   ListItems = new List <SelectListItem>();
                List <ResourceCategory> categories;
                using (var context = ExigoDAL.Sql())
                {
                    categories = context.Query <ResourceCategory>(@"
                        SELECT 
                            CategoryID,
                            CategoryDescription
                        FROM
                            ExigoWebContext.ResourceCategories
                        WHERE
                            ParentID = @parentid
                        AND
                            CategoryID <> @categoryid

                        ", new { parentid = Guid.Empty, categoryid = categoryID }).ToList();
                    foreach (var cat in categories)
                    {
                        SelectListItem item = new SelectListItem()
                        {
                            Value = cat.CategoryID.ToString(),
                            Text  = cat.CategoryDescription
                        };
                        ListItems.Add(item);
                    }
                }
                model.ParentCategories = ListItems;


                model.allLanguages = ExigoDAL.GetUniqueLanguages().ToList();

                if (categoryID != Guid.Empty)
                {
                    model.Categories = ExigoDAL.GetCategoryTranslations(new GetTranslatedCategoryRequest()
                    {
                        CategoryID = categoryID
                    }).ToList();
                }
                var parentCat = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    CategoryID = categoryID
                }).FirstOrDefault();
                model.SelectedParentCategoryID = parentCat == null ? Guid.Empty : parentCat.ParentID;
                model.hasSubCategories         = categoryID == Guid.Empty ? false : ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest {
                    ParentID = categoryID
                }).Count() > 0;
                var html = this.RenderPartialViewToString("partials/_categorypartial", model);

                return(new JsonNetResult(new
                {
                    success = true,
                    html = html
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        public JsonNetResult DeleteCategory(Guid categoryID)
        {
            try
            {
                //Create new List Guids
                List <Guid> categoryList = new List <Guid>();

                //Get Children Categories
                categoryList = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    ParentID = categoryID
                }).Select(cat => cat.CategoryID).ToList();

                //Add Parent Category to List
                categoryList.Add(categoryID);

                //reset the resource items
                var resources = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                {
                    CategoryIDs = categoryList
                }).Select(v => v.ItemID);

                if (resources.Any())
                {
                    using (var context = ExigoDAL.Sql())
                    {
                        context.Execute(@"
                            UPDATE 
                                ExigoWebContext.ResourceCategoryItems
                            SET
                                CategoryID = cast(cast(0 as binary) as uniqueidentifier)
                            WHERE
                                CategoryID IN @categorylist
                            AND
                                ItemID IN @resources
                         
                        ", new { categorylist = categoryList, resources = resources });
                    }
                }



                var allCategoryItems = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest());

                var listWithNoCategory = allCategoryItems.Where(c => c.CategoryID == Guid.Empty);
                var deleteItemList     = new List <ResourceCategoryItem>();

                foreach (var item in listWithNoCategory)
                {
                    if (allCategoryItems.Where(c => c.ItemID == item.ItemID && c.CategoryID != Guid.Empty).Any())
                    {
                        deleteItemList.Add(item);
                    }
                }

                var deleteIDs = deleteItemList.Select(c => c.ItemID);

                if (deleteItemList.Any())
                {
                    using (var context = ExigoDAL.Sql())
                    {
                        context.Execute(@"
                            DELETE FROM 
                            ExigoWebContext.ResourceCategoryItems
                        WHERE
                            CategoryID = cast(cast(0 as binary) as uniqueidentifier)
                        AND
                            ItemID IN @itemids
                        
                         
                        ", new { itemids = deleteIDs });
                    }
                }

                //delete the Category and Children
                var category = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    CategoryID = categoryID
                }).FirstOrDefault();
                var categoryOrder = category.CategoryOrder ?? 0;
                using (var context = ExigoDAL.Sql())
                {
                    context.Execute(@"
                        DELETE FROM 
                            ExigoWebContext.ResourceCategories
                        WHERE
                            CategoryID = @categoryid
                        OR  
                            ParentID = @categoryid
                         
                    ", new { categoryid = category.CategoryID });
                }

                //delete the translations
                var translatedCategoryIDs = ExigoDAL.GetCategoryTranslations(new GetTranslatedCategoryRequest()
                {
                    CategoryID = categoryID
                }).Select(t => t.TranslatedCategoryID);
                using (var context = ExigoDAL.Sql())
                {
                    context.Execute(@"
                        DELETE FROM 
                            ExigoWebContext.ResourceTranslatedCategoryItems
                        WHERE
                            TranslatedCategoryID IN @translatedids
                         
                    ", new { translatedids = translatedCategoryIDs });
                }

                //2015-09-08
                //Ivan S.
                //66
                //Decreases the order for the following categories
                var categoryOrders = ExigoDAL.GetResourceCategories(new GetResourceCategoriesRequest()
                {
                    CategoryOrder = categoryOrder
                }).Select(rc => rc.CategoryID);
                using (var context = ExigoDAL.Sql())
                {
                    context.Execute(@"
                        UPDATE 
                            ExigoWebContext.ResourceCategories
                        SET
                            CategoryOrder = CategoryOrder - 1
                        WHERE
                            CategoryID IN @categoryorders
                         
                    ", new { categoryorders = categoryOrders });
                }

                return(new JsonNetResult(new
                {
                    success = true,
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = true,
                    message = ex.Message
                }));
            }
        }