InlineEdit::setPositionTop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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\InlineEdit;
10
11
use Nette;
12
use Nette\SmartObject;
13
use Nette\Utils\Html;
14
use Ublaboo\DataGrid\DataGrid;
15
use Ublaboo\DataGrid\Traits;
16
17
/**
18
 * @method onSubmit($id, Nette\Utils\ArrayHash $values)
19
 * @method onSubmit(Nette\Utils\ArrayHash $values)
20
 * @method onControlAdd(Nette\Forms\Container $container)
21
 * @method onControlAfterAdd(Nette\Forms\Container $container)
22
 * @method onSetDefaults(Nette\Forms\Container $container, $item)
23
 */
24
class InlineEdit
25
{
26
27
	use SmartObject;
28
29
	use Traits\TButtonTryAddIcon;
30
	use Traits\TButtonIcon;
31
	use Traits\TButtonClass;
32
	use Traits\TButtonTitle;
33
	use Traits\TButtonText;
34
35
	/**
36
	 * @var callable[]
37
	 */
38
	public $onSubmit;
39
40
	/**
41
	 * @var callable[]
42
	 */
43
	public $onControlAdd;
44
45
	/**
46
	 * @var callable[]
47
	 */
48
	public $onControlAfterAdd;
49
50
	/**
51
	 * @var callable[]
52
	 */
53
	public $onSetDefaults;
54
55
	/**
56
	 * @var callable[]
57
	 */
58
	public $onCustomRedraw;
59
60
	/**
61
	 * @var mixed
62
	 */
63
	protected $item_id;
64
65
	/**
66
	 * @var DataGrid
67
	 */
68
	protected $grid;
69
70
	/**
71
	 * @var string|NULL
72
	 */
73
	protected $primary_where_column;
74
75
	/**
76
	 * Inline adding - render on the top or in the bottom?
77
	 * @var bool
78
	 */
79
	protected $position_top = false;
80
81
	/**
82
	 * Columns that are not edited can displey normal value instaad of nothing..
83
	 * @var bool
84
	 */
85
	protected $showNonEditingColumns = true;
86
87
88
	/**
89
	 * @param DataGrid $grid
90
	 * @param string|NULL   $primary_where_column
91
	 */
92
	public function __construct(DataGrid $grid, $primary_where_column = null)
93
	{
94
		$this->grid = $grid;
95
		$this->primary_where_column = $primary_where_column;
96
97
		$this->title = 'ublaboo_datagrid.edit';
98
		$this->class = 'btn btn-xs btn-default ajax';
99
		$this->icon = 'pencil';
100
101
		$this->onControlAfterAdd[] = [$this, 'addControlsClasses'];
102
	}
103
104
105
	/**
106
	 * @param mixed $id
107
	 * @return static
108
	 */
109
	public function setItemId($id)
110
	{
111
		$this->item_id = $id;
112
113
		return $this;
114
	}
115
116
117
	/**
118
	 * @return mixed
119
	 */
120
	public function getItemId()
121
	{
122
		return $this->item_id;
123
	}
124
125
126
	/**
127
	 * @return string
128
	 */
129
	public function getPrimaryWhereColumn()
130
	{
131
		return $this->primary_where_column;
132
	}
133
134
135
	/**
136
	 * Render row item detail button
137
	 * @param  Row $row
138
	 * @return Html
139
	 */
140
	public function renderButton($row)
141
	{
142
		$a = Html::el('a')
143
			->href($this->grid->link('inlineEdit!', ['id' => $row->getId()]));
144
145
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
146
147
		$a->addText($this->text);
148
149
		if ($this->title) {
150
			$a->title($this->grid->getTranslator()->translate($this->title));
151
		}
152
		if ($this->class) {
153
			$a->class[] = $this->class;
154
		}
155
156
		$a->class[] = 'datagrid-inline-edit-trigger';
157
158
		return $a;
159
	}
160
161
162
	/**
163
	 * Render row item detail button
164
	 * @return Html
165
	 */
166
	public function renderButtonAdd()
167
	{
168
		$a = Html::el('a')->data('datagrid-toggle-inline-add', true);
169
170
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
171
172
		$a->addText($this->text);
173
174
		if ($this->title) {
175
			$a->title($this->grid->getTranslator()->translate($this->title));
176
		}
177
		if ($this->class) {
178
			$a->class($this->class);
179
		}
180
181
		return $a;
182
	}
183
184
185
	/**
186
	 * Setter for inline adding position
187
	 * @param bool $position_top
188
	 * @return static
189
	 */
190
	public function setPositionTop($position_top = true)
191
	{
192
		$this->position_top = (bool) $position_top;
193
194
		return $this;
195
	}
196
197
198
	/**
199
	 * Getter for inline adding
200
	 * @return bool
201
	 */
202
	public function isPositionTop()
203
	{
204
		return $this->position_top;
205
	}
206
207
208
	/**
209
	 * @return bool
210
	 */
211
	public function isPositionBottom()
212
	{
213
		return !$this->position_top;
214
	}
215
216
217
	/**
218
	 * @param Nette\Forms\Container $container
219
	 */
220
	public function addControlsClasses(Nette\Forms\Container $container)
221
	{
222
		foreach ($container->getControls() as $key => $control) {
223
			switch ($key) {
224
				case 'submit':
225
					$control->setValidationScope([$container]);
226
					$control->setAttribute('class', 'btn btn-xs btn-primary');
227
228
					break;
229
230
				case 'cancel':
231
					$control->setAttribute('class', 'btn btn-xs btn-danger');
232
233
					break;
234
235
				default:
236
					if (empty($control->getControl()->getClass())) {
237
						$control->setAttribute('class', 'form-control input-sm');
238
					}
239
240
					break;
241
			}
242
		}
243
	}
244
245
246
	/**
247
	 * @param bool $show
248
	 * @return static
249
	 */
250
	public function setShowNonEditingColumns($show = true)
251
	{
252
		$this->showNonEditingColumns = (bool) $show;
253
254
		return $this;
255
	}
256
257
258
	/**
259
	 * @return bool
260
	 */
261
	public function showNonEditingColumns()
262
	{
263
		return $this->showNonEditingColumns;
264
	}
265
}
266