using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.UIControls;
public partial class CMSModules_SmartSearch_SearchIndex_Sites : SiteManagerPage, IPostBackEventHandler
{
private int indexId = 0;
private string currentValues = string.Empty;
#region "Methods"
protected void Page_Load(object sender, EventArgs e)
{
// Check read permission
if (!CMSContext.CurrentUser.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");
lblAvialable.Text = GetString("SearchIndex_Sites.Available");
indexId = QueryHelper.GetInteger("indexid", 0);
// Get the user sites
currentValues = GetIndexSites();
if (!RequestHelper.IsPostBack())
{
usSites.Value = currentValues;
}
usSites.OnSelectionChanged += usSites_OnSelectionChanged;
}
///
/// Returns string with site ids where user is member.
///
private string GetIndexSites()
{
DataSet ds = SearchIndexSiteInfoProvider.GetIndexSites("SiteID", "IndexID = " + indexId, null, 0);
if (!DataHelper.DataSourceIsEmpty(ds))
{
return TextHelper.Join(";", SystemDataHelper.GetStringValues(ds.Tables[0], "SiteID"));
}
return String.Empty;
}
///
/// Handles site selector selection change event.
///
protected void usSites_OnSelectionChanged(object sender, EventArgs e)
{
SaveIndexes();
}
///
/// Saves changes in site assignment.
///
protected void SaveIndexes()
{
// Check permissions
if (!CMSContext.CurrentUser.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"), "" + GetString("General.clickhere") + ""));
}
#endregion
#region "IPostBackEventHandler Members"
///
/// Handles click on rebuild link (after sites are saved).
///
/// Event argument
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == "saved")
{
// Check permissions
if (!CMSContext.CurrentUser.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
}