using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.PortalEngine; using CMS.SettingsProvider; public partial class CMSWebParts_Layouts_Wizard_Wizard : CMSAbstractLayoutWebPart { #region "Variables" /// /// Current step index /// protected int currentStep = 0; /// /// Step container /// protected PlaceHolder mStepContainer = null; /// /// Step headers /// protected string[] headers = null; /// /// Step icons /// protected string[] icons = null; /// /// Step descriptions /// protected string[] descriptions = null; #endregion #region "Properties" /// /// First row width /// public string Width { get { return ValidationHelper.GetString(this.GetValue("Width"), ""); } set { this.SetValue("Width", value); } } /// /// Number of the steps /// public int Steps { get { return ValidationHelper.GetInteger(this.GetValue("Steps"), 3); } set { this.SetValue("Steps", value); } } /// /// Active step number /// public int ActiveStep { get { return ValidationHelper.GetInteger(this.GetValue("ActiveStep"), 1); } set { this.SetValue("ActiveStep", value); } } /// /// Show header /// public bool ShowHeader { get { return ValidationHelper.GetBoolean(this.GetValue("ShowHeader"), true); } set { this.SetValue("ShowHeader", value); } } /// /// Use automatic header /// public bool UseAutomaticHeader { get { return ValidationHelper.GetBoolean(this.GetValue("UseAutomaticHeader"), true); } set { this.SetValue("UseAutomaticHeader", value); } } /// /// First row height /// public string HeaderHeight { get { return ValidationHelper.GetString(this.GetValue("HeaderHeight"), ""); } set { this.SetValue("HeaderHeight", value); } } /// /// First row CSS class /// public string HeaderCSSClass { get { return ValidationHelper.GetString(this.GetValue("HeaderCSSClass"), ""); } set { this.SetValue("HeaderCSSClass", value); } } /// /// Step headers /// public string StepHeaders { get { return ValidationHelper.GetString(this.GetValue("StepHeaders"), ""); } set { this.SetValue("StepHeaders", value); } } /// /// Custom step icons /// public string StepIcons { get { return ValidationHelper.GetString(this.GetValue("StepIcons"), ""); } set { this.SetValue("StepIcons", value); } } /// /// Custom step descriptions /// public string StepDescriptions { get { return ValidationHelper.GetString(this.GetValue("StepDescriptions"), ""); } set { this.SetValue("StepDescriptions", value); } } /// /// Second row height /// public string ContentHeight { get { return ValidationHelper.GetString(this.GetValue("ContentHeight"), ""); } set { this.SetValue("ContentHeight", value); } } /// /// Second row CSS class /// public string ContentCSSClass { get { return ValidationHelper.GetString(this.GetValue("ContentCSSClass"), ""); } set { this.SetValue("ContentCSSClass", value); } } /// /// Show footer /// public bool ShowFooter { get { return ValidationHelper.GetBoolean(this.GetValue("ShowFooter"), true); } set { this.SetValue("ShowFooter", value); } } /// /// Use automatic footer /// public bool UseAutomaticFooter { get { return ValidationHelper.GetBoolean(this.GetValue("UseAutomaticFooter"), true); } set { this.SetValue("UseAutomaticFooter", value); } } /// /// Third row height /// public string FooterHeight { get { return ValidationHelper.GetString(this.GetValue("FooterHeight"), ""); } set { this.SetValue("FooterHeight", value); } } /// /// Third row CSS class /// public string FooterCSSClass { get { return ValidationHelper.GetString(this.GetValue("FooterCSSClass"), ""); } set { this.SetValue("FooterCSSClass", value); } } /// /// Current step index, indexed from 1 /// public int CurrentStepIndex { get { if (currentStep == 0) { currentStep = ActiveStep; // In design mode, take the default active step from request if (RequestHelper.IsPostBack()) { currentStep = ValidationHelper.GetInteger(Request.Form[UniqueID + "$current"], currentStep); } else if (IsDesign) { currentStep = GetLastCurrentItem(currentStep); } } // Ensure the correct step index if (currentStep > Steps) { currentStep = Steps; } if (currentStep < 1) { currentStep = 1; } return currentStep; } set { currentStep = value; } } /// /// Redirect to URL when finished /// public string FinishRedirectUrl { get { return ValidationHelper.GetString(this.GetValue("FinishRedirectUrl"), ""); } set { this.SetValue("FinishRedirectUrl", value); } } #endregion #region "Methods" /// /// Content loaded event handler /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties /// protected void SetupControl() { if (this.StopProcessing) { // Do not process } else { // Register the events ComponentEvents.RequestEvents.RegisterForComponentEvent(this.WebPartID, ComponentEvents.NEXT, NextStep); ComponentEvents.RequestEvents.RegisterForComponentEvent(this.WebPartID, ComponentEvents.SUBMIT, NextStep); ComponentEvents.RequestEvents.RegisterForComponentEvent(this.WebPartID, ComponentEvents.PREVIOUS, PreviousStep); ComponentEvents.RequestEvents.RegisterForComponentEvent(this.WebPartID, ComponentEvents.BACK, PreviousStep); ComponentEvents.RequestEvents.RegisterForComponentEvent(this.WebPartID, ComponentEvents.FINISH, Finish); } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); // Load the initial step if (!RequestHelper.IsPostBack()) { LoadStep(null, CurrentStepIndex); } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); hdnCurrent.Value = CurrentStepIndex.ToString(); hdnItems.Value = Steps.ToString(); hdnCurrent.ID = "current"; hdnItems.ID = "items"; } /// /// Prepares the layout of the web part. /// protected override void PrepareLayout() { mLayoutContainer = plcL; // Prepare the main markup StartLayout(); Append("
"); Append(""); if (ViewMode == ViewModeEnum.Design) { Append(""); } Append(""); if (AllowDesignMode) { // Width resizer Append(""); } Append(""); // Footer if (AllowDesignMode) { Append(""); } Append("
"); // Add header container AddHeaderContainer(); Append("
"); } else { Append(">"); } // Add the wizard sections (header, content, footer) if (ShowHeader) { CreateSection("Header", false, UseAutomaticHeader, "~/CMSWebParts/Layouts/Wizard/WizardHeader.ascx"); } // Steps CreateSection("Content", true, false, null); // Footer if (ShowFooter) { CreateSection("Footer", false, UseAutomaticFooter, "~/CMSWebParts/Layouts/Wizard/WizardButtons.ascx"); } if (IsDesign) { Append(" 
"); Append("
"); // Steps actions if (Steps > 1) { AppendRemoveAction(ResHelper.GetString("Layout.RemoveStep"), "Steps"); Append(" "); } AppendAddAction(ResHelper.GetString("Layout.AddStep"), "Steps"); Append("
"); // Previous, next step AppendPreviousItemAction(ResHelper.GetString("Layout.PreviousStep")); Append(" "); AppendNextItemAction(ResHelper.GetString("Layout.NextStep")); Append("
"); } Append("
"); // Finalize FinishLayout(); } /// /// Creates the section within the wizard /// /// Section name /// If true, this section renders steps /// If true, the given control is rendered instead of the web part zone /// Path to the control to load private void CreateSection(string name, bool renderSteps, bool loadControl, string path) { // Set the property name string heightPropertyName = name + "Height"; string height = ValidationHelper.GetString(GetValue(heightPropertyName), ""); string rowId = "section_" + name; if (IsDesign) { Append(""); // Resizers if (AllowDesignMode) { Append(""); } Append("
"); // Add the zone if (renderSteps) { int currentStep = CurrentStepIndex; if (IsDesign) { // Render all steps for (int i = 1; i <= Steps; i++) { AppendItemStart(currentStep, i); // Render the step RenderStep(i, mStepContainer); AppendItemEnd(); } } else { PortalContext.EditableControlsHidden = true; // Prepare the container for steps mStepContainer = new PlaceHolder(); mStepContainer.ID = "plcStep"; AddControl(mStepContainer); // Render only current step RenderStep(currentStep, mStepContainer); } } else { if (loadControl) { // Load the user control instead of zone AddControl(path); } else { // Render the section zone AddZone(ID + "_" + name, "[" + name + "]"); } } if (IsDesign) { Append("
 
"); } else { Append(""); } } /// /// Renders a single wizard step /// /// Step index /// Step container private CMSWebPartZone RenderStep(int index, Control container) { return AddZone(ID + "_" + index, "[Step " + index + "]", container); } /// /// Changes the current step /// /// Offset by which the step should move /// Event name to fire prior to step change protected void ChangeStep(int offset) { if (offset == 0) { return; } int steps = Steps; int original = CurrentStepIndex; // Fire the event to validate current step var args = new StepEventArgs(steps, original); ComponentEvents.RequestEvents.RaiseEvent(this, args, ComponentEvents.VALIDATE_STEP); if (args.Cancel) { // Cancel if not validated return; } // Fire the event to finish current step ComponentEvents.RequestEvents.RaiseEvent(this, args, ((offset > 0) ? ComponentEvents.FINISH_STEP : ComponentEvents.CANCEL_STEP)); if (args.Cancel) { // Cancel if not finished return; } // Reevaluate the current item index int current = original + offset; if (current < 1) { current = 1; } if (current > Steps) { current = Steps; } if (IsDesign) { // Change only by javascript ScriptHelper.RegisterStartupScript(this, typeof(string), this.ClientID + "_change", ScriptHelper.GetScript("ChangeLayoutItem('" + this.InstanceGUID + "', '" + this.ClientID + "', " + offset + ");")); // Load the step LoadStep(args, current); } else { // Move the step by the given index CurrentStepIndex = current; // Change the step if (original != current) { mStepContainer.Controls.Clear(); // Create the zone var zone = RenderStep(current, NO_CONTAINER); zone.Page = this.Page; // Load the zone content var zoneInstance = this.PagePlaceholder.TemplateInstance.GetZone(ID + "_" + current); zone.LoadWebParts(zoneInstance); zone.LoadWebPartsContent(false); zone.OnContentLoaded(); zone.LoadRegionsContent(true); // Add to the controls collection so it catches up the control cycle mStepContainer.Controls.Add(zone); // Load the step LoadStep(args, current); } } } /// /// Loads the given step /// /// Step arguments /// Step index private void LoadStep(StepEventArgs args, int current) { // Ensure the event arguments if (args == null) { args = new StepEventArgs(this.Steps, current); } args.CurrentStep = current; // Fire the event to finish current step ComponentEvents.RequestEvents.RaiseEvent(this, args, ComponentEvents.LOAD_STEP); // Skip the current step if requested if (args.Skip) { ChangeStep(1); } else { // Use the custom step header if set string header = GetHeader(current); if (!String.IsNullOrEmpty(header)) { args.StepHeader = header; } // Use the custom step icon if set string icon = GetIcon(current); if (!String.IsNullOrEmpty(icon)) { args.StepIcon = icon; } // Use the custom step description if set string description = GetDescription(current); if (!String.IsNullOrEmpty(description)) { args.StepInfo = description; } // Handle the next button display if ((args.Steps == current) && String.IsNullOrEmpty(FinishRedirectUrl)) { args.HideNextButton = true; } // Fire the event to notify that the step was loaded ComponentEvents.RequestEvents.RaiseEvent(this, args, ComponentEvents.STEP_LOADED); } } /// /// Moves the wizard to the next step /// protected void NextStep(object sender, EventArgs e) { ChangeStep(1); } /// /// Moves the wizard to the next step /// protected void PreviousStep(object sender, EventArgs e) { ChangeStep(-1); } /// /// Finishes the wizard /// protected void Finish(object sender, EventArgs e) { // Fire the event to validate current step var args = new StepEventArgs(Steps, CurrentStepIndex); ComponentEvents.RequestEvents.RaiseEvent(this, args, ComponentEvents.VALIDATE_STEP); if (args.Cancel) { // Cancel if not validated return; } // Fire the event to finish current step ComponentEvents.RequestEvents.RaiseEvent(this, args, ComponentEvents.FINISH_STEP); if (args.Cancel) { // Cancel if not finished return; } // Redirect to the given URL string url = FinishRedirectUrl; if (!String.IsNullOrEmpty(url)) { URLHelper.Redirect(url); } } /// /// Gets the header for the given step /// /// Step index protected string GetHeader(int stepIndex) { if (headers == null) { headers = this.StepHeaders.Split('\n'); } // Get the header from the list stepIndex--; if (headers.Length <= stepIndex) { return null; } return headers[stepIndex].Trim(); } /// /// Gets the icon for the given step /// /// Step index protected string GetIcon(int stepIndex) { if (icons == null) { icons = this.StepIcons.Split('\n'); } // Get the icon from the list stepIndex--; if (icons.Length <= stepIndex) { return null; } return icons[stepIndex].Trim(); } /// /// Gets the description for the given step /// /// Step index protected string GetDescription(int stepIndex) { if (descriptions == null) { descriptions = this.StepDescriptions.Split('\n'); } // Get the description from the list stepIndex--; if (descriptions.Length <= stepIndex) { return null; } return descriptions[stepIndex].Trim(); } #endregion }