using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.UIControls; public partial class CMSModules_AdminControls_Controls_Class_ClassSites : CMSUserControl { private int mClassId; private string mTitleString; private bool mCheckLicense = true; private string currentValues = String.Empty; #region "Public properties" /// /// Gets or sets the value that indicates whether license should be chcecked. /// public bool CheckLicense { get { return mCheckLicense; } set { mCheckLicense = value; } } /// /// Gets or sets class id. /// public int ClassId { get { return mClassId; } set { mClassId = value; } } /// /// Gets or sets the title at the top of the page. /// public string TitleString { get { return mTitleString; } set { mTitleString = value; } } #endregion /// /// Page load. /// protected void Page_Load(object sender, EventArgs e) { if (ClassId > 0) { // initializes labels lblAvialable.Text = TitleString; // Get the user sites currentValues = GetClassSites(); if (!RequestHelper.IsPostBack()) { usSites.Value = currentValues; } usSites.OnSelectionChanged += usSites_OnSelectionChanged; } else { // Hide control if not properly set Visible = false; } } /// /// Returns string with class sites. /// private string GetClassSites() { DataSet ds = ClassSiteInfoProvider.GetClassSites("SiteID", "ClassID = " + ClassId, 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) { SaveSites(); } protected void SaveSites() { // 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); ClassSiteInfoProvider.RemoveClassFromSite(ClassId, siteId); } } } // Add new items items = DataHelper.GetNewItemsInList(currentValues, newValues); if (!String.IsNullOrEmpty(items)) { string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (newItems != null) { bool falseValues = false; // Add all new items to site foreach (string item in newItems) { int siteId = ValidationHelper.GetInteger(item, 0); SiteInfo si = SiteInfoProvider.GetSiteInfo(siteId); if (si != null) { // Check license if (CheckLicense && !CustomTableItemProvider.LicenseVersionCheck(si.DomainName, FeatureEnum.CustomTables, VersionActionEnum.Insert)) { if (ClassSiteInfoProvider.GetClassSiteInfo(ClassId, siteId) == null) { // Show error message ShowError(GetString("LicenseVersion.CustomTables")); falseValues = true; continue; } } try { ClassSiteInfoProvider.AddClassToSite(ClassId, siteId); } catch (Exception ex) { // Show error message ShowError(ex.Message); return; } } } // If some of sites could not be assigned reload selector value if (falseValues) { usSites.Value = GetClassSites(); usSites.Reload(true); } } } if (CheckLicense) { CustomTableItemProvider.ClearCustomLicHash(); } // Show message ShowChangesSaved(); } }