forked from glpi-project/glpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateformforeachillustrationscommand.class.php
112 lines (98 loc) · 3.75 KB
/
generateformforeachillustrationscommand.class.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
use Glpi\Console\AbstractCommand;
use Glpi\Form\AccessControl\ControlType\AllowList;
use Glpi\Form\AccessControl\ControlType\AllowListConfig;
use Glpi\Form\AccessControl\FormAccessControl;
use Glpi\Form\Form;
use Glpi\UI\IllustrationManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class GenerateFormForeachIllustrationsCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this->setName('tools:generate_forms_for_each_illustrations');
$this->setDescription(__("Generate one form per available illustration."));
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$manager = new IllustrationManager();
$illustrations = $manager->getAllIllustrationsNames();
foreach ($illustrations as $illustration) {
if ($this->demoFormExistsForIllustration($illustration)) {
$output->writeln("A form already exist for $illustration.");
continue;
}
$this->createDemoFormForIllustration($illustration);
$output->writeln("Created a form for $illustration.");
}
return Command::SUCCESS;
}
private function demoFormExistsForIllustration(string $illustration): bool
{
$forms = (new Form())->find([
'description' => "Demo form for the '$illustration' illustration."
]);
return count($forms) > 0;
}
private function createDemoFormForIllustration(string $illustration): void
{
$form_id = (new Form())->add([
'name' => $this->normalizeIllustrationName($illustration),
'description' => "Demo form for the '$illustration' illustration.",
'illustration' => $illustration,
'entities_id' => 0,
'is_recursive' => true,
'is_active' => true,
]);
$form_access_control = new FormAccessControl();
$form_access_control->add([
Form::getForeignKeyField() => $form_id,
'strategy' => AllowList::class,
'_config' => new AllowListConfig(
user_ids: [AbstractRightsDropdown::ALL_USERS]
),
'is_active' => 1,
]);
}
private function normalizeIllustrationName(string $illustration): string
{
// Transform "my-illustration.svg' into "My illustration".
$illustration = substr($illustration, 0, -4);
$illustration = str_replace('-', ' ', $illustration);
return ucfirst($illustration);
}
}