/// <summary> /// Publishes document. /// </summary> /// <param name="node">Node to publish</param> /// <param name="wm">Workflow manager</param> /// <returns>Whether node is already published</returns> private static bool Publish(TreeNode node, WorkflowManager wm) { WorkflowStepInfo currentStep = wm.GetStepInfo(node); bool toReturn = true; if (currentStep != null) { // For archive step start new version if (currentStep.StepName.ToLower() == "archived") { VersionManager vm = new VersionManager(node.TreeProvider); currentStep = vm.CheckOut(node, node.IsPublished, true); vm.CheckIn(node, null, null); } // Remove possible checkout if (node.GetIntegerValue("DocumentCheckedOutByUserID") > 0) { TreeProvider.ClearCheckoutInformation(node); node.Update(); } // Approve until the document is published while ((currentStep != null) && (currentStep.StepName.ToLower() != "published")) { currentStep = wm.MoveToNextStep(node, string.Empty); toReturn = false; } } return toReturn; }
/// <summary> /// Provides operations necessary to create and store new attachment. /// </summary> private void HandleAttachmentUpload(bool fieldAttachment) { TreeProvider tree = null; TreeNode node = null; // New attachment AttachmentInfo newAttachment = null; string message = string.Empty; bool fullRefresh = false; try { // Get the existing document if (DocumentID != 0) { // Get document tree = new TreeProvider(CMSContext.CurrentUser); node = DocumentHelper.GetDocument(DocumentID, tree); if (node == null) { throw new Exception("Given document doesn't exist!"); } } #region "Check permissions" if (this.CheckPermissions) { CheckNodePermissions(node); } #endregion // Check the allowed extensions CheckAllowedExtensions(); // Standard attachments if (DocumentID != 0) { // Ensure automatic check-in/check-out bool useWorkflow = false; bool autoCheck = false; WorkflowManager workflowMan = new WorkflowManager(tree); VersionManager vm = null; // Get workflow info WorkflowInfo wi = workflowMan.GetNodeWorkflow(node); // Check if the document uses workflow if (wi != null) { useWorkflow = true; autoCheck = !wi.UseCheckInCheckOut(CMSContext.CurrentSiteName); } // Check out the document if (autoCheck) { // Get original step Id int originalStepId = node.DocumentWorkflowStepID; // Get current step info WorkflowStepInfo si = workflowMan.GetStepInfo(node); if (si != null) { // Decide if full refresh is needed bool automaticPublish = wi.WorkflowAutoPublishChanges; string stepName = si.StepName.ToLower(); // Document is published or archived or uses automatic publish or step is different than original (document was updated) fullRefresh = (stepName == "published") || (stepName == "archived") || (automaticPublish && (stepName != "published")) || (originalStepId != node.DocumentWorkflowStepID); } vm = new VersionManager(tree); vm.CheckOut(node, node.IsPublished, true); } // Handle field attachment if (fieldAttachment) { newAttachment = DocumentHelper.AddAttachment(node, AttachmentGUIDColumnName, Guid.Empty, Guid.Empty, ucFileUpload.PostedFile, tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); // Update attachment field DocumentHelper.UpdateDocument(node, tree); } // Handle grouped and unsorted attachments else { // Grouped attachment if (AttachmentGroupGUID != Guid.Empty) { newAttachment = DocumentHelper.AddGroupedAttachment(node, AttachmentGUID, AttachmentGroupGUID, ucFileUpload.PostedFile, tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); } // Unsorted attachment else { newAttachment = DocumentHelper.AddUnsortedAttachment(node, AttachmentGUID, ucFileUpload.PostedFile, tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); } } // Check in the document if (autoCheck) { if (vm != null) { vm.CheckIn(node, null, null); } } // Log synchronization task if not under workflow if (!useWorkflow) { DocumentSynchronizationHelper.LogDocumentChange(node, TaskTypeEnum.UpdateDocument, tree); } } // Temporary attachments if (FormGUID != Guid.Empty) { newAttachment = AttachmentManager.AddTemporaryAttachment(FormGUID, AttachmentGUIDColumnName, AttachmentGUID, AttachmentGroupGUID, ucFileUpload.PostedFile, CMSContext.CurrentSiteID, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); } // Ensure properties update if ((newAttachment != null) && !InsertMode) { AttachmentGUID = newAttachment.AttachmentGUID; } if (newAttachment == null) { throw new Exception("The attachment hasn't been created since no DocumentID or FormGUID was supplied."); } } catch (Exception ex) { message = ex.Message; lblError.Text = message; } finally { // Call aftersave javascript if exists if ((string.IsNullOrEmpty(message)) && (newAttachment != null)) { string closeString = "window.close();"; // Register wopener script ScriptHelper.RegisterWOpenerScript(Page); if (!String.IsNullOrEmpty(AfterSaveJavascript)) { string url = null; string saveName = URLHelper.GetSafeFileName(newAttachment.AttachmentName, CMSContext.CurrentSiteName); if (node != null) { SiteInfo si = SiteInfoProvider.GetSiteInfo(node.NodeSiteID); if (si != null) { bool usePermanent = SettingsKeyProvider.GetBoolValue(si.SiteName + ".CMSUsePermanentURLs"); if (usePermanent) { url = ResolveUrl(AttachmentManager.GetAttachmentUrl(newAttachment.AttachmentGUID, saveName)); } else { url = ResolveUrl(AttachmentManager.GetAttachmentUrl(saveName, node.NodeAliasPath)); } } } else { url = ResolveUrl(AttachmentManager.GetAttachmentUrl(newAttachment.AttachmentGUID, saveName)); } // Calling javascript function with parameters attachments url, name, width, height string jsParams = "('" + url + "', '" + newAttachment.AttachmentName + "', '" + newAttachment.AttachmentImageWidth + "', '" + newAttachment.AttachmentImageHeight + "');"; string script = "if ((wopener.parent != null) && (wopener.parent." + AfterSaveJavascript + " != null)){wopener.parent." + AfterSaveJavascript + jsParams + "}"; script += "else if (wopener." + AfterSaveJavascript + " != null){wopener." + AfterSaveJavascript + jsParams + "}"; ScriptHelper.RegisterStartupScript(this.Page, typeof(Page), "refreshAfterSave", ScriptHelper.GetScript(script + closeString)); } // Create attachment info string string attachmentInfo = ""; if ((newAttachment != null) && (newAttachment.AttachmentGUID != Guid.Empty) && (this.IncludeNewItemInfo)) { attachmentInfo = newAttachment.AttachmentGUID.ToString(); } // Ensure message text message = HTMLHelper.EnsureLineEnding(message, " "); // Call function to refresh parent window ScriptHelper.RegisterStartupScript(this.Page, typeof(Page), "refresh", ScriptHelper.GetScript("if ((wopener.parent != null) && (wopener.parent.InitRefresh_" + ParentElemID + " != null)){wopener.parent.InitRefresh_" + ParentElemID + "(" + ScriptHelper.GetString(message.Trim()) + ", " + (fullRefresh ? "true" : "false") + ", false" + ((attachmentInfo != "") ? ", '" + attachmentInfo + "'" : "") + (InsertMode ? ", 'insert'" : ", 'update'") + ");}" + closeString)); } } }
/// <summary> /// UniGrid action buttons event handler. /// </summary> protected void gridAttachments_OnAction(string actionName, object actionArgument) { if (Enabled && !HideActions) { // Check the permissions #region "Check permissions" if (CheckPermissions) { if (FormGUID != Guid.Empty) { if (!RaiseOnCheckPermissions("Create", this)) { if (!CMSContext.CurrentUser.IsAuthorizedToCreateNewDocument(NodeParentNodeID, NodeClassName)) { lblError.Text = GetString("attach.actiondenied"); return; } } } else { if (!RaiseOnCheckPermissions("Modify", this)) { if (CMSContext.CurrentUser.IsAuthorizedPerDocument(Node, NodePermissionsEnum.Modify) == AuthorizationResultEnum.Denied) { lblError.Text = GetString("attach.actiondenied"); return; } } } } #endregion Guid attachmentGuid = Guid.Empty; // Get action argument (Guid or int) if (ValidationHelper.IsGuid(actionArgument)) { attachmentGuid = ValidationHelper.GetGuid(actionArgument, Guid.Empty); } // Process proper action switch (actionName.ToLower()) { case "moveup": if (attachmentGuid != Guid.Empty) { // Move attachment up if (FormGUID == Guid.Empty) { // Ensure automatic check-in/ check-out VersionManager vm = null; // Check out the document if (AutoCheck) { vm = new VersionManager(TreeProvider); vm.CheckOut(Node, Node.IsPublished, true); } DocumentHelper.MoveAttachmentUp(attachmentGuid, Node); // Check in the document if (AutoCheck) { if (vm != null) { vm.CheckIn(Node, null, null); VersionHistoryID = Node.DocumentCheckedOutVersionHistoryID; } // Ensure full page refresh ScriptHelper.RegisterStartupScript(Page, typeof(Page), "moveUpRefresh", ScriptHelper.GetScript(String.Format("InitRefresh_{0}('', true, false, 'moveup');", ClientID))); } // Log synchronization task if not under workflow if (!UsesWorkflow) { DocumentSynchronizationHelper.LogDocumentChange(Node, TaskTypeEnum.UpdateDocument, TreeProvider); } } else { AttachmentManager.MoveAttachmentUp(attachmentGuid, 0); } } break; case "movedown": if (attachmentGuid != Guid.Empty) { // Move attachment down if (FormGUID == Guid.Empty) { // Ensure automatic check-in/ check-out VersionManager vm = null; // Check out the document if (AutoCheck) { vm = new VersionManager(TreeProvider); vm.CheckOut(Node, Node.IsPublished, true); } DocumentHelper.MoveAttachmentDown(attachmentGuid, Node); // Check in the document if (AutoCheck) { if (vm != null) { vm.CheckIn(Node, null, null); VersionHistoryID = Node.DocumentCheckedOutVersionHistoryID; } // Ensure full page refresh ScriptHelper.RegisterStartupScript(Page, typeof(Page), "moveDownRefresh", ScriptHelper.GetScript(String.Format("InitRefresh_{0}('', true, false, 'movedown');", ClientID))); } // Log synchronization task if not under workflow if (!UsesWorkflow) { DocumentSynchronizationHelper.LogDocumentChange(Node, TaskTypeEnum.UpdateDocument, TreeProvider); } } else { AttachmentManager.MoveAttachmentDown(attachmentGuid, 0); } } break; case "delete": if (attachmentGuid != Guid.Empty) { // Delete attachment if (FormGUID == Guid.Empty) { // Ensure automatic check-in/ check-out VersionManager vm = null; // Check out the document if (AutoCheck) { vm = new VersionManager(TreeProvider); vm.CheckOut(Node, Node.IsPublished, true); } DocumentHelper.DeleteAttachment(Node, attachmentGuid, TreeProvider); // Check in the document if (AutoCheck) { if (vm != null) { vm.CheckIn(Node, null, null); VersionHistoryID = Node.DocumentCheckedOutVersionHistoryID; } // Ensure full page refresh ScriptHelper.RegisterStartupScript(Page, typeof(Page), "deleteRefresh", ScriptHelper.GetScript(String.Format("InitRefresh_{0}('', true, false, 'delete');", ClientID))); } // Log synchronization task if not under workflow if (!UsesWorkflow) { DocumentSynchronizationHelper.LogDocumentChange(Node, TaskTypeEnum.UpdateDocument, TreeProvider); } } else { AttachmentManager.DeleteTemporaryAttachment(attachmentGuid, CMSContext.CurrentSiteName); } lblInfo.Text = GetString("attach.deleted"); } break; } } }
/// <summary> /// Publishes document. /// </summary> /// <param name="node">Node to publish</param> /// <param name="wm">Workflow manager</param> /// <param name="currentStep">Current workflow step</param> /// <returns>Whether node is already published</returns> private static bool Publish(TreeNode node, WorkflowManager wm, WorkflowStepInfo currentStep) { bool toReturn = true; if (currentStep != null) { // For archive step start new version if (currentStep.StepName.ToLower() == "archived") { VersionManager vm = new VersionManager(node.TreeProvider); currentStep = vm.CheckOut(node, node.IsPublished, true); vm.CheckIn(node, null, null); } // Approve until the step is publish while ((currentStep != null) && (currentStep.StepName.ToLower() != "published")) { currentStep = wm.MoveToNextStep(node, string.Empty); toReturn = false; } // Document is already published, check if still under workflow if (toReturn && (currentStep.StepName.ToLower() == "published")) { WorkflowScopeInfo wsi = wm.GetNodeWorkflowScope(node); if (wsi == null) { DocumentHelper.ClearWorkflowInformation(node); VersionManager vm = new VersionManager(node.TreeProvider); vm.RemoveWorkflow(node); } } } return toReturn; }
/// <summary> /// Gets a dataset of documents in the example section and updates them. Called when the "Get and update documents" button is pressed. /// Expects the "CreateExampleObjects" and "CreateDocument" methods to be run first. /// </summary> private bool GetAndUpdateDocuments() { // Create an instance of the Tree provider first TreeProvider tree = new TreeProvider(CMSContext.CurrentUser); // Prepare parameters string siteName = CMSContext.CurrentSiteName; string aliasPath = "/API-Example/%"; string culture = "en-us"; bool combineWithDefaultCulture = false; string classNames = "CMS.MenuItem"; string where = null; string orderBy = null; int maxRelativeLevel = -1; bool selectOnlyPublished = false; // Fill dataset with documents DataSet documents = DocumentHelper.GetDocuments(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, tree); if (!DataHelper.DataSourceIsEmpty(documents)) { // Create a new Version manager instance VersionManager manager = new VersionManager(tree); // Loop through all documents foreach (DataRow documentRow in documents.Tables[0].Rows) { // Create a new Tree node from the data row TreeNode editDocument = TreeNode.New(documentRow, "CMS.MenuItem", tree); // Check out the document manager.CheckOut(editDocument); string newName = editDocument.DocumentName.ToLower(); // Change document data editDocument.DocumentName = newName; // Change coupled data editDocument.SetValue("MenuItemName", newName); // Save the changes DocumentHelper.UpdateDocument(editDocument, tree); // Check in the document manager.CheckIn(editDocument, null, null); } return true; } return false; }
/// <summary> /// If the document hasn't been checked out, creates a new modified version of the document. If it has, the example modifies the checked out version. Called when the "Edit document" button is pressed. /// Expects the "CreateExampleObjects" method and optionally the "CheckOut" method to be run first. /// </summary> private bool EditDocument() { TreeProvider tree = new TreeProvider(CMSContext.CurrentUser); // Prepare parameters string siteName = CMSContext.CurrentSiteName; string aliasPath = "/API-Example"; string culture = "en-us"; bool combineWithDefaultCulture = false; string classNames = TreeProvider.ALL_CLASSNAMES; string where = null; string orderBy = null; int maxRelativeLevel = -1; bool selectOnlyPublished = false; string columns = null; // Get the document TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree); if (node != null) { WorkflowManager workflowmanager = new WorkflowManager(tree); // Make sure the document uses workflow WorkflowInfo workflow = workflowmanager.GetNodeWorkflow(node); if (workflow != null) { // Check if the workflow uses check-in/check-out bool autoCheck = !workflow.UseCheckInCheckOut(CMSContext.CurrentSiteName); // Create a new version manager instance VersionManager versionmanager = new VersionManager(tree); // If it does not use check-in/check-out, check out the document automatically if (autoCheck) { versionmanager.CheckOut(node); } if (node.IsCheckedOut) { // Edit the last version of the document string newName = node.DocumentName.ToLower(); node.DocumentName = newName; node.SetValue("MenuItemName", newName); // Save the document version DocumentHelper.UpdateDocument(node, tree); // Automatically check in if (autoCheck) { versionmanager.CheckIn(node, null, null); } return true; } else { apiEditDocument.ErrorMessage = "The document hasn't been checked out."; } } else { apiEditDocument.ErrorMessage = "The document doesn't use workflow."; } } return false; }
/// <summary> /// Checks out the document. If check-in/check-out is disabled for the default workflow, the example can't be used. Called when the "Check out document" button is pressed. /// Expects the "CreateExampleObjects" method to be run first. /// </summary> private bool CheckOut() { TreeProvider tree = new TreeProvider(CMSContext.CurrentUser); // Prepare parameters string siteName = CMSContext.CurrentSiteName; string aliasPath = "/API-Example"; string culture = "en-us"; bool combineWithDefaultCulture = false; string classNames = TreeProvider.ALL_CLASSNAMES; string where = null; string orderBy = null; int maxRelativeLevel = -1; bool selectOnlyPublished = false; string columns = null; // Get the document TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree); if (node != null) { // Create a new Workflow manager instance WorkflowManager workflowmanager = new WorkflowManager(tree); // Make sure the document uses workflow WorkflowInfo workflow = workflowmanager.GetNodeWorkflow(node); if (workflow != null) { // Check if the workflow uses check-in/check-out functionality if (workflow.UseCheckInCheckOut(CMSContext.CurrentSiteName)) { // The document has to be checked in if (!node.IsCheckedOut) { // Create a new version manager instance VersionManager versionmanager = new VersionManager(tree); // Check out the document to create a new document version versionmanager.CheckOut(node); return true; } else { apiCheckOut.ErrorMessage = "The document has already been checked out."; } } else { apiCheckOut.ErrorMessage = "The workflow does not use check-in/check-out. See the \"Edit document\" example, which checks the document out and in automatically."; } } else { apiCheckOut.ErrorMessage = "The document doesn't use workflow."; } } return false; }
/// <summary> /// Provides operations necessary to create and store new attachment. /// </summary> private void HandleAttachmentUpload(bool fieldAttachment) { TreeProvider tree = null; TreeNode node = null; // New attachment AttachmentInfo newAttachment = null; string message = string.Empty; bool fullRefresh = false; try { // Get the existing document if (DocumentID != 0) { // Get document tree = new TreeProvider(CMSContext.CurrentUser); node = DocumentHelper.GetDocument(DocumentID, tree); if (node == null) { throw new Exception("Given document doesn't exist!"); } } #region "Check permissions" if (CheckPermissions) { CheckNodePermissions(node); } #endregion // Check the allowed extensions CheckAllowedExtensions(); // Standard attachments if (DocumentID != 0) { // Ensure automatic check-in/check-out bool useWorkflow = false; bool autoCheck = false; WorkflowManager workflowMan = WorkflowManager.GetInstance(tree); VersionManager vm = null; // Get workflow info WorkflowInfo wi = workflowMan.GetNodeWorkflow(node); // Check if the document uses workflow if (wi != null) { useWorkflow = true; autoCheck = !wi.UseCheckInCheckOut(CMSContext.CurrentSiteName); } // Check out the document if (autoCheck) { // Get original step Id int originalStepId = node.DocumentWorkflowStepID; // Get current step info WorkflowStepInfo si = workflowMan.GetStepInfo(node); if (si != null) { // Decide if full refresh is needed bool automaticPublish = wi.WorkflowAutoPublishChanges; // Document is published or archived or uses automatic publish or step is different than original (document was updated) fullRefresh = si.StepIsPublished || si.StepIsArchived || (automaticPublish && !si.StepIsPublished) || (originalStepId != node.DocumentWorkflowStepID); } vm = VersionManager.GetInstance(tree); vm.CheckOut(node, node.IsPublished, true); } // Handle field attachment if (fieldAttachment) { newAttachment = DocumentHelper.AddAttachment(node, AttachmentGUIDColumnName, Guid.Empty, Guid.Empty, ucFileUpload.PostedFile, tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); // Update attachment field DocumentHelper.UpdateDocument(node, tree); } // Handle grouped and unsorted attachments else { // Grouped attachment if (AttachmentGroupGUID != Guid.Empty) { newAttachment = DocumentHelper.AddGroupedAttachment(node, AttachmentGUID, AttachmentGroupGUID, ucFileUpload.PostedFile, tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); } // Unsorted attachment else { newAttachment = DocumentHelper.AddUnsortedAttachment(node, AttachmentGUID, ucFileUpload.PostedFile, tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); } } // Check in the document if (autoCheck) { if (vm != null) { vm.CheckIn(node, null, null); } } // Log synchronization task if not under workflow if (!useWorkflow) { DocumentSynchronizationHelper.LogDocumentChange(node, TaskTypeEnum.UpdateDocument, tree); } } // Temporary attachments if (FormGUID != Guid.Empty) { newAttachment = AttachmentInfoProvider.AddTemporaryAttachment(FormGUID, AttachmentGUIDColumnName, AttachmentGUID, AttachmentGroupGUID, ucFileUpload.PostedFile, CMSContext.CurrentSiteID, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize); } // Ensure properties update if ((newAttachment != null) && !InsertMode) { AttachmentGUID = newAttachment.AttachmentGUID; } if (newAttachment == null) { throw new Exception("The attachment hasn't been created since no DocumentID or FormGUID was supplied."); } } catch (Exception ex) { message = ex.Message; lblError.Text = message; } finally { // Call aftersave javascript if exists if ((string.IsNullOrEmpty(message)) && (newAttachment != null)) { string closeString = "CloseDialog();"; // Register wopener script ScriptHelper.RegisterWOpenerScript(Page); if (!String.IsNullOrEmpty(AfterSaveJavascript)) { string url = null; string saveName = URLHelper.GetSafeFileName(newAttachment.AttachmentName, CMSContext.CurrentSiteName); if (node != null) { SiteInfo si = SiteInfoProvider.GetSiteInfo(node.NodeSiteID); if (si != null) { bool usePermanent = SettingsKeyProvider.GetBoolValue(si.SiteName + ".CMSUsePermanentURLs"); if (usePermanent) { url = ResolveUrl(AttachmentInfoProvider.GetAttachmentUrl(newAttachment.AttachmentGUID, saveName)); } else { url = ResolveUrl(AttachmentInfoProvider.GetAttachmentUrl(saveName, node.NodeAliasPath)); } } } else { url = ResolveUrl(AttachmentInfoProvider.GetAttachmentUrl(newAttachment.AttachmentGUID, saveName)); } // Calling javascript function with parameters attachments url, name, width, height string jsParams = "('" + url + "', '" + newAttachment.AttachmentName + "', '" + newAttachment.AttachmentImageWidth + "', '" + newAttachment.AttachmentImageHeight + "');"; string script = "if ((wopener.parent != null) && (wopener.parent." + AfterSaveJavascript + " != null)){wopener.parent." + AfterSaveJavascript + jsParams + "}"; script += "else if (wopener." + AfterSaveJavascript + " != null){wopener." + AfterSaveJavascript + jsParams + "}"; ScriptHelper.RegisterStartupScript(Page, typeof(Page), "refreshAfterSave", ScriptHelper.GetScript(script + closeString)); } // Create attachment info string string attachmentInfo = ""; if ((newAttachment != null) && (newAttachment.AttachmentGUID != Guid.Empty) && (IncludeNewItemInfo)) { attachmentInfo = newAttachment.AttachmentGUID.ToString(); } // Ensure message text message = HTMLHelper.EnsureLineEnding(message, " "); // Call function to refresh parent window ScriptHelper.RegisterStartupScript(Page, typeof(Page), "refresh", ScriptHelper.GetScript("if ((wopener.parent != null) && (wopener.parent.InitRefresh_" + ParentElemID + " != null)){wopener.parent.InitRefresh_" + ParentElemID + "(" + ScriptHelper.GetString(message.Trim()) + ", " + (fullRefresh ? "true" : "false") + ", false" + ((attachmentInfo != "") ? ", '" + attachmentInfo + "'" : "") + (InsertMode ? ", 'insert'" : ", 'update'") + ");}" + closeString)); } } }