1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Form\Permissions; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use App\Services\PermissionResolver; |
36
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
37
|
|
|
use Symfony\Component\Form\FormInterface; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* This class is a data mapper that maps the permission data from DB (accessed via a PermissionResolver), |
41
|
|
|
* to TristateCheckboxes and vice versa. |
42
|
|
|
*/ |
43
|
|
|
class PermissionsMapper implements DataMapperInterface |
44
|
|
|
{ |
45
|
|
|
protected $resolver; |
46
|
|
|
protected $inherit; |
47
|
|
|
|
48
|
|
|
public function __construct(PermissionResolver $resolver, bool $inherit = false) |
49
|
|
|
{ |
50
|
|
|
$this->inherit = $inherit; |
51
|
|
|
$this->resolver = $resolver; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Maps the view data of a compound form to its children. |
56
|
|
|
* |
57
|
|
|
* The method is responsible for calling {@link FormInterface::setData()} |
58
|
|
|
* on the children of compound forms, defining their underlying model data. |
59
|
|
|
* |
60
|
|
|
* @param mixed $viewData View data of the compound form being initialized |
61
|
|
|
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
public function mapDataToForms($viewData, $forms) |
65
|
|
|
{ |
66
|
|
|
foreach ($forms as $form) { |
67
|
|
|
if ($this->inherit) { |
68
|
|
|
$value = $this->resolver->inherit( |
69
|
|
|
$viewData, |
70
|
|
|
$form->getParent()->getConfig()->getOption('perm_name'), |
71
|
|
|
$form->getName() |
72
|
|
|
) ?? false; |
73
|
|
|
} else { |
74
|
|
|
$value = $this->resolver->dontInherit( |
75
|
|
|
$viewData, |
76
|
|
|
$form->getParent()->getConfig()->getOption('perm_name'), |
77
|
|
|
$form->getName() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
$form->setData($value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Maps the model data of a list of children forms into the view data of their parent. |
86
|
|
|
* |
87
|
|
|
* This is the internal cascade call of FormInterface::submit for compound forms, since they |
88
|
|
|
* cannot be bound to any input nor the request as scalar, but their children may: |
89
|
|
|
* |
90
|
|
|
* $compoundForm->submit($arrayOfChildrenViewData) |
91
|
|
|
* // inside: |
92
|
|
|
* $childForm->submit($childViewData); |
93
|
|
|
* // for each entry, do the same and/or reverse transform |
94
|
|
|
* $this->dataMapper->mapFormsToData($compoundForm, $compoundInitialViewData) |
95
|
|
|
* // then reverse transform |
96
|
|
|
* |
97
|
|
|
* When a simple form is submitted the following is happening: |
98
|
|
|
* |
99
|
|
|
* $simpleForm->submit($submittedViewData) |
100
|
|
|
* // inside: |
101
|
|
|
* $this->viewData = $submittedViewData |
102
|
|
|
* // then reverse transform |
103
|
|
|
* |
104
|
|
|
* The model data can be an array or an object, so this second argument is always passed |
105
|
|
|
* by reference. |
106
|
|
|
* |
107
|
|
|
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances |
108
|
|
|
* @param mixed $viewData The compound form's view data that get mapped |
109
|
|
|
* its children model data |
110
|
|
|
* |
111
|
|
|
*/ |
112
|
|
|
public function mapFormsToData($forms, &$viewData) |
113
|
|
|
{ |
114
|
|
|
if ($this->inherit) { |
115
|
|
|
throw new \RuntimeException('The permission type is readonly when it is showing read only data!'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($forms as $form) { |
119
|
|
|
$value = $form->getData(); |
120
|
|
|
$this->resolver->setPermission( |
121
|
|
|
$viewData, |
122
|
|
|
$form->getParent()->getConfig()->getOption('perm_name'), |
123
|
|
|
$form->getName(), |
124
|
|
|
$value |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |