using System; using System.Web.UI; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.ExtendedControls; using CMS.FormControls; using CMS.FormEngine; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.DocumentEngine; using CMS.UIControls; using CMS.WorkflowEngine; using TreeNode = CMS.DocumentEngine.TreeNode; public partial class CMSModules_Content_FormControls_Relationships_RelatedDocuments : ReadOnlyFormEngineUserControl { #region "Variables" private TreeProvider mTreeProvider = null; private bool mShowAddRelation = true; private string mPageSize = "5,10,25,50,100,##ALL##"; private int mDefaultPageSize = 5; private bool mAllowSwitchSides = true; private DialogConfiguration mConfig = null; #endregion #region "Private properties" /// /// Gets the configuration for Copy and Move dialog. /// private DialogConfiguration Config { get { if (mConfig == null) { mConfig = new DialogConfiguration(); mConfig.HideLibraries = true; mConfig.ContentSelectedSite = CMSContext.CurrentSiteName; mConfig.HideAnchor = true; mConfig.HideAttachments = true; mConfig.HideContent = false; mConfig.HideEmail = true; mConfig.HideLibraries = true; mConfig.HideWeb = true; mConfig.ContentSelectedSite = CMSContext.CurrentSiteName; mConfig.OutputFormat = OutputFormatEnum.Custom; mConfig.CustomFormatCode = "relationship"; mConfig.ContentSites = AvailableSitesEnum.OnlyCurrentSite; } return mConfig; } } #endregion #region "Properties" /// /// Gets or sets relationship name. /// public string RelationshipName { get; set; } /// /// Indicates if allow switch sides. /// public bool AllowSwitchSides { get { return mAllowSwitchSides; } set { mAllowSwitchSides = value; } } /// /// Default side (False - left, True - right). /// public bool DefaultSide { get; set; } /// /// Default page size. /// public int DefaultPageSize { get { return mDefaultPageSize; } set { mDefaultPageSize = value; } } /// /// Page size values separated with comma. /// public string PageSize { get { return mPageSize; } set { mPageSize = value; } } /// /// Indicates id show link 'Add relation'. /// public bool ShowAddRelation { get { return mShowAddRelation; } set { mShowAddRelation = value; } } /// /// Path where content tree in document selection dialog will start. /// public string StartingPath { get; set; } /// /// Gets or sets the document;. /// public TreeNode TreeNode { get; set; } /// /// Gets tree provider. /// public TreeProvider TreeProvider { get { if (mTreeProvider == null) { mTreeProvider = new TreeProvider(CMSContext.CurrentUser); } return mTreeProvider; } } /// /// Enables or disables control. /// public override bool Enabled { get { return base.Enabled; } set { base.Enabled = value; UniGridRelationship.GridView.Enabled = value; lnkNewRelationship.Enabled = value; } } /// /// Messages placeholder /// public override MessagesPlaceHolder MessagesPlaceHolder { get { return plcMess; } } #endregion #region "Page events" protected override void OnInit(EventArgs e) { base.OnInit(e); // Paging UniGridRelationship.PageSize = ValidationHelper.GetString(GetValue("PageSize"), PageSize); UniGridRelationship.Pager.DefaultPageSize = ValidationHelper.GetInteger(GetValue("DefaultPageSize"), DefaultPageSize); } protected void Page_Load(object sender, EventArgs e) { if (StopProcessing) { UniGridRelationship.StopProcessing = StopProcessing; } else { // Set tree node from Form object if ((TreeNode == null) && (Form != null) && (Form.EditedObject != null)) { TreeNode node = Form.EditedObject as TreeNode; if ((node != null) && (Form.Mode == FormModeEnum.Update)) { TreeNode = node; } else { ShowError(GetString("relationship.editdocumenterror")); } } if (TreeNode != null) { // Settings RelationshipName = ValidationHelper.GetString(GetValue("RelationshipName"), RelationshipName); AllowSwitchSides = ValidationHelper.GetBoolean(GetValue("AllowSwitchSides"), AllowSwitchSides); DefaultSide = ValidationHelper.GetBoolean(GetValue("DefaultSide"), DefaultSide); // Set unigrid UniGridRelationship.Columns = "LeftNodeID, RightNodeID, RelationshipNameID, LeftNodeName, RightNodeName, RelationshipDisplayName"; UniGridRelationship.OnExternalDataBound += UniGridRelationship_OnExternalDataBound; UniGridRelationship.OnBeforeDataReload += UniGridRelationship_OnBeforeDataReload; UniGridRelationship.OnAction += UniGridRelationship_OnAction; UniGridRelationship.ZeroRowsText = GetString("relationship.nodatafound"); int nodeId = TreeNode.NodeID; bool oneRelationshipName = !string.IsNullOrEmpty(RelationshipName); string where = null; if (oneRelationshipName) { where = SqlHelperClass.AddWhereCondition(where, "RelationshipName = N'" + SqlHelperClass.GetSafeQueryString(RelationshipName, false) + "'"); } // Switch sides is disabled if (!AllowSwitchSides) { if (DefaultSide) { where = SqlHelperClass.AddWhereCondition(where, "RightNodeID = " + nodeId); } else { where = SqlHelperClass.AddWhereCondition(where, "LeftNodeID = " + nodeId); } } else { where = SqlHelperClass.AddWhereCondition(where, "(LeftNodeID = " + nodeId + ") OR (RightNodeID = " + nodeId + ")"); } UniGridRelationship.WhereCondition = where; if (ShowAddRelation) { if (!AllowSwitchSides && !string.IsNullOrEmpty(RelationshipName)) { lnkNewRelationship.Attributes.Add("onclick", GetAddRelatedDocumentScript()); } else { // Initialize controls lnkNewRelationship.NavigateUrl = "about:blank"; lnkNewRelationship.Attributes.Add("onclick", GetAddRelatedDocumentScript() + " return false;"); } imgNewRelationship.ImageUrl = GetImageUrl("CMSModules/CMS_Content/Properties/addrelationship.png"); imgNewRelationship.DisabledImageUrl = GetImageUrl("CMSModules/CMS_Content/Properties/addrelationshipdisabled.png"); } else { pnlNewLink.Visible = false; } } else { UniGridRelationship.StopProcessing = true; UniGridRelationship.Visible = false; pnlNewLink.Visible = false; } if (RequestHelper.IsPostBack()) { string target = Request["__EVENTTARGET"]; if ((target == pnlUpdate.ClientID) || (target == pnlUpdate.UniqueID)) { string action = Request["__EVENTARGUMENT"]; if (!string.IsNullOrEmpty(action)) { switch (action.ToLowerCSafe()) { // Insert from 'Add related document' dialog case "insert": ShowConfirmation(GetString("relationship.wasadded")); break; // Insert from 'Select document' dialog case "insertfromselectdocument": SaveRelationship(); break; // Nothing default: break; } } } } bool inserted = QueryHelper.GetBoolean("inserted", false); if (inserted) { ShowConfirmation(GetString("relationship.wasadded")); } } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (!Enabled) { lnkNewRelationship.NavigateUrl = null; if (lnkNewRelationship.Attributes["onclick"] != null) { lnkNewRelationship.Attributes["onclick"] = null; } } } #endregion #region "Event handlers" /// /// Performs actions before reload grid. /// private void UniGridRelationship_OnBeforeDataReload() { DataControlField rightColumn = UniGridRelationship.NamedColumns["RightNodeName"]; // Hide columns if (!string.IsNullOrEmpty(RelationshipName) && !AllowSwitchSides) { string headerText = GetString("relationship.relateddocument"); DataControlField leftColumn = UniGridRelationship.NamedColumns["LeftNodeName"]; DataControlField relationshipNameColumn = UniGridRelationship.NamedColumns["RelationshipDisplayName"]; if (DefaultSide) { leftColumn.HeaderText = headerText; leftColumn.HeaderStyle.Width = new Unit("100%"); rightColumn.Visible = false; } else { rightColumn.HeaderText = headerText; rightColumn.HeaderStyle.Width = new Unit("100%"); leftColumn.Visible = false; } // Hide relationship name column relationshipNameColumn.Visible = false; } else { rightColumn.HeaderStyle.Width = new Unit("100%"); } } /// /// Fires on the grid action. /// /// Action name /// Action argument private void UniGridRelationship_OnAction(string actionName, object actionArgument) { // Check modify permissions if (CMSContext.CurrentUser.IsAuthorizedPerDocument(TreeNode, NodePermissionsEnum.Modify) == AuthorizationResultEnum.Denied) { return; } if (actionName == "delete") { string[] parameters = ((string)actionArgument).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (parameters.Length == 3) { // Parse parameters int leftNodeId = ValidationHelper.GetInteger(parameters[0], 0); int rightNodeId = ValidationHelper.GetInteger(parameters[1], 0); int relationshipNameId = ValidationHelper.GetInteger(parameters[2], 0); // If parameters are valid if ((leftNodeId > 0) && (rightNodeId > 0) && (relationshipNameId > 0)) { // Remove relationship RelationshipProvider.RemoveRelationship(leftNodeId, rightNodeId, relationshipNameId); // Log synchronization DocumentSynchronizationHelper.LogDocumentChange(TreeNode.NodeSiteName, TreeNode.NodeAliasPath, TaskTypeEnum.UpdateDocument, TreeProvider); ShowConfirmation(GetString("relationship.wasdeleted")); } } } } /// /// Binds the grid columns. /// /// Sender /// Source name /// Parameter private object UniGridRelationship_OnExternalDataBound(object sender, string sourceName, object parameter) { switch (sourceName.ToLowerCSafe()) { case "leftnodename": case "rightnodename": return HTMLHelper.HTMLEncode(DataHelper.GetNotEmpty(parameter, "/")); case "delete": ImageButton btn = ((ImageButton)sender); btn.PreRender += imgDelete_PreRender; break; } return parameter; } protected void imgDelete_PreRender(object sender, EventArgs e) { ImageButton imgDelete = (ImageButton)sender; if (!Enabled) { // Disable delete icon in case that editing is not allowed imgDelete.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/deletedisabled.png"); imgDelete.Enabled = false; imgDelete.Style.Add("cursor", "default"); } } #endregion #region "Methods" /// /// Saves relationship. /// public void SaveRelationship() { if (TreeNode != null) { // Check modify permissions if (CMSContext.CurrentUser.IsAuthorizedPerDocument(TreeNode, NodePermissionsEnum.Modify) == AuthorizationResultEnum.Denied) { return; } bool currentNodeIsOnLeftSide = !DefaultSide; // Selected node Id int selectedNodeId = ValidationHelper.GetInteger(hdnSelectedNodeId.Value, 0); // Get relationshipname RelationshipNameInfo relationshipNameInfo = RelationshipNameInfoProvider.GetRelationshipNameInfo(RelationshipName); int relationshipNameId = 0; if (relationshipNameInfo != null) { relationshipNameId = relationshipNameInfo.RelationshipNameId; } if ((selectedNodeId > 0) && (relationshipNameId > 0)) { try { // Left side if (currentNodeIsOnLeftSide) { RelationshipProvider.AddRelationship(TreeNode.NodeID, selectedNodeId, relationshipNameId); } // Right side else { RelationshipProvider.AddRelationship(selectedNodeId, TreeNode.NodeID, relationshipNameId); } // Log synchronization DocumentSynchronizationHelper.LogDocumentChange(TreeNode.NodeSiteName, TreeNode.NodeAliasPath, TaskTypeEnum.UpdateDocument, TreeProvider); ShowConfirmation(GetString("relationship.wasadded")); } catch (Exception ex) { ShowError(ex.Message); } } } } /// /// Returns Javascript used for invoking 'add related document' dialog. /// public string GetAddRelatedDocumentScript() { string postbackArgument = null; if (!AllowSwitchSides && !string.IsNullOrEmpty(RelationshipName)) { postbackArgument = "insertfromselectdocument"; // Register javascript 'postback' function string script = "function RefreshRelatedPanel(elementId) { if (elementId != null) { __doPostBack(elementId, '" + postbackArgument + "'); } } \n"; ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "RefreshRelatedPanel", ScriptHelper.GetScript(script)); // Dialog 'Select document' Config.EditorClientID = pnlUpdate.ClientID + ";" + hdnSelectedNodeId.ClientID; // Set dialog starting path if (!string.IsNullOrEmpty(StartingPath)) { Config.ContentStartingPath = StartingPath; } string url = CMSDialogHelper.GetDialogUrl(Config, IsLiveSite, false, null, false); return string.Format("modalDialog('{0}', 'contentselectnode', '90%', '85%');", url); } else { postbackArgument = "insert"; // Register javascript 'postback' function ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "RefreshUpdatePanel_" + ClientID, ScriptHelper.GetScript( "function RefreshUpdatePanel_" + ClientID + "(){ " + Page.ClientScript.GetPostBackEventReference(pnlUpdate, postbackArgument) + "; } \n")); // Dialog 'Add related document' string query = "?nodeid=" + TreeNode.NodeID; query = URLHelper.AddUrlParameter(query, "defaultside", DefaultSide.ToString()); query = URLHelper.AddUrlParameter(query, "allowswitchsides", AllowSwitchSides.ToString()); query = URLHelper.AddUrlParameter(query, "relationshipname", RelationshipName); query = URLHelper.AddUrlParameter(query, "externalControlID", ClientID); query = URLHelper.AddUrlParameter(query, "startingpath", StartingPath ?? ""); query = query.Replace("%", "%25").Replace("/", "%2F"); query = URLHelper.AddUrlParameter(query, "hash", QueryHelper.GetHash(query)); string url = null; if (IsLiveSite) { url = ResolveUrl("~/CMSFormControls/LiveSelectors/RelatedDocuments.aspx" + query); } else { url = ResolveUrl("~/CMSFormControls/Selectors/RelatedDocuments.aspx" + query); } return string.Format("modalDialog('{0}', 'AddRelatedDocument', '900', '300');", url); } } #endregion }