using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.PortalEngine; using CMS.SettingsProvider; using CMS.UIControls; public partial class CMSModules_Widgets_Controls_WidgetFlatSelector : CMSAdminControl { #region "Variables" private bool mSelectGroupWidgets = false; private bool mSelectEditorWidgets = false; private bool mSelectUserWidgets = false; private bool mSelectDashboardWidgets = false; private bool mSelectInlineWidgets = false; private int mGroupID = 0; private string mTreeSelectedItem = null; private WidgetCategoryInfo mSelectedCategory = null; #endregion #region "Widget flat selector properties" /// /// Determines whether the flat selector will only display widgets available for group administrators. /// public bool SelectGroupWidgets { get { return mSelectGroupWidgets; } set { mSelectGroupWidgets = value; } } /// /// Determines whether the flat selector will display widgets available for editors. /// public bool SelectEditorWidgets { get { return mSelectEditorWidgets; } set { mSelectEditorWidgets = value; } } /// /// Determines whether the flat selector will only display widgets available for authenticated users. /// public bool SelectUserWidgets { get { return mSelectUserWidgets; } set { mSelectUserWidgets = value; } } /// /// Indicates whether use this selector for inline widgets. /// public bool SelectInlineWidgets { get { return mSelectInlineWidgets; } set { mSelectInlineWidgets = value; } } /// /// Determines whether the flat selector will only display widgets available for dashboard. /// public bool SelectDashboardWidgets { get { return mSelectDashboardWidgets; } set { mSelectDashboardWidgets = value; } } /// /// Gets or sets the group ID. Should be set with enabled SelectOnlyGroupWidget property. /// public int GroupID { get { return mGroupID; } set { mGroupID = value; } } #endregion #region "Flat selector properties" /// /// Returns inner instance of UniFlatSelector control. /// public UniFlatSelector UniFlatSelector { get { return flatElem; } } /// /// Gets or sets selected item in flat selector. /// public string SelectedItem { get { return flatElem.SelectedItem; } set { flatElem.SelectedItem = value; } } /// /// Gets or sets the current widget category. /// public WidgetCategoryInfo SelectedCategory { get { // If not loaded yet if (mSelectedCategory == null) { int categoryId = ValidationHelper.GetInteger(TreeSelectedItem, 0); if (categoryId > 0) { mSelectedCategory = WidgetCategoryInfoProvider.GetWidgetCategoryInfo(categoryId); } } return mSelectedCategory; } set { mSelectedCategory = value; // Update ID if (mSelectedCategory != null) { mTreeSelectedItem = mSelectedCategory.WidgetCategoryID.ToString(); } } } /// /// Gets or sets the selected item in tree, usually the category id. /// public string TreeSelectedItem { get { return mTreeSelectedItem; } set { // Clear loaded category if change if (value != mTreeSelectedItem) { mSelectedCategory = null; } mTreeSelectedItem = value; } } /// /// Indicates if the control should perform the operations. /// public override bool StopProcessing { get { return base.StopProcessing; } set { base.StopProcessing = value; flatElem.StopProcessing = value; EnableViewState = !value; } } #endregion #region "Page events" protected void Page_Load(object sender, EventArgs e) { if (StopProcessing) { return; } // Setup flat selector flatElem.QueryName = "cms.widget.selectall"; flatElem.ValueColumn = "WidgetID"; flatElem.SearchLabelResourceString = "widget.widgetname"; flatElem.SearchColumn = "WidgetDisplayName"; flatElem.SelectedColumns = "WidgetName, WidgetThumbnailGUID, WidgetDisplayName, WidgetID, WidgetSkipInsertProperties"; flatElem.SkipPropertiesDialogColumn = "WidgetSkipInsertProperties"; flatElem.PageSize = 15; flatElem.OrderBy = "WidgetDisplayName"; flatElem.NotAvailableImageUrl = GetImageUrl("Objects/CMS_Widget/notavailable.png"); flatElem.NoRecordsMessage = "widgets.norecordsincategory"; flatElem.NoRecordsSearchMessage = "widgets.norecords"; flatElem.OnItemSelected += new UniFlatSelector.ItemSelectedEventHandler(flatElem_OnItemSelected); flatElem.SearchCheckBox.Visible = true; flatElem.SearchCheckBox.Text = GetString("webparts.searchindescription"); if (!URLHelper.IsPostback()) { // Search in description default value flatElem.SearchCheckBox.Checked = false; } if (flatElem.SearchCheckBox.Checked) { flatElem.SearchColumn += ";WidgetDescription"; } } /// /// Creates authorization and authentication where condition. /// private string CreateAuthWhereCondition() { CurrentUserInfo currentUser = CMSContext.CurrentUser; // Allowed for all string securityWhere = " AND ((WidgetSecurity = 0) "; if (currentUser.IsAuthenticated()) { // Authenticated securityWhere += " OR (WidgetSecurity = 1)"; // Global admin if (currentUser.IsGlobalAdministrator) { securityWhere += " OR (WidgetSecurity = 7)"; } // Authorized roles securityWhere += " OR ((WidgetSecurity = 2)"; if (!currentUser.IsGlobalAdministrator) { securityWhere += "AND (WidgetID IN ( SELECT WidgetID FROM CMS_WidgetRole WHERE RoleID IN (SELECT RoleID FROM View_CMS_UserRole_MembershipRole_ValidOnly_Joined WHERE UserID = " + currentUser.UserID + ")))))"; } else { securityWhere += "))"; } } else { securityWhere += ")"; } return securityWhere; } /// /// On PreRender. /// protected override void OnPreRender(EventArgs e) { if (StopProcessing) { return; } // Security CurrentUserInfo currentUser = CMSContext.CurrentUser; if (SelectGroupWidgets) { // Shows group widgets without other security checks string where = "WidgetForGroup = 1"; // But user must be group admin, otherwise show nothing if (!currentUser.IsGroupAdministrator(GroupID) && ((CMSContext.ViewMode != ViewModeEnum.Design) || ((CMSContext.ViewMode == ViewModeEnum.Design) && (!currentUser.IsAuthorizedPerResource("CMS.Design", "Design"))))) { flatElem.ErrorText = GetString("widget.notgroupadmin"); } flatElem.WhereCondition = SqlHelperClass.AddWhereCondition(flatElem.WhereCondition, where); } else { // Create security where condition string securityWhere = String.Empty; if (SelectEditorWidgets) { securityWhere += "WidgetForEditor = 1 "; } else if (SelectUserWidgets) { securityWhere += "WidgetForUser = 1 "; } else if (SelectDashboardWidgets) { securityWhere += "WidgetForDashboard = 1 "; } else if (SelectInlineWidgets) { securityWhere += " WidgetForInline = 1 "; } else { securityWhere += " 1 = 2 "; } securityWhere += CreateAuthWhereCondition(); flatElem.WhereCondition = SqlHelperClass.AddWhereCondition(flatElem.WhereCondition, securityWhere); } // Restrict to items in selected category (if not root) if ((SelectedCategory != null) && (SelectedCategory.WidgetCategoryParentID > 0)) { flatElem.WhereCondition = SqlHelperClass.AddWhereCondition(flatElem.WhereCondition, "WidgetCategoryID = " + SelectedCategory.WidgetCategoryID + " OR WidgetCategoryID IN (SELECT WidgetCategoryID FROM CMS_WidgetCategory WHERE WidgetCategoryPath LIKE '" + SelectedCategory.WidgetCategoryPath + "/%')"); } // Recently used items if (TreeSelectedItem.ToLowerCSafe() == "recentlyused") { flatElem.WhereCondition = SqlHelperClass.AddWhereCondition(flatElem.WhereCondition, SqlHelperClass.GetWhereCondition("WidgetName", currentUser.UserSettings.UserUsedWidgets.Split(';'))); } // Description area and recently used litCategory.Text = ShowInDescriptionArea(SelectedItem); base.OnPreRender(e); } #endregion #region "Event handling" /// /// Updates description after item is selected in flat selector. /// protected string flatElem_OnItemSelected(string selectedValue) { return ShowInDescriptionArea(selectedValue); } #endregion #region "Methods" /// /// Reloads data. /// public override void ReloadData() { flatElem.ReloadData(); flatElem.ResetToDefault(); pnlUpdate.Update(); } /// /// Generates HTML text to be used in description area. /// ///Selected item for which generate description private string ShowInDescriptionArea(string selectedValue) { string name = String.Empty; string description = String.Empty; if (!String.IsNullOrEmpty(selectedValue)) { int widgetId = ValidationHelper.GetInteger(selectedValue, 0); WidgetInfo wi = WidgetInfoProvider.GetWidgetInfo(widgetId); if (wi != null) { name = wi.WidgetDisplayName; description = wi.WidgetDescription; } } // No selection show selected category else if (SelectedCategory != null) { name = SelectedCategory.WidgetCategoryDisplayName; } // Recently used else { name = GetString("widgets.recentlyused"); } string text = "
" + HTMLHelper.HTMLEncode(ResHelper.LocalizeString(name)) + "
"; if (description != null) { text += "
" + HTMLHelper.HTMLEncode(ResHelper.LocalizeString(description)) + "
"; } return text; } #endregion }