Completed
Push — master ( e259e3...f37a30 )
by Pavel
02:33
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\DataGridHasToBeAttachedToPresenterComponentException;
14
use Ublaboo\DataGrid\Row;
15
16
class Action extends Column
17
{
18
19
	/**
20
	 * @var string
21
	 */
22
	protected $title;
23
24
	/**
25
	 * @var string
26
	 */
27
	protected $class;
28
29
	/**
30
	 * @var string
31
	 */
32
	protected $icon;
33
34
	/**
35
	 * @var DataGrid
36
	 */
37
	protected $grid;
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $href;
43
44
	/**
45
	 * @var string
46
	 */
47
	protected $name;
48
49
	/**
50
	 * @var array
51
	 */
52
	protected $params;
53
54
	/**
55
	 * @var array
56
	 */
57
	protected $confirm;
58
59
	/**
60
	 * @var array
61
	 */
62
	protected $data_attributes = [];
63
64
65
	/**
66
	 * @param DataGrid $grid
67
	 * @param string   $href
68
	 * @param string   $name
69
	 * @param array    $params
70
	 */
71 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...
72
	{
73
		$this->grid = $grid;
74
		$this->href = $href;
75
		$this->name = $name;
76
		$this->params = $params;
77
78
		$this->class = 'btn btn-xs btn-default';
79
	}
80
81
82
	/**
83
	 * Render row item into template
84
	 * @param  Row   $row
85
	 * @return mixed
86
	 */
87
	public function render(Row $row)
88
	{
89
		/**
90
		 * Renderer function may be used
91
		 */
92 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...
93
			if (!$renderer->getConditionCallback()) {
94
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
95
			}
96
97
			if (call_user_func_array($this->getRenderer(), [$row->getItem()])) {
98
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
99
			}
100
		}
101
102
		$link = $this->createLink($row);
103
104
		$a = Html::el('a')->href($link);
105
106 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...
107
			$a->add(Html::el('span')->class(DataGrid::$icon_prefix.$this->icon));
108
109
			if (strlen($this->name)) {
110
				$a->add('&nbsp;');
111
			}
112
		}
113
114
		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...
115
			foreach ($this->data_attributes as $key => $value) {
116
				$a->data($key, $value);
117
			}
118
		}
119
120
		$a->add($this->translate($this->name));
121
122
		if ($this->title) { $a->title($this->translate($this->title)); }
123
		if ($this->class) { $a->class($this->class); }
124
		if ($confirm = $this->getConfirm($row)) { $a->data('confirm', $this->translate($confirm)); }
125
126
		return $a;
127
	}
128
129
130
	/**
131
	 * Set attribute title
132
	 * @param string $title
133
	 * @return static
134
	 */
135
	public function setTitle($title)
136
	{
137
		$this->title = $title;
138
139
		return $this;
140
	}
141
142
143
	/**
144
	 * Get attribute title
145
	 * @return string
146
	 */
147
	public function getTitle()
148
	{
149
		return $this->title;
150
	}
151
152
153
	/**
154
	 * Set attribute class
155
	 * @param string $class
156
	 * @return static
157
	 */
158
	public function setClass($class)
159
	{
160
		$this->class = $class;
161
162
		return $this;
163
	}
164
165
166
	/**
167
	 * Get attribute class
168
	 * @return string
169
	 */
170
	public function getClass()
171
	{
172
		return $this->class;
173
	}
174
175
176
	/**
177
	 * Set icon
178
	 * @param string $icon
179
	 * @return static
180
	 */
181
	public function setIcon($icon)
182
	{
183
		$this->icon = $icon;
184
185
		return $this;
186
	}
187
188
189
	/**
190
	 * Get icon
191
	 * @return string
192
	 */
193
	public function getIcon()
194
	{
195
		return $this->icon;
196
	}
197
198
199
	/**
200
	 * Set confirm dialog
201
	 * @param string $message
202
	 * @param string $column
203
	 * @return static
204
	 */
205
	public function setConfirm($message, $column = NULL)
206
	{
207
		$this->confirm = [$message, $column];
208
209
		return $this;
210
	}
211
212
213
	/**
214
	 * Get confirm dialog for particular row item
215
	 * @param Row $row
216
	 * @return string
217
	 */
218
	public function getConfirm(Row $row)
219
	{
220
		if (!$this->confirm) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->confirm 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...
221
			return NULL;
222
		}
223
224
		if (!$this->confirm[1]) {
225
			return $this->confirm[0];
226
		}
227
228
		return str_replace('%s', $row->getValue($this->confirm[1]), $this->confirm[0]);
229
	}
230
231
232
	/**
233
	 * Setting data attributes
234
	 * @param string $key
235
	 * @param mixed $value
236
	 */
237
	public function setDataAttribute($key, $value)
238
	{
239
		$this->data_attributes[$key] = $value;
240
	}
241
242
243
	/**
244
	 * Create link to custom destination
245
	 * @param  Row    $row
246
	 * @return string
247
	 * @throws DataGridHasToBeAttachedToPresenterComponentException
248
	 */
249
	protected function createLink(Row $row)
250
	{
251
		try {
252
			$parent = $this->grid->getParent();
253
		} catch (DataGridHasToBeAttachedToPresenterComponentException $e) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\DataGri...enterComponentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
254
			$parent = $this->grid->getPresenter();
255
		}
256
257
		return $parent->link($this->href, $this->getItemParams($row));
258
	}
259
260
261
	/**
262
	 * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...)
263
	 * @param  Row   $row
264
	 * @return array
265
	 */
266 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...
267
	{
268
		$return = [];
269
270
		foreach ($this->params as $param_name => $param) {
271
			$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param);
272
		}
273
274
		return $return;
275
	}
276
277
278
	/**
279
	 * Translator helper
280
	 * @param  string $message
281
	 * @return string
282
	 */
283
	protected function translate($message)
284
	{
285
		return $this->grid->getTranslator()->translate($message);
286
	}
287
288
}
289