HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/RMourik/bassol.nl/wwwroot/CMSModules/SmartSearch/SearchIndex_Sites.aspx.cs
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

using CMS.Helpers;
using CMS.Base;
using CMS.SiteProvider;
using CMS.Membership;
using CMS.UIControls;
using CMS.Search;

public partial class CMSModules_SmartSearch_SearchIndex_Sites : GlobalAdminPage, IPostBackEventHandler
{
    private int indexId = 0;
    private string currentValues = string.Empty;


    #region "Methods"

    protected void Page_Load(object sender, EventArgs e)
    {
        // Check read permission
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.searchindex", CMSAdminControl.PERMISSION_READ))
        {
            RedirectToAccessDenied("cms.searchindex", CMSAdminControl.PERMISSION_READ);
        }

        // Show panel with message how to enable indexing
        ucDisabledModule.SettingsKeys = "CMSSearchIndexingEnabled";
        ucDisabledModule.InfoText = GetString("srch.searchdisabledinfo");

        indexId = QueryHelper.GetInteger("indexid", 0);

        // Get the user sites
        currentValues = GetIndexSites();

        if (!RequestHelper.IsPostBack())
        {
            usSites.Value = currentValues;
        }

        usSites.OnSelectionChanged += usSites_OnSelectionChanged;
    }


    /// <summary>
    /// Returns string with site ids where user is member.
    /// </summary>    
    private string GetIndexSites()
    {
        var siteIDs = SearchIndexSiteInfoProvider.GetIndexSiteBindings(indexId).Column("IndexSiteID").Select(x => x.IndexSiteID).ToList();
        if (siteIDs.Count > 0)
        {
            return TextHelper.Join(";", siteIDs);
        }

        return String.Empty;
    }


    /// <summary>
    /// Handles site selector selection change event.
    /// </summary>
    protected void usSites_OnSelectionChanged(object sender, EventArgs e)
    {
        SaveIndexes();
    }


    /// <summary>
    /// Saves changes in site assignment.
    /// </summary>
    protected void SaveIndexes()
    {
        // Check permissions 
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.searchindex", CMSAdminControl.PERMISSION_MODIFY))
        {
            RedirectToAccessDenied("cms.searchindex", CMSAdminControl.PERMISSION_MODIFY);
        }

        // Remove old items
        string newValues = ValidationHelper.GetString(usSites.Value, null);
        string items = DataHelper.GetNewItemsInList(newValues, currentValues);
        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                // Add all new items to site
                foreach (string item in newItems)
                {
                    int siteId = ValidationHelper.GetInteger(item, 0);

                    // Unassign site from index
                    SearchIndexSiteInfoProvider.DeleteSearchIndexSiteInfo(indexId, siteId);
                }
            }
        }


        // Add new items
        items = DataHelper.GetNewItemsInList(currentValues, newValues);
        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                // Add all new items to site
                foreach (string item in newItems)
                {
                    int siteId = ValidationHelper.GetInteger(item, 0);

                    // Assign site to index
                    SearchIndexSiteInfoProvider.AddSearchIndexToSite(indexId, siteId);
                }
            }
        }

        // Show saved message with rebuild link
        ShowChangesSaved();
        ShowInformation(String.Format(GetString("srch.indexrequiresrebuild"), "<a href=\"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "saved") + "\">" + GetString("General.clickhere") + "</a>"));
    }

    #endregion


    #region "IPostBackEventHandler Members"

    /// <summary>
    /// Handles click on rebuild link (after sites are saved).
    /// </summary>
    /// <param name="eventArgument">Event argument</param>
    public void RaisePostBackEvent(string eventArgument)
    {
        if (eventArgument == "saved")
        {
            // Check permissions 
            if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.searchindex", CMSAdminControl.PERMISSION_MODIFY))
            {
                RedirectToAccessDenied("cms.searchindex", CMSAdminControl.PERMISSION_MODIFY);
            }

            if (SearchHelper.CreateRebuildTask(indexId))
            {
                ShowInformation(GetString("srch.index.rebuildstarted"));
            }
            else
            {
                ShowError(GetString("index.nocontent"));
            }
        }
    }

    #endregion
}