Completed
Push — master ( add6f7...11d029 )
by Pavel
03:09
created

Action::getConfirm()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
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\ButtonIconTrait;
23
24
	/**
25
	 * @var string
26
	 */
27
	public static $data_confirm_attribute_name = 'datagrid-confirm';
28
29
	/**
30
	 * @var string|callable
31
	 */
32
	protected $title;
33
34
	/**
35
	 * @var string|callable
36
	 */
37
	protected $class;
38
39
	/**
40
	 * @var string|callable
41
	 */
42
	protected $icon;
43
44
	/**
45
	 * @var DataGrid
46
	 */
47
	protected $grid;
48
49
	/**
50
	 * @var string
51
	 */
52
	protected $href;
53
54
	/**
55
	 * @var string
56
	 */
57
	protected $name;
58
59
	/**
60
	 * @var array
61
	 */
62
	protected $params;
63
64
	/**
65
	 * @var array|callable
66
	 */
67
	protected $confirm;
68
69
	/**
70
	 * @var array
71
	 */
72
	protected $data_attributes = [];
73
74
75
	/**
76
	 * @param DataGrid $grid
77
	 * @param string   $href
78
	 * @param string   $name
79
	 * @param array    $params
80
	 */
81
	public function __construct(DataGrid $grid, $href, $name, $params)
82
	{
83
		$this->grid = $grid;
84
		$this->href = $href;
85
		$this->name = $name;
86
		$this->params = $params;
87
88
		$this->class = 'btn btn-xs btn-default';
89
	}
90
91
92
	/**
93
	 * Render row item into template
94
	 * @param  Row   $row
95
	 * @return mixed
96
	 */
97
	public function render(Row $row)
98
	{
99
		/**
100
		 * Renderer function may be used
101
		 */
102
		try {
103
			return $this->useRenderer($row);
104
		} catch (DataGridColumnRendererException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
105
			/**
106
			 * Do not use renderer
107
			 */
108
		}
109
110
		$link = $this->createLink($this->href, $this->getItemParams($row, $this->params));
111
112
		$a = Html::el('a')->href($link);
113
114
		$this->tryAddIcon($a, $this->getIcon($row), $this->getName());
115
116
		if (!empty($this->data_attributes)) {
117
			foreach ($this->data_attributes as $key => $value) {
118
				$a->data($key, $value);
119
			}
120
		}
121
122
		$a->add($this->translate($this->getName()));
123
124
		if ($this->title) {
125
			$a->title($this->translate($this->getTitle($row)));
126
		}
127
128
		if ($this->class) {
129
			$a->class($this->getClass($row));
130
		}
131
132
		if ($confirm = $this->getConfirm($row)) {
133
			$a->data(static::$data_confirm_attribute_name, $confirm);
134
		}
135
136
		return $a;
137
	}
138
139
140
	/**
141
	 * Set attribute title
142
	 * @param string|callable $title
143
	 * @return static
144
	 * @throws DataGridException
145
	 */
146
	public function setTitle($title)
147
	{
148
		$this->checkPropertyStringOrCallable($title, 'title');
149
150
		$this->title = $title;
151
152
		return $this;
153
	}
154
155
156
	/**
157
	 * Get attribute title
158
	 * @param Row $row
159
	 * @return string
160
	 * @throws DataGridException
161
	 */
162
	public function getTitle(Row $row)
163
	{
164
		/**
165
		 * If user callback was used for setting action title, it has to return string
166
		 */
167
		return $this->getPropertyStringOrCallableGetString($row, $this->title, 'title');
168
	}
169
170
171
	/**
172
	 * Set attribute class
173
	 * @param string|callable $class
174
	 * @return static
175
	 * @throws DataGridException
176
	 */
177
	public function setClass($class)
178
	{
179
		$this->checkPropertyStringOrCallable($class, 'class');
180
181
		$this->class = $class;
182
183
		return $this;
184
	}
185
186
187
	/**
188
	 * Get attribute class
189
	 * @param Row $row
190
	 * @return string
191
	 * @throws DataGridException
192
	 */
193
	public function getClass(Row $row)
