Completed
Push — master ( ea0b5b...2eb559 )
by Martin
02:52
created

Action::getConfirm()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 11
nc 5
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\DataGrid;
13
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException;
14
use Ublaboo\DataGrid\Exception\DataGridException;
15
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
16
use Ublaboo\DataGrid\Row;
17
use Ublaboo\DataGrid\Traits;
18
19
class Action extends Column
20
{
21
22
	use Traits\TButton;
23
	use Traits\TLink;
24
25
	/**
26
	 * @var string
27
	 */
28
	public static $data_confirm_attribute_name = 'datagrid-confirm';
29
30
	/**
31
	 * @var DataGrid
32
	 */
33
	protected $grid;
34
35
	/**
36
	 * @var string
37
	 */
38
	protected $href;
39
40
	/**
41
	 * @var string
42
	 */
43
	protected $name;
44
45
	/**
46
	 * @var array
47
	 */
48
	protected $params;
49
50
	/**
51
	 * @var array|callable
52
	 */
53
	protected $confirm;
54
55
	/**
56
	 * @var array
57
	 */
58
	protected $data_attributes = [];
59
60
	/**
61
	 * @var array
62
	 */
63
	protected $attributes = [];
64
65
	/**
66
	 * @var array
67
	 */
68
	protected $parameters = [];
69
70
	/**
71
	 * @param DataGrid $grid
72
	 * @param string   $href
73
	 * @param string   $name
74
	 * @param array    $params
75
	 */
76
	public function __construct(DataGrid $grid, $href, $name, $params)
77
	{
78
		$this->grid = $grid;
79
		$this->href = $href;
80
		$this->name = $name;
81
		$this->params = $params;
82
	}
83
84
85
	/**
86
	 * Render row item into template
87
	 * @param  Row   $row
88
	 * @return mixed
89
	 */
90
	public function render(Row $row)
91
	{
92
		/**
93
		 * Renderer function may be used
94
		 */
95
		try {
96
			return $this->useRenderer($row);
97
		} catch (DataGridColumnRendererException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
98
			/**
99
			 * Do not use renderer
100
			 */
101
		}
102
103
		$link = $this->createLink(
104
			$this->grid,
105
			$this->href,
106
			$this->getItemParams($row, $this->params) + $this->parameters
107
		);
108
109
		$a = Html::el('a')->href($link);
110
111
		$this->tryAddIcon($a, $this->getIcon($row), $this->getName());
112
113
		if (!empty($this->data_attributes)) {
114
			foreach ($this->data_attributes as $key => $value) {
115
				$a->data($key, $value);
116
			}
117
		}
118
119
		if (!empty($this->attributes)) {
120
			$a->addAttributes($this->attributes);
121
		}
122
123
		$a->addText($this->translate($this->getName()));
124
125
		if ($this->title) {
126
			$a->title($this->translate($this->getTitle($row)));
127
		}
128
129
		if ($this->class) {
130
			$a->class($this->getClass($row));
131
		}
132
133
		if ($confirm = $this->getConfirm($row)) {
134
			$a->data(static::$data_confirm_attribute_name, $confirm);
135
		}
136
137
		return $a;
138
	}
139
140
141
	/**
142
	 * Add parameters to link
143
	 * @param array $parameters
144
	 * @return static
145
	 */
146
	public function addParameters(array $parameters)
147
	{
148
		$this->parameters = $parameters;
149
150
		return $this;
151
	}
152
153
154
	/**
155
	 * Set attribute title
156
	 * @param string|callable $title
157
	 * @return static
158
	 * @throws DataGridException
159
	 */
160
	public function setTitle($title)
161
	{
162
		$this->checkPropertyStringOrCallable($title, 'title');
163
164
		$this->title = $title;
165
166
		return $this;
167
	}
168
169
170
	/**
171
	 * Get attribute title
172
	 * @param Row $row
173
	 * @return string
174
	 * @throws DataGridException
175
	 */
176
	public function getTitle(Row $row)
177
	{
178
		/**
179
		 * If user callback was used for setting action title, it has to return string
180
		 */
181
		return $this->getPropertyStringOrCallableGetString($row, $this->title, 'title');
182
	}
183
184
185
	/**
186
	 * Set attribute class
187
	 * @param string|callable $class
188
	 * @return static
189
	 * @throws DataGridException
190
	 */
191
	public function setClass($class)
192
	{
193
		$this->checkPropertyStringOrCallable($class, 'class');
194
195
		$this->class = $class;
0 ignored issues
show
Documentation Bug introduced by
It seems like $class of type callable is incompatible with the declared type string of property $class.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
196
197
		return $this;
198
	}
199
200
201
	/**
202
	 * Get attribute class
203
	 * @param Row $row
204
	 * @return string
205
	 * @throws DataGridException
206
	 */
207
	public function getClass(Row $row)
208
	{
209
		/**
210
		 * If user callback was used for setting action class, it has to return string
211
		 */
212
		return $this->getPropertyStringOrCallableGetString($row, $this->class, 'class');
213
	}
214
215
216
	/**
217
	 * Set icon
218
	 * @param string|callable $icon
219
	 * @return static|callable
220
	 * @throws DataGridException
221
	 */
222
	public function setIcon($icon)
223
	{
224
		$this->checkPropertyStringOrCallable($icon, 'icon');
225
226
		$this->icon = $icon;
227
228
		return $this;
229
	}
230
231
232
	/**
233
	 * Get icon
234
	 * @param Row $row
235
	 * @return string
236
	 * @throws DataGridException
237
	 */
238
	public function getIcon(Row $row)
239
	{
240
		/**
241
		 * If user callback was used for setting action icon, it has to return string
242
		 */
243
		return $this->getPropertyStringOrCallableGetString($row, $this->icon, 'icon');
244
	}
245
246
247
	/**
248
	 * Set confirm dialog
249
	 * @param string|callable $message
250
	 * @param string $column
251
	 * @return static
252
	 * @throws DataGridException
253
	 */
254
	public function setConfirm($message, $column = NULL)
255
	{
256
		$this->checkPropertyStringOrCallable($message, 'confirmation message');
257
258
		$this->confirm = [$message, $column];
259
260
		return $this;
261
	}
262
263
264
	/**
265
	 * Get confirm dialog for particular row item
266
	 * @param Row $row
267
	 * @return string
268
	 * @throws DataGridException
269
	 */
270
	public function getConfirm(Row $row)
271
	{
272
		if (!$this->confirm) {
273
			return NULL;
274
		}
275
276
		$question = $this->confirm[0];
277
278
		if (is_string($question)) {
279
			$question = $this->translate($question);
280
		} else {
281
			/**
282
			 * If user callback was used for setting action confirmation dialog, it has to return string
283
			 */
284
			$question = $this->getPropertyStringOrCallableGetString($row, $question, 'confirmation dialog');
285
		}
286
287
		if (!$this->confirm[1]) {
288
			return $question;
289
		}
290
291
		return str_replace('%s', $row->getValue($this->confirm[1]), $question);
292
	}
293
294
295
	/**
296
	 * Setting data attributes
297
	 * @param string $key
298
	 * @param mixed $value
299
	 * @return static
300
	 */
301
	public function setDataAttribute($key, $value)
302
	{
303
		$this->data_attributes[$key] = $value;
304
305
		return $this;
306
	}
307
308
309
	/**
310
	 * Set attributes for a element
311
	 * @param array $attrs
312
	 * @return static
313
	 */
314
	public function addAttributes(array $attrs)
315
	{
316
		$this->attributes = $this->attributes + $attrs;
317
318
		return $this;
319
	}
320
321
322
	/**
323
	 * Check whether given property is string or callable
324
	 * @param  mixed $property
325
	 * @return void
326
	 * @throws DataGridException
327
	 */
328
	protected function checkPropertyStringOrCallable($property, $name)
329
	{
330
		if (!is_string($property) && !is_callable($property) && !is_null($property)) {
331
			throw new DataGridException(
332
				"Action {$name} has to be either string or a callback returning string"
333
			);
334
		}
335
	}
336
337
338
	/**
339
	 * Check whether given property is string or callable
340
	 * 	in that case call callback and check property and return it
341
	 * @param  Row                  $row
342
	 * @param  string|callable|null $property
343
	 * @param  string               $name
344
	 * @return string
345
	 * @throws DataGridException
346
	 */
347
	public function getPropertyStringOrCallableGetString(Row $row, $property, $name)
348
	{
349
		/**
350
		 * String
351
		 */
352
		if (is_string($property)) {
353
			return $property;
354
		}
355
356
		/**
357
		 * Callable
358
		 */
359
		if (is_callable($property)) {
360
			$value = call_user_func($property, $row->getItem());
361
362
			if (!is_string($value)) {
363
				throw new DataGridException("Action {$name} callback has to return a string");
364
			}
365
366
			return $value;
367
		}
368
369
		return $property;
370
	}
371
372
373
	/**
374
	 * Translator helper
375
	 * @param  string $message
376
	 * @return string
377
	 */
378
	protected function translate($message)
379
	{
380
		return $this->grid->getTranslator()->translate($message);
381
	}
382
383
}
384