summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TWizard.php
blob: 9bc6a1e0f6c2e9375aaf6be1c4e05fe1f0679a0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<?php
/**
 * TWizard component.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by Xiang Wei Zhuo.
 *
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: 1.8 $  $Date: 2005/12/17 06:11:28 $
 * @package System.Web.UI.WebControls
 */

/**
 * TWizard splits a large form and present the user with a series
 * of smaller form to complete. The TWizard is analogous to the
 * installation wizard commonly used to install software in Windows.
 *
 * TWizard centralizes the required events to manipulate the flow of
 * the form. It also renders the appropriate step along with the navigation
 * elements. The wizard allows the steps to be presented linearly or otherwise
 * in a nonlinear fashion. That is, the forms can be filled sequentially or
 * if permitted allowed the user to choose which ever step he/she wishes.
 * In addition, the steps can be programmed to be skipped or repeated.
 *
 * A simple example of 3 steps.
 *<code>
 *  <com:TWizard ID="ContactWizard" >
 *      <com:TWizardStep Title="Step 1: Name">
 *          <com:TFormLabel For="Name">Full name:</com:TFormLabel>
 *          <com:TTextBox ID="Name" />
 *      </com:TWizardStep>
 *      <com:TWizardStep Title="Step 2: Contact">
 *          <com:TFormLabel For="Phone">Telephone Number:</com:TFormLabel>
 *          <com:TTextBox ID="Phone" />
 *          <com:TFormLabel For="Email">Email:</com:TFormLabel>
 *          <com:TTextBox ID="Email" />
 *      </com:TWizardStep>
 *      <com:TWizardStep Title="Step 3: Confirmation">
 *          <table><tr><th>Name:</th>
 *              <td><%= $this->Page->ContactWizard->Name->Text %></td>
 *          </tr><tr><th>Phone:</th>
 *              <td><%= $this->Page->ContactWizard->Phone->Text %></td>
 *          </tr><tr><th>Email:</th>
 *              <td><%= $this->Page->ContactWizard->Email->Text %></td>
 *          </tr></table>
 *      </com:TWizardStep>
 *  </com:TWizard>
 *</code>
 *
 * TWizard also intercepts the following bubbled events. E.g TButton
 * has CommandName and CommandParameter properties that bubbles as
 * "OnBubbleEvent". The following are the supported bubble event names
 * and how TWizard handles them.
 *
 *	Bubble Events
 *	- <b>next</b>, TWizard fires <b>OnNextCommand</b> event.
 *	- <b>previous</b>, TWizard fires <b>OnPreviousCommand</b> event.
 *	- <b>finish</b>, TWizard fires <b>OnFinishCommand</b> event.
 *	- <b>cancel</b>, TWizard fires <b>OnCancelCommand</b> event.
 *	- <b>jumpto</b>, TWizard fires <b>OnJumpToCommand</b> event.
 *                   <b>jumpto</b> requires a parameter, the destination step.
 *
 * E.g. anywhere within the TWizard, a button like the following
 *  <code><com:TButton CommandName="jumpto" CommandParameter="2" /></code>
 * when click will bubble to TWizard and in turn fires the OnJumpToCommand
 * with parameter value of "2".
 *
 * Namespace: System.Web.UI.WebControls
 *
 * Properties
 * - <b>ActiveStep</b>, TWizardStep,
 *   <br>Gets the current active step.
 * - <b>ActiveStepIndex</b>, integer,
 *   <br>Gets or sets the active step specified by a zero-starting index.
 * - <b>DisplaySideBar</b>, boolean
 *	 <br>isSideBarVisible or setDisplaySideBar, show or hides the side bar.
 * - <b>FinishStepButtonText</b>, string
 *   <br>Gets or sets the string for the "Finish" button.
 * - <b>NextStepButtonText</b>, string
 *   <br>Gets or sets the string for the "Next" button.
 * - <b>PreviousStepButtonText</b>, string
 *   <br>Gets or sets the string for the "Previous" button.
 * - <b>CancelButtonText</b>, string
 *   <br>Gets or sets the string for the "Cancel" button.
 *
 * Events
 * - <b>OnStepChanged</b> Occurs when the step is changed.
 * - <b>OnCancelCommand</b> Occurs when the "Cancel" button is pressed.
 * - <b>OnFinishCommand</b> Occurs when the "Finish" button is pressed.
 * - <b>OnNextCommand</b> Occurs when the "Next" button is pressed.
 * - <b>OnPreviousCommand</b> Occurs when the "Previous" button is pressed.
 * - <b>OnJumpToCommand</b> Occurs when the "JumpTo" button is pressed.
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Sat Dec 11 15:25:11 EST 2004
 * @package System.Web.UI.WebControls
 */