194
	{
195
		/**
196
		 * If user callback was used for setting action class, it has to return string
197
		 */
198
		return $this->getPropertyStringOrCallableGetString($row, $this->class, 'class');
199
	}
200
201
202
	/**
203
	 * Set icon
204
	 * @param string|callable $icon
205
	 * @return static|callable
206
	 * @throws DataGridException
207
	 */
208
	public function setIcon($icon)
209
	{
210
		$this->checkPropertyStringOrCallable($icon, 'icon');
211
212
		$this->icon = $icon;
213
214
		return $this;
215
	}
216
217
218
	/**
219
	 * Get icon
220
	 * @param Row $row
221
	 * @return string
222
	 * @throws DataGridException
223
	 */
224
	public function getIcon(Row $row)
225
	{
226
		/**
227
		 * If user callback was used for setting action icon, it has to return string
228
		 */
229
		return $this->getPropertyStringOrCallableGetString($row, $this->icon, 'icon');
230
	}
231
232
233
	/**
234
	 * Set confirm dialog
235
	 * @param string|callable $message
236
	 * @param string $column
237
	 * @return static
238
	 * @throws DataGridException
239
	 */
240
	public function setConfirm($message, $column = NULL)
241
	{
242
		$this->checkPropertyStringOrCallable($message, 'confirmation message');
243
244
		$this->confirm = [$message, $column];
245
246
		return $this;
247
	}
248
249
250
	/**
251
	 * Get confirm dialog for particular row item
252
	 * @param Row $row
253
	 * @return string
254
	 * @throws DataGridException
255
	 */
256
	public function getConfirm(Row $row)
257
	{
258
		if (!$this->confirm) {
259
			return NULL;
260
		}
261
262
		$question = $this->confirm[0];
263
264
		if (is_string($question)) {
265
			$question = $this->translate($question);
266
		} else {
267
			/**
268
			 * If user callback was used for setting action confirmation dialog, it has to return string
269
			 */
270
			$question = $this->getPropertyStringOrCallableGetString($row, $question, 'confirmation dialog');
271
		}
272
273
		if (!$this->confirm[1]) {
274
			return $question;
275
		}
276
277
		return str_replace('%s', $row->getValue($this->confirm[1]), $question);
278
	}
279
280
281
	/**
282
	 * Setting data attributes
283
	 * @param string $key
284
	 * @param mixed $value
285
	 * @return static
286
	 */
287
	public function setDataAttribute($key, $value)
288
	{
289
		$this->data_attributes[$key] = $value;
290
		
291
		return $this;
292
	}
293
294
295
	/**
296
	 * Check whether given property is string or callable
297
	 * @param  mixed $property
298
	 * @return void
299
	 * @throws DataGridException
300
	 */
301
	protected function checkPropertyStringOrCallable($property, $name)
302
	{
303
		if (!is_string($property) && !is_callable($property) && !is_null($property)) {
304
			throw new DataGridException(
305
				"Action {$name} has to be either string or a callback returning string"
306
			);
307
		}
308
	}
309
310
311
	/**
312
	 * Check whether given property is string or callable
313
	 * 	in that case call callback and check property and return it
314
	 * @param  Row                  $row
315
	 * @param  string|callable|null $property
316
	 * @param  string               $name
317
	 * @return string
318
	 * @throws DataGridException
319
	 */
320
	public function getPropertyStringOrCallableGetString(Row $row, $property, $name)
321
	{
322
		/**
323
		 * String
324
		 */
325
		if (is_string($property)) {
326
			return $property;
327
		}
328
329
		/**
330
		 * Callable
331
		 */
332
		if (is_callable($property)) {
333
			$value = call_user_func($property, $row->getItem());
334
335
			if (!is_string($value)) {
336
				throw new DataGridException("Action {$name} callback has to return a string");
337
			}
338
339
			return $value;
340
		}
341
342
		return $property;
343
	}
344
345
346
	/**
347
	 * Translator helper
348
	 * @param  string $message
349
	 * @return string
350
	 */
351
	protected function translate($message)
352
	{
353
		return $this->grid->getTranslator()->translate($message);
354
	}
355
356
}
357