Row::hasInlineEdit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
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 DibiRow;
12
use LeanMapper;
13
use Nette;
14
use Nette\Database\Table\ActiveRow;
15
use Nette\SmartObject;
16
use Nette\Utils\Html;
17
use Nextras;
18
use Ublaboo\DataGrid\Exception\DataGridException;
19
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;
20
21 1
class Row
22
{
23
24 1
	use SmartObject;
25
26
	/**
27
	 * @var DataGrid
28
	 */
29
	protected $datagrid;
30
31
	/**
32
	 * @var mixed
33
	 */
34
	protected $item;
35
36
	/**
37
	 * @var string
38
	 */
39
	protected $primary_key;
40
41
	/**
42
	 * @var mixed
43
	 */
44
	protected $id;
45
46
	/**
47
	 * @var Html
48
	 */
49
	protected $control;
50
51
52
	/**
53
	 * @param DataGrid $datagrid
54
	 * @param mixed    $item
55
	 * @param string   $primary_key
56
	 */
57
	public function __construct(DataGrid $datagrid, $item, $primary_key)
58
	{
59 1
		$this->control = Html::el('tr');
60 1
		$this->datagrid = $datagrid;
61 1
		$this->item = $item;
62 1
		$this->primary_key = $primary_key;
63 1
		$this->id = $this->getValue($primary_key);
64
65 1
		if ($datagrid->hasColumnsSummary()) {
66
			$datagrid->getColumnsSummary()->add($this);
67
		}
68 1
	}
69
70
71
	/**
72
	 * Get id value of item
73
	 * @return mixed
74
	 */
75
	public function getId()
76
	{
77 1
		return $this->id;
78
	}
79
80
81
	/**
82
	 * Get item value of key
83
	 * @param  mixed $key
84
	 * @return mixed
85
	 */
86
	public function getValue($key)
87
	{
88 1
		if ($this->item instanceof LeanMapper\Entity) {
0 ignored issues
show
Bug introduced by
The class LeanMapper\Entity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
89 1
			return $this->getLeanMapperEntityProperty($this->item, $key);
90
91 1
		} elseif ($this->item instanceof Nextras\Orm\Entity\Entity) {
0 ignored issues
show
Bug introduced by
The class Nextras\Orm\Entity\Entity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
92
			return $this->getNextrasEntityProperty($this->item, $key);
93
94 1
		} elseif ($this->item instanceof DibiRow) {
0 ignored issues
show
Bug introduced by
The class DibiRow does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
95
			return $this->item->{$this->formatDibiRowKey($key)};
96
97 1
		} elseif ($this->item instanceof ActiveRow) {
0 ignored issues
show
Bug introduced by
The class Nette\Database\Table\ActiveRow does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
98
			return $this->getActiveRowProperty($this->item, $key);
99
100 1
		} elseif ($this->item instanceof Nette\Database\Row) {
0 ignored issues
show
Bug introduced by
The class Nette\Database\Row does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
101
			return $this->item->{$key};
102
103 1
		} elseif (is_array($this->item)) {
104 1
			return $this->item[$key];
105
106
		} else {
107
			/**
108
			 * Doctrine entity
109
			 */
110 1
			return $this->getDoctrineEntityProperty($this->item, $key);
111
		}
112
	}
113
114
115
	/**
116
	 * @return Html
117
	 */
118
	public function getControl()
119
	{
120 1
		return $this->control;
121
	}
122
123
124
	/**
125
	 * @return string
126
	 */
127
	public function getControlClass()
128
	{
129 1
		if (!$class = $this->control->class) {
130
			return '';
131
		}
132
133 1
		return implode(' ', array_keys($class));
134
	}
135
136
137
	/**
138
	 * @param  ActiveRow $item
139
	 * @param  string    $key
140
	 * @return mixed|NULL
141
	 */
142
	public function getActiveRowProperty(ActiveRow $item, $key)
143
	{
144 View Code Duplication
		if (preg_match("/^:([a-zA-Z0-9_$]+)\.([a-zA-Z0-9_$]+)(:([a-zA-Z0-9_$]+))?$/", $key, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
145
			$relatedTable = $matches[1];
146
			$relatedColumn = $matches[2];
147
			$throughColumn = isset($matches[4]) ? $matches[4] : null;
148
149
			$relatedRow = $item->related($relatedTable, $throughColumn)->fetch();
150
151
			return $relatedRow ? $relatedRow->{$relatedColumn} : null;
152
		}
153
154 View Code Duplication
		if (preg_match("/^([a-zA-Z0-9_$]+)\.([a-zA-Z0-9_$]+)(:([a-zA-Z0-9_$]+))?$/", $key, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
155
			$referencedTable = $matches[1];
156
			$referencedColumn = $matches[2];
157
			$throughColumn = isset($matches[4]) ? $matches[4] : null;
158
159
			$referencedRow = $item->ref($referencedTable, $throughColumn);
160
161
			return $referencedRow ? $referencedRow->{$referencedColumn} : null;
162
		}
163
164
		return $item->{$key};
165
	}
166
167
168
	/**
169
	 * LeanMapper: Access object properties to get a item value
170
	 * @param  LeanMapper\Entity $item
171
	 * @param  mixed             $key
172
	 * @return mixed
173
	 */
174 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...
175
	{
176 1
		$properties = explode('.', $key);
177 1
		$value = $item;
178
179 1
		while ($property = array_shift($properties)) {
180 1
			if (!isset($value->{$property})) {
181
				if ($this->datagrid->strict_entity_property) {
182
					throw new DataGridException(sprintf(
183
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
184
						$value, str_replace('.', '->', $key)
185
					));
186
				}
187
188
				return null;
189
			}
190
191 1
			$value = $value->{$property};
192
		}
193
194 1
		return $value;
195
	}
196
197
198
	/**
199
	 * Nextras: Access object properties to get a item value
200
	 * @param  Nextras\Orm\Entity\Entity $item
201
	 * @param  string                    $key
202
	 * @return mixed
203
	 */
204 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...
205
	{
206
		$properties = explode('.', $key);
207
		$value = $item;
208
209
		while ($property = array_shift($properties)) {
210
			if (!isset($value->{$property})) {
211
				if ($this->datagrid->strict_entity_property) {
212
					throw new DataGridException(sprintf(
213
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
214
						$value, str_replace('.', '->', $key)
215
					));
216
				}
217
218
				return null;
219
			}
220
221
			$value = $value->{$property};
222
		}
223
224
		return $value;
225
	}
226
227
228
	/**
229
	 * Doctrine: Access object properties to get a item value
230
	 * @param  mixed $item
231
	 * @param  mixed $key
232
	 * @return mixed
233
	 */
234
	public function getDoctrineEntityProperty($item, $key)
235
	{
236 1
		$properties = explode('.', $key);
237 1
		$value = $item;
238 1
		$accessor = PropertyAccessHelper::getAccessor();
239
240 1
		while ($property = array_shift($properties)) {
241 1
			if (!is_object($value) && !$value) {
242
				if ($this->datagrid->strict_entity_property) {
243
					throw new DataGridException(sprintf(
244
						'Target Property [%s] is not an object or is empty, trying to get [%s]',
245
						$value, str_replace('.', '->', $key)
246
					));
247
				}
248
249
				return null;
250
			}
251
252 1
			$value = $accessor->getValue($value, $property);
253
		}
254
255 1
		return $value;
256
	}
257
258
259
	/**
260
	 * Get original item
261
	 * @return mixed
262
	 */
263
	public function getItem()
264
	{
265 1
		return $this->item;
266
	}
267
268
269
	/**
270
	 * Has particular row group actions allowed?
271
	 * @return bool
272
	 */
273
	public function hasGroupAction()
274
	{
275
		$condition = $this->datagrid->getRowCondition('group_action');
276
277
		return $condition ? $condition($this->item) : true;
278
	}
279
280
281
	/**
282
	 * Has particular row a action allowed?
283
	 * @param  mixed  $key
284
	 * @return bool
285
	 */
286
	public function hasAction($key)
287
	{
288
		$condition = $this->datagrid->getRowCondition('action', $key);
289
290
		return $condition ? $condition($this->item) : true;
291
	}
292
293
294
	/**
295
	 * Has particular row inlie edit allowed?
296
	 * @return bool
297
	 */
298
	public function hasInlineEdit()
299
	{
300
		$condition = $this->datagrid->getRowCondition('inline_edit');
301
302
		return $condition ? $condition($this->item) : true;
303
	}
304
305
306
	/**
307
	 * @param  string        $key
308
	 * @param  Column\Column $column
309
	 * @return void
310
	 */
311
	public function applyColumnCallback($key, Column\Column $column)
312
	{
313
		$callback = $this->datagrid->getColumnCallback($key);
314
315
		if ($callback !== null) {
316
			call_user_func($callback, $column, $this->getItem());
317
		}
318
319
		return $column;
320
	}
321
322
323
	/**
324
	 * Key may contain ".", get rid of it (+ the table alias)
325
	 * 
326
	 * @param  string $key
327
	 * @return string
328
	 */
329
	private function formatDibiRowKey($key)
330
	{
331
		if ($offset = strpos($key, '.')) {
332
			return substr($key, $offset + 1);
333
		}
334
335
		return $key;
336
	}
337
}
338