Completed
Push — master ( 8d2beb...c205bc )
by Pavel
03:13
created

InlineEdit::renderButton()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 4
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\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
	/**
77
	 * @param DataGrid $grid
78
	 * @param string|NULL   $primary_where_column
79
	 */
80
	public function __construct(DataGrid $grid, $primary_where_column = NULL)
81
	{
82
		$this->grid = $grid;
83
		$this->primary_where_column = $primary_where_column;
84
85
		$this->title = 'ublaboo_datagrid.edit';
86
		$this->class = 'btn btn-xs btn-default ajax';
87
		$this->icon = 'pencil';
88
89
		$this->onControlAfterAdd[] = [$this, 'addControlsClasses'];
90
	}
91
92
93
	/**
94
	 * @param mixed $id
95
	 */
96
	public function setItemId($id)
97
	{
98
		$this->item_id = $id;
99
	}
100
101
102
	/**
103
	 * @return mixed
104
	 */
105
	public function getItemId()
106
	{
107
		return $this->item_id;
108
	}
109
110
111
	/**
112
	 * @return string
113
	 */
114
	public function getPrimaryWhereColumn()
115
	{
116
		return $this->primary_where_column;
117
	}
118
119
120
	/**
121
	 * Render row item detail button
122
	 * @param  Row $row
123
	 * @return Html
124
	 */
125
	public function renderButton($row)
126
	{
127
		$a = Html::el('a')
128
			->href($this->grid->link('inlineEdit!', ['id' => $row->getId()]));
129
130
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
131
132
		$a->addText($this->text);
133
134
		if ($this->title) { $a->title($this->title); }
135
		if ($this->class) { $a->class[] = $this->class; }
136
137
		$a->class[] = 'datagrid-inline-edit-trigger';
138
139
		return $a;
140
	}
141
142
143
	/**
144
	 * Render row item detail button
145
	 * @return Html
146
	 */
147
	public function renderButtonAdd()
148
	{
149
		$a = Html::el('a')->data('datagrid-toggle-inline-add', TRUE);
150
151
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
152
153
		$a->addText($this->text);
154
155
		if ($this->title) { $a->title($this->title); }
156
		if ($this->class) { $a->class($this->class); }
157
158
		return $a;
159
	}
160
161
162
	/**
163
	 * Setter for inline adding position
164
	 * @param bool $position_top
165
	 * @return static
166
	 */
167
	public function setPositionTop($position_top = TRUE)
168
	{
169
		$this->position_top = (bool) $position_top;
170
171
		return $this;
172
	}
173
174
175
	/**
176
	 * Getter for inline adding
177
	 * @return bool
178
	 */
179
	public function isPositionTop()
180
	{
181
		return $this->position_top;
182
	}
183
184
185
	/**
186
	 * @return bool
187
	 */
188
	public function isPositionBottom()
189
	{
190
		return !$this->position_top;
191
	}
192
193
194
195
	/**
196
	 * @param Nette\Forms\Container $container
197
	 */
198
	public function addControlsClasses(Nette\Forms\Container $container)
199
	{
200
		foreach ($container->getControls() as $key => $control) {
201
			switch ($key) {
202
				case 'submit':
203
					$control->setAttribute('class', 'btn btn-xs btn-primary');
204
205
					break;
206
207
				case 'cancel':
208
					$control->setAttribute('class', 'btn btn-xs btn-danger');
209
210
					break;
211
				
212
				default:
213
					if (empty($control->getControl()->getAttribute('class'))) {
214
						$control->setAttribute('class', 'form-control input-sm');
215
					}
216
217
					break;
218
			}
219
		}
220
	}
221
222
}
223