Completed
Push — master ( 49127a...e49a18 )
by Pavel
02:25
created

Action::getClass()   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 0
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
	public function __construct(DataGrid $grid, $href, $name, $params)
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
		try {
103
			$parent = $this->grid->getParent();
104
		} 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...
105
			$parent = $this->grid->getPresenter();
106
		}
107
108
		$a = Html::el('a')
109
			->href($parent->link($this->href, $this->getItemParams($row)));
110
111 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...
112
			$a->add(Html::el('span')->class(DataGrid::$icon_prefix.$this->icon));
113
114
			if (strlen($this->name)) {
115
				$a->add('&nbsp;');
116
			}
117
		}
118
119
		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...
120
			foreach ($this->data_attributes as $key => $value) {
121
				$a->data($key, $value);
122
			}
123
		}
124
125
		$a->add($this->translate($this->name));
126
127
		if ($this->title) { $a->title($this->translate($this->title)); }
128
		if ($this->class) { $a->class($this->class); }
129
		if ($confirm = $this->getConfirm($row)) { $a->data('confirm', $this->translate($confirm)); }
130
131
		return $a;
132
	}
133
134
135
	/**
136
	 * Set attribute title
137
	 * @param string $title
138
	 * @return static
139
	 */
140
	public function setTitle($title)
141
	{
142
		$this->title = $title;
143
144
		return $this;
145
	}
146
147
148
	/**
149
	 * Get attribute title
150
	 * @return string
151
	 */
152
	public function getTitle()
153
	{
154
		return $this->title;
155
	}
156
157
158
	/**
159
	 * Set attribute class
160
	 * @param string $class
161
	 * @return static
162
	 */
163
	public function setClass($class)
164
	{
165
		$this->class = $class;
166
167
		return $this;
168
	}
169
170
171
	/**
172
	 * Get attribute class
173
	 * @return string
174
	 */
175
	public function getClass()
176
	{
177
		return $this->class;
178
	}
179
180
181
	/**
182
	 * Set icon
183
	 * @param string $icon
184
	 * @return static
185
	 */
186
	public function setIcon($icon)
187
	{
188
		$this->icon = $icon;
189
190
		return $this;
191
	}
192
193
194
	/**
195
	 * Get icon
196
	 * @return string
197
	 */
198
	public function getIcon()
199
	{
200
		return $this->icon;
201
	}
202
203
204
	/**
205
	 * Set confirm dialog
206
	 * @param string $message
207
	 * @param string $column
208
	 * @return static
209
	 */
210
	public function setConfirm($message, $column = NULL)
211
	{
212
		$this->confirm = [$message, $column];
213
214
		return $this;
215
	}
216
217
218
	/**
219
	 * Get confirm dialog for particular row item
220
	 * @param Row $row
221
	 * @return string
222
	 */
223
	public function getConfirm(Row $row)
224
	{
225
		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...
226
			return NULL;
227
		}
228
229
		if (!$this->confirm[1]) {
230
			return $this->confirm[0];
231
		}
232
233
		return str_replace('%s', $row->getValue($this->confirm[1]), $this->confirm[0]);
234
	}
235
236
237
	/**
238
	 * Setting data attributes
239
	 * @param string $key
240
	 * @param mixed $value
241
	 */
242
	public function setDataAttribute($key, $value)
243
	{
244
		$this->data_attributes[$key] = $value;
245
	}
246
247
248
	/**
249
	 * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...)
250
	 * @param  Row   $row
251
	 * @return array
252
	 */
253 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...
254
	{
255
		$return = [];
256
257
		foreach ($this->params as $param_name => $param) {
258
			$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param);
259
		}
260
261
		return $return;
262
	}
263
264
265
	/**
266
	 * Translator helper
267
	 * @param  string $message
268
	 * @return string
269
	 */
270
	protected function translate($message)
271
	{
272
		return $this->grid->getTranslator()->translate($message);
273
	}
274
275
}
276