Completed
Pull Request — master (#404)
by
unknown
03:19
created

ItemDetail::shouldBeRendered()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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\Column;
10
11
use Nette\Utils\Html;
12
use Ublaboo\DataGrid\Utils\ItemDetailForm;
13
use Ublaboo\DataGrid\DataGrid;
14
use Ublaboo;
15
use Ublaboo\DataGrid\Traits;
16
use Ublaboo\DataGrid\Exception\DataGridItemDetailException;
17
use Ublaboo\DataGrid\Row;
18
19
class ItemDetail
20
{
21
22
	use Traits\TButton;
23
24
	/**
25
	 * (renderer | template | block)
26
	 * @var string
27
	 */
28
	protected $type;
29
30
	/**
31
	 * @var string
32
	 */
33
	protected $template;
34
35
	/**
36
	 * @var callable
37
	 */
38
	protected $renderer;
39
40
	/**
41
	 * @var DataGrid
42
	 */
43
	protected $grid;
44
45
	/**
46
	 * @var string|bool
47
	 */
48
	protected $primary_where_column;
49
50
	/**
51
	 * @var ItemDetailForm
52
	 */
53
	protected $form;
54
55
	/**
56
	 * @var array
57
	 */
58
	protected $template_parameters = [];
59
	
60
	/**
61
	 * @var callable
62
	 */
63
	protected $render_condition_callback;
64
65
66
	/**
67
	 * @param DataGrid $grid
68
	 * @param string   $primary_where_column
69
	 */
70
	public function __construct(DataGrid $grid, $primary_where_column)
71
	{
72
		$this->grid = $grid;
73
		$this->primary_where_column = $primary_where_column;
74
75
		$this->title = 'ublaboo_datagrid.show';
76
		$this->class = 'btn btn-xs btn-default ajax';
77
		$this->icon = 'eye';
78
	}
79
80
81
	/**
82
	 * Render row item detail button
83
	 * @param  Row $row
84
	 * @return Html
85
	 */
86
	public function renderButton($row)
87
	{
88
		$a = Html::el('a')
89
			->href($this->grid->link('getItemDetail!', ['id' => $row->getId()]))
90
			->data('toggle-detail', $row->getId())
91
			->data('toggle-detail-grid', $this->grid->getName());
92
93
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
94
95
		$a->addText($this->text);
96
97
		if ($this->title) {
98
			$a->title($this->grid->getTranslator()->translate($this->title));
99
		}
100
101
		if ($this->class) {
102
			$a->class($this->class);
103
		}
104
105
		return $a;
106
	}
107
108
109
	/**
110
	 * Render item detail
111
	 * @param  mixed $item
112
	 * @return mixed
113
	 */
114
	public function render($item)
115
	{
116
		if ($this->getType() == 'block') {
117
			throw new DataGridItemDetailException(
118
				'ItemDetail is set to render as block, but block #detail is not defined'
119
			);
120
		}
121
122
		return call_user_func($this->getRenderer(), $item);
123
	}
124
125
126
	/**
127
	 * Get primary column for where clause
128
	 * @return string|bool
129
	 */
130
	public function getPrimaryWhereColumn()
131
	{
132
		return $this->primary_where_column;
133
	}
134
135
136
	/**
137
	 * Set item detail type
138
	 * @param string $type
139
	 */
140
	public function setType($type)
141
	{
142
		$this->type = (string) $type;
143
144
		return $this;
145
	}
146
147
148
	/**
149
	 * Get item detail type
150
	 * @return string
151
	 */
152
	public function getType()
153
	{
154
		return $this->type;
155
	}
156
157
158
	/**
159
	 * Set item detail template
160
	 * @param string $template
161
	 */
162
	public function setTemplate($template)
163
	{
164
		$this->template = (string) $template;
165
166
		return $this;
167
	}
168
169
170
	/**
171
	 * Get item detail template
172
	 * @return string
173
	 */
174
	public function getTemplate()
175
	{
176
		return $this->template;
177
	}
178
179
180
	/**
181
	 * Set item detail renderer
182
	 * @param callable $renderer
183
	 */
184
	public function setRenderer($renderer)
185
	{
186
		$this->renderer = $renderer;
187
188
		return $this;
189
	}
190
191
192
	/**
193
	 * Get item detail renderer
194
	 * @return callable
195
	 */
196
	public function getRenderer()
197
	{
198
		return $this->renderer;
199
	}
200
201
202
	/**
203
	 * @param ItemDetailForm $form
204
	 * @return static
205
	 */
206
	public function setForm(ItemDetailForm $form)
207
	{
208
		$this->form = $form;
209
210
		return $this;
211
	}
212
213
214
	/**
215
	 * @return ItemDetailForm
216
	 */
217
	public function getForm()
218
	{
219
		return $this->form;
220
	}
221
222
223
	/**
224
	 * @param array $template_parameters
225
	 * @return static
226
	 */
227
	public function setTemplateParameters(array $template_parameters)
228
	{
229
		$this->template_parameters = $template_parameters;
230
231
		return $this;
232
	}
233
234
235
	public function getTemplateVariables()
236
	{
237
		return $this->template_parameters;
238
	}
239
240
	/**
241
	 * @param callable $condition
242
	 * @return static
243
	 */
244
	public function setRenderCondition(callable $condition)
245
	{
246
		$this->render_condition_callback = $condition;
247
248
		return $this;
249
	}
250
251
	/**
252
	 * @param Row $row
253
	 * @return bool
254
	 */
255
	public function shouldBeRendered(Row $row)
256
	{
257
		$condition = $this->render_condition_callback;
258
259
		return $condition ? $condition($row->getItem()) : TRUE;
260
	}
261
262
}
263