class TWizard extends TPanel
{
	/**
	 * The command name for the OnNextCommand.
	 * @var string
	 */
	const CMD_NEXT = 'next';

	/**
	 * The command name for the OnPreviousCommand.
	 * @var string
	 */
	const CMD_PREVIOUS = 'previous';

	/**
	 * The command name for the OnFinishCommand.
	 * @var string
	 */
	const CMD_FINISH = 'finish';

	/**
	 * The command name for the OnCancelCommand.
	 * @var string
	 */
	const CMD_CANCEL = 'cancel';

	/**
	 * The command name for the OnJumpToCommand.
	 * @var string
	 */
	const CMD_JUMP = 'jumpto';

	/**
	 * A list of steps.
	 * @var array
	 */
	protected $steps=array();

	/**
	 * A list of navigation templates, including built-in defaults.
	 * @var array
	 */
	protected $navigation = array();

	/**
	 * A list of links for the side bar.
	 * @var array
	 */
	protected $sidebarLinks = array();

	/**
	 * Set the Finish button text.
	 * @param string button text
	 */
	public function setFinishStepButtonText($value)
	{
		$this->setViewState('FinishStepButtonText', $value, 'Finish');
	}

	/**
	 * Get the Finish button text.
	 * @return string button text.
	 */
	public function getFinishStepButtonText()
	{
		return $this->getViewState('FinishStepButtonText', 'Finish');
	}

	/**
	 * Set the Next button text.
	 * @param string button text
	 */
	public function setNextStepButtonText($value)
	{
		$this->setViewState('NextStepButtonText', $value, 'Next >');

	}

	/**
	 * Get the Next button text.
	 * @return string button text.
	 */
	public function getNextStepButtonText()
	{
		return $this->getViewState('NextStepButtonText', 'Next >');
	}

	/**
	 * Set the Previous button text.
	 * @param string button text
	 */
	public function setPreviousStepButtonText($value)
	{
		$this->setViewState('PreviousStepButtonText',$value, '< Back');
	}

	/**
	 * Get the Previous button text.
	 * @return string button text.
	 */
	public function getPreviousStepButtonText()
	{
		return $this->getViewState('PreviousStepButtonText', '< Back');
	}

	/**
	 * Set the Cancel button text.
	 * @param string button text
	 */
	public function setCancelButtonText($value)
	{
		$this->setViewState('CancelButtonText', $value, 'Cancel');
	}

	/**
	 * Get the Cancel button text.
	 * @return string button text.
	 */
	public function getCancelButtonText()
	{
		return $this->getViewState('CancelButtonText', 'Cancel');
	}

	/**
	 * Show or hide the side bar.
	 * @param boolean true to show the side bar, false hides it.
	 */
	public function setDisplaySideBar($value)
	{
		$this->setViewState('DisplaySideBar',$value,true);
	}

	/**
	 * Determine if the side bar's visibility.
	 * @return boolean true if visible, false otherwise.
	 */
	public function isSideBarVisible()
	{
		return $this->getViewState('DisplaySideBar',true);
	}

	/**
	 * Get the current step. null if the ActiveStepIndex is not valid.
	 * @return TWizardStep
	 */
	public function getActiveStep()
	{
		$index = $this->getActiveStepIndex();
		if(isset($this->steps[$index]))
			return $this->steps[$index];
	}

	/**
	 * Set the active step index. This determines which step to show.
	 * @param int the current step to show.
	 */
	public function setActiveStepIndex($index)
	{
		$this->setViewState('ActiveStepIndex',$index,0);
	}

	/**
	 * Get the current step index.
	 * @return int current step index.
	 */
	public function getActiveStepIndex()
	{
		return $this->getViewState('ActiveStepIndex', 0);
	}

	/**
	 * Override the parent implementation.
	 * It adds any components that are instance of TWizardStep or TWizardTemplate
	 * as a child and body of the TWizard. Other components are handled by the parent.
	 * By adding components as child of TWizard, these component's parent
	 * is the TWizard.
	 * @param object a component object.
	 * @param object the template owner object
	 */
	public function addParsedObject($object,$context)
	{
		if($object instanceof TWizardStep)
		{
			   $object->setVisible(false);
			   $this->steps[] = $object;
			   $this->addChild($object);
			   $this->addBody($object);
		}
		else if ($object instanceof TWizardTemplate)
		{
			   $object->setVisible(false);
			   $this->navigation[$object->Type][] = $object;
			   $this->addChild($object);
			   $this->addBody($object);
		}
		else
			parent::addParsedObject($object,$context);
	}

