Passed
Push — master ( dc84c0...f35cb6 )
by Fabio
06:45
created

TBoundColumn::initializeCell()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 30
nc 11
nop 3
dl 0
loc 36
ccs 0
cts 36
cp 0
crap 90
rs 8.0555
c 1
b 0
f 0
1
<?php
2
/**
3
 * TBoundColumn class file
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Web\UI\WebControls
9
 */
10
11
namespace Prado\Web\UI\WebControls;
12
13
use Prado\Prado;
14
use Prado\TPropertyValue;
15
16
/**
17
 * TBoundColumn class
18
 *
19
 * TBoundColumn represents a column that is bound to a field in a data source.
20
 * The cells in the column will be displayed using the data indexed by
21
 * {@link setDataField DataField}. You can customize the display by
22
 * setting {@link setDataFormatString DataFormatString}.
23
 *
24
 * If {@link setReadOnly ReadOnly} is false, TBoundColumn will display cells in edit mode
25
 * with textboxes. Otherwise, a static text is displayed.
26
 *
27
 * When a datagrid row is in edit mode, the textbox control in the TBoundColumn
28
 * can be accessed by one of the following two methods:
29
 * <code>
30
 * $datagridItem->BoundColumnID->TextBox
31
 * $datagridItem->BoundColumnID->Controls[0]
32
 * </code>
33
 * The second method is possible because the textbox control created within the
34
 * datagrid cell is the first child.
35
 *
36
 * Since v3.1.0, TBoundColumn has introduced two new properties {@link setItemRenderer ItemRenderer}
37
 * and {@link setEditItemRenderer EditItemRenderer} which can be used to specify
38
 * the layout of the datagrid cells in browsing and editing mode.
39
 * A renderer refers to a control class that is to be instantiated as a control.
40
 * For more details, see {@link TRepeater} and {@link TDataList}.
41
 *
42
 * @author Qiang Xue <[email protected]>
43
 * @package Prado\Web\UI\WebControls
44
 * @since 3.0
45
 */
