-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseFormTrait.php
More file actions
89 lines (75 loc) · 2.3 KB
/
Copy pathBaseFormTrait.php
File metadata and controls
89 lines (75 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace ADT\FancyAdmin\UI\Components\Forms;
use ADT\DoctrineForms\Form;
use ADT\FancyAdmin\DI\Injects\AccountQueryFactoryInject;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\DI\Injects\TranslatorInject;
use ADT\FancyAdmin\UI\Components\Controls\SidePanel\SidePanelSize;
use ADT\FancyAdmin\UI\Components\ControlTrait;
use ADT\Forms\BootstrapFormRenderer;
use App\UI\Portal\Components\Forms\Base\EntityForm;
trait BaseFormTrait
{
use ControlTrait;
use EntityManagerInject;
use TranslatorInject;
use AccountQueryFactoryInject;
protected bool $disableAccountInput = false;
protected bool $csrfProtection = true;
abstract protected function getEntityClass(): ?string;
/**
* @throws \ReflectionException
*/
public function onBeforeInitForm(Form $form): void
{
if ($this->getEntityClass() && !$this->disableAccountInput) {
$refClass = new \ReflectionClass($this->getEntityClass());
if ($refClass->hasProperty('account')) {
$accountyQuery = $this->_accountQueryFactory->create();
if ($this->securityUser->getIdentity()->getSelectedAccount()) {
$accountyQuery->byIdOrParentId($this->securityUser->getIdentity()->getSelectedAccount());
}
if ($accountyQuery->count() > 1) {
$pairs = [];
foreach ($accountyQuery->orderBy(['parent' => 'ASC', 'name' => 'ASC'])->fetch() as $_account) {
if ($_account->getParent()) {
$pairs[$_account->getId()] = '-- ' . $_account->getName();
} else {
$pairs[$_account->getId()] = $_account->getName();
}
}
$form->addSelect('account', 'fcadmin.forms.user.labels.account', $pairs)
->setPrompt('---')
->setRequired();
}
}
}
}
protected function createComponentForm(): Form
{
$form = new Form();
$form->setTranslator($this->_translator);
$form->setEntityManager($this->_em);
$form->setRenderer(new BootstrapFormRenderer($form));
if ($this->csrfProtection) {
$form->addProtection('fcadmin.forms.errors.csrf');
}
return $form;
}
public function getSidePanelSize(): SidePanelSize
{
return SidePanelSize::Medium;
}
public function getRedirect($entity = null): ?array
{
return null;
}
public function getSnippetsToRedraw(): array
{
return [];
}
public static function getDefaultTemplateFile(): string
{
return __DIR__ . '/BaseForm.latte';
}
}