summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TActiveTableRow.php
blob: 6beeb27e3e6cc1fc0fad2ba820d3c258a6dc10fb (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
<?php
/**
 * TActiveTableRow and TActiveTableRowEventParameter class file
 *
 * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
 * @link http://www.landwehr-software.de/
 * @copyright Copyright &copy; 2009 LANDWEHR Computer und Software GmbH
 * @license http://www.pradosoft.com/license/
 * @package System.Web.UI.ActiveControls
 * @version $Id$
 */

/**
 * Includes the following used classes
 */
Prado::using('System.Web.UI.WebControls.TTableRow');
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
Prado::using('System.Web.UI.ActiveControls.TCallbackEventParameter');

/**
 * TActiveTableRow class.
 *
 * TActiveTableRow is the active counterpart to the original {@link TTableRow} control
 * and displays a table row. The table cells in the row can be accessed
 * via {@link getCells Cells}. The horizontal and vertical alignments of the row
 * are specified via {@link setHorizontalAlign HorizontalAlign} and
 * {@link setVerticalAlign VerticalAlign} properties, respectively.
 *
 * TActiveTableRow allows the contents of the table row to be changed during callback. When
 * {@link onRowSelected RowSelected} property is set, selecting (clicking on) the row will
 * perform a callback request causing {@link onRowSelected OnRowSelected} event to be fired.
 *
 * It will also respond to a bubbled {@link onCellSelected OnCellSelected} event of a
 * {@link TActiveTableCell} child control and fire a {@link onRowSelected OnRowSelected} event.
 *
 * TActiveTableRow allows the client-side row contents to be updated during a
 * callback response by getting a new writer, invoking the render method and flushing the
 * output, similar to a {@link TActivePanel} control.
 * <code>
 * function callback_request($sender, $param)
 * {
 *     $this->active_row->render($param->getNewWriter());
 * }
 * </code>
 *
 * Please refer to the original documentation of the regular counterpart for usage.
 *
 * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
 * @package System.Web.UI.ActiveControls
 * @version $Id$
 * @since 3.1.9
 */
class TActiveTableRow extends TTableRow implements ICallbackEventHandler, IActiveControl
{

	/**
	 * @var TTable parent table control containing the row
	 */
	private $_table;

	/**
	 * Creates a new callback control, sets the adapter to TActiveControlAdapter.
	 * */
	public function __construct()
	{
		parent::__construct();
		$this->setAdapter(new TActiveControlAdapter($this));
	}

	/**
	 * @return TBaseActiveCallbackControl standard callback control options.
	 */
	public function getActiveControl()
	{
		return $this->getAdapter()->getBaseActiveControl();
	}

	/**
	 * @return TCallbackClientSide client side request options.
	 */
	public function getClientSide()
	{
		return $this->getAdapter()->getBaseActiveControl()->getClientSide();
	}

	/**
	 * @return string corresponding javascript class name for this TActiveTableRow.
	 */
	protected function getClientClassName()
	{
		return 'Prado.WebUI.TActiveTableRow';
	}

	/**
	 * Raises the callback event. This method is required by {@link ICallbackEventHandler}
	 * interface. It will raise {@link onRowSelected OnRowSelected} event with a
	 * {@link TActiveTableRowEventParameter} containing the zero-based index of the
	 * TActiveTableRow.
	 * This method is mainly used by framework and control developers.
	 * @param TCallbackEventParameter the event parameter
	 */
	public function raiseCallbackEvent($param)
	{
		$parameter = new TActiveTableRowEventParameter($this->getResponse(), $param->getCallbackParameter(), $this->getRowIndex());
		$this->onRowSelected($parameter);
	}

	/**
	 * This method overrides parent's implementation and raises the control's
	 * callback event. This will fire the {@link onRowSelected OnRowSelected}
	 * event if an appropriate event handler is implemented.
	 * @param TControl the sender of the event
	 * @param TEventParameter event parameter
	 * @return boolean whether the event bubbling should stop here.
	 */
	public function bubbleEvent($sender, $param)
	{
		if ($param instanceof TActiveTableCellEventParameter)
		{
			$this->raiseCallbackEvent($param);
			return true;
		}
		else return false;
	}

	/**
	 * This method is invoked when a callback is requested. The method raises
	 * 'OnRowSelected' event to fire up the event handlers. If you override this
	 * method, be sure to call the parent implementation so that the event
	 * handler can be invoked.
	 * @param TActiveTableRowEventParameter event parameter to be passed to the event handlers
	 */
	public function onRowSelected($param)
	{
		$this->raiseEvent('OnRowSelected', $this, $param);
	}

	/**
	 * Ensure that the ID attribute is rendered and registers the javascript code
	 * for initializing the active control if the event handler for the
	 * {@link onRowSelected OnRowSelected} event is set.
	 * @param THtmlWriter the writer responsible for rendering
	 */
	protected function addAttributesToRender($writer)
	{
		parent::addAttributesToRender($writer);
		$writer->addAttribute('id', $this->getClientID());
		if ($this->hasEventHandler('OnRowSelected'))
			$this->getActiveControl()->registerCallbackClientScript($this->getClientClassName(), $this->getPostBackOptions());
	}

	/**
	 * Renders and replaces the row's content on the client-side. When render() is
	 * called before the OnPreRender event, such as when render() is called during
	 * a callback event handler, the rendering is defered until OnPreRender event
	 * is raised.
	 * @param THtmlWriter html writer
	 */
	public function render($writer)
	{
		if ($this->getHasPreRendered())
		{
			parent::render($writer);
			if ($this->getActiveControl()->canUpdateClientSide())
				$this->getPage()->getCallbackClient()->replaceContent($this, $writer);
		}
		else
		{
			$this->getPage()->getAdapter()->registerControlToRender($this, $writer);
			// If we update a TActiveTableRow on callback, we shouldn't update all childs,
			// because the whole content will be replaced by the parent.
			if ($this->getHasControls())
			{
				foreach ($this->findControlsByType('IActiveControl', false) as $control)
					$control->getActiveControl()->setEnableUpdate(false);
			}
		}
	}

	/**
	 * Returns postback specifications for the table row.
	 * This method is used by framework and control developers.
	 * @return array parameters about how the row defines its postback behavior.
	 */
	protected function getPostBackOptions()
	{
		$options['ID'] = $this->getClientID();
		$options['EventTarget'] = $this->getUniqueID();
		return $options;
	}

	/**
	 * Returns the zero-based index of the TActiveTableRow within the {@link TTableRowCollection}
	 * of the parent {@link TTable} control. Raises a {@link TConfigurationException} if the row
	 * is no member of the row collection.
	 * @return integer the zero-based index of the row
	 */
	public function getRowIndex()
	{
		foreach ($this->getTable()->getRows() as $key => $row)
			if ($row == $this) return $key;
		throw new TConfigurationException('tactivetablerow_control_notincollection', get_class($this), $this->getUniqueID());
	}

	/**
	 * Returns the parent {@link TTable} control by looping through all parents until a {@link TTable}
	 * is found. Raises a {@link TConfigurationException} if no table control is found.
	 * @return TTable the parent table control
	 */
	public function getTable()
	{
		if ($this->_table === null)
		{
			$table = $this->getParent();
			while (!($table instanceof TTable) && $table !== null)
			{
				$table = $table->getParent();
			}
			if ($table instanceof TTable) $this->_table = $table;
			else throw new TConfigurationException('tactivetablerow_control_outoftable', get_class($this), $this->getUniqueID());
		}
		return $this->_table;
	}

}

/**
 * TActiveTableRowEventParameter class.
 *
 * The TActiveTableRowEventParameter provides the parameter passed during the callback
 * requestion in the {@link getCallbackParameter CallbackParameter} property. The
 * callback response content (e.g. new HTML content) must be rendered
 * using an THtmlWriter obtained from the {@link getNewWriter NewWriter}
 * property, which returns a <b>NEW</b> instance of TCallbackResponseWriter.
 *
 * The {@link getSelectedRowIndex SelectedRowIndex} is a zero-based index of the
 * TActiveTableRow , -1 if the row is not part of the row collection (this shouldn't
 * happen though since an exception is thrown before).
 *
 * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
 * @package System.Web.UI.ActiveControls
 * @since 3.1.9
 */
class TActiveTableRowEventParameter extends TCallbackEventParameter
{
	/**
	* @var integer the zero-based index of the row.
	*/
	private $_selectedRowIndex = -1;

	/**
	 * Creates a new TActiveTableRowEventParameter.
	 */
	public function __construct($response, $parameter, $index=-1)
	{
		parent::__construct($response, $parameter);
		$this->_selectedRowIndex = $index;
	}

	/**
	 * Returns the zero-based index of the {@link TActiveTableRow} within the
	 * {@link TTableRowCollection} of the parent {@link TTable} control.
	 * @return integer the zero-based index of the row.
	 */
	public function getSelectedRowIndex()
	{
		return $this->_selectedRowIndex;
	}

}