|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Core\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use UniMan\Core\Driver\DriverInterface; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Nette\Application\UI\Control; |
|
8
|
|
|
use Nette\Application\UI\Form; |
|
9
|
|
|
use Nette\Localization\ITranslator; |
|
10
|
|
|
use Tomaj\Form\Renderer\BootstrapVerticalRenderer; |
|
11
|
|
|
|
|
12
|
|
|
class ItemForm extends Control |
|
13
|
|
|
{ |
|
14
|
|
|
private $translator; |
|
15
|
|
|
|
|
16
|
|
|
private $driver; |
|
17
|
|
|
|
|
18
|
|
|
private $database; |
|
19
|
|
|
|
|
20
|
|
|
private $type; |
|
21
|
|
|
|
|
22
|
|
|
private $table; |
|
23
|
|
|
|
|
24
|
|
|
private $item; |
|
25
|
|
|
|
|
26
|
|
View Code Duplication |
public function __construct(ITranslator $translator, DriverInterface $driver, $database, $type, $table, $item = null) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
$this->translator = $translator; |
|
30
|
|
|
$this->driver = $driver; |
|
31
|
|
|
$this->database = $database; |
|
32
|
|
|
$this->type = $type; |
|
33
|
|
|
$this->table = $table; |
|
34
|
|
|
$this->item = $item; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function render() |
|
38
|
|
|
{ |
|
39
|
|
|
echo $this['form']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function createComponentForm() |
|
43
|
|
|
{ |
|
44
|
|
|
$form = new Form(); |
|
45
|
|
|
$form->setTranslator($this->translator); |
|
46
|
|
|
$form->setRenderer(new BootstrapVerticalRenderer()); |
|
47
|
|
|
|
|
48
|
|
|
$itemForm = $this->driver->formManager()->itemForm($this->database, $this->type, $this->table, $this->item); |
|
49
|
|
|
if (!$itemForm) { |
|
50
|
|
|
throw new InvalidArgumentException('Item form not set'); |
|
51
|
|
|
} |
|
52
|
|
|
$itemForm->addFieldsToForm($form); |
|
53
|
|
|
$form->addSubmit('save', 'Save'); |
|
54
|
|
|
$form->onSuccess[] = [$itemForm, 'submit']; |
|
55
|
|
|
$form->onSuccess[] = function () { |
|
56
|
|
|
$this->item |
|
57
|
|
|
? $this->presenter->flashMessage('Item was successfully updated', 'success') |
|
58
|
|
|
: $this->presenter->flashMessage('Item was successfully created', 'success'); |
|
59
|
|
|
$this->presenter->redirect('Item:default', $this->driver->type(), $this->database, $this->type, $this->table); |
|
60
|
|
|
}; |
|
61
|
|
|
return $form; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
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.