Completed
Pull Request — master (#339)
by
unknown
03:32
created

Row::getControl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid;
10
11
use Nette;
12
use LeanMapper;
13
use Nextras;
14
use DibiRow;
15
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;
16
use Nette\Utils\Html;
17
use Ublaboo\DataGrid\Exception\DataGridException;
18
use Nette\Database\Table\ActiveRow;
19
20
class Row extends Nette\Object
21
{
22
23
	/**
24
	 * @var DataGrid
25
	 */
26
	protected $datagrid;
27
28
	/**
29
	 * @var mixed
30
	 */
31
	protected $item;
32
33
	/**
34
	 * @var string
35
	 */
36
	protected $primary_key;
37
38
	/**
39
	 * @var mixed
40
	 */
41
	protected $id;
42
43
	/**
44
	 * @var Html
45
	 */
46
	protected $control;
47
48
49
	/**
50
	 * @param DataGrid $datagrid
51
	 * @param mixed    $item
52
	 * @param string   $primary_key
53
	 */
54
	public function __construct(DataGrid $datagrid, $item, $primary_key)
55
	{
56
		$this->control = Html::el('tr');
57
		$this->datagrid = $datagrid;
58
		$this->item = $item;
59
		$this->primary_key = $primary_key;
60
		$this->id = $this->getValue($primary_key);
61
62
		if ($datagrid->hasColumnsSummary()) {
63
			$datagrid->getColumnsSummary()->add($this);
64
		}
65
	}
66
67
68
	/**
69
	 * Get id value of item
70
	 * @return mixed
71
	 */
72
	public function getId()
73
	{
74
		return $this->id;
75
	}
76
77
78
	/**
79
	 * Get item value of key
80
	 * @param  mixed $key
81
	 * @return mixed
82
	 */
83
	public function getValue($key)
84
	{
85
		if ($this->item instanceof LeanMapper\Entity) {
86
			return $this->getLeanMapperEntityProperty($this->item, $key);
87
88
		} else if ($this->item instanceof Nextras\Orm\Entity\Entity) {
89
			return $this->getNextrasEntityProperty($this->item, $key);
90
91
		} else if ($this->item instanceof DibiRow) {
92
			return $this->item->{$key};
93
94
		} else if ($this->item instanceof ActiveRow) {
95
			return $this->item->{$key};
96
97
		} else if ($this->item instanceof Nette\Database\Row) {
98
			return $this->item->{$key};
99
100
		} else if (is_array($this->item)) {
101
			return $this->item[$key];
102
103
		} else {
104
			/**
105
			 * Doctrine entity
106
			 */
107
			return $this->getDoctrineEntityProperty($this->item, $key);
108
109
		}
110
	}
111
112
113
	/**
114
	 * @return Html
115
	 */
116
	public function getControl()
117
	{
118
		return $this->control;
119
	}
120
121
122
	/**
123
	 * @return string
124
	 */
125
	public function getControlClass()
126
	{
127
		if (!$class = $this->control->class) {
128
			return '';
129
		}
130
131
		return implode(' ', array_keys($class));
132
	}
133
134
135
	/**
136
	 * LeanMapper: Access object properties to get a item value
137
	 * @param  LeanMapper\Entity $item
138
	 * @param  mixed             $key
139
	 * @return mixed
140
	 */
141 View Code Duplication
	public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
	{
143
		$properties = explode('.', $key);
144
		$value = $item;
145
146
		while ($property = array_shift($properties)) {
147
			if (!isset($value->{$property})) {
148
				if ($this->datagrid->strict_entity_property) {
149
					throw new DataGridException(sprintf(
150
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
151
						$value, str_replace('.', '->', $key)
152
					));
153
				}
154
155
				return NULL;
156
			}
157
158
			$value = $value->{$property};
159
		}
160
161
		return $value;
162
	}
163
164
165
	/**
166
	 * Nextras: Access object properties to get a item value
167
	 * @param  Nextras\Orm\Entity\Entity $item
168
	 * @param  string                    $key
169
	 * @return mixed
170
	 */
171 View Code Duplication
	public function getNextrasEntityProperty(Nextras\Orm\Entity\Entity $item, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
	{
173
		$properties = explode('.', $key);
174
		$value = $item;
175
176
		while ($property = array_shift($properties)) {
177
			if (!isset($value->{$property})) {
178
				if ($this->datagrid->strict_entity_property) {
179
					throw new DataGridException(sprintf(
180
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
181
						$value, str_replace('.', '->', $key)
182
					));
183
				}
184
185
				return NULL;
186
			}
187
188
			$value = $value->{$property};
189
		}
190
191
		return $value;
192
	}
193
194
195
	/**
196
	 * Doctrine: Access object properties to get a item value
197
	 * @param  mixed $item
198
	 * @param  mixed $key
199
	 * @return mixed
200
	 */
201
	public function getDoctrineEntityProperty($item, $key)
202
	{
203
		$properties = explode('.', $key);
204
		$value = $item;
205
		$accessor = PropertyAccessHelper::getAccessor();
206
207
		while ($property = array_shift($properties)) {
208
			if (!is_object($value) && !$value) {
209
				if ($this->datagrid->strict_entity_property) {
210
					throw new DataGridException(sprintf(
211
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
212
						$value, str_replace('.', '->', $key)
213
					));
214
				}
215
216
				return NULL;
217
			}
218
219
			$value = $accessor->getValue($value, $property);
220
		}
221
222
		return $value;
223
	}
224
225
226
	/**
227
	 * Get original item
228
	 * @return mixed
229
	 */
230
	public function getItem()
231
	{
232
		return $this->item;
233
	}
234
235
236
	/**
237
	 * Has particular row group actions allowed?
238
	 * @return bool
239
	 */
240
	public function hasGroupAction()
241
	{
242
		$condition = $this->datagrid->getRowCondition('group_action');
243
244
		return $condition ? $condition($this->item) : TRUE;
245
	}
246
247
248
	/**
249
	 * Has particular row a action allowed?
250
	 * @param  mixed  $key
251
	 * @return bool
252
	 */
253
	public function hasAction($key)
254
	{
255
		$condition = $this->datagrid->getRowCondition('action', $key);
256
257
		return $condition ? $condition($this->item) : TRUE;
258
	}
259
260
261
	/**
262
	 * Has particular row inlie edit allowed?
263
	 * @return bool
264
	 */
265
	public function hasInlineEdit()
266
	{
267
		$condition = $this->datagrid->getRowCondition('inline_edit');
268
269
		return $condition ? $condition($this->item) : TRUE;
270
	}
271
272
273
	/**
274
	 * @param  string        $key
275
	 * @param  Column\Column $column
276
	 * @return void
277
	 */
278
	public function applyColumnCallback($key, Column\Column $column)
279
	{
280
		$callback = $this->datagrid->getColumnCallback($key);
281
282
		if ($callback !== NULL) {
283
			call_user_func($callback, $column, $this->getItem());
284
		}
285
286
		return $column;
287
	}
288
289
}
290