Completed
Push — master ( d8781a...8c85b0 )
by Pavel
03:02
created

InlineEdit::showNonEditingColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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