<commit_before>﻿using FrameworkLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

namespace WebApplication.Admin.Views.MasterPages
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public void CreateChild(long id)
        {
            if (!FrameworkSettings.CurrentUser.HasPermission(PermissionsEnum.SaveItems))
            {
                throw new Exception("You do not have the appropriate permissions to create items");
            }

            var mediaDetail = MediaDetailsMapper.GetByID(id);

            AdminBasePage.SelectedMediaDetail = mediaDetail;
            AdminBasePage.SelectedMedia = mediaDetail.Media;
        }

        [WebMethod(EnableSession = true)]
        public void Delete(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetDeleteStatus(detail, true);
        }

        [WebMethod(EnableSession = true)]
        public void UnDelete(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetDeleteStatus(detail, false);
        }

        [WebMethod(EnableSession = true)]
        public void DeletePermanently(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);

            if (detail != null)
            {
                HandleDeletePermanentlyRecursive(detail.Media);
                ContextHelper.ClearAllMemoryCache();
                FileCacheHelper.ClearAllCache();
            }
        }

        [WebMethod(EnableSession = true)]
        public string Duplicate(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);

            if (detail != null)
            {
                var mediaDetail = HandleDuplicate(detail, detail.Media.ParentMedia);

                ContextHelper.ClearAllMemoryCache();

                var url = WebApplication.BasePage.GetRedirectToMediaDetailUrl(mediaDetail.MediaTypeID, mediaDetail.MediaID);
                return url;
            }

            return "";
        }

        [WebMethod(EnableSession = true)]
        public void ShowInMenu(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetShowInMenuStatus((MediaDetail)detail, true);
        }

        [WebMethod(EnableSession = true)]
        public void HideFromMenu(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetShowInMenuStatus((MediaDetail)detail, false);
        }

        [WebMethod(EnableSession = true)]
        public void MoveUp(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            detail.Media.MoveUp();
        }

        [WebMethod(EnableSession = true)]
        public void MoveDown(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            detail.Media.MoveDown();
        }

        [WebMethod(EnableSession = true)]
        public string HandleNodeDragDrop(long sourceMediaDetailId, long parentMediaDetailId, int newPosition)
        {
            var sourceMedia = BaseMapper.GetObjectFromContext(MediaDetailsMapper.GetByID(sourceMediaDetailId).Media);
            var parentMedia = BaseMapper.GetObjectFromContext(MediaDetailsMapper.GetByID(parentMediaDetailId).Media);

            var oldParentId = sourceMedia.ParentMediaID;

            sourceMedia.MoveToIndex(newPosition);

            sourceMedia.ParentMedia = parentMedia;

            if(oldParentId != parentMedia.ID)
            {
                foreach (var item in sourceMedia.MediaDetails)
                {
                    item.CachedVirtualPath = item.CalculatedVirtualPath();
                }
            }

            var returnObj = MediasMapper.Update(sourceMedia);

            return returnObj.ToJSON();
        }

        private void SetShowInMenuStatus(MediaDetail detail, bool showInMenu)
        {
            if ((detail == null) || (detail.ShowInMenu == showInMenu))
                return;

            detail = BaseMapper.GetObjectFromContext(detail);
            detail.ShowInMenu = showInMenu;

            Return returnObj = MediaDetailsMapper.Update(detail);

            if (returnObj.IsError)
                throw returnObj.Error.Exception;
            else
            {
                ContextHelper.ClearAllMemoryCache();
                FileCacheHelper.ClearAllCache();
            }
        }

        public IMediaDetail HandleDuplicate(IMediaDetail detail, Media parentMedia)
        {
            var duplicatedItem = MediaDetailsMapper.CreateObject(detail.MediaTypeID, null, parentMedia);
            duplicatedItem.CopyFrom(detail, new List<string> { "MediaID", "Media" });
            duplicatedItem.CachedVirtualPath = duplicatedItem.CalculatedVirtualPath();

            /*if (duplicatedItem.ID == 0)
            {
                var mediaType = MediaTypesMapper.GetByID(detail.MediaTypeID);
                var createdItems = mediaType.MediaDetails.Where(i => !i.IsHistory && i.ParentMediaID == detail.ParentMediaID && i.LanguageID == detail.LanguageID).Select(i => i);

                var newIndex = createdItems.Count() + 1;

                duplicatedItem.LinkTitle = detail.Language.DisplayName + " - " + mediaType.Name + " " + newIndex;
            }*/

            duplicatedItem.Media.OrderIndex = parentMedia.ChildMedias.Count(i => i.ID != 0);

            foreach (var item in detail.Fields)
            {
                var mediaDetailField = new MediaDetailField();
                mediaDetailField.FieldCode = item.FieldCode;
                mediaDetailField.FieldLabel = item.FieldLabel;
                mediaDetailField.UseMediaTypeFieldFrontEndLayout = item.UseMediaTypeFieldFrontEndLayout;
                mediaDetailField.FrontEndLayout = item.FrontEndLayout;
                mediaDetailField.RenderLabelAfterControl = item.RenderLabelAfterControl;
                mediaDetailField.AdminControl = item.AdminControl;
                mediaDetailField.FieldValue = item.FieldValue;
                mediaDetailField.GroupName = item.GroupName;
                mediaDetailField.GetAdminControlValue = item.GetAdminControlValue;
                mediaDetailField.SetAdminControlValue = item.SetAdminControlValue;
                mediaDetailField.DateCreated = mediaDetailField.DateLastModified = DateTime.Now;

                duplicatedItem.Fields.Add(mediaDetailField);
            }

            var returnObj = MediaDetailsMapper.Insert(duplicatedItem);

            if (returnObj.IsError)
                throw returnObj.Error.Exception;
            else
            {
                foreach (var child in detail.Media.ChildMedias)
                {
                    var mediaDetailsToCopy = child.MediaDetails.Where(i => !i.IsDraft && !i.IsHistory);

                    foreach (var childDetail in mediaDetailsToCopy)
                    {
                        HandleDuplicate(childDetail, duplicatedItem.Media);
                    }
                }
            }

            return duplicatedItem;
        }

        private void HandleDeletePermanently(Media item)
        {
            if (!FrameworkSettings.CurrentUser.HasPermission(PermissionsEnum.DeleteItemsPermanently))
            {<commit_msg>Updated Duplicating Pages Functionality<commit_after>﻿using FrameworkLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

namespace WebApplication.Admin.Views.MasterPages
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public void CreateChild(long id)
        {
            if (!FrameworkSettings.CurrentUser.HasPermission(PermissionsEnum.SaveItems))
            {
                throw new Exception("You do not have the appropriate permissions to create items");
            }

            var mediaDetail = MediaDetailsMapper.GetByID(id);

            AdminBasePage.SelectedMediaDetail = mediaDetail;
            AdminBasePage.SelectedMedia = mediaDetail.Media;
        }

        [WebMethod(EnableSession = true)]
        public void Delete(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetDeleteStatus(detail, true);
        }

        [WebMethod(EnableSession = true)]
        public void UnDelete(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetDeleteStatus(detail, false);
        }

        [WebMethod(EnableSession = true)]
        public void DeletePermanently(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);

            if (detail != null)
            {
                HandleDeletePermanentlyRecursive(detail.Media);
                ContextHelper.ClearAllMemoryCache();
                FileCacheHelper.ClearAllCache();
            }
        }

        [WebMethod(EnableSession = true)]
        public string Duplicate(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);

            if (detail != null)
            {
                var mediaDetail = HandleDuplicate(detail, detail.Media.ParentMedia);

                ContextHelper.ClearAllMemoryCache();

                var url = WebApplication.BasePage.GetRedirectToMediaDetailUrl(mediaDetail.MediaTypeID, mediaDetail.MediaID);
                return url;
            }

            return "";
        }

        [WebMethod(EnableSession = true)]
        public void ShowInMenu(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetShowInMenuStatus((MediaDetail)detail, true);
        }

        [WebMethod(EnableSession = true)]
        public void HideFromMenu(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            SetShowInMenuStatus((MediaDetail)detail, false);
        }

        [WebMethod(EnableSession = true)]
        public void MoveUp(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            detail.Media.MoveUp();
        }

        [WebMethod(EnableSession = true)]
        public void MoveDown(long id)
        {
            var detail = MediaDetailsMapper.GetByID(id);
            detail.Media.MoveDown();
        }

        [WebMethod(EnableSession = true)]
        public string HandleNodeDragDrop(long sourceMediaDetailId, long parentMediaDetailId, int newPosition)
        {
            var sourceMedia = BaseMapper.GetObjectFromContext(MediaDetailsMapper.GetByID(sourceMediaDetailId).Media);
            var parentMedia = BaseMapper.GetObjectFromContext(MediaDetailsMapper.GetByID(parentMediaDetailId).Media);

            var oldParentId = sourceMedia.ParentMediaID;

            sourceMedia.MoveToIndex(newPosition);

            sourceMedia.ParentMedia = parentMedia;

            if(oldParentId != parentMedia.ID)
            {
                foreach (var item in sourceMedia.MediaDetails)
                {
                    item.CachedVirtualPath = item.CalculatedVirtualPath();
                }
            }

            var returnObj = MediasMapper.Update(sourceMedia);

            return returnObj.ToJSON();
        }

        private void SetShowInMenuStatus(MediaDetail detail, bool showInMenu)
        {
            if ((detail == null) || (detail.ShowInMenu == showInMenu))
                return;

            detail = BaseMapper.GetObjectFromContext(detail);
            detail.ShowInMenu = showInMenu;

            Return returnObj = MediaDetailsMapper.Update(detail);

            if (returnObj.IsError)
                throw returnObj.Error.Exception;
            else
            {
                ContextHelper.ClearAllMemoryCache();
                FileCacheHelper.ClearAllCache();
            }
        }

        public IMediaDetail HandleDuplicate(IMediaDetail detail, Media parentMedia)
        {
            var duplicatedItem = MediaDetailsMapper.CreateObject(detail.MediaTypeID, null, parentMedia);
            duplicatedItem.CopyFrom(detail, new List<string> { "MediaID", "Media" });
            duplicatedItem.CachedVirtualPath = duplicatedItem.CalculatedVirtualPath();

            /*if (duplicatedItem.ID == 0)
            {
                var mediaType = MediaTypesMapper.GetByID(detail.MediaTypeID);
                var createdItems = mediaType.MediaDetails.Where(i => !i.IsHistory && i.ParentMediaID == detail.ParentMediaID && i.LanguageID == detail.LanguageID).Select(i => i);

                var newIndex = createdItems.Count() + 1;

                duplicatedItem.LinkTitle = detail.Language.DisplayName + " - " + mediaType.Name + " " + newIndex;
            }*/

            duplicatedItem.Media.OrderIndex = parentMedia.ChildMedias.Count(i => i.ID != 0);

            foreach (var item in detail.Fields)
            {
                var mediaDetailField = new MediaDetailField();
                mediaDetailField.CopyFrom(item);

                mediaDetailField.DateCreated = mediaDetailField.DateLastModified = DateTime.Now;

                foreach (var association in item.FieldAssociations)
                {
                    var fieldAssociation = new FieldAssociation();
                    fieldAssociation.CopyFrom(association);

                    mediaDetailField.FieldAssociations.Add(fieldAssociation);
                }

                duplicatedItem.Fields.Add(mediaDetailField);
            }

            var returnObj = MediaDetailsMapper.Insert(duplicatedItem);

            if (returnObj.IsError)
                throw returnObj.Error.Exception;
            else
            {
                foreach (var child in detail.Media.ChildMedias)
                {
                    var mediaDetailsToCopy = child.MediaDetails.Where(i => !i.IsDraft && !i.IsHistory);

                    foreach (var childDetail in mediaDetailsToCopy)
                    {
                        HandleDuplicate(childDetail, duplicatedItem.Media);
                    }
                }
            }

            return duplicatedItem;
        }

        private void HandleDeletePermanently(Media item)
        {
            if (!FrameworkSettings.CurrentUser.HasPermission(PermissionsEnum.DeleteItemsPermanently))
            {
{'fraction_non_alphanumeric': 0.08380445626870635, 'fraction_numerical': 0.0006651147322913202, 'mean_word_length': 1.963926670609107, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1689883', 'n_tokens_mistral': 3796, 'n_tokens_neox': 3610, 'n_words': 691}