using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.UIControls; using CMS.WorkflowEngine; using CMS.WorkflowEngine.Definitions; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.DocumentEngine; using CMS.ExtendedControls; using TreeNode = CMS.DocumentEngine.TreeNode; public partial class CMSModules_Workflows_Controls_UI_WorkflowStep_SourcePoint_Edit : CMSAdminEditControl { #region "Variables" private WorkflowStepInfo mCurrentStepInfo = null; private SourcePoint mSourcePoint = null; #endregion #region "Properties" /// /// Workflow step ID /// public int WorkflowStepId { get; set; } /// /// Redirect URL that is used after new source point is created. /// public string AfterCreateRedirectURL { get; set; } /// /// Indicates if simple mode should be displayed /// public bool SimpleMode { get; set; } /// /// Current workflow step info /// private WorkflowStepInfo CurrentStepInfo { get { if (mCurrentStepInfo == null) { mCurrentStepInfo = WorkflowStepInfoProvider.GetWorkflowStepInfo(WorkflowStepId); } return mCurrentStepInfo; } } /// /// Current source point on current step /// private SourcePoint CurrentSourcePoint { get { if (mSourcePoint == null) { if ((SourcePointGuid != Guid.Empty) && (CurrentStepInfo != null)) { mSourcePoint = CurrentStepInfo.StepDefinition.SourcePoints.Find(i => i.Guid == SourcePointGuid); // Check definition point var defPoint = CurrentStepInfo.StepDefinition.DefinitionPoint; if (defPoint != null) { if ((mSourcePoint == null) && (SourcePointGuid == defPoint.Guid)) { mSourcePoint = defPoint; } } } } return mSourcePoint; } } /// /// Source point GUID /// public Guid SourcePointGuid { get; set; } /// /// Indicates if condition editing control should be displayed /// public bool ShowCondition { get { return plcCondition.Visible; } set { plcCondition.Visible = value; } } /// /// Gets or sets name(s) of the Macro rule category(ies) which should be displayed in Rule designer. Items should be separated by semicolon. /// public string RuleCategoryNames { get { return cbCondition.RuleCategoryNames; } set { cbCondition.RuleCategoryNames = value; } } #endregion #region "Page events" protected override void OnInit(EventArgs e) { base.OnInit(e); // Register validate event ComponentEvents.RequestEvents.RegisterForComponentEvent(ComponentName, ComponentEvents.VALIDATE_DATA, null, (s, args) => { args.IsValid = ValidateData(); }); // Register save event ComponentEvents.RequestEvents.RegisterForComponentEvent(ComponentName, ComponentEvents.SAVE_DATA, (s, args) => { SaveData(false); }); } protected void Page_Load(object sender, EventArgs e) { if (StopProcessing) { // Do nothing! } else { plcLabel.Visible = !SimpleMode; RequiredFieldValidatorLabel.ErrorMessage = GetString("workflowstep.sourcepoint.requireslabel"); if (WorkflowStepId > 0) { EditedObject = CurrentStepInfo; } if (CurrentSourcePoint != null) { // Switch default doesn't have condition if ((CurrentSourcePoint.Type == SourcePointTypeEnum.SwitchDefault) || (CurrentSourcePoint.Type == SourcePointTypeEnum.Timeout)) { lblCondition.Visible = cbCondition.Visible = false; } if (!RequestHelper.IsPostBack()) { txtLabel.Text = CurrentSourcePoint.Label; txtText.Text = CurrentSourcePoint.Text; txtTooltip.Text = CurrentSourcePoint.Tooltip; cbCondition.Text = CurrentSourcePoint.Condition; } } if (cbCondition.Visible) { WorkflowInfo wi = WorkflowInfoProvider.GetWorkflowInfo(CurrentStepInfo.StepWorkflowID); cbCondition.ResolverName = WorkflowHelper.GetResolverName(wi); } } } #endregion #region "Methods" /// /// Validates the data, returns true if succeeded. /// public bool ValidateData() { if (Visible && !StopProcessing) { // Validate control settings if ((WorkflowStepId <= 0) || (CurrentStepInfo == null)) { ShowError(GetString("editedobject.notexists")); return false; } // Validate user input string result = null; if (!SimpleMode) { result = new Validator().NotEmpty(txtLabel.Text.Trim(), GetString("workflowstep.sourcepoint.requireslabel")).Result; } if (!String.IsNullOrEmpty(result)) { ShowError(result); return false; } } // Everything is OK return true; } /// /// Saves data of edited source point from controls to edited object. /// /// Indicates whether form data should be validated prior to save public void SaveData(bool validateData) { if (Visible && !StopProcessing) { if (!validateData || ValidateData()) { if (CurrentSourcePoint == null) { // Create new source point SourcePoint sp = WorkflowHelper.CreateSourcePoint(CurrentStepInfo.StepType); SetValues(sp); // AddSourcePoint saves the workflow step to database CurrentStepInfo.AddSourcePoint(sp); SourcePointGuid = sp.Guid; if (String.IsNullOrEmpty(AfterCreateRedirectURL)) { ShowChangesSaved(); } else { URLHelper.Redirect(URLHelper.AppendQuery(AfterCreateRedirectURL, String.Format("?workflowstepid={0}&sourcepointguid={1}&saved=1", WorkflowStepId, SourcePointGuid))); } } else { // Edit existing source point if (CurrentSourcePoint.Label != txtLabel.Text.Trim()) { // Refresh header ScriptHelper.RefreshTabHeader(Page, null); } SetValues(CurrentSourcePoint); } } } } /// /// Saves edited object to database. /// /// Indicates whether form data should be validated prior to save public void Save(bool validateData) { if (!validateData || ValidateData()) { if (validateData) { SaveData(false); } WorkflowStepInfoProvider.SetWorkflowStepInfo(CurrentStepInfo); ShowChangesSaved(); } } /// /// Sets values from UI to provided source point object. /// /// Source point private SourcePoint SetValues(SourcePoint sourcePoint) { if (sourcePoint != null) { sourcePoint.Label = txtLabel.Text; sourcePoint.Text = txtText.Text; sourcePoint.Tooltip = txtTooltip.Text; if (ShowCondition && (sourcePoint.Type != SourcePointTypeEnum.SwitchDefault)) { sourcePoint.Condition = cbCondition.Text; } } return sourcePoint; } #endregion }