TCallback::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TCallback class file.
5
 *
6
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web\UI\ActiveControls;
12
13
/**
14
 * Load active control adapter.
15
 */
16
use Prado\Prado;
17
use Prado\Web\UI\TControl;
18
19
/**
20
 * TCallback component class.
21
 *
22
 * The TCallback provides a basic callback handler that can be invoked from the
23
 * client side by running the javascript code obtained from the
24
 * {@see \Prado\Web\UI\ActiveControls\TBaseActiveCallbackControl::getJavascript ActiveControl.Javascript} property.
25
 * The event {@see onCallback OnCallback} is raised when a callback is requested made.
26
 *
27
 * Example usage:
28
 * ```php
29
 * 	<com:TCallback ID="callback1" OnCallback="callback1_Requested" />
30
 *  <script>
31
 * 		function do_callback1()
32
 *      {
33
 *           var request = <%= $this->callback1->ActiveControl->Javascript %>;
34
 *			 request.dispatch();
35
 *      }
36
 *  </script>
37
 *  <div onclick="do_callback1()">Click Me!</div>
38
 * ```
39
 *
40
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
41
 * @since 3.1
42
 * @method TActiveControlAdapter getAdapter()
43
 */
44
class TCallback extends TControl implements IActiveControl, ICallbackEventHandler
45
{
46
	/**
47
	 * Creates a new callback control, sets the adapter to
48
	 * TActiveControlAdapter. If you override this class, be sure to set the
49
	 * adapter appropriately by, for example, call this constructor.
50
	 */
51
	public function __construct()
52
	{
53
		parent::__construct();
54
		$this->setAdapter(new TActiveControlAdapter($this));
55
	}
56
57
	/**
58
	 * @return TBaseActiveCallbackControl standard callback options.
59
	 */
60
	public function getActiveControl()
61
	{
62
		return $this->getAdapter()->getBaseActiveControl();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAdapter...>getBaseActiveControl() also could return the type Prado\Web\UI\ActiveControls\TBaseActiveControl which includes types incompatible with the return type mandated by Prado\Web\UI\ActiveContr...rol::getActiveControl() of Prado\Web\UI\ActiveContr...seActiveCallbackControl. Consider adding a type-check to rule them out.
Loading history...
63
	}
64
65
	/**
66
	 * @return TCallbackClientSide client side request options.
67
	 */
68
	public function getClientSide()
69
	{
70
		return $this->getActiveControl()->getClientSide();
71
	}
72
73
	/**
74
	 * Raises the callback event. This method is required by
75
	 * {@see ICallbackEventHandler ICallbackEventHandler} interface. If
76
	 * {@see getCausesValidation ActiveControl.CausesValidation} is true,
77
	 * it will invoke the page's {@see \Prado\Web\UI\TPage::validate validate} method first.
78
	 * It will raise {@see onCallback OnCallback} event. This method is mainly
79
	 * used by framework and control developers.
80
	 * @param TCallbackEventParameter $param the event parameter
81
	 */
82
	public function raiseCallbackEvent($param)
83
	{
84
		if ($this->getActiveControl()->canCauseValidation()) {
85
			$this->getPage()->validate($this->getActiveControl()->getValidationGroup());
86
		}
87
		$this->onCallback($param);
88
	}
89
90
	/**
91
	 * This method is invoked when a callback is requested. The method raises
92
	 * 'OnCallback' event to fire up the event handlers. If you override this
93
	 * method, be sure to call the parent implementation so that the event
94
	 * handler can be invoked.
95
	 * @param TCallbackEventParameter $param event parameter to be passed to the event handlers
96
	 */
97
	public function onCallback($param)
98
	{
99
		$this->raiseEvent('OnCallback', $this, $param);
100
	}
101
}
102