<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Utilities;

namespace OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers
{
    internal class ObjectCustomActions : ObjectHandlerBase
    {
        public override string Name
        {
            get { return "Custom Actions"; }
        }

        public override void ProvisionObjects(Web web, ProvisioningTemplate template)
        {
            Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_CustomActions);

            var context = web.Context as ClientContext;
            var site = context.Site;

            // if this is a sub site then we're not enabling the site collection scoped custom actions
            if (!web.IsSubSite())
            {
                var siteCustomActions = template.CustomActions.SiteCustomActions;
                ProvisionCustomActionImplementation(site, siteCustomActions);
            }

            var webCustomActions = template.CustomActions.WebCustomActions;
            ProvisionCustomActionImplementation(web, webCustomActions);
        }

        private void ProvisionCustomActionImplementation(object parent, List<CustomAction> customActions)
        {
            Web web = null;
            Site site = null;
            if (parent is Site)
            {
                site = parent as Site;

                // Switch parser context;
                TokenParser.Rebase(site.RootWeb);
            }
            else
            {
                web = parent as Web;
            }
            foreach (var customAction in customActions)
            {
                var caExists = false;
                if (site != null)
                {
                    caExists = site.CustomActionExists(customAction.Name);
                }
                else
                {
                    caExists = web.CustomActionExists(customAction.Name);
                }
                if (!caExists)
                {
                    var customActionEntity = new CustomActionEntity();
                    customActionEntity.CommandUIExtension = customAction.CommandUIExtension.ToParsedString();
                    
                    
                    customActionEntity.Description = customAction.Description;
                    customActionEntity.Group = customAction.Group;
                    customActionEntity.ImageUrl = customAction.ImageUrl.ToParsedString();
                    customActionEntity.Location = customAction.Location;
                    customActionEntity.Name = customAction.Name;
                    customActionEntity.RegistrationId = customAction.RegistrationId;
                    customActionEntity.RegistrationType = customAction.RegistrationType;
                    customActionEntity.Remove = customAction.Remove;
                    customActionEntity.Rights = customAction.Rights;
                    customActionEntity.ScriptBlock = customAction.ScriptBlock.ToParsedString();
                    customActionEntity.ScriptSrc = customAction.ScriptSrc.ToParsedString();
                    customActionEntity.Sequence = customAction.Sequence;
                    customActionEntity.Title = customAction.Title;
                    customActionEntity.Url = customAction.Url.ToParsedString();

                    if (site != null)
                    {
                        site.AddCustomAction(customActionEntity);
                    }
                    else
                    {
                        web.AddCustomAction(customActionEntity);
                    }
                }
            }

            // Rebase parser context to current web
            TokenParser.Rebase(web);
        }

        public override ProvisioningTemplate CreateEntities(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            var context = web.Context as ClientContext;
            bool isSubSite = web.IsSubSite();
            var webCustomActions = web.GetCustomActions();
            var siteCustomActions = context.Site.GetCustomActions();

            var customActions = new CustomActions();
            foreach (var customAction in webCustomActions)
            {
                customActions.WebCustomActions.Add(CopyUserCustomAction(customAction));
            }
            
            // if this is a sub site then we're not creating entities for site collection scoped custom actions
            if (!isSubSite)
            {
                foreach (var customAction in siteCustomActions)
                {
                    customActions.SiteCustomActions.Add(CopyUserCustomAction(customAction));
                }
            }

            template.CustomActions = customActions;

            // If a base template is specified then use that one to "cleanup" the generated template model<commit_msg>HostWeb custom action token parser context issue. The context was null of the TokenParser.<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Utilities;

namespace OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers
{
    internal class ObjectCustomActions : ObjectHandlerBase
    {
        public override string Name
        {
            get { return "Custom Actions"; }
        }

        public override void ProvisionObjects(Web web, ProvisioningTemplate template)
        {
            Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_CustomActions);

            var context = web.Context as ClientContext;
            var site = context.Site;

            // if this is a sub site then we're not enabling the site collection scoped custom actions
            if (!web.IsSubSite())
            {
                var siteCustomActions = template.CustomActions.SiteCustomActions;
                ProvisionCustomActionImplementation(site, siteCustomActions);
            }

            var webCustomActions = template.CustomActions.WebCustomActions;
            ProvisionCustomActionImplementation(web, webCustomActions);
        }

        private void ProvisionCustomActionImplementation(object parent, List<CustomAction> customActions)
        {
            Web web = null;
            Site site = null;
            if (parent is Site)
            {
                site = parent as Site;

                // Switch parser context;
                TokenParser.Rebase(site.RootWeb);
            }
            else
            {
                web = parent as Web;

                // Switch parser context
                TokenParser.Rebase(web);
            }
            foreach (var customAction in customActions)
            {
                var caExists = site?.CustomActionExists(customAction.Name) ?? web.CustomActionExists(customAction.Name);
                if (caExists) continue;

                var customActionEntity = new CustomActionEntity
                {
                    CommandUIExtension = customAction.CommandUIExtension.ToParsedString(),
                    Description = customAction.Description,
                    Group = customAction.Group,
                    ImageUrl = customAction.ImageUrl.ToParsedString(),
                    Location = customAction.Location,
                    Name = customAction.Name,
                    RegistrationId = customAction.RegistrationId,
                    RegistrationType = customAction.RegistrationType,
                    Remove = customAction.Remove,
                    Rights = customAction.Rights,
                    ScriptBlock = customAction.ScriptBlock.ToParsedString(),
                    ScriptSrc = customAction.ScriptSrc.ToParsedString(),
                    Sequence = customAction.Sequence,
                    Title = customAction.Title,
                    Url = customAction.Url.ToParsedString()
                };

                if (site != null)
                {
                    site.AddCustomAction(customActionEntity);
                }
                else
                {
                    web.AddCustomAction(customActionEntity);
                }
            }
        }

        public override ProvisioningTemplate CreateEntities(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            var context = web.Context as ClientContext;
            bool isSubSite = web.IsSubSite();
            var webCustomActions = web.GetCustomActions();
            var siteCustomActions = context.Site.GetCustomActions();

            var customActions = new CustomActions();
            foreach (var customAction in webCustomActions)
            {
                customActions.WebCustomActions.Add(CopyUserCustomAction(customAction));
            }
            
            // if this is a sub site then we're not creating entities for site collection scoped custom actions
            if (!isSubSite)
            {
                foreach (var customAction in siteCustomActions)
                {
                    customActions.SiteCustomActions.Add(CopyUserCustomAction(customAction));
                }
            }

            template.CustomActions = customActions;

            // If a base template is specified then use that one to "cleanup" the generated template model
{'fraction_non_alphanumeric': 0.06161917498199774, 'fraction_numerical': 0.0, 'mean_word_length': 1.7332021366319932, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5048860', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2079, 'n_words': 489}