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 ActionResult DeleteResource(ResourceListViewModel res)
        {
            var itemID     = res.EditItemID;
            var categoryID = res.DeleteCategoryID;


            //A resource will be allowed in mulitple categories Thus delete this entries from this table
            var categoryItems = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
            {
                ItemID = itemID
            });

            foreach (var item in categoryItems)
            {
                //2015-09-08
                //Ivan S.
                //66
                //Reorders the following resources (setting their order to a minus 1 value)
                var itemOrder         = item.ItemOrder;
                var categoryItemOrder = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                {
                    CategoryID = categoryID, ItemOrder = itemOrder
                }).ToList();
                foreach (var catitem in categoryItemOrder)
                {
                    catitem.ItemOrder = catitem.ItemOrder - 1;
                    ExigoDAL.ModifyResourceCategoryItemOrder(catitem);
                }
            }
            //Delete the availabilitiy of the resource
            var availability = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
            {
                ItemID = itemID
            }).Select(v => v.AvailabilityID).ToList();

            ExigoDAL.DeleteResourceAvailabilities(availability);

            //Delete The Tags Associated with the item
            var tags = ExigoDAL.GetResourceItemTags(new GetResourceItemTagsRequest()
            {
                ItemID = itemID
            }).Select(t => t.TagID).ToList();

            ExigoDAL.DeleteResourceItemTags(tags);

            //Delete the resource itself
            var resource = ExigoDAL.GetResourceItems(new GetResourcesRequest()
            {
                ItemID = itemID
            }).FirstOrDefault();

            ExigoDAL.DeleteResourceItem(resource);

            return(RedirectToAction("ManageResources"));
        }
        public JsonNetResult GetResourceList(ResourceFilters filter)
        {
            try
            {
                var model = new ResourceListViewModel();
                model.Filter             = filter;
                model.IsResourceManager  = filter.isResourceManager;
                model.ResourceCategories = new JavaScriptSerializer().Deserialize <IEnumerable <ExigoService.ResourceCategory> >(filter.Categories);
                var filteredcatfilter = filter.CategoryFilter != null ? new JavaScriptSerializer().Deserialize <string>(filter.CategoryFilter) : "";
                var categoryFilter    = (filteredcatfilter != null && filteredcatfilter != "") ? Guid.Parse(filteredcatfilter) : Guid.Empty;
                model.ResourceList     = ExigoDAL.GetResourceItems(new GetResourcesRequest());
                model.CategoryItemList = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()).OrderBy(t => t.ItemOrder);
                model.Types            = ExigoDAL.GetResourceTypes(new GetResourceTypeRequest()
                {
                    TypeDescriptions = filter.MediaFilter
                });
                //Get a List of CategoryIDs from the Rescource Category List
                var catIds = model.ResourceCategories.Select(c => c.CategoryID).ToList();
                //Get all resources assigned to a CURRENT Category ID
                var assigned = model.CategoryItemList.Where(c => catIds.Contains(c.CategoryID)).ToList();
                //Assign all remaining items so they may be displayed in the Unassigned Category
                model.UnassignedResources = model.CategoryItemList.Except(assigned).ToList();
                //Get a list of Unassigned Resources before filtering by market
                var unassignedresourceIDs = model.UnassignedResources.Select(ur => ur.ItemID).ToList();
                model.UnassignedRsourceList = model.ResourceList.Where(rl => unassignedresourceIDs.Contains(rl.ItemID)).ToList();

                if (filter.MarketFilter != null && filter.MarketFilter != string.Empty)
                {
                    if (filter.LanguageFilter != null && filter.LanguageFilter != string.Empty)
                    {
                        var language = Convert.ToInt32(filter.LanguageFilter);
                        var cc       = filter.MarketFilter;
                        model.GetAvailableResources = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
                        {
                            CountryCode = cc, LanguageID = language
                        });
                        model.AvailableResources = model.GetAvailableResources.Select(ar => ar.ItemID).ToList();
                    }
                    else
                    {
                        var cc = filter.MarketFilter;
                        model.GetAvailableResources = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
                        {
                            CountryCode = cc
                        });
                        model.AvailableResources = model.GetAvailableResources.Select(ar => ar.ItemID).ToList();
                    }
                }

                model.ResourceList = model.ResourceList.Where(x => model.AvailableResources.Contains(x.ItemID));

                bool filtered = false;
                if (filter.MediaFilter != null && filter.MediaFilter.Count() > 0)
                {
                    var typeIDs = model.Types.Select(v => v.TypeID);
                    filtered           = true;
                    model.ResourceList = model.ResourceList.Where(x => typeIDs.Contains(x.TypeID)).ToList();
                }


                if (categoryFilter != null && categoryFilter != Guid.Empty)
                {
                    model.SelectedCategoryID = model.ResourceCategories.Where(c => c.CategoryID == categoryFilter).FirstOrDefault().CategoryID;
                }

                if (filter.KeyWord != null && filter.KeyWord.Any())
                {
                    var keyword = filter.KeyWord;
                    var tag     = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest()
                    {
                        Names = keyword
                    }).FirstOrDefault();
                    if (tag != null)
                    {
                        var resourceitemtagIDs = ExigoDAL.GetResourceItemTags(new GetResourceItemTagsRequest()
                        {
                            TagID = tag.TagID
                        }).AsEnumerable().Select(rt => rt.ItemID);

                        //var tagitems = model.ResourceList.Where(rl => resourceitemtagIDs.Contains(rl.ItemID));
                        //var matchingitems = model.ResourceList.Where(rl => keyword.Contains(rl.Title) || rl.ItemDescription.ToLower().Contains(keyword));
                        //var keywordSearchResult = tagitems.Union(matchingitems).ToList().Select(r => r.ItemID);
                        //model.ResourceList = model.ResourceList.Where(rl => keywordSearchResult.Contains(rl.ItemID));

                        model.ResourceList = model.ResourceList.Where(rl => resourceitemtagIDs.Contains(rl.ItemID));
                    }
                }

                var html = this.RenderPartialViewToString("partials/_resourcelist", model);

                return(new JsonNetResult(new
                {
                    success = true,
                    html = html,
                    isFiltered = filtered
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        public JsonNetResult EditResource(CreateResource res, string editID)
        {
            try
            {
                var itemID = Guid.Parse(editID);

                //Update ResoureItem

                ResourceItem originalResource = new ResourceItem()
                {
                    ItemID          = itemID,
                    Title           = res.Title,
                    ItemDescription = res.ItemDescription,
                    Url             = res.Url,
                    UrlThumbnail    = res.UrlThumbnail,
                    TypeID          = res.TypeID,
                    StatusID        = res.StatusID,
                    PostDate        = res.PostDate ?? DateTime.Now.ToCST(),
                    Language        = res.Language,
                    CreatedDate     = DateTime.Now.ToCST(),
                };

                ExigoDAL.ModifyResourceItem(originalResource);

                //Current Availabilities
                List <ResourceAvailability> currentAvailability = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
                {
                    ItemID = originalResource.ItemID
                }).ToList();

                //Wanted Availabilities
                List <ResourceAvailability> keepAvailabilities   = new List <ResourceAvailability>();
                List <Available>            neededAvailabilities = new List <Available>();
                foreach (var market in res.Markets)
                {
                    var availability = new Available();
                    availability.Language = res.Language;
                    availability.Market   = market;
                    var exist = currentAvailability.Where(ca => ca.Language == availability.Language && ca.Market == availability.Market).FirstOrDefault();
                    if (exist == null)
                    {
                        neededAvailabilities.Add(availability);
                    }
                    else
                    {
                        keepAvailabilities.Add(exist);
                    }
                }



                //Add NEW Availabilities
                foreach (var avail in neededAvailabilities)
                {
                    ResourceAvailability availableresource = new ResourceAvailability()
                    {
                        AvailabilityID = Guid.NewGuid(),
                        ItemID         = originalResource.ItemID,
                        Market         = avail.Market,
                        Language       = avail.Language
                    };
                    ExigoDAL.CreateResourceAvailabilities(availableresource);
                }

                //Delete Availabilities no longer wanted
                List <Guid> deleteAvailabilities = currentAvailability.Except(keepAvailabilities).Select(t => t.AvailabilityID).ToList();
                ExigoDAL.DeleteResourceAvailabilities(deleteAvailabilities);

                //Modify ResourceCategoryItems

                // Current ResourceCategoryItems CategoryIDs
                var RCI = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                {
                    ItemID = originalResource.ItemID
                });
                var currentRCI = RCI.Select(rci => rci.CategoryID);

                // Wanted CategoryIDs
                var wantedRCI = res.CategoryID;

                // Needed CategoryIDs
                var neededRCI = wantedRCI.Except(currentRCI);

                //Add New CategoryItems


                foreach (var category in neededRCI)
                {
                    var lastResource = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                    {
                        CategoryID = category
                    })
                                       .OrderByDescending(r => r.ItemOrder)
                                       .FirstOrDefault();
                    int lastItemOrder = InitialOrderValue - 1;
                    if (lastResource != null)
                    {
                        lastItemOrder = lastResource.ItemOrder;
                    }

                    ResourceCategoryItem categoryItem = new ResourceCategoryItem()
                    {
                        ItemID     = originalResource.ItemID,
                        CategoryID = category,
                        ItemOrder  = lastItemOrder
                    };
                    ExigoDAL.CreateResourceCategoryItem(categoryItem);
                }
                //Delete Unwanted CategoryItems
                var unwantedRCICategoryIDs = currentRCI.Except(wantedRCI).ToList();
                if (unwantedRCICategoryIDs.Any())
                {
                    ExigoDAL.DeleteResourceCategoryItems(itemID, unwantedRCICategoryIDs);
                }


                List <Tag> tags = new List <Tag>();



                if (res.Keywords.Any())
                {
                    // check to see if tag existis with these keywords. if not add it

                    //Get A List Of Tags Using Provided Keywords
                    var existingTags = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest()
                    {
                        Names = res.Keywords
                    });
                    var existingTagNames = existingTags.Select(et => et.Name);

                    //Get a List of Keywords we need to make tags for
                    var newTagsneeded = res.Keywords.Except(existingTagNames);

                    //Create The New Tags
                    List <Tag> NewlyCreatedTags = new List <Tag>();
                    foreach (var keyword in newTagsneeded)
                    {
                        Tag newTag = new Tag()
                        {
                            TagID = Guid.NewGuid(),
                            Name  = keyword
                        };
                        NewlyCreatedTags.Add(newTag);
                        ExigoDAL.CreateTag(newTag);
                    }

                    //Combine existing and New Tags
                    var completeTagList = existingTags.Union(NewlyCreatedTags).ToList();
                    var completeTagIDs  = completeTagList.Select(v => v.TagID);

                    //Get a list of ItemTags currently associated with the Item
                    var currentItemTagIDs = ExigoDAL.GetResourceItemTags(new GetResourceItemTagsRequest()
                    {
                        ItemID = originalResource.ItemID
                    }).Select(v => v.TagID);

                    //Get a list of Tags we need to make ItemTags for
                    var newItemTagsneeded = completeTagList.Where(t => !currentItemTagIDs.Contains(t.TagID));

                    // Update ResourceItemTags Table with new Tags
                    foreach (var tagneeded in newItemTagsneeded)
                    {
                        ResourceItemTag rit = new ResourceItemTag()
                        {
                            TagID  = tagneeded.TagID,
                            ItemID = itemID,
                        };
                        ExigoDAL.CreateResourceItemTag(rit);
                    }

                    // remove any pivot table tags that are no longer being used for this resource
                    var nolongerwanted = currentItemTagIDs.Except(completeTagIDs).ToList();
                    if (nolongerwanted.Count > 0)
                    {
                        ExigoDAL.DeleteResourceItemTags(nolongerwanted);
                    }
                }

                return(new JsonNetResult(new
                {
                    success = true,
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        public JsonNetResult SortCategoryByDate(Guid categoryID, string sortType, ResourceFilters filter)
        {
            try
            {
                var model = new CategoryResourceItemsViewModel();
                var availableResources = new List <Guid>();
                model.CategoryItem = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                {
                    CategoryID = categoryID
                }).FirstOrDefault();
                model.IsResourceManager = filter.isResourceManager;
                model.ResourceList      = ExigoDAL.GetResourceItemsByCategory(categoryID);
                model.Types             = ExigoDAL.GetResourceTypes(new GetResourceTypeRequest()
                {
                    TypeDescriptions = filter.MediaFilter
                });

                if (filter.MarketFilter != null && filter.MarketFilter != string.Empty)
                {
                    if (filter.LanguageFilter != null && filter.LanguageFilter != string.Empty)
                    {
                        var language = Convert.ToInt32(filter.LanguageFilter);
                        var cc       = filter.MarketFilter;
                        var getAvailableResources = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
                        {
                            CountryCode = cc, LanguageID = language
                        });
                        availableResources = getAvailableResources.Select(ar => ar.ItemID).ToList();
                    }
                    else
                    {
                        var cc = filter.MarketFilter;
                        var getAvailableResources = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest()
                        {
                            CountryCode = cc
                        });
                        availableResources = getAvailableResources.Select(ar => ar.ItemID).ToList();
                    }
                }

                model.ResourceList = model.ResourceList.Where(x => availableResources.Contains(x.ItemID));

                if (!model.IsResourceManager)
                {
                    model.ResourceList = model.ResourceList.Where(c => c.StatusID == ExigoDAL.ResourceStatuses.Active && (c.PostDate <= DateTime.Now.ToCST() || c.PostDate == null)).ToList();
                }

                switch (sortType)
                {
                case "asc":
                    model.ResourceList = model.ResourceList.OrderBy(c => c.CreatedDate);
                    break;

                case "desc":
                    model.ResourceList = model.ResourceList.OrderByDescending(c => c.CreatedDate);
                    break;

                default:
                    model.ResourceList = model.ResourceList.OrderBy(c => c.ItemOrder);
                    break;
                }

                if (filter.MediaFilter != null && filter.MediaFilter.Count() > 0)
                {
                    var typeIDs = model.Types.Select(v => v.TypeID);
                    model.ResourceList = model.ResourceList.Where(x => typeIDs.Contains(x.TypeID)).ToList();
                }

                if (filter.KeyWord != null && filter.KeyWord.Any())
                {
                    var keyword = filter.KeyWord;
                    var tag     = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest()
                    {
                        Names = keyword
                    }).FirstOrDefault();
                    if (tag != null)
                    {
                        var resourceitemtagIDs = ExigoDAL.GetResourceItemTags(new GetResourceItemTagsRequest()
                        {
                            TagID = tag.TagID
                        }).AsEnumerable().Select(rt => rt.ItemID);

                        model.ResourceList = model.ResourceList.Where(rl => resourceitemtagIDs.Contains(rl.ItemID));
                    }
                }

                var html = this.RenderPartialViewToString("Partials/_CategoryResourceItems", model);

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