Skip to content

Commit 7ede2ec

Browse files
Clear useless form conditions (#21979)
1 parent e255632 commit 7ede2ec

15 files changed

+307
-0
lines changed

src/Glpi/Form/Comment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ private function prepareInput(array $input): array
169169
unset($input['_conditions']);
170170
}
171171

172+
$input = $this->removeSavedConditionsIfAlwaysVisible($input);
173+
172174
return $input;
173175
}
174176

src/Glpi/Form/Condition/ConditionableCreationInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@
3737
interface ConditionableCreationInterface extends ConditionableInterface
3838
{
3939
public function getConfiguredCreationStrategy(): CreationStrategy;
40+
41+
// TODO: uncomment on main to prevent BC breaks
42+
// protected function removeSavedConditionsIfAlwaysCreated(array $input): array;
4043
}

src/Glpi/Form/Condition/ConditionableCreationTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
namespace Glpi\Form\Condition;
3636

37+
use function Safe\json_encode;
38+
3739
trait ConditionableCreationTrait
3840
{
3941
use ConditionableTrait;
@@ -44,4 +46,19 @@ public function getConfiguredCreationStrategy(): CreationStrategy
4446
$strategy = CreationStrategy::tryFrom($strategy_value);
4547
return $strategy ?? CreationStrategy::ALWAYS_CREATED;
4648
}
49+
50+
protected function removeSavedConditionsIfAlwaysCreated(array $input): array
51+
{
52+
$strategy_field = 'creation_strategy';
53+
$condition_field = $this->getConditionsFieldName();
54+
55+
if (
56+
isset($input[$strategy_field])
57+
&& $input[$strategy_field] == CreationStrategy::ALWAYS_CREATED->value
58+
) {
59+
$input[$condition_field] = json_encode([]);
60+
}
61+
62+
return $input;
63+
}
4764
}

src/Glpi/Form/Condition/ConditionableValidationTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
namespace Glpi\Form\Condition;
3636

37+
use function Safe\json_encode;
38+
3739
trait ConditionableValidationTrait
3840
{
3941
use ConditionableTrait {
@@ -76,4 +78,19 @@ public function getConfiguredValidationStrategy(): ValidationStrategy
7678
$strategy = ValidationStrategy::tryFrom($strategy_value);
7779
return $strategy ?? ValidationStrategy::NO_VALIDATION;
7880
}
81+
82+
protected function removeSavedConditionsIfNoValidation(array $input): array
83+
{
84+
$strategy_field = $this->getValidationStrategyFieldName();
85+
$condition_field = $this->getValidationConditionsFieldName();
86+
87+
if (
88+
isset($input[$strategy_field])
89+
&& $input[$strategy_field] == ValidationStrategy::NO_VALIDATION->value
90+
) {
91+
$input[$condition_field] = json_encode([]);
92+
}
93+
94+
return $input;
95+
}
7996
}

src/Glpi/Form/Condition/ConditionableVisibilityInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ public function getConfiguredVisibilityStrategy(): VisibilityStrategy;
4949
* @return string
5050
*/
5151
public function getUUID(): string;
52+
53+
// TODO: uncomment on main to prevent BC breaks
54+
// protected function removeSavedConditionsIfAlwaysVisible(array $input): array;
5255
}

src/Glpi/Form/Condition/ConditionableVisibilityTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
namespace Glpi\Form\Condition;
3636

37+
use function Safe\json_encode;
38+
3739
trait ConditionableVisibilityTrait
3840
{
3941
use ConditionableTrait;
@@ -56,4 +58,19 @@ public function getConfiguredVisibilityStrategy(): VisibilityStrategy
5658
$strategy = VisibilityStrategy::tryFrom($strategy_value);
5759
return $strategy ?? VisibilityStrategy::ALWAYS_VISIBLE;
5860
}
61+
62+
protected function removeSavedConditionsIfAlwaysVisible(array $input): array
63+
{
64+
$visibility_field = $this->getVisibilityStrategyFieldName();
65+
$condition_field = $this->getConditionsFieldName();
66+
67+
if (
68+
isset($input[$visibility_field])
69+
&& $input[$visibility_field] == VisibilityStrategy::ALWAYS_VISIBLE->value
70+
) {
71+
$input[$condition_field] = json_encode([]);
72+
}
73+
74+
return $input;
75+
}
5976
}

src/Glpi/Form/Destination/FormDestination.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ public function prepareInput($input): array
284284
unset($input['_conditions']);
285285
}
286286

287+
$input = $this->removeSavedConditionsIfAlwaysCreated($input);
288+
287289
return $input;
288290
}
289291

src/Glpi/Form/Form.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ private function prepareInput(array $input): array
375375
unset($input['_submit_button_conditions']);
376376
}
377377

378+
$input = $this->removeSavedConditionsIfAlwaysVisible($input);
379+
378380
return $input;
379381
}
380382

src/Glpi/Form/Question.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ private function prepareInput(array $input): array
401401
}
402402
}
403403

404+
$input = $this->removeSavedConditionsIfAlwaysVisible($input);
405+
$input = $this->removeSavedConditionsIfNoValidation($input);
406+
404407
return $input;
405408
}
406409

src/Glpi/Form/Section.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ private function prepareInput(array $input): array
144144
unset($input['_conditions']);
145145
}
146146

147+
$input = $this->removeSavedConditionsIfAlwaysVisible($input);
148+
147149
return $input;
148150
}
149151

0 commit comments

Comments
 (0)