	/**
	 * Initalize and add the default navigation templates. Add the side bar
	 * if required.
	 * @param TEventParameter event parameter to be passed to the event handlers
	 */
	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->addNavigationButtons();

		if($this->isSideBarVisible())
			$this->addNavigationSideBar();
	}

	/**
	 * Determins which wizard step to show and appropriate navigation elements.
	 * @param TEventParameter event parameter to be passed to the event handlers
	 */
	public function onPreRender($param)
	{
		parent::onPreRender($param);

		$index = $this->getActiveStepIndex();
		$totalSteps = count($this->steps);

		//show the current step
		for($i = 0; $i < $totalSteps; $i++)
			$this->steps[$i]->setVisible($i == $index);

		//determine which link is active
		for($i = 0; $i < count($this->sidebarLinks); $i++)
			$this->sidebarLinks[$i]->CssClass= ($i == $index)?'active':'';

		//hide all the navigations first.
		foreach($this->navigation as $navigation)
		{
			foreach($navigation as $nav)
				$nav->setVisible(false);
		}

		$final = $this->steps[$index]->Type == TWizardStep::TYPE_FINAL;

		//if it is not the final step
		if(!$final && $this->isSideBarVisible())
			$this->showNavigation(TWizardTemplate::ID_SIDEBAR);

		$finishStep = $index == $totalSteps-1;
		$finishStep = $finishStep || (isset($this->steps[$index+1]) &&
					$this->steps[$index+1]->Type == TWizardStep::TYPE_FINAL);

		//now show the appropriate navigation elements.
		if($index == 0)
			$this->showNavigation(TWizardTemplate::ID_START);
		else if($final) ; //skip it
		else if($finishStep)
			$this->showNavigation(TWizardTemplate::ID_FINISH);
		else
			$this->showNavigation(TWizardTemplate::ID_STEP);
	}

	/**
	 * Show of the navigation elements for a particular type.
	 * @param string navigation type.
	 */
	private function showNavigation($index)
	{
		if(!isset($this->navigation[$index])) return;
		foreach($this->navigation[$index] as $nav)
		{
			$nav->setVisible(true);
			$nav->dataBind();
		}
	}

	/**
	 * Construct the default navigation elements for the wizard.
	 * The default navigations are only added if the template for that
	 * particular navigation type is not customized.
	 */
	private function addNavigationButtons()
	{
		//create the 3 navigation components
		$start = $this->createComponent('TPanel',TWizardTemplate::ID_START);
		$start->CssClass = 'navigation';

		$step = $this->createComponent('TPanel',TWizardTemplate::ID_STEP);
		$step->CssClass = 'navigation';

		$finish = $this->createComponent('TPanel',TWizardTemplate::ID_FINISH);
		$finish->CssClass = 'navigation';

		$previousButton = $this->createComponent('TButton');
		$previousButton->setText($this->getPreviousStepButtonText());
		$previousButton->setCommandName(self::CMD_PREVIOUS);
		$previousButton->setCausesValidation(false);

		$finishButton = $this->createComponent('TButton');
		$finishButton->setText($this->getFinishStepButtonText());
		$finishButton->setCommandName(self::CMD_FINISH);

		$nextButton = $this->createComponent('TButton');
		$nextButton->setText($this->getNextStepButtonText());
		$nextButton->setCommandName(self::CMD_NEXT);

		$hiddenButton = $this->createComponent('TButton');
		$hiddenButton->setCommandName(self::CMD_NEXT);
		$hiddenButton->setStyle(array('display'=>'none'));

		$cancelButton = $this->createComponent('TButton');
		$cancelButton->setText($this->getCancelButtonText());
		$cancelButton->setCommandName(self::CMD_CANCEL);
		$cancelButton->CssClass='Cancel';
		$cancelButton->setCausesValidation(false);

		if(!isset($this->navigation[TWizardTemplate::ID_START]))
		{
			$start->addBody($nextButton);
			$start->addBody($cancelButton);
			$this->addBody($start);
			$this->navigation[TWizardTemplate::ID_START][] = $start;
		}

		if(!isset($this->navigation[TWizardTemplate::ID_STEP]))
		{

			$step->addBody($hiddenButton);
			$step->addBody($previousButton);
			$step->addBody($nextButton);
			$step->addBody($cancelButton);
			$this->addBody($step);
			$this->navigation[TWizardTemplate::ID_STEP][] = $step;
		}

		if(!isset($this->navigation[TWizardTemplate::ID_FINISH]))
		{
			$finish->addBody($previousButton);
			$finish->addBody($finishButton);
			$finish->addBody($cancelButton);
			$this->addBody($finish);
			$this->navigation[TWizardTemplate::ID_FINISH][] = $finish;
		}

	}

	/**
	 * Add the navigation side bar, a list of links to each step.
	 * The default navigation is added only if the templates for
	 * side bar are not present in the TWizard.
	 */
	private function addNavigationSideBar()
	{
		if(isset($this->navigation[TWizardTemplate::ID_SIDEBAR]))
			return;

		$total = count($this->steps);
		$current = $this->getActiveStepIndex();

		$sidebar = $this->createComponent('TPanel',TWizardTemplate::ID_SIDEBAR);
		$sidebar->CssClass = 'sidebar';

		if($total > 0) $sidebar->addBody("<ul>\n");
		for($i = 0; $i < $total; $i++)
		{
			if($this->steps[$i]->Type == TWizardStep::TYPE_FINAL)
				continue;
			$sidebar->addBody("<li>");
			$link = $this->createComponent('TLinkButton');
			$link->setCommandName(self::CMD_JUMP);
			$link->setCommandParameter($i);
			$link->Text = $this->steps[$i]->Title;
			$this->sidebarLinks[] = $link;
			$sidebar->addBody($link);
			$sidebar->addBody("</li>\n");
		}
		if($total > 0) $sidebar->addBody("</ul>\n");

		$this->addBody($sidebar);
		$this->navigation[TWizardTemplate::ID_SIDEBAR][] = $sidebar;
	}

	/**
	 * This method responds to a bubbled event. It will capture the event
	 * and fire the appropriate events, e.g. OnNextCommand if the parameter
	 * event name is "next". After the command event, a step changed event
	 * (OnStepChanged) is fire unless the event parameter variable $cancel
	 * is set to true.
	 * @param TComponent sender of the event
	 * @param TEventParameter event parameters
	 */
	public function onBubbleEvent($sender,$param)
	{
		//if false on validation, do nothing.
		if (!$this->Page->isValid()) return;

		$event = new TWizardCommandEventParameter();
		$event->currentStepIndex = $this->getActiveStepIndex();
		$event->nextStepIndex = $event->currentStepIndex;

		switch($param->name)
		{
			case self::CMD_NEXT:
				$event->nextStepIndex++;
				$this->raiseEvent('OnNextCommand',$this,$event);
				if(!$event->cancel)
				{
					$this->setActiveStepIndex($event->nextStepIndex);
					$this->raiseEvent('OnStepChanged',$this,$event);
				}
				break;
			case self::CMD_PREVIOUS:
				$event->nextStepIndex--;
				$this->raiseEvent('OnPreviousCommand',$this,$event);
				if(!$event->cancel)
				{
					$this->setActiveStepIndex($event->nextStepIndex);
					$this->raiseEvent('OnStepChanged',$this,$event);
				}
				break;
			case self::CMD_FINISH:
				if(isset($this->steps[$event->nextStepIndex+1]))
					$event->nextStepIndex++;
				$this->raiseEvent('OnFinishCommand',$this,$event);
				if(!$event->cancel)
				{
					$this->setActiveStepIndex($event->nextStepIndex);
					$this->raiseEvent('OnStepChanged',$this,$event);
				}
				break;
			case self::CMD_CANCEL:
				$event->cancel = true;
				$this->raiseEvent('OnCancelCommand',$this,$event);
				break;
			case self::CMD_JUMP:
				$event->nextStepIndex = $param->parameter;
				$this->raiseEvent('OnJumpToCommand',$this,$event);
				if(!$event->cancel)
				{
					$this->setActiveStepIndex($event->nextStepIndex);
					$this->raiseEvent('OnStepChanged',$this,$event);
				}
				break;
		}
	}
}

/**
 * TWizard command event parameter.
 *
 * This is passed as the parameter to all event orginating from TWizard.
 * If the event was a particular OnXXXXCommand, the variable $cancel
 * determine if the step will be changed. e.g in handling the "next" command
 * setting the parameter, $param->cancel = true will not result in a step change.
 *
 * The parameter also contains the current step index, and the next step index.
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail.com>
 * @version v1.0, last update on Sat Jan 22 13:59:56 EST 2005
 * @package System.Web.UI.WebControls
 */
class TWizardCommandEventParameter extends TEventParameter
{
	public $currentStepIndex = null;
	public $nextStepIndex = null;
	public $cancel = false;
}

?>