|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ------------------------------------------------------------------------- |
| 5 | + * Fields plugin for GLPI |
| 6 | + * ------------------------------------------------------------------------- |
| 7 | + * |
| 8 | + * LICENSE |
| 9 | + * |
| 10 | + * This file is part of Fields. |
| 11 | + * |
| 12 | + * Fields is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 2 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * Fields is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with Fields. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * ------------------------------------------------------------------------- |
| 25 | + * @copyright Copyright (C) 2013-2023 by Fields plugin team. |
| 26 | + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html |
| 27 | + * @link https://github.com/pluginsGLPI/fields |
| 28 | + * ------------------------------------------------------------------------- |
| 29 | + */ |
| 30 | + |
| 31 | +use Glpi\Application\View\TemplateRenderer; |
| 32 | +use Glpi\DBAL\JsonFieldInterface; |
| 33 | +use Glpi\Form\AnswersSet; |
| 34 | +use Glpi\Form\Destination\AbstractCommonITILFormDestination; |
| 35 | +use Glpi\Form\Destination\AbstractConfigField; |
| 36 | +use Glpi\Form\Destination\CommonITILField\Category; |
| 37 | +use Glpi\Form\Destination\CommonITILField\SimpleValueConfig; |
| 38 | +use Glpi\Form\Destination\FormDestination; |
| 39 | +use Glpi\Form\Form; |
| 40 | +use Glpi\Form\Question; |
| 41 | +use Glpi\Form\QuestionType\QuestionTypeItemDropdown; |
| 42 | + |
| 43 | +class PluginFieldsDestinationField extends AbstractConfigField |
| 44 | +{ |
| 45 | + public function __construct(private AbstractCommonITILFormDestination $itil_destination) {} |
| 46 | + |
| 47 | + #[Override] |
| 48 | + public function getLabel(): string |
| 49 | + { |
| 50 | + return __('Additional fields', 'fields'); |
| 51 | + } |
| 52 | + |
| 53 | + #[Override] |
| 54 | + public function getConfigClass(): string |
| 55 | + { |
| 56 | + return SimpleValueConfig::class; |
| 57 | + } |
| 58 | + |
| 59 | + #[Override] |
| 60 | + public function renderConfigForm( |
| 61 | + Form $form, |
| 62 | + FormDestination $destination, |
| 63 | + JsonFieldInterface $config, |
| 64 | + string $input_name, |
| 65 | + array $display_options |
| 66 | + ): string { |
| 67 | + if (!$config instanceof SimpleValueConfig) { |
| 68 | + throw new InvalidArgumentException("Unexpected config class"); |
| 69 | + } |
| 70 | + |
| 71 | + $twig = TemplateRenderer::getInstance(); |
| 72 | + return $twig->render('@fields/destinationfield.html.twig', [ |
| 73 | + 'value' => $config->getValue(), |
| 74 | + 'input_name' => $input_name . "[" . SimpleValueConfig::VALUE . "]", |
| 75 | + 'options' => $display_options, |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + #[Override] |
| 80 | + public function applyConfiguratedValueToInputUsingAnswers( |
| 81 | + JsonFieldInterface $config, |
| 82 | + array $input, |
| 83 | + AnswersSet $answers_set |
| 84 | + ): array { |
| 85 | + if (!$config instanceof SimpleValueConfig) { |
| 86 | + throw new InvalidArgumentException("Unexpected config class"); |
| 87 | + } |
| 88 | + |
| 89 | + if ((bool) $config->getValue()) { |
| 90 | + $answers = $answers_set->getAnswersByTypes([ |
| 91 | + PluginFieldsQuestionType::class, |
| 92 | + QuestionTypeItemDropdown::class, |
| 93 | + ]); |
| 94 | + |
| 95 | + foreach ($answers as $answer) { |
| 96 | + $question = Question::getById($answer->getQuestionId()); |
| 97 | + $block_id = PluginFieldsContainer::findContainer($this->itil_destination->getTarget()::class, 'dom'); |
| 98 | + if (!$block_id) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if ($question->getQuestionType() instanceof QuestionTypeItemDropdown) { |
| 103 | + $itemtype = (new QuestionTypeItemDropdown())->getDefaultValueItemtype($question); |
| 104 | + $field_name = $itemtype::getForeignKeyField(); |
| 105 | + if (!str_starts_with($field_name, 'plugin_fields_')) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + /** @var object{field_name: string} $item */ |
| 110 | + $item = getItemForItemtype($itemtype); |
| 111 | + $field = new PluginFieldsField(); |
| 112 | + if (!$field->getFromDBByCrit(['name' => $item->field_name])) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + $value = $answer->getRawAnswer()['items_id']; |
| 117 | + } else { |
| 118 | + $field_id = (new PluginFieldsQuestionType())->getDefaultValueFieldId($question); |
| 119 | + $field = PluginFieldsField::getById($field_id); |
| 120 | + } |
| 121 | + |
| 122 | + // Check that the field belongs to the correct block |
| 123 | + if ($block_id != $field->fields[PluginFieldsContainer::getForeignKeyField()]) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + $input['c_id'] = $block_id; |
| 128 | + if ($field->fields['type'] == 'dropdown') { |
| 129 | + $field_name = 'plugin_fields_' . $field->fields['name'] . 'dropdowns_id'; |
| 130 | + } else { |
| 131 | + $field_name = $field->fields['name']; |
| 132 | + } |
| 133 | + |
| 134 | + if ($field->fields['type'] == 'glpi_item') { |
| 135 | + $input[sprintf('itemtype_%s', $field_name)] = $answer->getRawAnswer()['itemtype']; |
| 136 | + $input[sprintf('items_id_%s', $field_name)] = $answer->getRawAnswer()['items_id']; |
| 137 | + } else { |
| 138 | + $input[$field_name] = $value ?? $answer->getRawAnswer(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return $input; |
| 143 | + } |
| 144 | + |
| 145 | + #[Override] |
| 146 | + public function getDefaultConfig(Form $form): SimpleValueConfig |
| 147 | + { |
| 148 | + return new SimpleValueConfig("1"); |
| 149 | + } |
| 150 | + |
| 151 | + #[Override] |
| 152 | + public function getWeight(): int |
| 153 | + { |
| 154 | + return 1000; |
| 155 | + } |
| 156 | + |
| 157 | + #[Override] |
| 158 | + public function getCategory(): Category |
| 159 | + { |
| 160 | + return Category::PROPERTIES; |
| 161 | + } |
| 162 | +} |
0 commit comments