Completed
Push — master ( e11dfd...c4fb7d )
by Pavel
02:49
created

Action::setOpenInNewTab()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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