Completed
Push — master ( fe6898...6997a4 )
by Pavel
20:55
created

Action::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Row;
16
17
class Action extends Column
18
{
19
20
	/**
21
	 * @var string|callable
22
	 */
23
	protected $title;
24
25
	/**
26
	 * @var string|callable
27
	 */
28
	protected $class;
29
30
	/**
31
	 * @var string|callable
32
	 */
33
	protected $icon;
34
35
	/**
36
	 * @var DataGrid
37
	 */
38
	protected $grid;
39
40
	/**
41
	 * @var string
42
	 */
43
	protected $href;
44
45
	/**
46
	 * @var string
47
	 */
48
	protected $name;
49
50
	/**
51
	 * @var array
52
	 */
53
	protected $params;
54
55
	/**
56
	 * @var array|callable
57
	 */
58
	protected $confirm;
59
60
	/**
61
	 * @var array
62
	 */
63
	protected $data_attributes = [];
64
65
66
	/**
67
	 * @param DataGrid $grid
68
	 * @param string   $href
69
	 * @param string   $name
70
	 * @param array    $params
71
	 */
72 View Code Duplication
	public function __construct(DataGrid $grid, $href, $name, $params)
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...
73
	{
74
		$this->grid = $grid;
75
		$this->href = $href;
76
		$this->name = $name;
77
		$this->params = $params;
78
79
		$this->class = 'btn btn-xs btn-default';
80
	}
81
82
83
	/**
84
	 * Render row item into template
85
	 * @param  Row   $row
86
	 * @return mixed
87
	 */
88
	public function render(Row $row)
89
	{
90
		/**
91
		 * Renderer function may be used
92
		 */
93 View Code Duplication
		if ($renderer = $this->getRenderer()) {
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...
94
			if (!$renderer->getConditionCallback()) {
95
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
96
			}
97
98
			if (call_user_func_array($this->getRenderer(), [$row->getItem()])) {
99
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
100
			}
101
		}
102
103
		$link = $this->createLink($this->href, $this->getItemParams($row));
104
105
		$a = Html::el('a')->href($link);
106
107 View Code Duplication
		if ($this->icon) {
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...
108
			$a->add(Html::el('span')->class(DataGrid::$icon_prefix.$this->getIcon($row)));
109
110
			if (strlen($this->name)) {
111
				$a->add('&nbsp;');
112
			}
113
		}
114
115
		if ($this->data_attributes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data_attributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116
			foreach ($this->data_attributes as $key => $value) {
117
				$a->data($key, $value);
118
			}
119
		}
120
121
		$a->add($this->translate($this->name));
122
123
		if ($this->title) { $a->title($this->translate($this->getTitle($row))); }
124
		if ($this->class) { $a->class($this->getClass($row)); }
125
		if ($confirm = $this->getConfirm($row)) { $a->data('confirm', $this->translate($confirm)); }
126
127
		return $a;
128
	}
129
130
131
	/**
132
	 * Set attribute title
133
	 * @param string|callable $title
134
	 * @return static
135
	 */
136 View Code Duplication
	public function setTitle($title)
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...
137
	{
138
		if (!is_string($title) && !is_callable($title) && !is_null($title)) {
139
			throw new DataGridException(
140
				'Action title has to be either string or callback, that will return string'
141
			);
142
		}
143
144
		$this->title = $title;
145
146
		return $this;
147
	}
148
149
150
	/**
151
	 * Get attribute title
152
	 * @param Row $row
153
	 * @return string
154
	 */
155 View Code Duplication
	public function getTitle(Row $row)
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...
156
	{
157
		/**
158
		 * If user callback was used for setting action title, it has to return string
159
		 */
160
		if (is_callable($this->title)) {
161
			$title = call_user_func($this->title, $row->getItem());
162
163
			if (!is_string($title)) {
164
				throw new DataGridException('Action class callback has to return string');
165
			}
166
167
			return $title;
168
		}
169
170
		return $this->title;
171
	}
172
173
174
	/**
175
	 * Set attribute class
176
	 * @param string|callable $class
177
	 * @return static
178
	 */
179 View Code Duplication
	public function setClass($class)
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...
180
	{
181
		if (!is_string($class) && !is_callable($class) && !is_null($class)) {
182
			throw new DataGridException(
183
				'Action class has to be either string or callback, that will return string'
184
			);
185
		}
186
187
		$this->class = $class;
188
189
		return $this;
190
	}
191
192
193
	/**
194
	 * Get attribute class
195
	 * @param Row $row
196
	 * @return string
197
	 */
198 View Code Duplication
	public function getClass(Row $row)
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...
199
	{
200
		/**
201
		 * If user callback was used for setting action class, it has to return string
202
		 */
203
		if (is_callable($this->class)) {
204
			$class = call_user_func($this->class, $row->getItem());
205
206
			if (!is_string($class)) {
207
				throw new DataGridException('Action class callback has to return string');
208
			}
209
210
			return $class;
211
		}
212
213
		return $this->class;
214
	}
215
216
217
	/**
218
	 * Set icon
219
	 * @param string|callable $icon
220
	 * @return static|callable
221
	 */
222 View Code Duplication
	public function setIcon($icon)
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...
223
	{
224
		if (!is_string($icon) && !is_callable($icon) && !is_null($icon)) {
225
			throw new DataGridException(
226
				'Action icon has to be either string or callback, that will return string'
227
			);
228
		}
229
230
		$this->icon = $icon;
231
232
		return $this;
233
	}
234
235
236
	/**
237
	 * Get icon
238
	 * @param Row $row
239
	 * @return string
240
	 */
241 View Code Duplication
	public function getIcon(Row $row)
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...
242
	{
243
		/**
244
		 * If user callback was used for setting action icon, it has to return string
245
		 */
246
		if (is_callable($this->icon)) {
247
			$icon = call_user_func($this->icon, $row->getItem());
248
249
			if (!is_string($icon)) {
250
				throw new DataGridException('Action icon callback has to return string');
251
			}
252
253
			return $icon;
254
		}
255
256
		return $this->icon;
257
	}
258
259
260
	/**
261
	 * Set confirm dialog
262
	 * @param string|callable $message
263
	 * @param string $column
264
	 * @return static
265
	 */
266 View Code Duplication
	public function setConfirm($message, $column = NULL)
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...
267
	{
268
		if (!is_string($message) && !is_callable($message) && !is_null($message)) {
269
			throw new DataGridException(
270
				'Action message has to be either string or callback, that will return string'
271
			);
272
		}
273
274
		$this->confirm = [$message, $column];
275
276
		return $this;
277
	}
278
279
280
	/**
281
	 * Get confirm dialog for particular row item
282
	 * @param Row $row
283
	 * @return string
284
	 */
285
	public function getConfirm(Row $row)
286
	{
287
		if (!$this->confirm) {
288
			return NULL;
289
		}
290
291
		$question = $this->confirm[0];
292
293
		/**
294
		 * If user callback was used for setting action confirmation dialog, it has to return string
295
		 */
296
		if (is_callable($question)) {
297
			$question = call_user_func($question, $row->getItem());
298
299
			if (!is_string($question)) {
300
				throw new DataGridException('Action confirmation dialog callback has to return string');
301
			}
302
		}
303
304
		if (!$this->confirm[1]) {
305
			return $question;
306
		}
307
308
		return str_replace('%s', $row->getValue($this->confirm[1]), $question);
309
	}
310
311
312
	/**
313
	 * Setting data attributes
314
	 * @param string $key
315
	 * @param mixed $value
316
	 */
317
	public function setDataAttribute($key, $value)
318
	{
319
		$this->data_attributes[$key] = $value;
320
	}
321
322
323
	/**
324
	 * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...)
325
	 * @param  Row   $row
326
	 * @return array
327
	 */
328 View Code Duplication
	protected function getItemParams(Row $row)
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...
329
	{
330
		$return = [];
331
332
		foreach ($this->params as $param_name => $param) {
333
			$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param);
334
		}
335
336
		return $return;
337
	}
338
339
340
	/**
341
	 * Translator helper
342
	 * @param  string $message
343
	 * @return string
344
	 */
345
	protected function translate($message)
346
	{
347
		return $this->grid->getTranslator()->translate($message);
348
	}
349
350
}
351