46
class TBoundColumn extends TDataGridColumn
47
{
48
	/**
49
	 * @return string the class name for the item cell renderer. Defaults to empty, meaning not set.
50
	 * @since 3.1.0
51
	 */
52
	public function getItemRenderer()
53
	{
54
		return $this->getViewState('ItemRenderer', '');
55
	}
56
57
	/**
58
	 * Sets the item cell renderer class.
59
	 *
60
	 * If not empty, the class will be used to instantiate as a child control in the item cells of the column.
61
	 *
62
	 * If the class implements {@link \Prado\IDataRenderer}, the <b>Data</b> property
63
	 * will be set as the data associated with the datagrid cell during databinding.
64
	 * The data can be either the whole data row or a field of the row if
65
	 * {@link getDataField DataField} is not empty. If {@link getDataFormatString DataFormatString}
66
	 * is not empty, the data will be formatted first before passing to the renderer.
67
	 *
68
	 * @param string $value the renderer class name in namespace format.
69
	 * @since 3.1.0
70
	 */
71
	public function setItemRenderer($value)
72
	{
73
		$this->setViewState('ItemRenderer', $value, '');
74
	}
75
76
	/**
77
	 * @return string the class name for the edit item cell renderer. Defaults to empty, meaning not set.
78
	 * @since 3.1.0
79
	 */
80
	public function getEditItemRenderer()
81
	{
82
		return $this->getViewState('EditItemRenderer', '');
83
	}
84
85
	/**
86
	 * Sets the edit item cell renderer class.
87
	 *
88
	 * If not empty, the class will be used to instantiate as a child control in the item cell that is in edit mode.
89
	 *
90
	 * If the class implements {@link \Prado\IDataRenderer}, the <b>Data</b> property
91
	 * will be set as the data associated with the datagrid cell during databinding.
92
	 * The data can be either the whole data row or a field of the row if
93
	 * {@link getDataField DataField} is not empty. If {@link getDataFormatString DataFormatString}
94
	 * is not empty, the data will be formatted first before passing to the renderer.
95
	 *
96
	 * @param string $value the renderer class name in namespace format.
97
	 * @since 3.1.0
98
	 */
99
	public function setEditItemRenderer($value)
100
	{
101
		$this->setViewState('EditItemRenderer', $value, '');
102
	}
103
104
	/**
105
	 * @return string the field name from the data source to bind to the column
106
	 */
107
	public function getDataField()
108
	{
109
		return $this->getViewState('DataField', '');
110
	}
111
112
	/**
113
	 * @param string $value the field name from the data source to bind to the column
114
	 */
115
	public function setDataField($value)
116
	{
117
		$this->setViewState('DataField', $value, '');
118
	}
119
120
	/**
121
	 * @return string the formatting string used to control how the bound data will be displayed.
122
	 */
123
	public function getDataFormatString()
124
	{
125
		return $this->getViewState('DataFormatString', '');
126
	}
127
128
	/**
129
	 * @param string $value the formatting string used to control how the bound data will be displayed.
130
	 */
131
	public function setDataFormatString($value)
132
	{
133
		$this->setViewState('DataFormatString', $value, '');
134
	}
135
136
	/**
137
	 * @return bool whether the items in the column can be edited. Defaults to false.
138
	 */
139
	public function getReadOnly()
140
	{
141
		return $this->getViewState('ReadOnly', false);
142
	}
143
144
	/**
145
	 * @param bool $value whether the items in the column can be edited
146
	 */
147
	public function setReadOnly($value)
148
	{
149
		$this->setViewState('ReadOnly', TPropertyValue::ensureBoolean($value), false);
150
	}
151
152
	/**
153
	 * Initializes the specified cell to its initial values.
154
	 * This method overrides the parent implementation.
155
	 * It creates a textbox for item in edit mode and the column is not read-only.
156
	 * Otherwise it displays a static text.
157
	 * The caption of the button and the static text are retrieved
158
	 * from the datasource.
159
	 * @param TTableCell $cell the cell to be initialized.
160
	 * @param int $columnIndex the index to the Columns property that the cell resides in.
161
	 * @param string $itemType the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
162
	 */
163
	public function initializeCell($cell, $columnIndex, $itemType)
164
	{
165
		$item = $cell->getParent();
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
166
		switch ($itemType) {
167
			case TListItemType::Item:
168
			case TListItemType::AlternatingItem:
169
			case TListItemType::SelectedItem:
170
				if (($classPath = $this->getItemRenderer()) !== '') {
171
					$control = $this->initializeCellRendererControl($cell, $classPath);
172
				} else {
173
					$control = $cell;
174
				}
175
				$control->attachEventHandler('OnDataBinding', [$this, 'dataBindColumn']);
176
				break;
177
			case TListItemType::EditItem:
178
				if (!$this->getReadOnly()) {
179
					if (($classPath = $this->getEditItemRenderer()) !== '') {
180
						$control = $this->initializeCellRendererControl($cell, $classPath);
181
						$cell->registerObject('EditControl', $control);
182
					} else {
183
						$control = new TTextBox;
184
						$cell->getControls()->add($control);
185
						$cell->registerObject('TextBox', $control);
186
					}
187
				} else {
188
					if (($classPath = $this->getItemRenderer()) !== '') {
189
						$control = $this->initializeCellRendererControl($cell, $classPath);
190
					} else {
191
						$control = $cell;
192
					}
193
				}
194
				$control->attachEventHandler('OnDataBinding', [$this, 'dataBindColumn']);
195
				break;
196
			default:
197
				parent::initializeCell($cell, $columnIndex, $itemType);
198
				break;
199
		}
200
	}
201
202
	/**
203
	 * Databinds a cell in the column.
204
	 * This method is invoked when datagrid performs databinding.
205
	 * It populates the content of the cell with the relevant data from data source.
206
	 * @param mixed $sender
207
	 * @param mixed $param
208
	 */
209
	public function dataBindColumn($sender, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

209
	public function dataBindColumn($sender, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
	{
211
		$item = $sender->getNamingContainer();
212
		$data = $item->getData();
213
		$formatString = $this->getDataFormatString();
214
		if (($field = $this->getDataField()) !== '') {
215
			$value = $this->formatDataValue($formatString, $this->getDataFieldValue($data, $field));
216
		} else {
217
			$value = $this->formatDataValue($formatString, $data);
218
		}
219
		$sender->setData($value);
220
	}
221
}
222